org.el 1.0 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252
  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. (mapcar (lambda (x)
  4122. (if (string-match "(..?)$" x)
  4123. (substring x 0 (match-beginning 0))
  4124. x))
  4125. list))
  4126. ;; FIXME: this could be done much better, using second characters etc.
  4127. (defun org-assign-fast-keys (alist)
  4128. "Assign fast keys to a keyword-key alist.
  4129. Respect keys that are already there."
  4130. (let (new e k c c1 c2 (char ?a))
  4131. (while (setq e (pop alist))
  4132. (cond
  4133. ((equal e '(:startgroup)) (push e new))
  4134. ((equal e '(:endgroup)) (push e new))
  4135. (t
  4136. (setq k (car e) c2 nil)
  4137. (if (cdr e)
  4138. (setq c (cdr e))
  4139. ;; automatically assign a character.
  4140. (setq c1 (string-to-char
  4141. (downcase (substring
  4142. k (if (= (string-to-char k) ?@) 1 0)))))
  4143. (if (or (rassoc c1 new) (rassoc c1 alist))
  4144. (while (or (rassoc char new) (rassoc char alist))
  4145. (setq char (1+ char)))
  4146. (setq c2 c1))
  4147. (setq c (or c2 char)))
  4148. (push (cons k c) new))))
  4149. (nreverse new)))
  4150. ;;; Some variables ujsed in various places
  4151. (defvar org-window-configuration nil
  4152. "Used in various places to store a window configuration.")
  4153. (defvar org-finish-function nil
  4154. "Function to be called when `C-c C-c' is used.
  4155. This is for getting out of special buffers like remember.")
  4156. ;; FIXME: Occasionally check by commenting these, to make sure
  4157. ;; no other functions uses these, forgetting to let-bind them.
  4158. (defvar entry)
  4159. (defvar state)
  4160. (defvar last-state)
  4161. (defvar date)
  4162. (defvar description)
  4163. ;; Defined somewhere in this file, but used before definition.
  4164. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4165. (defvar org-agenda-buffer-name)
  4166. (defvar org-agenda-undo-list)
  4167. (defvar org-agenda-pending-undo-list)
  4168. (defvar org-agenda-overriding-header)
  4169. (defvar orgtbl-mode)
  4170. (defvar org-html-entities)
  4171. (defvar org-struct-menu)
  4172. (defvar org-org-menu)
  4173. (defvar org-tbl-menu)
  4174. (defvar org-agenda-keymap)
  4175. ;;;; Emacs/XEmacs compatibility
  4176. ;; Overlay compatibility functions
  4177. (defun org-make-overlay (beg end &optional buffer)
  4178. (if (featurep 'xemacs)
  4179. (make-extent beg end buffer)
  4180. (make-overlay beg end buffer)))
  4181. (defun org-delete-overlay (ovl)
  4182. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4183. (defun org-detach-overlay (ovl)
  4184. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4185. (defun org-move-overlay (ovl beg end &optional buffer)
  4186. (if (featurep 'xemacs)
  4187. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4188. (move-overlay ovl beg end buffer)))
  4189. (defun org-overlay-put (ovl prop value)
  4190. (if (featurep 'xemacs)
  4191. (set-extent-property ovl prop value)
  4192. (overlay-put ovl prop value)))
  4193. (defun org-overlay-display (ovl text &optional face evap)
  4194. "Make overlay OVL display TEXT with face FACE."
  4195. (if (featurep 'xemacs)
  4196. (let ((gl (make-glyph text)))
  4197. (and face (set-glyph-face gl face))
  4198. (set-extent-property ovl 'invisible t)
  4199. (set-extent-property ovl 'end-glyph gl))
  4200. (overlay-put ovl 'display text)
  4201. (if face (overlay-put ovl 'face face))
  4202. (if evap (overlay-put ovl 'evaporate t))))
  4203. (defun org-overlay-before-string (ovl text &optional face evap)
  4204. "Make overlay OVL display TEXT with face FACE."
  4205. (if (featurep 'xemacs)
  4206. (let ((gl (make-glyph text)))
  4207. (and face (set-glyph-face gl face))
  4208. (set-extent-property ovl 'begin-glyph gl))
  4209. (if face (org-add-props text nil 'face face))
  4210. (overlay-put ovl 'before-string text)
  4211. (if evap (overlay-put ovl 'evaporate t))))
  4212. (defun org-overlay-get (ovl prop)
  4213. (if (featurep 'xemacs)
  4214. (extent-property ovl prop)
  4215. (overlay-get ovl prop)))
  4216. (defun org-overlays-at (pos)
  4217. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4218. (defun org-overlays-in (&optional start end)
  4219. (if (featurep 'xemacs)
  4220. (extent-list nil start end)
  4221. (overlays-in start end)))
  4222. (defun org-overlay-start (o)
  4223. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4224. (defun org-overlay-end (o)
  4225. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4226. (defun org-find-overlays (prop &optional pos delete)
  4227. "Find all overlays specifying PROP at POS or point.
  4228. If DELETE is non-nil, delete all those overlays."
  4229. (let ((overlays (org-overlays-at (or pos (point))))
  4230. ov found)
  4231. (while (setq ov (pop overlays))
  4232. (if (org-overlay-get ov prop)
  4233. (if delete (org-delete-overlay ov) (push ov found))))
  4234. found))
  4235. ;; Region compatibility
  4236. (defun org-add-hook (hook function &optional append local)
  4237. "Add-hook, compatible with both Emacsen."
  4238. (if (and local (featurep 'xemacs))
  4239. (add-local-hook hook function append)
  4240. (add-hook hook function append local)))
  4241. (defvar org-ignore-region nil
  4242. "To temporarily disable the active region.")
  4243. (defun org-region-active-p ()
  4244. "Is `transient-mark-mode' on and the region active?
  4245. Works on both Emacs and XEmacs."
  4246. (if org-ignore-region
  4247. nil
  4248. (if (featurep 'xemacs)
  4249. (and zmacs-regions (region-active-p))
  4250. (if (fboundp 'use-region-p)
  4251. (use-region-p)
  4252. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4253. ;; Invisibility compatibility
  4254. (defun org-add-to-invisibility-spec (arg)
  4255. "Add elements to `buffer-invisibility-spec'.
  4256. See documentation for `buffer-invisibility-spec' for the kind of elements
  4257. that can be added."
  4258. (cond
  4259. ((fboundp 'add-to-invisibility-spec)
  4260. (add-to-invisibility-spec arg))
  4261. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4262. (setq buffer-invisibility-spec (list arg)))
  4263. (t
  4264. (setq buffer-invisibility-spec
  4265. (cons arg buffer-invisibility-spec)))))
  4266. (defun org-remove-from-invisibility-spec (arg)
  4267. "Remove elements from `buffer-invisibility-spec'."
  4268. (if (fboundp 'remove-from-invisibility-spec)
  4269. (remove-from-invisibility-spec arg)
  4270. (if (consp buffer-invisibility-spec)
  4271. (setq buffer-invisibility-spec
  4272. (delete arg buffer-invisibility-spec)))))
  4273. (defun org-in-invisibility-spec-p (arg)
  4274. "Is ARG a member of `buffer-invisibility-spec'?"
  4275. (if (consp buffer-invisibility-spec)
  4276. (member arg buffer-invisibility-spec)
  4277. nil))
  4278. ;;;; Define the Org-mode
  4279. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4280. (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."))
  4281. ;; We use a before-change function to check if a table might need
  4282. ;; an update.
  4283. (defvar org-table-may-need-update t
  4284. "Indicates that a table might need an update.
  4285. This variable is set by `org-before-change-function'.
  4286. `org-table-align' sets it back to nil.")
  4287. (defvar org-mode-map)
  4288. (defvar org-mode-hook nil)
  4289. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4290. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4291. (defvar org-table-buffer-is-an nil)
  4292. (defconst org-outline-regexp "\\*+ ")
  4293. ;;;###autoload
  4294. (define-derived-mode org-mode outline-mode "Org"
  4295. "Outline-based notes management and organizer, alias
  4296. \"Carsten's outline-mode for keeping track of everything.\"
  4297. Org-mode develops organizational tasks around a NOTES file which
  4298. contains information about projects as plain text. Org-mode is
  4299. implemented on top of outline-mode, which is ideal to keep the content
  4300. of large files well structured. It supports ToDo items, deadlines and
  4301. time stamps, which magically appear in the diary listing of the Emacs
  4302. calendar. Tables are easily created with a built-in table editor.
  4303. Plain text URL-like links connect to websites, emails (VM), Usenet
  4304. messages (Gnus), BBDB entries, and any files related to the project.
  4305. For printing and sharing of notes, an Org-mode file (or a part of it)
  4306. can be exported as a structured ASCII or HTML file.
  4307. The following commands are available:
  4308. \\{org-mode-map}"
  4309. ;; Get rid of Outline menus, they are not needed
  4310. ;; Need to do this here because define-derived-mode sets up
  4311. ;; the keymap so late. Still, it is a waste to call this each time
  4312. ;; we switch another buffer into org-mode.
  4313. (if (featurep 'xemacs)
  4314. (when (boundp 'outline-mode-menu-heading)
  4315. ;; Assume this is Greg's port, it used easymenu
  4316. (easy-menu-remove outline-mode-menu-heading)
  4317. (easy-menu-remove outline-mode-menu-show)
  4318. (easy-menu-remove outline-mode-menu-hide))
  4319. (define-key org-mode-map [menu-bar headings] 'undefined)
  4320. (define-key org-mode-map [menu-bar hide] 'undefined)
  4321. (define-key org-mode-map [menu-bar show] 'undefined))
  4322. (easy-menu-add org-org-menu)
  4323. (easy-menu-add org-tbl-menu)
  4324. (org-install-agenda-files-menu)
  4325. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4326. (org-add-to-invisibility-spec '(org-cwidth))
  4327. (when (featurep 'xemacs)
  4328. (org-set-local 'line-move-ignore-invisible t))
  4329. (org-set-local 'outline-regexp org-outline-regexp)
  4330. (org-set-local 'outline-level 'org-outline-level)
  4331. (when (and org-ellipsis
  4332. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4333. (fboundp 'make-glyph-code))
  4334. (unless org-display-table
  4335. (setq org-display-table (make-display-table)))
  4336. (set-display-table-slot
  4337. org-display-table 4
  4338. (vconcat (mapcar
  4339. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4340. org-ellipsis)))
  4341. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4342. (setq buffer-display-table org-display-table))
  4343. (org-set-regexps-and-options)
  4344. ;; Calc embedded
  4345. (org-set-local 'calc-embedded-open-mode "# ")
  4346. (modify-syntax-entry ?# "<")
  4347. (modify-syntax-entry ?@ "w")
  4348. (if org-startup-truncated (setq truncate-lines t))
  4349. (org-set-local 'font-lock-unfontify-region-function
  4350. 'org-unfontify-region)
  4351. ;; Activate before-change-function
  4352. (org-set-local 'org-table-may-need-update t)
  4353. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4354. 'local)
  4355. ;; Check for running clock before killing a buffer
  4356. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4357. ;; Paragraphs and auto-filling
  4358. (org-set-autofill-regexps)
  4359. (setq indent-line-function 'org-indent-line-function)
  4360. (org-update-radio-target-regexp)
  4361. ;; Comment characters
  4362. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4363. (org-set-local 'comment-padding " ")
  4364. ;; Align options lines
  4365. (org-set-local
  4366. 'align-mode-rules-list
  4367. '((org-in-buffer-settings
  4368. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4369. (modes . '(org-mode)))))
  4370. ;; Imenu
  4371. (org-set-local 'imenu-create-index-function
  4372. 'org-imenu-get-tree)
  4373. ;; Make isearch reveal context
  4374. (if (or (featurep 'xemacs)
  4375. (not (boundp 'outline-isearch-open-invisible-function)))
  4376. ;; Emacs 21 and XEmacs make use of the hook
  4377. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4378. ;; Emacs 22 deals with this through a special variable
  4379. (org-set-local 'outline-isearch-open-invisible-function
  4380. (lambda (&rest ignore) (org-show-context 'isearch))))
  4381. ;; If empty file that did not turn on org-mode automatically, make it to.
  4382. (if (and org-insert-mode-line-in-empty-file
  4383. (interactive-p)
  4384. (= (point-min) (point-max)))
  4385. (insert "# -*- mode: org -*-\n\n"))
  4386. (unless org-inhibit-startup
  4387. (when org-startup-align-all-tables
  4388. (let ((bmp (buffer-modified-p)))
  4389. (org-table-map-tables 'org-table-align)
  4390. (set-buffer-modified-p bmp)))
  4391. (org-cycle-hide-drawers 'all)
  4392. (cond
  4393. ((eq org-startup-folded t)
  4394. (org-cycle '(4)))
  4395. ((eq org-startup-folded 'content)
  4396. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4397. (org-cycle '(4)) (org-cycle '(4)))))))
  4398. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4399. (defsubst org-call-with-arg (command arg)
  4400. "Call COMMAND interactively, but pretend prefix are was ARG."
  4401. (let ((current-prefix-arg arg)) (call-interactively command)))
  4402. (defsubst org-current-line (&optional pos)
  4403. (save-excursion
  4404. (and pos (goto-char pos))
  4405. ;; works also in narrowed buffer, because we start at 1, not point-min
  4406. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4407. (defun org-current-time ()
  4408. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4409. (if (> org-time-stamp-rounding-minutes 0)
  4410. (let ((r org-time-stamp-rounding-minutes)
  4411. (time (decode-time)))
  4412. (apply 'encode-time
  4413. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4414. (nthcdr 2 time))))
  4415. (current-time)))
  4416. (defun org-add-props (string plist &rest props)
  4417. "Add text properties to entire string, from beginning to end.
  4418. PLIST may be a list of properties, PROPS are individual properties and values
  4419. that will be added to PLIST. Returns the string that was modified."
  4420. (add-text-properties
  4421. 0 (length string) (if props (append plist props) plist) string)
  4422. string)
  4423. (put 'org-add-props 'lisp-indent-function 2)
  4424. ;;;; Font-Lock stuff, including the activators
  4425. (defvar org-mouse-map (make-sparse-keymap))
  4426. (org-defkey org-mouse-map
  4427. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4428. (org-defkey org-mouse-map
  4429. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4430. (when org-mouse-1-follows-link
  4431. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4432. (when org-tab-follows-link
  4433. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4434. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4435. (when org-return-follows-link
  4436. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4437. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4438. (require 'font-lock)
  4439. (defconst org-non-link-chars "]\t\n\r<>")
  4440. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4441. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4442. (defvar org-link-re-with-space nil
  4443. "Matches a link with spaces, optional angular brackets around it.")
  4444. (defvar org-link-re-with-space2 nil
  4445. "Matches a link with spaces, optional angular brackets around it.")
  4446. (defvar org-angle-link-re nil
  4447. "Matches link with angular brackets, spaces are allowed.")
  4448. (defvar org-plain-link-re nil
  4449. "Matches plain link, without spaces.")
  4450. (defvar org-bracket-link-regexp nil
  4451. "Matches a link in double brackets.")
  4452. (defvar org-bracket-link-analytic-regexp nil
  4453. "Regular expression used to analyze links.
  4454. Here is what the match groups contain after a match:
  4455. 1: http:
  4456. 2: http
  4457. 3: path
  4458. 4: [desc]
  4459. 5: desc")
  4460. (defvar org-any-link-re nil
  4461. "Regular expression matching any link.")
  4462. (defun org-make-link-regexps ()
  4463. "Update the link regular expressions.
  4464. This should be called after the variable `org-link-types' has changed."
  4465. (setq org-link-re-with-space
  4466. (concat
  4467. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4468. "\\([^" org-non-link-chars " ]"
  4469. "[^" org-non-link-chars "]*"
  4470. "[^" org-non-link-chars " ]\\)>?")
  4471. org-link-re-with-space2
  4472. (concat
  4473. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4474. "\\([^" org-non-link-chars " ]"
  4475. "[^]\t\n\r]*"
  4476. "[^" org-non-link-chars " ]\\)>?")
  4477. org-angle-link-re
  4478. (concat
  4479. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4480. "\\([^" org-non-link-chars " ]"
  4481. "[^" org-non-link-chars "]*"
  4482. "\\)>")
  4483. org-plain-link-re
  4484. (concat
  4485. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4486. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4487. org-bracket-link-regexp
  4488. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4489. org-bracket-link-analytic-regexp
  4490. (concat
  4491. "\\[\\["
  4492. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4493. "\\([^]]+\\)"
  4494. "\\]"
  4495. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4496. "\\]")
  4497. org-any-link-re
  4498. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4499. org-angle-link-re "\\)\\|\\("
  4500. org-plain-link-re "\\)")))
  4501. (org-make-link-regexps)
  4502. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4503. "Regular expression for fast time stamp matching.")
  4504. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4505. "Regular expression for fast time stamp matching.")
  4506. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4507. "Regular expression matching time strings for analysis.
  4508. This one does not require the space after the date.")
  4509. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4510. "Regular expression matching time strings for analysis.")
  4511. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4512. "Regular expression matching time stamps, with groups.")
  4513. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4514. "Regular expression matching time stamps (also [..]), with groups.")
  4515. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4516. "Regular expression matching a time stamp range.")
  4517. (defconst org-tr-regexp-both
  4518. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4519. "Regular expression matching a time stamp range.")
  4520. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4521. org-ts-regexp "\\)?")
  4522. "Regular expression matching a time stamp or time stamp range.")
  4523. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4524. org-ts-regexp-both "\\)?")
  4525. "Regular expression matching a time stamp or time stamp range.
  4526. The time stamps may be either active or inactive.")
  4527. (defvar org-emph-face nil)
  4528. (defun org-do-emphasis-faces (limit)
  4529. "Run through the buffer and add overlays to links."
  4530. (let (rtn)
  4531. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4532. (if (not (= (char-after (match-beginning 3))
  4533. (char-after (match-beginning 4))))
  4534. (progn
  4535. (setq rtn t)
  4536. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4537. 'face
  4538. (nth 1 (assoc (match-string 3)
  4539. org-emphasis-alist)))
  4540. (add-text-properties (match-beginning 2) (match-end 2)
  4541. '(font-lock-multiline t))
  4542. (when org-hide-emphasis-markers
  4543. (add-text-properties (match-end 4) (match-beginning 5)
  4544. '(invisible org-link))
  4545. (add-text-properties (match-beginning 3) (match-end 3)
  4546. '(invisible org-link)))))
  4547. (backward-char 1))
  4548. rtn))
  4549. (defun org-emphasize (&optional char)
  4550. "Insert or change an emphasis, i.e. a font like bold or italic.
  4551. If there is an active region, change that region to a new emphasis.
  4552. If there is no region, just insert the marker characters and position
  4553. the cursor between them.
  4554. CHAR should be either the marker character, or the first character of the
  4555. HTML tag associated with that emphasis. If CHAR is a space, the means
  4556. to remove the emphasis of the selected region.
  4557. If char is not given (for example in an interactive call) it
  4558. will be prompted for."
  4559. (interactive)
  4560. (let ((eal org-emphasis-alist) e det
  4561. (erc org-emphasis-regexp-components)
  4562. (prompt "")
  4563. (string "") beg end move tag c s)
  4564. (if (org-region-active-p)
  4565. (setq beg (region-beginning) end (region-end)
  4566. string (buffer-substring beg end))
  4567. (setq move t))
  4568. (while (setq e (pop eal))
  4569. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4570. c (aref tag 0))
  4571. (push (cons c (string-to-char (car e))) det)
  4572. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4573. (substring tag 1)))))
  4574. (unless char
  4575. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4576. (setq char (read-char-exclusive)))
  4577. (setq char (or (cdr (assoc char det)) char))
  4578. (if (equal char ?\ )
  4579. (setq s "" move nil)
  4580. (unless (assoc (char-to-string char) org-emphasis-alist)
  4581. (error "No such emphasis marker: \"%c\"" char))
  4582. (setq s (char-to-string char)))
  4583. (while (and (> (length string) 1)
  4584. (equal (substring string 0 1) (substring string -1))
  4585. (assoc (substring string 0 1) org-emphasis-alist))
  4586. (setq string (substring string 1 -1)))
  4587. (setq string (concat s string s))
  4588. (if beg (delete-region beg end))
  4589. (unless (or (bolp)
  4590. (string-match (concat "[" (nth 0 erc) "\n]")
  4591. (char-to-string (char-before (point)))))
  4592. (insert " "))
  4593. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4594. (char-to-string (char-after (point))))
  4595. (insert " ") (backward-char 1))
  4596. (insert string)
  4597. (and move (backward-char 1))))
  4598. (defconst org-nonsticky-props
  4599. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4600. (defun org-activate-plain-links (limit)
  4601. "Run through the buffer and add overlays to links."
  4602. (catch 'exit
  4603. (let (f)
  4604. (while (re-search-forward org-plain-link-re limit t)
  4605. (setq f (get-text-property (match-beginning 0) 'face))
  4606. (if (or (eq f 'org-tag)
  4607. (and (listp f) (memq 'org-tag f)))
  4608. nil
  4609. (add-text-properties (match-beginning 0) (match-end 0)
  4610. (list 'mouse-face 'highlight
  4611. 'rear-nonsticky org-nonsticky-props
  4612. 'keymap org-mouse-map
  4613. ))
  4614. (throw 'exit t))))))
  4615. (defun org-activate-code (limit)
  4616. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4617. (unless (get-text-property (match-beginning 1) 'face)
  4618. (remove-text-properties (match-beginning 0) (match-end 0)
  4619. '(display t invisible t intangible t))
  4620. t)))
  4621. (defun org-activate-angle-links (limit)
  4622. "Run through the buffer and add overlays to links."
  4623. (if (re-search-forward org-angle-link-re limit t)
  4624. (progn
  4625. (add-text-properties (match-beginning 0) (match-end 0)
  4626. (list 'mouse-face 'highlight
  4627. 'rear-nonsticky org-nonsticky-props
  4628. 'keymap org-mouse-map
  4629. ))
  4630. t)))
  4631. (defmacro org-maybe-intangible (props)
  4632. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4633. In emacs 21, invisible text is not avoided by the command loop, so the
  4634. intangible property is needed to make sure point skips this text.
  4635. In Emacs 22, this is not necessary. The intangible text property has
  4636. led to problems with flyspell. These problems are fixed in flyspell.el,
  4637. but we still avoid setting the property in Emacs 22 and later.
  4638. We use a macro so that the test can happen at compilation time."
  4639. (if (< emacs-major-version 22)
  4640. `(append '(intangible t) ,props)
  4641. props))
  4642. (defun org-activate-bracket-links (limit)
  4643. "Run through the buffer and add overlays to bracketed links."
  4644. (if (re-search-forward org-bracket-link-regexp limit t)
  4645. (let* ((help (concat "LINK: "
  4646. (org-match-string-no-properties 1)))
  4647. ;; FIXME: above we should remove the escapes.
  4648. ;; but that requires another match, protecting match data,
  4649. ;; a lot of overhead for font-lock.
  4650. (ip (org-maybe-intangible
  4651. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4652. 'keymap org-mouse-map 'mouse-face 'highlight
  4653. 'font-lock-multiline t 'help-echo help)))
  4654. (vp (list 'rear-nonsticky org-nonsticky-props
  4655. 'keymap org-mouse-map 'mouse-face 'highlight
  4656. ' font-lock-multiline t 'help-echo help)))
  4657. ;; We need to remove the invisible property here. Table narrowing
  4658. ;; may have made some of this invisible.
  4659. (remove-text-properties (match-beginning 0) (match-end 0)
  4660. '(invisible nil))
  4661. (if (match-end 3)
  4662. (progn
  4663. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4664. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4665. (add-text-properties (match-end 3) (match-end 0) ip))
  4666. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4667. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4668. (add-text-properties (match-end 1) (match-end 0) ip))
  4669. t)))
  4670. (defun org-activate-dates (limit)
  4671. "Run through the buffer and add overlays to dates."
  4672. (if (re-search-forward org-tsr-regexp-both limit t)
  4673. (progn
  4674. (add-text-properties (match-beginning 0) (match-end 0)
  4675. (list 'mouse-face 'highlight
  4676. 'rear-nonsticky org-nonsticky-props
  4677. 'keymap org-mouse-map))
  4678. (when org-display-custom-times
  4679. (if (match-end 3)
  4680. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4681. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4682. t)))
  4683. (defvar org-target-link-regexp nil
  4684. "Regular expression matching radio targets in plain text.")
  4685. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4686. "Regular expression matching a link target.")
  4687. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4688. "Regular expression matching a radio target.")
  4689. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4690. "Regular expression matching any target.")
  4691. (defun org-activate-target-links (limit)
  4692. "Run through the buffer and add overlays to target matches."
  4693. (when org-target-link-regexp
  4694. (let ((case-fold-search t))
  4695. (if (re-search-forward org-target-link-regexp limit t)
  4696. (progn
  4697. (add-text-properties (match-beginning 0) (match-end 0)
  4698. (list 'mouse-face 'highlight
  4699. 'rear-nonsticky org-nonsticky-props
  4700. 'keymap org-mouse-map
  4701. 'help-echo "Radio target link"
  4702. 'org-linked-text t))
  4703. t)))))
  4704. (defun org-update-radio-target-regexp ()
  4705. "Find all radio targets in this file and update the regular expression."
  4706. (interactive)
  4707. (when (memq 'radio org-activate-links)
  4708. (setq org-target-link-regexp
  4709. (org-make-target-link-regexp (org-all-targets 'radio)))
  4710. (org-restart-font-lock)))
  4711. (defun org-hide-wide-columns (limit)
  4712. (let (s e)
  4713. (setq s (text-property-any (point) (or limit (point-max))
  4714. 'org-cwidth t))
  4715. (when s
  4716. (setq e (next-single-property-change s 'org-cwidth))
  4717. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4718. (goto-char e)
  4719. t)))
  4720. (defvar org-latex-and-specials-regexp nil
  4721. "Regular expression for highlighting export special stuff.")
  4722. (defvar org-match-substring-regexp)
  4723. (defvar org-match-substring-with-braces-regexp)
  4724. (defvar org-export-html-special-string-regexps)
  4725. (defun org-compute-latex-and-specials-regexp ()
  4726. "Compute regular expression for stuff treated specially by exporters."
  4727. (if (not org-highlight-latex-fragments-and-specials)
  4728. (org-set-local 'org-latex-and-specials-regexp nil)
  4729. (let*
  4730. ((matchers (plist-get org-format-latex-options :matchers))
  4731. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4732. org-latex-regexps)))
  4733. (options (org-combine-plists (org-default-export-plist)
  4734. (org-infile-export-plist)))
  4735. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4736. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4737. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4738. (org-export-html-expand (plist-get options :expand-quoted-html))
  4739. (org-export-with-special-strings (plist-get options :special-strings))
  4740. (re-sub
  4741. (cond
  4742. ((equal org-export-with-sub-superscripts '{})
  4743. (list org-match-substring-with-braces-regexp))
  4744. (org-export-with-sub-superscripts
  4745. (list org-match-substring-regexp))
  4746. (t nil)))
  4747. (re-latex
  4748. (if org-export-with-LaTeX-fragments
  4749. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4750. (re-macros
  4751. (if org-export-with-TeX-macros
  4752. (list (concat "\\\\"
  4753. (regexp-opt
  4754. (append (mapcar 'car org-html-entities)
  4755. (if (boundp 'org-latex-entities)
  4756. org-latex-entities nil))
  4757. 'words))) ; FIXME
  4758. ))
  4759. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4760. (re-special (if org-export-with-special-strings
  4761. (mapcar (lambda (x) (car x))
  4762. org-export-html-special-string-regexps)))
  4763. (re-rest
  4764. (delq nil
  4765. (list
  4766. (if org-export-html-expand "@<[^>\n]+>")
  4767. ))))
  4768. (org-set-local
  4769. 'org-latex-and-specials-regexp
  4770. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4771. re-rest) "\\|")))))
  4772. (defface org-latex-and-export-specials
  4773. (let ((font (cond ((assq :inherit custom-face-attributes)
  4774. '(:inherit underline))
  4775. (t '(:underline t)))))
  4776. `((((class grayscale) (background light))
  4777. (:foreground "DimGray" ,@font))
  4778. (((class grayscale) (background dark))
  4779. (:foreground "LightGray" ,@font))
  4780. (((class color) (background light))
  4781. (:foreground "SaddleBrown"))
  4782. (((class color) (background dark))
  4783. (:foreground "burlywood"))
  4784. (t (,@font))))
  4785. "Face used to highlight math latex and other special exporter stuff."
  4786. :group 'org-faces)
  4787. (defun org-do-latex-and-special-faces (limit)
  4788. "Run through the buffer and add overlays to links."
  4789. (when org-latex-and-specials-regexp
  4790. (let (rtn d)
  4791. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4792. limit t))
  4793. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4794. 'face))
  4795. '(org-code org-verbatim underline)))
  4796. (progn
  4797. (setq rtn t
  4798. d (cond ((member (char-after (1+ (match-beginning 0)))
  4799. '(?_ ?^)) 1)
  4800. (t 0)))
  4801. (font-lock-prepend-text-property
  4802. (+ d (match-beginning 0)) (match-end 0)
  4803. 'face 'org-latex-and-export-specials)
  4804. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4805. '(font-lock-multiline t)))))
  4806. rtn)))
  4807. (defun org-restart-font-lock ()
  4808. "Restart font-lock-mode, to force refontification."
  4809. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4810. (font-lock-mode -1)
  4811. (font-lock-mode 1)))
  4812. (defun org-all-targets (&optional radio)
  4813. "Return a list of all targets in this file.
  4814. With optional argument RADIO, only find radio targets."
  4815. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4816. rtn)
  4817. (save-excursion
  4818. (goto-char (point-min))
  4819. (while (re-search-forward re nil t)
  4820. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4821. rtn)))
  4822. (defun org-make-target-link-regexp (targets)
  4823. "Make regular expression matching all strings in TARGETS.
  4824. The regular expression finds the targets also if there is a line break
  4825. between words."
  4826. (and targets
  4827. (concat
  4828. "\\<\\("
  4829. (mapconcat
  4830. (lambda (x)
  4831. (while (string-match " +" x)
  4832. (setq x (replace-match "\\s-+" t t x)))
  4833. x)
  4834. targets
  4835. "\\|")
  4836. "\\)\\>")))
  4837. (defun org-activate-tags (limit)
  4838. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4839. (progn
  4840. (add-text-properties (match-beginning 1) (match-end 1)
  4841. (list 'mouse-face 'highlight
  4842. 'rear-nonsticky org-nonsticky-props
  4843. 'keymap org-mouse-map))
  4844. t)))
  4845. (defun org-outline-level ()
  4846. (save-excursion
  4847. (looking-at outline-regexp)
  4848. (if (match-beginning 1)
  4849. (+ (org-get-string-indentation (match-string 1)) 1000)
  4850. (1- (- (match-end 0) (match-beginning 0))))))
  4851. (defvar org-font-lock-keywords nil)
  4852. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4853. "Regular expression matching a property line.")
  4854. (defun org-set-font-lock-defaults ()
  4855. (let* ((em org-fontify-emphasized-text)
  4856. (lk org-activate-links)
  4857. (org-font-lock-extra-keywords
  4858. (list
  4859. ;; Headlines
  4860. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4861. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4862. ;; Table lines
  4863. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4864. (1 'org-table t))
  4865. ;; Table internals
  4866. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4867. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4868. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4869. ;; Drawers
  4870. (list org-drawer-regexp '(0 'org-special-keyword t))
  4871. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4872. ;; Properties
  4873. (list org-property-re
  4874. '(1 'org-special-keyword t)
  4875. '(3 'org-property-value t))
  4876. (if org-format-transports-properties-p
  4877. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4878. ;; Links
  4879. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4880. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4881. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4882. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4883. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4884. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4885. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4886. '(org-hide-wide-columns (0 nil append))
  4887. ;; TODO lines
  4888. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4889. '(1 (org-get-todo-face 1) t))
  4890. ;; DONE
  4891. (if org-fontify-done-headline
  4892. (list (concat "^[*]+ +\\<\\("
  4893. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4894. "\\)\\(.*\\)")
  4895. '(2 'org-headline-done t))
  4896. nil)
  4897. ;; Priorities
  4898. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4899. ;; Special keywords
  4900. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4901. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4902. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4903. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4904. ;; Emphasis
  4905. (if em
  4906. (if (featurep 'xemacs)
  4907. '(org-do-emphasis-faces (0 nil append))
  4908. '(org-do-emphasis-faces)))
  4909. ;; Checkboxes
  4910. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4911. 2 'bold prepend)
  4912. (if org-provide-checkbox-statistics
  4913. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4914. (0 (org-get-checkbox-statistics-face) t)))
  4915. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4916. '(1 'org-archived prepend))
  4917. ;; Specials
  4918. '(org-do-latex-and-special-faces)
  4919. ;; Code
  4920. '(org-activate-code (1 'org-code t))
  4921. ;; COMMENT
  4922. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4923. "\\|" org-quote-string "\\)\\>")
  4924. '(1 'org-special-keyword t))
  4925. '("^#.*" (0 'font-lock-comment-face t))
  4926. )))
  4927. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4928. ;; Now set the full font-lock-keywords
  4929. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4930. (org-set-local 'font-lock-defaults
  4931. '(org-font-lock-keywords t nil nil backward-paragraph))
  4932. (kill-local-variable 'font-lock-keywords) nil))
  4933. (defvar org-m nil)
  4934. (defvar org-l nil)
  4935. (defvar org-f nil)
  4936. (defun org-get-level-face (n)
  4937. "Get the right face for match N in font-lock matching of healdines."
  4938. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4939. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4940. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4941. (cond
  4942. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4943. ((eq n 2) org-f)
  4944. (t (if org-level-color-stars-only nil org-f))))
  4945. (defun org-get-todo-face (kwd)
  4946. "Get the right face for a TODO keyword KWD.
  4947. If KWD is a number, get the corresponding match group."
  4948. (if (numberp kwd) (setq kwd (match-string kwd)))
  4949. (or (cdr (assoc kwd org-todo-keyword-faces))
  4950. (and (member kwd org-done-keywords) 'org-done)
  4951. 'org-todo))
  4952. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4953. "Remove fontification and activation overlays from links."
  4954. (font-lock-default-unfontify-region beg end)
  4955. (let* ((buffer-undo-list t)
  4956. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4957. (inhibit-modification-hooks t)
  4958. deactivate-mark buffer-file-name buffer-file-truename)
  4959. (remove-text-properties beg end
  4960. '(mouse-face t keymap t org-linked-text t
  4961. invisible t intangible t))))
  4962. ;;;; Visibility cycling, including org-goto and indirect buffer
  4963. ;;; Cycling
  4964. (defvar org-cycle-global-status nil)
  4965. (make-variable-buffer-local 'org-cycle-global-status)
  4966. (defvar org-cycle-subtree-status nil)
  4967. (make-variable-buffer-local 'org-cycle-subtree-status)
  4968. ;;;###autoload
  4969. (defun org-cycle (&optional arg)
  4970. "Visibility cycling for Org-mode.
  4971. - When this function is called with a prefix argument, rotate the entire
  4972. buffer through 3 states (global cycling)
  4973. 1. OVERVIEW: Show only top-level headlines.
  4974. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4975. 3. SHOW ALL: Show everything.
  4976. - When point is at the beginning of a headline, rotate the subtree started
  4977. by this line through 3 different states (local cycling)
  4978. 1. FOLDED: Only the main headline is shown.
  4979. 2. CHILDREN: The main headline and the direct children are shown.
  4980. From this state, you can move to one of the children
  4981. and zoom in further.
  4982. 3. SUBTREE: Show the entire subtree, including body text.
  4983. - When there is a numeric prefix, go up to a heading with level ARG, do
  4984. a `show-subtree' and return to the previous cursor position. If ARG
  4985. is negative, go up that many levels.
  4986. - When point is not at the beginning of a headline, execute
  4987. `indent-relative', like TAB normally does. See the option
  4988. `org-cycle-emulate-tab' for details.
  4989. - Special case: if point is at the beginning of the buffer and there is
  4990. no headline in line 1, this function will act as if called with prefix arg.
  4991. But only if also the variable `org-cycle-global-at-bob' is t."
  4992. (interactive "P")
  4993. (let* ((outline-regexp
  4994. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4995. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4996. outline-regexp))
  4997. (bob-special (and org-cycle-global-at-bob (bobp)
  4998. (not (looking-at outline-regexp))))
  4999. (org-cycle-hook
  5000. (if bob-special
  5001. (delq 'org-optimize-window-after-visibility-change
  5002. (copy-sequence org-cycle-hook))
  5003. org-cycle-hook))
  5004. (pos (point)))
  5005. (if (or bob-special (equal arg '(4)))
  5006. ;; special case: use global cycling
  5007. (setq arg t))
  5008. (cond
  5009. ((org-at-table-p 'any)
  5010. ;; Enter the table or move to the next field in the table
  5011. (or (org-table-recognize-table.el)
  5012. (progn
  5013. (if arg (org-table-edit-field t)
  5014. (org-table-justify-field-maybe)
  5015. (call-interactively 'org-table-next-field)))))
  5016. ((eq arg t) ;; Global cycling
  5017. (cond
  5018. ((and (eq last-command this-command)
  5019. (eq org-cycle-global-status 'overview))
  5020. ;; We just created the overview - now do table of contents
  5021. ;; This can be slow in very large buffers, so indicate action
  5022. (message "CONTENTS...")
  5023. (org-content)
  5024. (message "CONTENTS...done")
  5025. (setq org-cycle-global-status 'contents)
  5026. (run-hook-with-args 'org-cycle-hook 'contents))
  5027. ((and (eq last-command this-command)
  5028. (eq org-cycle-global-status 'contents))
  5029. ;; We just showed the table of contents - now show everything
  5030. (show-all)
  5031. (message "SHOW ALL")
  5032. (setq org-cycle-global-status 'all)
  5033. (run-hook-with-args 'org-cycle-hook 'all))
  5034. (t
  5035. ;; Default action: go to overview
  5036. (org-overview)
  5037. (message "OVERVIEW")
  5038. (setq org-cycle-global-status 'overview)
  5039. (run-hook-with-args 'org-cycle-hook 'overview))))
  5040. ((and org-drawers org-drawer-regexp
  5041. (save-excursion
  5042. (beginning-of-line 1)
  5043. (looking-at org-drawer-regexp)))
  5044. ;; Toggle block visibility
  5045. (org-flag-drawer
  5046. (not (get-char-property (match-end 0) 'invisible))))
  5047. ((integerp arg)
  5048. ;; Show-subtree, ARG levels up from here.
  5049. (save-excursion
  5050. (org-back-to-heading)
  5051. (outline-up-heading (if (< arg 0) (- arg)
  5052. (- (funcall outline-level) arg)))
  5053. (org-show-subtree)))
  5054. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5055. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5056. ;; At a heading: rotate between three different views
  5057. (org-back-to-heading)
  5058. (let ((goal-column 0) eoh eol eos)
  5059. ;; First, some boundaries
  5060. (save-excursion
  5061. (org-back-to-heading)
  5062. (save-excursion
  5063. (beginning-of-line 2)
  5064. (while (and (not (eobp)) ;; this is like `next-line'
  5065. (get-char-property (1- (point)) 'invisible))
  5066. (beginning-of-line 2)) (setq eol (point)))
  5067. (outline-end-of-heading) (setq eoh (point))
  5068. (org-end-of-subtree t)
  5069. (unless (eobp)
  5070. (skip-chars-forward " \t\n")
  5071. (beginning-of-line 1) ; in case this is an item
  5072. )
  5073. (setq eos (1- (point))))
  5074. ;; Find out what to do next and set `this-command'
  5075. (cond
  5076. ((= eos eoh)
  5077. ;; Nothing is hidden behind this heading
  5078. (message "EMPTY ENTRY")
  5079. (setq org-cycle-subtree-status nil)
  5080. (save-excursion
  5081. (goto-char eos)
  5082. (outline-next-heading)
  5083. (if (org-invisible-p) (org-flag-heading nil))))
  5084. ((or (>= eol eos)
  5085. (not (string-match "\\S-" (buffer-substring eol eos))))
  5086. ;; Entire subtree is hidden in one line: open it
  5087. (org-show-entry)
  5088. (show-children)
  5089. (message "CHILDREN")
  5090. (save-excursion
  5091. (goto-char eos)
  5092. (outline-next-heading)
  5093. (if (org-invisible-p) (org-flag-heading nil)))
  5094. (setq org-cycle-subtree-status 'children)
  5095. (run-hook-with-args 'org-cycle-hook 'children))
  5096. ((and (eq last-command this-command)
  5097. (eq org-cycle-subtree-status 'children))
  5098. ;; We just showed the children, now show everything.
  5099. (org-show-subtree)
  5100. (message "SUBTREE")
  5101. (setq org-cycle-subtree-status 'subtree)
  5102. (run-hook-with-args 'org-cycle-hook 'subtree))
  5103. (t
  5104. ;; Default action: hide the subtree.
  5105. (hide-subtree)
  5106. (message "FOLDED")
  5107. (setq org-cycle-subtree-status 'folded)
  5108. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5109. ;; TAB emulation
  5110. (buffer-read-only (org-back-to-heading))
  5111. ((org-try-cdlatex-tab))
  5112. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5113. (or (not (bolp))
  5114. (not (looking-at outline-regexp))))
  5115. (call-interactively (global-key-binding "\t")))
  5116. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5117. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5118. (or (and (eq org-cycle-emulate-tab 'white)
  5119. (= (match-end 0) (point-at-eol)))
  5120. (and (eq org-cycle-emulate-tab 'whitestart)
  5121. (>= (match-end 0) pos))))
  5122. t
  5123. (eq org-cycle-emulate-tab t))
  5124. ; (if (and (looking-at "[ \n\r\t]")
  5125. ; (string-match "^[ \t]*$" (buffer-substring
  5126. ; (point-at-bol) (point))))
  5127. ; (progn
  5128. ; (beginning-of-line 1)
  5129. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5130. (call-interactively (global-key-binding "\t")))
  5131. (t (save-excursion
  5132. (org-back-to-heading)
  5133. (org-cycle))))))
  5134. ;;;###autoload
  5135. (defun org-global-cycle (&optional arg)
  5136. "Cycle the global visibility. For details see `org-cycle'."
  5137. (interactive "P")
  5138. (let ((org-cycle-include-plain-lists
  5139. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5140. (if (integerp arg)
  5141. (progn
  5142. (show-all)
  5143. (hide-sublevels arg)
  5144. (setq org-cycle-global-status 'contents))
  5145. (org-cycle '(4)))))
  5146. (defun org-overview ()
  5147. "Switch to overview mode, shoing only top-level headlines.
  5148. Really, this shows all headlines with level equal or greater than the level
  5149. of the first headline in the buffer. This is important, because if the
  5150. first headline is not level one, then (hide-sublevels 1) gives confusing
  5151. results."
  5152. (interactive)
  5153. (let ((level (save-excursion
  5154. (goto-char (point-min))
  5155. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5156. (progn
  5157. (goto-char (match-beginning 0))
  5158. (funcall outline-level))))))
  5159. (and level (hide-sublevels level))))
  5160. (defun org-content (&optional arg)
  5161. "Show all headlines in the buffer, like a table of contents.
  5162. With numerical argument N, show content up to level N."
  5163. (interactive "P")
  5164. (save-excursion
  5165. ;; Visit all headings and show their offspring
  5166. (and (integerp arg) (org-overview))
  5167. (goto-char (point-max))
  5168. (catch 'exit
  5169. (while (and (progn (condition-case nil
  5170. (outline-previous-visible-heading 1)
  5171. (error (goto-char (point-min))))
  5172. t)
  5173. (looking-at outline-regexp))
  5174. (if (integerp arg)
  5175. (show-children (1- arg))
  5176. (show-branches))
  5177. (if (bobp) (throw 'exit nil))))))
  5178. (defun org-optimize-window-after-visibility-change (state)
  5179. "Adjust the window after a change in outline visibility.
  5180. This function is the default value of the hook `org-cycle-hook'."
  5181. (when (get-buffer-window (current-buffer))
  5182. (cond
  5183. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5184. ; ((eq state 'overview) (org-beginning-of-line))
  5185. ((eq state 'content) nil)
  5186. ((eq state 'all) nil)
  5187. ((eq state 'folded) nil)
  5188. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5189. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5190. (defun org-compact-display-after-subtree-move ()
  5191. (let (beg end)
  5192. (save-excursion
  5193. (if (org-up-heading-safe)
  5194. (progn
  5195. (hide-subtree)
  5196. (show-entry)
  5197. (show-children)
  5198. (org-cycle-show-empty-lines 'children)
  5199. (org-cycle-hide-drawers 'children))
  5200. (org-overview)))))
  5201. (defun org-cycle-show-empty-lines (state)
  5202. "Show empty lines above all visible headlines.
  5203. The region to be covered depends on STATE when called through
  5204. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5205. entire buffer covered. Note that an empty line is only shown if there
  5206. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5207. (when (> org-cycle-separator-lines 0)
  5208. (save-excursion
  5209. (let* ((n org-cycle-separator-lines)
  5210. (re (cond
  5211. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5212. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5213. (t (let ((ns (number-to-string (- n 2))))
  5214. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5215. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5216. beg end)
  5217. (cond
  5218. ((memq state '(overview contents t))
  5219. (setq beg (point-min) end (point-max)))
  5220. ((memq state '(children folded))
  5221. (setq beg (point) end (progn (org-end-of-subtree t t)
  5222. (beginning-of-line 2)
  5223. (point)))))
  5224. (when beg
  5225. (goto-char beg)
  5226. (while (re-search-forward re end t)
  5227. (if (not (get-char-property (match-end 1) 'invisible))
  5228. (outline-flag-region
  5229. (match-beginning 1) (match-end 1) nil)))))))
  5230. ;; Never hide empty lines at the end of the file.
  5231. (save-excursion
  5232. (goto-char (point-max))
  5233. (outline-previous-heading)
  5234. (outline-end-of-heading)
  5235. (if (and (looking-at "[ \t\n]+")
  5236. (= (match-end 0) (point-max)))
  5237. (outline-flag-region (point) (match-end 0) nil))))
  5238. (defun org-subtree-end-visible-p ()
  5239. "Is the end of the current subtree visible?"
  5240. (pos-visible-in-window-p
  5241. (save-excursion (org-end-of-subtree t) (point))))
  5242. (defun org-first-headline-recenter (&optional N)
  5243. "Move cursor to the first headline and recenter the headline.
  5244. Optional argument N means, put the headline into the Nth line of the window."
  5245. (goto-char (point-min))
  5246. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5247. (beginning-of-line)
  5248. (recenter (prefix-numeric-value N))))
  5249. ;;; Org-goto
  5250. (defvar org-goto-window-configuration nil)
  5251. (defvar org-goto-marker nil)
  5252. (defvar org-goto-map
  5253. (let ((map (make-sparse-keymap)))
  5254. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5255. (while (setq cmd (pop cmds))
  5256. (substitute-key-definition cmd cmd map global-map)))
  5257. (suppress-keymap map)
  5258. (org-defkey map "\C-m" 'org-goto-ret)
  5259. (org-defkey map [(return)] 'org-goto-ret)
  5260. (org-defkey map [(left)] 'org-goto-left)
  5261. (org-defkey map [(right)] 'org-goto-right)
  5262. (org-defkey map [(control ?g)] 'org-goto-quit)
  5263. (org-defkey map "\C-i" 'org-cycle)
  5264. (org-defkey map [(tab)] 'org-cycle)
  5265. (org-defkey map [(down)] 'outline-next-visible-heading)
  5266. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5267. (if org-goto-auto-isearch
  5268. (if (fboundp 'define-key-after)
  5269. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5270. nil)
  5271. (org-defkey map "q" 'org-goto-quit)
  5272. (org-defkey map "n" 'outline-next-visible-heading)
  5273. (org-defkey map "p" 'outline-previous-visible-heading)
  5274. (org-defkey map "f" 'outline-forward-same-level)
  5275. (org-defkey map "b" 'outline-backward-same-level)
  5276. (org-defkey map "u" 'outline-up-heading))
  5277. (org-defkey map "/" 'org-occur)
  5278. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5279. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5280. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5281. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5282. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5283. map))
  5284. (defconst org-goto-help
  5285. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5286. RET=jump to location [Q]uit and return to previous location
  5287. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5288. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5289. (defun org-goto (&optional alternative-interface)
  5290. "Look up a different location in the current file, keeping current visibility.
  5291. When you want look-up or go to a different location in a document, the
  5292. fastest way is often to fold the entire buffer and then dive into the tree.
  5293. This method has the disadvantage, that the previous location will be folded,
  5294. which may not be what you want.
  5295. This command works around this by showing a copy of the current buffer
  5296. in an indirect buffer, in overview mode. You can dive into the tree in
  5297. that copy, use org-occur and incremental search to find a location.
  5298. When pressing RET or `Q', the command returns to the original buffer in
  5299. which the visibility is still unchanged. After RET is will also jump to
  5300. the location selected in the indirect buffer and expose the
  5301. the headline hierarchy above."
  5302. (interactive "P")
  5303. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5304. (org-refile-use-outline-path t)
  5305. (interface
  5306. (if (not alternative-interface)
  5307. org-goto-interface
  5308. (if (eq org-goto-interface 'outline)
  5309. 'outline-path-completion
  5310. 'outline)))
  5311. (org-goto-start-pos (point))
  5312. (selected-point
  5313. (if (eq interface 'outline)
  5314. (car (org-get-location (current-buffer) org-goto-help))
  5315. (nth 3 (org-refile-get-location "Goto: ")))))
  5316. (if selected-point
  5317. (progn
  5318. (org-mark-ring-push org-goto-start-pos)
  5319. (goto-char selected-point)
  5320. (if (or (org-invisible-p) (org-invisible-p2))
  5321. (org-show-context 'org-goto)))
  5322. (message "Quit"))))
  5323. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5324. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5325. (defvar org-goto-local-auto-isearch-map) ; defined below
  5326. (defun org-get-location (buf help)
  5327. "Let the user select a location in the Org-mode buffer BUF.
  5328. This function uses a recursive edit. It returns the selected position
  5329. or nil."
  5330. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5331. (isearch-hide-immediately nil)
  5332. (isearch-search-fun-function
  5333. (lambda () 'org-goto-local-search-forward-headings))
  5334. (org-goto-selected-point org-goto-exit-command))
  5335. (save-excursion
  5336. (save-window-excursion
  5337. (delete-other-windows)
  5338. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5339. (switch-to-buffer
  5340. (condition-case nil
  5341. (make-indirect-buffer (current-buffer) "*org-goto*")
  5342. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5343. (with-output-to-temp-buffer "*Help*"
  5344. (princ help))
  5345. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5346. (setq buffer-read-only nil)
  5347. (let ((org-startup-truncated t)
  5348. (org-startup-folded nil)
  5349. (org-startup-align-all-tables nil))
  5350. (org-mode)
  5351. (org-overview))
  5352. (setq buffer-read-only t)
  5353. (if (and (boundp 'org-goto-start-pos)
  5354. (integer-or-marker-p org-goto-start-pos))
  5355. (let ((org-show-hierarchy-above t)
  5356. (org-show-siblings t)
  5357. (org-show-following-heading t))
  5358. (goto-char org-goto-start-pos)
  5359. (and (org-invisible-p) (org-show-context)))
  5360. (goto-char (point-min)))
  5361. (org-beginning-of-line)
  5362. (message "Select location and press RET")
  5363. (use-local-map org-goto-map)
  5364. (recursive-edit)
  5365. ))
  5366. (kill-buffer "*org-goto*")
  5367. (cons org-goto-selected-point org-goto-exit-command)))
  5368. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5369. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5370. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5371. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5372. (defun org-goto-local-search-forward-headings (string bound noerror)
  5373. "Search and make sure that anu matches are in headlines."
  5374. (catch 'return
  5375. (while (search-forward string bound noerror)
  5376. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5377. (and (member :headline context)
  5378. (not (member :tags context))))
  5379. (throw 'return (point))))))
  5380. (defun org-goto-local-auto-isearch ()
  5381. "Start isearch."
  5382. (interactive)
  5383. (goto-char (point-min))
  5384. (let ((keys (this-command-keys)))
  5385. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5386. (isearch-mode t)
  5387. (isearch-process-search-char (string-to-char keys)))))
  5388. (defun org-goto-ret (&optional arg)
  5389. "Finish `org-goto' by going to the new location."
  5390. (interactive "P")
  5391. (setq org-goto-selected-point (point)
  5392. org-goto-exit-command 'return)
  5393. (throw 'exit nil))
  5394. (defun org-goto-left ()
  5395. "Finish `org-goto' by going to the new location."
  5396. (interactive)
  5397. (if (org-on-heading-p)
  5398. (progn
  5399. (beginning-of-line 1)
  5400. (setq org-goto-selected-point (point)
  5401. org-goto-exit-command 'left)
  5402. (throw 'exit nil))
  5403. (error "Not on a heading")))
  5404. (defun org-goto-right ()
  5405. "Finish `org-goto' by going to the new location."
  5406. (interactive)
  5407. (if (org-on-heading-p)
  5408. (progn
  5409. (setq org-goto-selected-point (point)
  5410. org-goto-exit-command 'right)
  5411. (throw 'exit nil))
  5412. (error "Not on a heading")))
  5413. (defun org-goto-quit ()
  5414. "Finish `org-goto' without cursor motion."
  5415. (interactive)
  5416. (setq org-goto-selected-point nil)
  5417. (setq org-goto-exit-command 'quit)
  5418. (throw 'exit nil))
  5419. ;;; Indirect buffer display of subtrees
  5420. (defvar org-indirect-dedicated-frame nil
  5421. "This is the frame being used for indirect tree display.")
  5422. (defvar org-last-indirect-buffer nil)
  5423. (defun org-tree-to-indirect-buffer (&optional arg)
  5424. "Create indirect buffer and narrow it to current subtree.
  5425. With numerical prefix ARG, go up to this level and then take that tree.
  5426. If ARG is negative, go up that many levels.
  5427. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5428. indirect buffer previously made with this command, to avoid proliferation of
  5429. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5430. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5431. is kept so that you can work with several indirect buffers at the same time.
  5432. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5433. requests that a new frame be made for the new buffer, so that the dedicated
  5434. frame is not changed."
  5435. (interactive "P")
  5436. (let ((cbuf (current-buffer))
  5437. (cwin (selected-window))
  5438. (pos (point))
  5439. beg end level heading ibuf)
  5440. (save-excursion
  5441. (org-back-to-heading t)
  5442. (when (numberp arg)
  5443. (setq level (org-outline-level))
  5444. (if (< arg 0) (setq arg (+ level arg)))
  5445. (while (> (setq level (org-outline-level)) arg)
  5446. (outline-up-heading 1 t)))
  5447. (setq beg (point)
  5448. heading (org-get-heading))
  5449. (org-end-of-subtree t) (setq end (point)))
  5450. (if (and (buffer-live-p org-last-indirect-buffer)
  5451. (not (eq org-indirect-buffer-display 'new-frame))
  5452. (not arg))
  5453. (kill-buffer org-last-indirect-buffer))
  5454. (setq ibuf (org-get-indirect-buffer cbuf)
  5455. org-last-indirect-buffer ibuf)
  5456. (cond
  5457. ((or (eq org-indirect-buffer-display 'new-frame)
  5458. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5459. (select-frame (make-frame))
  5460. (delete-other-windows)
  5461. (switch-to-buffer ibuf)
  5462. (org-set-frame-title heading))
  5463. ((eq org-indirect-buffer-display 'dedicated-frame)
  5464. (raise-frame
  5465. (select-frame (or (and org-indirect-dedicated-frame
  5466. (frame-live-p org-indirect-dedicated-frame)
  5467. org-indirect-dedicated-frame)
  5468. (setq org-indirect-dedicated-frame (make-frame)))))
  5469. (delete-other-windows)
  5470. (switch-to-buffer ibuf)
  5471. (org-set-frame-title (concat "Indirect: " heading)))
  5472. ((eq org-indirect-buffer-display 'current-window)
  5473. (switch-to-buffer ibuf))
  5474. ((eq org-indirect-buffer-display 'other-window)
  5475. (pop-to-buffer ibuf))
  5476. (t (error "Invalid value.")))
  5477. (if (featurep 'xemacs)
  5478. (save-excursion (org-mode) (turn-on-font-lock)))
  5479. (narrow-to-region beg end)
  5480. (show-all)
  5481. (goto-char pos)
  5482. (and (window-live-p cwin) (select-window cwin))))
  5483. (defun org-get-indirect-buffer (&optional buffer)
  5484. (setq buffer (or buffer (current-buffer)))
  5485. (let ((n 1) (base (buffer-name buffer)) bname)
  5486. (while (buffer-live-p
  5487. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5488. (setq n (1+ n)))
  5489. (condition-case nil
  5490. (make-indirect-buffer buffer bname 'clone)
  5491. (error (make-indirect-buffer buffer bname)))))
  5492. (defun org-set-frame-title (title)
  5493. "Set the title of the current frame to the string TITLE."
  5494. ;; FIXME: how to name a single frame in XEmacs???
  5495. (unless (featurep 'xemacs)
  5496. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5497. ;;;; Structure editing
  5498. ;;; Inserting headlines
  5499. (defun org-insert-heading (&optional force-heading)
  5500. "Insert a new heading or item with same depth at point.
  5501. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5502. If point is at the beginning of a headline, insert a sibling before the
  5503. current headline. If point is not at the beginning, do not split the line,
  5504. but create the new hedline after the current line."
  5505. (interactive "P")
  5506. (if (= (buffer-size) 0)
  5507. (insert "\n* ")
  5508. (when (or force-heading (not (org-insert-item)))
  5509. (let* ((head (save-excursion
  5510. (condition-case nil
  5511. (progn
  5512. (org-back-to-heading)
  5513. (match-string 0))
  5514. (error "*"))))
  5515. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5516. pos)
  5517. (cond
  5518. ((and (org-on-heading-p) (bolp)
  5519. (or (bobp)
  5520. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5521. ;; insert before the current line
  5522. (open-line (if blank 2 1)))
  5523. ((and (bolp)
  5524. (or (bobp)
  5525. (save-excursion
  5526. (backward-char 1) (not (org-invisible-p)))))
  5527. ;; insert right here
  5528. nil)
  5529. (t
  5530. ;; in the middle of the line
  5531. (org-show-entry)
  5532. (if (and (org-on-heading-p)
  5533. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5534. ;; protect the tags
  5535. (let ((tags (match-string 2)) pos)
  5536. (delete-region (match-beginning 1) (match-end 1))
  5537. (setq pos (point-at-bol))
  5538. (newline (if blank 2 1))
  5539. (save-excursion
  5540. (goto-char pos)
  5541. (end-of-line 1)
  5542. (insert " " tags)
  5543. (org-set-tags nil 'align)))
  5544. (newline (if blank 2 1)))))
  5545. (insert head) (just-one-space)
  5546. (setq pos (point))
  5547. (end-of-line 1)
  5548. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5549. (run-hooks 'org-insert-heading-hook)))))
  5550. (defun org-insert-heading-after-current ()
  5551. "Insert a new heading with same level as current, after current subtree."
  5552. (interactive)
  5553. (org-back-to-heading)
  5554. (org-insert-heading)
  5555. (org-move-subtree-down)
  5556. (end-of-line 1))
  5557. (defun org-insert-todo-heading (arg)
  5558. "Insert a new heading with the same level and TODO state as current heading.
  5559. If the heading has no TODO state, or if the state is DONE, use the first
  5560. state (TODO by default). Also with prefix arg, force first state."
  5561. (interactive "P")
  5562. (when (not (org-insert-item 'checkbox))
  5563. (org-insert-heading)
  5564. (save-excursion
  5565. (org-back-to-heading)
  5566. (outline-previous-heading)
  5567. (looking-at org-todo-line-regexp))
  5568. (if (or arg
  5569. (not (match-beginning 2))
  5570. (member (match-string 2) org-done-keywords))
  5571. (insert (car org-todo-keywords-1) " ")
  5572. (insert (match-string 2) " "))))
  5573. (defun org-insert-subheading (arg)
  5574. "Insert a new subheading and demote it.
  5575. Works for outline headings and for plain lists alike."
  5576. (interactive "P")
  5577. (org-insert-heading arg)
  5578. (cond
  5579. ((org-on-heading-p) (org-do-demote))
  5580. ((org-at-item-p) (org-indent-item 1))))
  5581. (defun org-insert-todo-subheading (arg)
  5582. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5583. Works for outline headings and for plain lists alike."
  5584. (interactive "P")
  5585. (org-insert-todo-heading arg)
  5586. (cond
  5587. ((org-on-heading-p) (org-do-demote))
  5588. ((org-at-item-p) (org-indent-item 1))))
  5589. ;;; Promotion and Demotion
  5590. (defun org-promote-subtree ()
  5591. "Promote the entire subtree.
  5592. See also `org-promote'."
  5593. (interactive)
  5594. (save-excursion
  5595. (org-map-tree 'org-promote))
  5596. (org-fix-position-after-promote))
  5597. (defun org-demote-subtree ()
  5598. "Demote the entire subtree. See `org-demote'.
  5599. See also `org-promote'."
  5600. (interactive)
  5601. (save-excursion
  5602. (org-map-tree 'org-demote))
  5603. (org-fix-position-after-promote))
  5604. (defun org-do-promote ()
  5605. "Promote the current heading higher up the tree.
  5606. If the region is active in `transient-mark-mode', promote all headings
  5607. in the region."
  5608. (interactive)
  5609. (save-excursion
  5610. (if (org-region-active-p)
  5611. (org-map-region 'org-promote (region-beginning) (region-end))
  5612. (org-promote)))
  5613. (org-fix-position-after-promote))
  5614. (defun org-do-demote ()
  5615. "Demote the current heading lower down the tree.
  5616. If the region is active in `transient-mark-mode', demote all headings
  5617. in the region."
  5618. (interactive)
  5619. (save-excursion
  5620. (if (org-region-active-p)
  5621. (org-map-region 'org-demote (region-beginning) (region-end))
  5622. (org-demote)))
  5623. (org-fix-position-after-promote))
  5624. (defun org-fix-position-after-promote ()
  5625. "Make sure that after pro/demotion cursor position is right."
  5626. (let ((pos (point)))
  5627. (when (save-excursion
  5628. (beginning-of-line 1)
  5629. (looking-at org-todo-line-regexp)
  5630. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5631. (cond ((eobp) (insert " "))
  5632. ((eolp) (insert " "))
  5633. ((equal (char-after) ?\ ) (forward-char 1))))))
  5634. (defun org-reduced-level (l)
  5635. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5636. (defun org-get-legal-level (level &optional change)
  5637. "Rectify a level change under the influence of `org-odd-levels-only'
  5638. LEVEL is a current level, CHANGE is by how much the level should be
  5639. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5640. even level numbers will become the next higher odd number."
  5641. (if org-odd-levels-only
  5642. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5643. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5644. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5645. (max 1 (+ level change))))
  5646. (defun org-promote ()
  5647. "Promote the current heading higher up the tree.
  5648. If the region is active in `transient-mark-mode', promote all headings
  5649. in the region."
  5650. (org-back-to-heading t)
  5651. (let* ((level (save-match-data (funcall outline-level)))
  5652. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5653. (diff (abs (- level (length up-head) -1))))
  5654. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5655. (replace-match up-head nil t)
  5656. ;; Fixup tag positioning
  5657. (and org-auto-align-tags (org-set-tags nil t))
  5658. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5659. (defun org-demote ()
  5660. "Demote the current heading lower down the tree.
  5661. If the region is active in `transient-mark-mode', demote all headings
  5662. in the region."
  5663. (org-back-to-heading t)
  5664. (let* ((level (save-match-data (funcall outline-level)))
  5665. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5666. (diff (abs (- level (length down-head) -1))))
  5667. (replace-match down-head nil t)
  5668. ;; Fixup tag positioning
  5669. (and org-auto-align-tags (org-set-tags nil t))
  5670. (if org-adapt-indentation (org-fixup-indentation diff))))
  5671. (defun org-map-tree (fun)
  5672. "Call FUN for every heading underneath the current one."
  5673. (org-back-to-heading)
  5674. (let ((level (funcall outline-level)))
  5675. (save-excursion
  5676. (funcall fun)
  5677. (while (and (progn
  5678. (outline-next-heading)
  5679. (> (funcall outline-level) level))
  5680. (not (eobp)))
  5681. (funcall fun)))))
  5682. (defun org-map-region (fun beg end)
  5683. "Call FUN for every heading between BEG and END."
  5684. (let ((org-ignore-region t))
  5685. (save-excursion
  5686. (setq end (copy-marker end))
  5687. (goto-char beg)
  5688. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5689. (< (point) end))
  5690. (funcall fun))
  5691. (while (and (progn
  5692. (outline-next-heading)
  5693. (< (point) end))
  5694. (not (eobp)))
  5695. (funcall fun)))))
  5696. (defun org-fixup-indentation (diff)
  5697. "Change the indentation in the current entry by DIFF
  5698. However, if any line in the current entry has no indentation, or if it
  5699. would end up with no indentation after the change, nothing at all is done."
  5700. (save-excursion
  5701. (let ((end (save-excursion (outline-next-heading)
  5702. (point-marker)))
  5703. (prohibit (if (> diff 0)
  5704. "^\\S-"
  5705. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5706. col)
  5707. (unless (save-excursion (end-of-line 1)
  5708. (re-search-forward prohibit end t))
  5709. (while (and (< (point) end)
  5710. (re-search-forward "^[ \t]+" end t))
  5711. (goto-char (match-end 0))
  5712. (setq col (current-column))
  5713. (if (< diff 0) (replace-match ""))
  5714. (indent-to (+ diff col))))
  5715. (move-marker end nil))))
  5716. (defun org-convert-to-odd-levels ()
  5717. "Convert an org-mode file with all levels allowed to one with odd levels.
  5718. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5719. level 5 etc."
  5720. (interactive)
  5721. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5722. (let ((org-odd-levels-only nil) n)
  5723. (save-excursion
  5724. (goto-char (point-min))
  5725. (while (re-search-forward "^\\*\\*+ " nil t)
  5726. (setq n (- (length (match-string 0)) 2))
  5727. (while (>= (setq n (1- n)) 0)
  5728. (org-demote))
  5729. (end-of-line 1))))))
  5730. (defun org-convert-to-oddeven-levels ()
  5731. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5732. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5733. section with an even level, conversion would destroy the structure of the file. An error
  5734. is signaled in this case."
  5735. (interactive)
  5736. (goto-char (point-min))
  5737. ;; First check if there are no even levels
  5738. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5739. (org-show-context t)
  5740. (error "Not all levels are odd in this file. Conversion not possible."))
  5741. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5742. (let ((org-odd-levels-only nil) n)
  5743. (save-excursion
  5744. (goto-char (point-min))
  5745. (while (re-search-forward "^\\*\\*+ " nil t)
  5746. (setq n (/ (1- (length (match-string 0))) 2))
  5747. (while (>= (setq n (1- n)) 0)
  5748. (org-promote))
  5749. (end-of-line 1))))))
  5750. (defun org-tr-level (n)
  5751. "Make N odd if required."
  5752. (if org-odd-levels-only (1+ (/ n 2)) n))
  5753. ;;; Vertical tree motion, cutting and pasting of subtrees
  5754. (defun org-move-subtree-up (&optional arg)
  5755. "Move the current subtree up past ARG headlines of the same level."
  5756. (interactive "p")
  5757. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5758. (defun org-move-subtree-down (&optional arg)
  5759. "Move the current subtree down past ARG headlines of the same level."
  5760. (interactive "p")
  5761. (setq arg (prefix-numeric-value arg))
  5762. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5763. 'outline-get-last-sibling))
  5764. (ins-point (make-marker))
  5765. (cnt (abs arg))
  5766. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5767. ;; Select the tree
  5768. (org-back-to-heading)
  5769. (setq beg0 (point))
  5770. (save-excursion
  5771. (setq ne-beg (org-back-over-empty-lines))
  5772. (setq beg (point)))
  5773. (save-match-data
  5774. (save-excursion (outline-end-of-heading)
  5775. (setq folded (org-invisible-p)))
  5776. (outline-end-of-subtree))
  5777. (outline-next-heading)
  5778. (setq ne-end (org-back-over-empty-lines))
  5779. (setq end (point))
  5780. (goto-char beg0)
  5781. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5782. ;; include less whitespace
  5783. (save-excursion
  5784. (goto-char beg)
  5785. (forward-line (- ne-beg ne-end))
  5786. (setq beg (point))))
  5787. ;; Find insertion point, with error handling
  5788. (while (> cnt 0)
  5789. (or (and (funcall movfunc) (looking-at outline-regexp))
  5790. (progn (goto-char beg0)
  5791. (error "Cannot move past superior level or buffer limit")))
  5792. (setq cnt (1- cnt)))
  5793. (if (> arg 0)
  5794. ;; Moving forward - still need to move over subtree
  5795. (progn (org-end-of-subtree t t)
  5796. (save-excursion
  5797. (org-back-over-empty-lines)
  5798. (or (bolp) (newline)))))
  5799. (setq ne-ins (org-back-over-empty-lines))
  5800. (move-marker ins-point (point))
  5801. (setq txt (buffer-substring beg end))
  5802. (delete-region beg end)
  5803. (outline-flag-region (1- beg) beg nil)
  5804. (outline-flag-region (1- (point)) (point) nil)
  5805. (insert txt)
  5806. (or (bolp) (insert "\n"))
  5807. (setq ins-end (point))
  5808. (goto-char ins-point)
  5809. (org-skip-whitespace)
  5810. (when (and (< arg 0)
  5811. (org-first-sibling-p)
  5812. (> ne-ins ne-beg))
  5813. ;; Move whitespace back to beginning
  5814. (save-excursion
  5815. (goto-char ins-end)
  5816. (let ((kill-whole-line t))
  5817. (kill-line (- ne-ins ne-beg)) (point)))
  5818. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5819. (move-marker ins-point nil)
  5820. (org-compact-display-after-subtree-move)
  5821. (unless folded
  5822. (org-show-entry)
  5823. (show-children)
  5824. (org-cycle-hide-drawers 'children))))
  5825. (defvar org-subtree-clip ""
  5826. "Clipboard for cut and paste of subtrees.
  5827. This is actually only a copy of the kill, because we use the normal kill
  5828. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5829. (defvar org-subtree-clip-folded nil
  5830. "Was the last copied subtree folded?
  5831. This is used to fold the tree back after pasting.")
  5832. (defun org-cut-subtree (&optional n)
  5833. "Cut the current subtree into the clipboard.
  5834. With prefix arg N, cut this many sequential subtrees.
  5835. This is a short-hand for marking the subtree and then cutting it."
  5836. (interactive "p")
  5837. (org-copy-subtree n 'cut))
  5838. (defun org-copy-subtree (&optional n cut)
  5839. "Cut the current subtree into the clipboard.
  5840. With prefix arg N, cut this many sequential subtrees.
  5841. This is a short-hand for marking the subtree and then copying it.
  5842. If CUT is non-nil, actually cut the subtree."
  5843. (interactive "p")
  5844. (let (beg end folded (beg0 (point)))
  5845. (if (interactive-p)
  5846. (org-back-to-heading nil) ; take what looks like a subtree
  5847. (org-back-to-heading t)) ; take what is really there
  5848. (org-back-over-empty-lines)
  5849. (setq beg (point))
  5850. (skip-chars-forward " \t\r\n")
  5851. (save-match-data
  5852. (save-excursion (outline-end-of-heading)
  5853. (setq folded (org-invisible-p)))
  5854. (condition-case nil
  5855. (outline-forward-same-level (1- n))
  5856. (error nil))
  5857. (org-end-of-subtree t t))
  5858. (org-back-over-empty-lines)
  5859. (setq end (point))
  5860. (goto-char beg0)
  5861. (when (> end beg)
  5862. (setq org-subtree-clip-folded folded)
  5863. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5864. (setq org-subtree-clip (current-kill 0))
  5865. (message "%s: Subtree(s) with %d characters"
  5866. (if cut "Cut" "Copied")
  5867. (length org-subtree-clip)))))
  5868. (defun org-paste-subtree (&optional level tree)
  5869. "Paste the clipboard as a subtree, with modification of headline level.
  5870. The entire subtree is promoted or demoted in order to match a new headline
  5871. level. By default, the new level is derived from the visible headings
  5872. before and after the insertion point, and taken to be the inferior headline
  5873. level of the two. So if the previous visible heading is level 3 and the
  5874. next is level 4 (or vice versa), level 4 will be used for insertion.
  5875. This makes sure that the subtree remains an independent subtree and does
  5876. not swallow low level entries.
  5877. You can also force a different level, either by using a numeric prefix
  5878. argument, or by inserting the heading marker by hand. For example, if the
  5879. cursor is after \"*****\", then the tree will be shifted to level 5.
  5880. If you want to insert the tree as is, just use \\[yank].
  5881. If optional TREE is given, use this text instead of the kill ring."
  5882. (interactive "P")
  5883. (unless (org-kill-is-subtree-p tree)
  5884. (error "%s"
  5885. (substitute-command-keys
  5886. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5887. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5888. (^re (concat "^\\(" outline-regexp "\\)"))
  5889. (re (concat "\\(" outline-regexp "\\)"))
  5890. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5891. (old-level (if (string-match ^re txt)
  5892. (- (match-end 0) (match-beginning 0) 1)
  5893. -1))
  5894. (force-level (cond (level (prefix-numeric-value level))
  5895. ((string-match
  5896. ^re_ (buffer-substring (point-at-bol) (point)))
  5897. (- (match-end 1) (match-beginning 1)))
  5898. (t nil)))
  5899. (previous-level (save-excursion
  5900. (condition-case nil
  5901. (progn
  5902. (outline-previous-visible-heading 1)
  5903. (if (looking-at re)
  5904. (- (match-end 0) (match-beginning 0) 1)
  5905. 1))
  5906. (error 1))))
  5907. (next-level (save-excursion
  5908. (condition-case nil
  5909. (progn
  5910. (or (looking-at outline-regexp)
  5911. (outline-next-visible-heading 1))
  5912. (if (looking-at re)
  5913. (- (match-end 0) (match-beginning 0) 1)
  5914. 1))
  5915. (error 1))))
  5916. (new-level (or force-level (max previous-level next-level)))
  5917. (shift (if (or (= old-level -1)
  5918. (= new-level -1)
  5919. (= old-level new-level))
  5920. 0
  5921. (- new-level old-level)))
  5922. (delta (if (> shift 0) -1 1))
  5923. (func (if (> shift 0) 'org-demote 'org-promote))
  5924. (org-odd-levels-only nil)
  5925. beg end)
  5926. ;; Remove the forced level indicator
  5927. (if force-level
  5928. (delete-region (point-at-bol) (point)))
  5929. ;; Paste
  5930. (beginning-of-line 1)
  5931. (org-back-over-empty-lines) ;; FIXME: correct fix????
  5932. (setq beg (point))
  5933. (insert-before-markers txt) ;; FIXME: correct fix????
  5934. (unless (string-match "\n\\'" txt) (insert "\n"))
  5935. (setq end (point))
  5936. (goto-char beg)
  5937. (skip-chars-forward " \t\n\r")
  5938. (setq beg (point))
  5939. ;; Shift if necessary
  5940. (unless (= shift 0)
  5941. (save-restriction
  5942. (narrow-to-region beg end)
  5943. (while (not (= shift 0))
  5944. (org-map-region func (point-min) (point-max))
  5945. (setq shift (+ delta shift)))
  5946. (goto-char (point-min))))
  5947. (when (interactive-p)
  5948. (message "Clipboard pasted as level %d subtree" new-level))
  5949. (if (and kill-ring
  5950. (eq org-subtree-clip (current-kill 0))
  5951. org-subtree-clip-folded)
  5952. ;; The tree was folded before it was killed/copied
  5953. (hide-subtree))))
  5954. (defun org-kill-is-subtree-p (&optional txt)
  5955. "Check if the current kill is an outline subtree, or a set of trees.
  5956. Returns nil if kill does not start with a headline, or if the first
  5957. headline level is not the largest headline level in the tree.
  5958. So this will actually accept several entries of equal levels as well,
  5959. which is OK for `org-paste-subtree'.
  5960. If optional TXT is given, check this string instead of the current kill."
  5961. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5962. (start-level (and kill
  5963. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5964. org-outline-regexp "\\)")
  5965. kill)
  5966. (- (match-end 2) (match-beginning 2) 1)))
  5967. (re (concat "^" org-outline-regexp))
  5968. (start (1+ (match-beginning 2))))
  5969. (if (not start-level)
  5970. (progn
  5971. nil) ;; does not even start with a heading
  5972. (catch 'exit
  5973. (while (setq start (string-match re kill (1+ start)))
  5974. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5975. (throw 'exit nil)))
  5976. t))))
  5977. (defun org-narrow-to-subtree ()
  5978. "Narrow buffer to the current subtree."
  5979. (interactive)
  5980. (save-excursion
  5981. (save-match-data
  5982. (narrow-to-region
  5983. (progn (org-back-to-heading) (point))
  5984. (progn (org-end-of-subtree t t) (point))))))
  5985. ;;; Outline Sorting
  5986. (defun org-sort (with-case)
  5987. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5988. Optional argument WITH-CASE means sort case-sensitively."
  5989. (interactive "P")
  5990. (if (org-at-table-p)
  5991. (org-call-with-arg 'org-table-sort-lines with-case)
  5992. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5993. (defvar org-priority-regexp) ; defined later in the file
  5994. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5995. "Sort entries on a certain level of an outline tree.
  5996. If there is an active region, the entries in the region are sorted.
  5997. Else, if the cursor is before the first entry, sort the top-level items.
  5998. Else, the children of the entry at point are sorted.
  5999. Sorting can be alphabetically, numerically, and by date/time as given by
  6000. the first time stamp in the entry. The command prompts for the sorting
  6001. type unless it has been given to the function through the SORTING-TYPE
  6002. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6003. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6004. called with point at the beginning of the record. It must return either
  6005. a string or a number that should serve as the sorting key for that record.
  6006. Comparing entries ignores case by default. However, with an optional argument
  6007. WITH-CASE, the sorting considers case as well."
  6008. (interactive "P")
  6009. (let ((case-func (if with-case 'identity 'downcase))
  6010. start beg end stars re re2
  6011. txt what tmp plain-list-p)
  6012. ;; Find beginning and end of region to sort
  6013. (cond
  6014. ((org-region-active-p)
  6015. ;; we will sort the region
  6016. (setq end (region-end)
  6017. what "region")
  6018. (goto-char (region-beginning))
  6019. (if (not (org-on-heading-p)) (outline-next-heading))
  6020. (setq start (point)))
  6021. ((org-at-item-p)
  6022. ;; we will sort this plain list
  6023. (org-beginning-of-item-list) (setq start (point))
  6024. (org-end-of-item-list) (setq end (point))
  6025. (goto-char start)
  6026. (setq plain-list-p t
  6027. what "plain list"))
  6028. ((or (org-on-heading-p)
  6029. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6030. ;; we will sort the children of the current headline
  6031. (org-back-to-heading)
  6032. (setq start (point)
  6033. end (progn (org-end-of-subtree t t)
  6034. (org-back-over-empty-lines)
  6035. (point))
  6036. what "children")
  6037. (goto-char start)
  6038. (show-subtree)
  6039. (outline-next-heading))
  6040. (t
  6041. ;; we will sort the top-level entries in this file
  6042. (goto-char (point-min))
  6043. (or (org-on-heading-p) (outline-next-heading))
  6044. (setq start (point) end (point-max) what "top-level")
  6045. (goto-char start)
  6046. (show-all)))
  6047. (setq beg (point))
  6048. (if (>= beg end) (error "Nothing to sort"))
  6049. (unless plain-list-p
  6050. (looking-at "\\(\\*+\\)")
  6051. (setq stars (match-string 1)
  6052. re (concat "^" (regexp-quote stars) " +")
  6053. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6054. txt (buffer-substring beg end))
  6055. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6056. (if (and (not (equal stars "*")) (string-match re2 txt))
  6057. (error "Region to sort contains a level above the first entry")))
  6058. (unless sorting-type
  6059. (message
  6060. (if plain-list-p
  6061. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6062. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6063. what)
  6064. (setq sorting-type (read-char-exclusive))
  6065. (and (= (downcase sorting-type) ?f)
  6066. (setq getkey-func
  6067. (completing-read "Sort using function: "
  6068. obarray 'fboundp t nil nil))
  6069. (setq getkey-func (intern getkey-func)))
  6070. (and (= (downcase sorting-type) ?r)
  6071. (setq property
  6072. (completing-read "Property: "
  6073. (mapcar 'list (org-buffer-property-keys t))
  6074. nil t))))
  6075. (message "Sorting entries...")
  6076. (save-restriction
  6077. (narrow-to-region start end)
  6078. (let ((dcst (downcase sorting-type))
  6079. (now (current-time)))
  6080. (sort-subr
  6081. (/= dcst sorting-type)
  6082. ;; This function moves to the beginning character of the "record" to
  6083. ;; be sorted.
  6084. (if plain-list-p
  6085. (lambda nil
  6086. (if (org-at-item-p) t (goto-char (point-max))))
  6087. (lambda nil
  6088. (if (re-search-forward re nil t)
  6089. (goto-char (match-beginning 0))
  6090. (goto-char (point-max)))))
  6091. ;; This function moves to the last character of the "record" being
  6092. ;; sorted.
  6093. (if plain-list-p
  6094. 'org-end-of-item
  6095. (lambda nil
  6096. (save-match-data
  6097. (condition-case nil
  6098. (outline-forward-same-level 1)
  6099. (error
  6100. (goto-char (point-max)))))))
  6101. ;; This function returns the value that gets sorted against.
  6102. (if plain-list-p
  6103. (lambda nil
  6104. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6105. (cond
  6106. ((= dcst ?n)
  6107. (string-to-number (buffer-substring (match-end 0)
  6108. (point-at-eol))))
  6109. ((= dcst ?a)
  6110. (buffer-substring (match-end 0) (point-at-eol)))
  6111. ((= dcst ?t)
  6112. (if (re-search-forward org-ts-regexp
  6113. (point-at-eol) t)
  6114. (org-time-string-to-time (match-string 0))
  6115. now))
  6116. ((= dcst ?f)
  6117. (if getkey-func
  6118. (progn
  6119. (setq tmp (funcall getkey-func))
  6120. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6121. tmp)
  6122. (error "Invalid key function `%s'" getkey-func)))
  6123. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6124. (lambda nil
  6125. (cond
  6126. ((= dcst ?n)
  6127. (if (looking-at outline-regexp)
  6128. (string-to-number (buffer-substring (match-end 0)
  6129. (point-at-eol)))
  6130. nil))
  6131. ((= dcst ?a)
  6132. (funcall case-func (buffer-substring (point-at-bol)
  6133. (point-at-eol))))
  6134. ((= dcst ?t)
  6135. (if (re-search-forward org-ts-regexp
  6136. (save-excursion
  6137. (forward-line 2)
  6138. (point)) t)
  6139. (org-time-string-to-time (match-string 0))
  6140. now))
  6141. ((= dcst ?p)
  6142. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6143. (string-to-char (match-string 2))
  6144. org-default-priority))
  6145. ((= dcst ?r)
  6146. (or (org-entry-get nil property) ""))
  6147. ((= dcst ?f)
  6148. (if getkey-func
  6149. (progn
  6150. (setq tmp (funcall getkey-func))
  6151. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6152. tmp)
  6153. (error "Invalid key function `%s'" getkey-func)))
  6154. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6155. nil
  6156. (cond
  6157. ((= dcst ?a) 'string<)
  6158. ((= dcst ?t) 'time-less-p)
  6159. (t nil)))))
  6160. (message "Sorting entries...done")))
  6161. (defun org-do-sort (table what &optional with-case sorting-type)
  6162. "Sort TABLE of WHAT according to SORTING-TYPE.
  6163. The user will be prompted for the SORTING-TYPE if the call to this
  6164. function does not specify it. WHAT is only for the prompt, to indicate
  6165. what is being sorted. The sorting key will be extracted from
  6166. the car of the elements of the table.
  6167. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6168. (unless sorting-type
  6169. (message
  6170. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6171. what)
  6172. (setq sorting-type (read-char-exclusive)))
  6173. (let ((dcst (downcase sorting-type))
  6174. extractfun comparefun)
  6175. ;; Define the appropriate functions
  6176. (cond
  6177. ((= dcst ?n)
  6178. (setq extractfun 'string-to-number
  6179. comparefun (if (= dcst sorting-type) '< '>)))
  6180. ((= dcst ?a)
  6181. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6182. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6183. comparefun (if (= dcst sorting-type)
  6184. 'string<
  6185. (lambda (a b) (and (not (string< a b))
  6186. (not (string= a b)))))))
  6187. ((= dcst ?t)
  6188. (setq extractfun
  6189. (lambda (x)
  6190. (if (string-match org-ts-regexp x)
  6191. (time-to-seconds
  6192. (org-time-string-to-time (match-string 0 x)))
  6193. 0))
  6194. comparefun (if (= dcst sorting-type) '< '>)))
  6195. (t (error "Invalid sorting type `%c'" sorting-type)))
  6196. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6197. table)
  6198. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6199. ;;;; Plain list items, including checkboxes
  6200. ;;; Plain list items
  6201. (defun org-at-item-p ()
  6202. "Is point in a line starting a hand-formatted item?"
  6203. (let ((llt org-plain-list-ordered-item-terminator))
  6204. (save-excursion
  6205. (goto-char (point-at-bol))
  6206. (looking-at
  6207. (cond
  6208. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6209. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6210. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6211. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6212. (defun org-in-item-p ()
  6213. "It the cursor inside a plain list item.
  6214. Does not have to be the first line."
  6215. (save-excursion
  6216. (condition-case nil
  6217. (progn
  6218. (org-beginning-of-item)
  6219. (org-at-item-p)
  6220. t)
  6221. (error nil))))
  6222. (defun org-insert-item (&optional checkbox)
  6223. "Insert a new item at the current level.
  6224. Return t when things worked, nil when we are not in an item."
  6225. (when (save-excursion
  6226. (condition-case nil
  6227. (progn
  6228. (org-beginning-of-item)
  6229. (org-at-item-p)
  6230. (if (org-invisible-p) (error "Invisible item"))
  6231. t)
  6232. (error nil)))
  6233. (let* ((bul (match-string 0))
  6234. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6235. (match-end 0)))
  6236. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6237. pos)
  6238. (cond
  6239. ((and (org-at-item-p) (<= (point) eow))
  6240. ;; before the bullet
  6241. (beginning-of-line 1)
  6242. (open-line (if blank 2 1)))
  6243. ((<= (point) eow)
  6244. (beginning-of-line 1))
  6245. (t (newline (if blank 2 1))))
  6246. (insert bul (if checkbox "[ ]" ""))
  6247. (just-one-space)
  6248. (setq pos (point))
  6249. (end-of-line 1)
  6250. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6251. (org-maybe-renumber-ordered-list)
  6252. (and checkbox (org-update-checkbox-count-maybe))
  6253. t))
  6254. ;;; Checkboxes
  6255. (defun org-at-item-checkbox-p ()
  6256. "Is point at a line starting a plain-list item with a checklet?"
  6257. (and (org-at-item-p)
  6258. (save-excursion
  6259. (goto-char (match-end 0))
  6260. (skip-chars-forward " \t")
  6261. (looking-at "\\[[- X]\\]"))))
  6262. (defun org-toggle-checkbox (&optional arg)
  6263. "Toggle the checkbox in the current line."
  6264. (interactive "P")
  6265. (catch 'exit
  6266. (let (beg end status (firstnew 'unknown))
  6267. (cond
  6268. ((org-region-active-p)
  6269. (setq beg (region-beginning) end (region-end)))
  6270. ((org-on-heading-p)
  6271. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6272. ((org-at-item-checkbox-p)
  6273. (let ((pos (point)))
  6274. (replace-match
  6275. (cond (arg "[-]")
  6276. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6277. (t "[ ]"))
  6278. t t)
  6279. (goto-char pos))
  6280. (throw 'exit t))
  6281. (t (error "Not at a checkbox or heading, and no active region")))
  6282. (save-excursion
  6283. (goto-char beg)
  6284. (while (< (point) end)
  6285. (when (org-at-item-checkbox-p)
  6286. (setq status (equal (match-string 0) "[X]"))
  6287. (when (eq firstnew 'unknown)
  6288. (setq firstnew (not status)))
  6289. (replace-match
  6290. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6291. (beginning-of-line 2)))))
  6292. (org-update-checkbox-count-maybe))
  6293. (defun org-update-checkbox-count-maybe ()
  6294. "Update checkbox statistics unless turned off by user."
  6295. (when org-provide-checkbox-statistics
  6296. (org-update-checkbox-count)))
  6297. (defun org-update-checkbox-count (&optional all)
  6298. "Update the checkbox statistics in the current section.
  6299. This will find all statistic cookies like [57%] and [6/12] and update them
  6300. with the current numbers. With optional prefix argument ALL, do this for
  6301. the whole buffer."
  6302. (interactive "P")
  6303. (save-excursion
  6304. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6305. (beg (condition-case nil
  6306. (progn (outline-back-to-heading) (point))
  6307. (error (point-min))))
  6308. (end (move-marker (make-marker)
  6309. (progn (outline-next-heading) (point))))
  6310. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6311. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6312. (re-find (concat re "\\|" re-box))
  6313. beg-cookie end-cookie is-percent c-on c-off lim
  6314. eline curr-ind next-ind continue-from startsearch
  6315. (cstat 0)
  6316. )
  6317. (when all
  6318. (goto-char (point-min))
  6319. (outline-next-heading)
  6320. (setq beg (point) end (point-max)))
  6321. (goto-char end)
  6322. ;; find each statistic cookie
  6323. (while (re-search-backward re-find beg t)
  6324. (setq beg-cookie (match-beginning 1)
  6325. end-cookie (match-end 1)
  6326. cstat (+ cstat (if end-cookie 1 0))
  6327. startsearch (point-at-eol)
  6328. continue-from (point-at-bol)
  6329. is-percent (match-beginning 2)
  6330. lim (cond
  6331. ((org-on-heading-p) (outline-next-heading) (point))
  6332. ((org-at-item-p) (org-end-of-item) (point))
  6333. (t nil))
  6334. c-on 0
  6335. c-off 0)
  6336. (when lim
  6337. ;; find first checkbox for this cookie and gather
  6338. ;; statistics from all that are at this indentation level
  6339. (goto-char startsearch)
  6340. (if (re-search-forward re-box lim t)
  6341. (progn
  6342. (org-beginning-of-item)
  6343. (setq curr-ind (org-get-indentation))
  6344. (setq next-ind curr-ind)
  6345. (while (= curr-ind next-ind)
  6346. (save-excursion (end-of-line) (setq eline (point)))
  6347. (if (re-search-forward re-box eline t)
  6348. (if (member (match-string 2) '("[ ]" "[-]"))
  6349. (setq c-off (1+ c-off))
  6350. (setq c-on (1+ c-on))
  6351. )
  6352. )
  6353. (org-end-of-item)
  6354. (setq next-ind (org-get-indentation))
  6355. )))
  6356. (goto-char continue-from)
  6357. ;; update cookie
  6358. (when end-cookie
  6359. (delete-region beg-cookie end-cookie)
  6360. (goto-char beg-cookie)
  6361. (insert
  6362. (if is-percent
  6363. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6364. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6365. ;; update items checkbox if it has one
  6366. (when (org-at-item-p)
  6367. (org-beginning-of-item)
  6368. (when (and (> (+ c-on c-off) 0)
  6369. (re-search-forward re-box (point-at-eol) t))
  6370. (setq beg-cookie (match-beginning 2)
  6371. end-cookie (match-end 2))
  6372. (delete-region beg-cookie end-cookie)
  6373. (goto-char beg-cookie)
  6374. (cond ((= c-off 0) (insert "[X]"))
  6375. ((= c-on 0) (insert "[ ]"))
  6376. (t (insert "[-]")))
  6377. )))
  6378. (goto-char continue-from))
  6379. (when (interactive-p)
  6380. (message "Checkbox satistics updated %s (%d places)"
  6381. (if all "in entire file" "in current outline entry") cstat)))))
  6382. (defun org-get-checkbox-statistics-face ()
  6383. "Select the face for checkbox statistics.
  6384. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6385. it will be `org-todo'."
  6386. (if (match-end 1)
  6387. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6388. (if (and (> (match-end 2) (match-beginning 2))
  6389. (equal (match-string 2) (match-string 3)))
  6390. 'org-done
  6391. 'org-todo)))
  6392. (defun org-get-indentation (&optional line)
  6393. "Get the indentation of the current line, interpreting tabs.
  6394. When LINE is given, assume it represents a line and compute its indentation."
  6395. (if line
  6396. (if (string-match "^ *" (org-remove-tabs line))
  6397. (match-end 0))
  6398. (save-excursion
  6399. (beginning-of-line 1)
  6400. (skip-chars-forward " \t")
  6401. (current-column))))
  6402. (defun org-remove-tabs (s &optional width)
  6403. "Replace tabulators in S with spaces.
  6404. Assumes that s is a single line, starting in column 0."
  6405. (setq width (or width tab-width))
  6406. (while (string-match "\t" s)
  6407. (setq s (replace-match
  6408. (make-string
  6409. (- (* width (/ (+ (match-beginning 0) width) width))
  6410. (match-beginning 0)) ?\ )
  6411. t t s)))
  6412. s)
  6413. (defun org-fix-indentation (line ind)
  6414. "Fix indentation in LINE.
  6415. IND is a cons cell with target and minimum indentation.
  6416. If the current indenation in LINE is smaller than the minimum,
  6417. leave it alone. If it is larger than ind, set it to the target."
  6418. (let* ((l (org-remove-tabs line))
  6419. (i (org-get-indentation l))
  6420. (i1 (car ind)) (i2 (cdr ind)))
  6421. (if (>= i i2) (setq l (substring line i2)))
  6422. (if (> i1 0)
  6423. (concat (make-string i1 ?\ ) l)
  6424. l)))
  6425. (defcustom org-empty-line-terminates-plain-lists nil
  6426. "Non-nil means, an empty line ends all plain list levels.
  6427. When nil, empty lines are part of the preceeding item."
  6428. :group 'org-plain-lists
  6429. :type 'boolean)
  6430. (defun org-beginning-of-item ()
  6431. "Go to the beginning of the current hand-formatted item.
  6432. If the cursor is not in an item, throw an error."
  6433. (interactive)
  6434. (let ((pos (point))
  6435. (limit (save-excursion
  6436. (condition-case nil
  6437. (progn
  6438. (org-back-to-heading)
  6439. (beginning-of-line 2) (point))
  6440. (error (point-min)))))
  6441. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6442. ind ind1)
  6443. (if (org-at-item-p)
  6444. (beginning-of-line 1)
  6445. (beginning-of-line 1)
  6446. (skip-chars-forward " \t")
  6447. (setq ind (current-column))
  6448. (if (catch 'exit
  6449. (while t
  6450. (beginning-of-line 0)
  6451. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6452. (if (looking-at "[ \t]*$")
  6453. (setq ind1 ind-empty)
  6454. (skip-chars-forward " \t")
  6455. (setq ind1 (current-column)))
  6456. (if (< ind1 ind)
  6457. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6458. nil
  6459. (goto-char pos)
  6460. (error "Not in an item")))))
  6461. (defun org-end-of-item ()
  6462. "Go to the end 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. ind1
  6467. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6468. (limit (save-excursion (outline-next-heading) (point)))
  6469. (ind (save-excursion
  6470. (org-beginning-of-item)
  6471. (skip-chars-forward " \t")
  6472. (current-column)))
  6473. (end (catch 'exit
  6474. (while t
  6475. (beginning-of-line 2)
  6476. (if (eobp) (throw 'exit (point)))
  6477. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6478. (if (looking-at "[ \t]*$")
  6479. (setq ind1 ind-empty)
  6480. (skip-chars-forward " \t")
  6481. (setq ind1 (current-column)))
  6482. (if (<= ind1 ind)
  6483. (throw 'exit (point-at-bol)))))))
  6484. (if end
  6485. (goto-char end)
  6486. (goto-char pos)
  6487. (error "Not in an item"))))
  6488. (defun org-next-item ()
  6489. "Move to the beginning of the next item in the current plain list.
  6490. Error if not at a plain list, or if this is the last item in the list."
  6491. (interactive)
  6492. (let (ind ind1 (pos (point)))
  6493. (org-beginning-of-item)
  6494. (setq ind (org-get-indentation))
  6495. (org-end-of-item)
  6496. (setq ind1 (org-get-indentation))
  6497. (unless (and (org-at-item-p) (= ind ind1))
  6498. (goto-char pos)
  6499. (error "On last item"))))
  6500. (defun org-previous-item ()
  6501. "Move to the beginning of the previous item in the current plain list.
  6502. Error if not at a plain list, or if this is the first item in the list."
  6503. (interactive)
  6504. (let (beg ind ind1 (pos (point)))
  6505. (org-beginning-of-item)
  6506. (setq beg (point))
  6507. (setq ind (org-get-indentation))
  6508. (goto-char beg)
  6509. (catch 'exit
  6510. (while t
  6511. (beginning-of-line 0)
  6512. (if (looking-at "[ \t]*$")
  6513. nil
  6514. (if (<= (setq ind1 (org-get-indentation)) ind)
  6515. (throw 'exit t)))))
  6516. (condition-case nil
  6517. (if (or (not (org-at-item-p))
  6518. (< ind1 (1- ind)))
  6519. (error "")
  6520. (org-beginning-of-item))
  6521. (error (goto-char pos)
  6522. (error "On first item")))))
  6523. (defun org-first-list-item-p ()
  6524. "Is this heading the item in a plain list?"
  6525. (unless (org-at-item-p)
  6526. (error "Not at a plain list item"))
  6527. (org-beginning-of-item)
  6528. (= (point) (save-excursion (org-beginning-of-item-list))))
  6529. (defun org-move-item-down ()
  6530. "Move the plain list item at point down, i.e. swap with following item.
  6531. Subitems (items with larger indentation) are considered part of the item,
  6532. so this really moves item trees."
  6533. (interactive)
  6534. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6535. (org-beginning-of-item)
  6536. (setq beg0 (point))
  6537. (save-excursion
  6538. (setq ne-beg (org-back-over-empty-lines))
  6539. (setq beg (point)))
  6540. (goto-char beg0)
  6541. (setq ind (org-get-indentation))
  6542. (org-end-of-item)
  6543. (setq end0 (point))
  6544. (setq ind1 (org-get-indentation))
  6545. (setq ne-end (org-back-over-empty-lines))
  6546. (setq end (point))
  6547. (goto-char beg0)
  6548. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6549. ;; include less whitespace
  6550. (save-excursion
  6551. (goto-char beg)
  6552. (forward-line (- ne-beg ne-end))
  6553. (setq beg (point))))
  6554. (goto-char end0)
  6555. (if (and (org-at-item-p) (= ind ind1))
  6556. (progn
  6557. (org-end-of-item)
  6558. (org-back-over-empty-lines)
  6559. (setq txt (buffer-substring beg end))
  6560. (save-excursion
  6561. (delete-region beg end))
  6562. (setq pos (point))
  6563. (insert txt)
  6564. (goto-char pos) (org-skip-whitespace)
  6565. (org-maybe-renumber-ordered-list))
  6566. (goto-char pos)
  6567. (error "Cannot move this item further down"))))
  6568. (defun org-move-item-up (arg)
  6569. "Move the plain list item at point up, i.e. swap with previous item.
  6570. Subitems (items with larger indentation) are considered part of the item,
  6571. so this really moves item trees."
  6572. (interactive "p")
  6573. (let (beg beg0 end ind ind1 (pos (point)) txt
  6574. ne-beg ne-ins ins-end)
  6575. (org-beginning-of-item)
  6576. (setq beg0 (point))
  6577. (setq ind (org-get-indentation))
  6578. (save-excursion
  6579. (setq ne-beg (org-back-over-empty-lines))
  6580. (setq beg (point)))
  6581. (goto-char beg0)
  6582. (org-end-of-item)
  6583. (setq end (point))
  6584. (goto-char beg0)
  6585. (catch 'exit
  6586. (while t
  6587. (beginning-of-line 0)
  6588. (if (looking-at "[ \t]*$")
  6589. (if org-empty-line-terminates-plain-lists
  6590. (progn
  6591. (goto-char pos)
  6592. (error "Cannot move this item further up"))
  6593. nil)
  6594. (if (<= (setq ind1 (org-get-indentation)) ind)
  6595. (throw 'exit t)))))
  6596. (condition-case nil
  6597. (org-beginning-of-item)
  6598. (error (goto-char beg)
  6599. (error "Cannot move this item further up")))
  6600. (setq ind1 (org-get-indentation))
  6601. (if (and (org-at-item-p) (= ind ind1))
  6602. (progn
  6603. (setq ne-ins (org-back-over-empty-lines))
  6604. (setq txt (buffer-substring beg end))
  6605. (save-excursion
  6606. (delete-region beg end))
  6607. (setq pos (point))
  6608. (insert txt)
  6609. (setq ins-end (point))
  6610. (goto-char pos) (org-skip-whitespace)
  6611. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6612. ;; Move whitespace back to beginning
  6613. (save-excursion
  6614. (goto-char ins-end)
  6615. (let ((kill-whole-line t))
  6616. (kill-line (- ne-ins ne-beg)) (point)))
  6617. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6618. (org-maybe-renumber-ordered-list))
  6619. (goto-char pos)
  6620. (error "Cannot move this item further up"))))
  6621. (defun org-maybe-renumber-ordered-list ()
  6622. "Renumber the ordered list at point if setup allows it.
  6623. This tests the user option `org-auto-renumber-ordered-lists' before
  6624. doing the renumbering."
  6625. (interactive)
  6626. (when (and org-auto-renumber-ordered-lists
  6627. (org-at-item-p))
  6628. (if (match-beginning 3)
  6629. (org-renumber-ordered-list 1)
  6630. (org-fix-bullet-type))))
  6631. (defun org-maybe-renumber-ordered-list-safe ()
  6632. (condition-case nil
  6633. (save-excursion
  6634. (org-maybe-renumber-ordered-list))
  6635. (error nil)))
  6636. (defun org-cycle-list-bullet (&optional which)
  6637. "Cycle through the different itemize/enumerate bullets.
  6638. This cycle the entire list level through the sequence:
  6639. `-' -> `+' -> `*' -> `1.' -> `1)'
  6640. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6641. 0 meand `-', 1 means `+' etc."
  6642. (interactive "P")
  6643. (org-preserve-lc
  6644. (org-beginning-of-item-list)
  6645. (org-at-item-p)
  6646. (beginning-of-line 1)
  6647. (let ((current (match-string 0))
  6648. (prevp (eq which 'previous))
  6649. new)
  6650. (setq new (cond
  6651. ((and (numberp which)
  6652. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6653. ((string-match "-" current) (if prevp "1)" "+"))
  6654. ((string-match "\\+" current)
  6655. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6656. ((string-match "\\*" current) (if prevp "+" "1."))
  6657. ((string-match "\\." current) (if prevp "*" "1)"))
  6658. ((string-match ")" current) (if prevp "1." "-"))
  6659. (t (error "This should not happen"))))
  6660. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6661. (org-fix-bullet-type)
  6662. (org-maybe-renumber-ordered-list))))
  6663. (defun org-get-string-indentation (s)
  6664. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6665. (let ((n -1) (i 0) (w tab-width) c)
  6666. (catch 'exit
  6667. (while (< (setq n (1+ n)) (length s))
  6668. (setq c (aref s n))
  6669. (cond ((= c ?\ ) (setq i (1+ i)))
  6670. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6671. (t (throw 'exit t)))))
  6672. i))
  6673. (defun org-renumber-ordered-list (arg)
  6674. "Renumber an ordered plain list.
  6675. Cursor needs to be in the first line of an item, the line that starts
  6676. with something like \"1.\" or \"2)\"."
  6677. (interactive "p")
  6678. (unless (and (org-at-item-p)
  6679. (match-beginning 3))
  6680. (error "This is not an ordered list"))
  6681. (let ((line (org-current-line))
  6682. (col (current-column))
  6683. (ind (org-get-string-indentation
  6684. (buffer-substring (point-at-bol) (match-beginning 3))))
  6685. ;; (term (substring (match-string 3) -1))
  6686. ind1 (n (1- arg))
  6687. fmt)
  6688. ;; find where this list begins
  6689. (org-beginning-of-item-list)
  6690. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6691. (setq fmt (concat "%d" (match-string 1)))
  6692. (beginning-of-line 0)
  6693. ;; walk forward and replace these numbers
  6694. (catch 'exit
  6695. (while t
  6696. (catch 'next
  6697. (beginning-of-line 2)
  6698. (if (eobp) (throw 'exit nil))
  6699. (if (looking-at "[ \t]*$") (throw 'next nil))
  6700. (skip-chars-forward " \t") (setq ind1 (current-column))
  6701. (if (> ind1 ind) (throw 'next t))
  6702. (if (< ind1 ind) (throw 'exit t))
  6703. (if (not (org-at-item-p)) (throw 'exit nil))
  6704. (delete-region (match-beginning 2) (match-end 2))
  6705. (goto-char (match-beginning 2))
  6706. (insert (format fmt (setq n (1+ n)))))))
  6707. (goto-line line)
  6708. (move-to-column col)))
  6709. (defun org-fix-bullet-type ()
  6710. "Make sure all items in this list have the same bullet as the firsst item."
  6711. (interactive)
  6712. (unless (org-at-item-p) (error "This is not a list"))
  6713. (let ((line (org-current-line))
  6714. (col (current-column))
  6715. (ind (current-indentation))
  6716. ind1 bullet)
  6717. ;; find where this list begins
  6718. (org-beginning-of-item-list)
  6719. (beginning-of-line 1)
  6720. ;; find out what the bullet type is
  6721. (looking-at "[ \t]*\\(\\S-+\\)")
  6722. (setq bullet (match-string 1))
  6723. ;; walk forward and replace these numbers
  6724. (beginning-of-line 0)
  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. (skip-chars-forward " \t")
  6736. (looking-at "\\S-+")
  6737. (replace-match bullet))))
  6738. (goto-line line)
  6739. (move-to-column col)
  6740. (if (string-match "[0-9]" bullet)
  6741. (org-renumber-ordered-list 1))))
  6742. (defun org-beginning-of-item-list ()
  6743. "Go to the beginning of the current item list.
  6744. I.e. to the first item in this list."
  6745. (interactive)
  6746. (org-beginning-of-item)
  6747. (let ((pos (point-at-bol))
  6748. (ind (org-get-indentation))
  6749. ind1)
  6750. ;; find where this list begins
  6751. (catch 'exit
  6752. (while t
  6753. (catch 'next
  6754. (beginning-of-line 0)
  6755. (if (looking-at "[ \t]*$")
  6756. (throw (if (bobp) 'exit 'next) t))
  6757. (skip-chars-forward " \t") (setq ind1 (current-column))
  6758. (if (or (< ind1 ind)
  6759. (and (= ind1 ind)
  6760. (not (org-at-item-p)))
  6761. (bobp))
  6762. (throw 'exit t)
  6763. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6764. (goto-char pos)))
  6765. (defun org-end-of-item-list ()
  6766. "Go to the end of the current item list.
  6767. I.e. to the text after the last item."
  6768. (interactive)
  6769. (org-beginning-of-item)
  6770. (let ((pos (point-at-bol))
  6771. (ind (org-get-indentation))
  6772. ind1)
  6773. ;; find where this list begins
  6774. (catch 'exit
  6775. (while t
  6776. (catch 'next
  6777. (beginning-of-line 2)
  6778. (if (looking-at "[ \t]*$")
  6779. (throw (if (eobp) 'exit 'next) t))
  6780. (skip-chars-forward " \t") (setq ind1 (current-column))
  6781. (if (or (< ind1 ind)
  6782. (and (= ind1 ind)
  6783. (not (org-at-item-p)))
  6784. (eobp))
  6785. (progn
  6786. (setq pos (point-at-bol))
  6787. (throw 'exit t))))))
  6788. (goto-char pos)))
  6789. (defvar org-last-indent-begin-marker (make-marker))
  6790. (defvar org-last-indent-end-marker (make-marker))
  6791. (defun org-outdent-item (arg)
  6792. "Outdent a local list item."
  6793. (interactive "p")
  6794. (org-indent-item (- arg)))
  6795. (defun org-indent-item (arg)
  6796. "Indent a local list item."
  6797. (interactive "p")
  6798. (unless (org-at-item-p)
  6799. (error "Not on an item"))
  6800. (save-excursion
  6801. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6802. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6803. (setq beg org-last-indent-begin-marker
  6804. end org-last-indent-end-marker)
  6805. (org-beginning-of-item)
  6806. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6807. (org-end-of-item)
  6808. (setq end (move-marker org-last-indent-end-marker (point))))
  6809. (goto-char beg)
  6810. (setq tmp (org-item-indent-positions)
  6811. ind (car tmp)
  6812. ind-down (nth 2 tmp)
  6813. ind-up (nth 1 tmp)
  6814. delta (if (> arg 0)
  6815. (if ind-down (- ind-down ind) 2)
  6816. (if ind-up (- ind-up ind) -2)))
  6817. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6818. (while (< (point) end)
  6819. (beginning-of-line 1)
  6820. (skip-chars-forward " \t") (setq ind1 (current-column))
  6821. (delete-region (point-at-bol) (point))
  6822. (or (eolp) (indent-to-column (+ ind1 delta)))
  6823. (beginning-of-line 2))))
  6824. (org-fix-bullet-type)
  6825. (org-maybe-renumber-ordered-list-safe)
  6826. (save-excursion
  6827. (beginning-of-line 0)
  6828. (condition-case nil (org-beginning-of-item) (error nil))
  6829. (org-maybe-renumber-ordered-list-safe)))
  6830. (defun org-item-indent-positions ()
  6831. "Return indentation for plain list items.
  6832. This returns a list with three values: The current indentation, the
  6833. parent indentation and the indentation a child should habe.
  6834. Assumes cursor in item line."
  6835. (let* ((bolpos (point-at-bol))
  6836. (ind (org-get-indentation))
  6837. ind-down ind-up pos)
  6838. (save-excursion
  6839. (org-beginning-of-item-list)
  6840. (skip-chars-backward "\n\r \t")
  6841. (when (org-in-item-p)
  6842. (org-beginning-of-item)
  6843. (setq ind-up (org-get-indentation))))
  6844. (setq pos (point))
  6845. (save-excursion
  6846. (cond
  6847. ((and (condition-case nil (progn (org-previous-item) t)
  6848. (error nil))
  6849. (or (forward-char 1) t)
  6850. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6851. (setq ind-down (org-get-indentation)))
  6852. ((and (goto-char pos)
  6853. (org-at-item-p))
  6854. (goto-char (match-end 0))
  6855. (skip-chars-forward " \t")
  6856. (setq ind-down (current-column)))))
  6857. (list ind ind-up ind-down)))
  6858. ;;; The orgstruct minor mode
  6859. ;; Define a minor mode which can be used in other modes in order to
  6860. ;; integrate the org-mode structure editing commands.
  6861. ;; This is really a hack, because the org-mode structure commands use
  6862. ;; keys which normally belong to the major mode. Here is how it
  6863. ;; works: The minor mode defines all the keys necessary to operate the
  6864. ;; structure commands, but wraps the commands into a function which
  6865. ;; tests if the cursor is currently at a headline or a plain list
  6866. ;; item. If that is the case, the structure command is used,
  6867. ;; temporarily setting many Org-mode variables like regular
  6868. ;; expressions for filling etc. However, when any of those keys is
  6869. ;; used at a different location, function uses `key-binding' to look
  6870. ;; up if the key has an associated command in another currently active
  6871. ;; keymap (minor modes, major mode, global), and executes that
  6872. ;; command. There might be problems if any of the keys is otherwise
  6873. ;; used as a prefix key.
  6874. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6875. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6876. ;; addresses this by checking explicitly for both bindings.
  6877. (defvar orgstruct-mode-map (make-sparse-keymap)
  6878. "Keymap for the minor `orgstruct-mode'.")
  6879. (defvar org-local-vars nil
  6880. "List of local variables, for use by `orgstruct-mode'")
  6881. ;;;###autoload
  6882. (define-minor-mode orgstruct-mode
  6883. "Toggle the minor more `orgstruct-mode'.
  6884. This mode is for using Org-mode structure commands in other modes.
  6885. The following key behave as if Org-mode was active, if the cursor
  6886. is on a headline, or on a plain list item (both in the definition
  6887. of Org-mode).
  6888. M-up Move entry/item up
  6889. M-down Move entry/item down
  6890. M-left Promote
  6891. M-right Demote
  6892. M-S-up Move entry/item up
  6893. M-S-down Move entry/item down
  6894. M-S-left Promote subtree
  6895. M-S-right Demote subtree
  6896. M-q Fill paragraph and items like in Org-mode
  6897. C-c ^ Sort entries
  6898. C-c - Cycle list bullet
  6899. TAB Cycle item visibility
  6900. M-RET Insert new heading/item
  6901. S-M-RET Insert new TODO heading / Chekbox item
  6902. C-c C-c Set tags / toggle checkbox"
  6903. nil " OrgStruct" nil
  6904. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6905. ;;;###autoload
  6906. (defun turn-on-orgstruct ()
  6907. "Unconditionally turn on `orgstruct-mode'."
  6908. (orgstruct-mode 1))
  6909. ;;;###autoload
  6910. (defun turn-on-orgstruct++ ()
  6911. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6912. In addition to setting orgstruct-mode, this also exports all indentation and
  6913. autofilling variables from org-mode into the buffer. Note that turning
  6914. off orgstruct-mode will *not* remove these additional settings."
  6915. (orgstruct-mode 1)
  6916. (let (var val)
  6917. (mapc
  6918. (lambda (x)
  6919. (when (string-match
  6920. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6921. (symbol-name (car x)))
  6922. (setq var (car x) val (nth 1 x))
  6923. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6924. org-local-vars)))
  6925. (defun orgstruct-error ()
  6926. "Error when there is no default binding for a structure key."
  6927. (interactive)
  6928. (error "This key has no function outside structure elements"))
  6929. (defun orgstruct-setup ()
  6930. "Setup orgstruct keymaps."
  6931. (let ((nfunc 0)
  6932. (bindings
  6933. (list
  6934. '([(meta up)] org-metaup)
  6935. '([(meta down)] org-metadown)
  6936. '([(meta left)] org-metaleft)
  6937. '([(meta right)] org-metaright)
  6938. '([(meta shift up)] org-shiftmetaup)
  6939. '([(meta shift down)] org-shiftmetadown)
  6940. '([(meta shift left)] org-shiftmetaleft)
  6941. '([(meta shift right)] org-shiftmetaright)
  6942. '([(shift up)] org-shiftup)
  6943. '([(shift down)] org-shiftdown)
  6944. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6945. '("\M-q" fill-paragraph)
  6946. '("\C-c^" org-sort)
  6947. '("\C-c-" org-cycle-list-bullet)))
  6948. elt key fun cmd)
  6949. (while (setq elt (pop bindings))
  6950. (setq nfunc (1+ nfunc))
  6951. (setq key (org-key (car elt))
  6952. fun (nth 1 elt)
  6953. cmd (orgstruct-make-binding fun nfunc key))
  6954. (org-defkey orgstruct-mode-map key cmd))
  6955. ;; Special treatment needed for TAB and RET
  6956. (org-defkey orgstruct-mode-map [(tab)]
  6957. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6958. (org-defkey orgstruct-mode-map "\C-i"
  6959. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6960. (org-defkey orgstruct-mode-map "\M-\C-m"
  6961. (orgstruct-make-binding 'org-insert-heading 105
  6962. "\M-\C-m" [(meta return)]))
  6963. (org-defkey orgstruct-mode-map [(meta return)]
  6964. (orgstruct-make-binding 'org-insert-heading 106
  6965. [(meta return)] "\M-\C-m"))
  6966. (org-defkey orgstruct-mode-map [(shift meta return)]
  6967. (orgstruct-make-binding 'org-insert-todo-heading 107
  6968. [(meta return)] "\M-\C-m"))
  6969. (unless org-local-vars
  6970. (setq org-local-vars (org-get-local-variables)))
  6971. t))
  6972. (defun orgstruct-make-binding (fun n &rest keys)
  6973. "Create a function for binding in the structure minor mode.
  6974. FUN is the command to call inside a table. N is used to create a unique
  6975. command name. KEYS are keys that should be checked in for a command
  6976. to execute outside of tables."
  6977. (eval
  6978. (list 'defun
  6979. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6980. '(arg)
  6981. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6982. "Outside of structure, run the binding of `"
  6983. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6984. "'.")
  6985. '(interactive "p")
  6986. (list 'if
  6987. '(org-context-p 'headline 'item)
  6988. (list 'org-run-like-in-org-mode (list 'quote fun))
  6989. (list 'let '(orgstruct-mode)
  6990. (list 'call-interactively
  6991. (append '(or)
  6992. (mapcar (lambda (k)
  6993. (list 'key-binding k))
  6994. keys)
  6995. '('orgstruct-error))))))))
  6996. (defun org-context-p (&rest contexts)
  6997. "Check if local context is and of CONTEXTS.
  6998. Possible values in the list of contexts are `table', `headline', and `item'."
  6999. (let ((pos (point)))
  7000. (goto-char (point-at-bol))
  7001. (prog1 (or (and (memq 'table contexts)
  7002. (looking-at "[ \t]*|"))
  7003. (and (memq 'headline contexts)
  7004. (looking-at "\\*+"))
  7005. (and (memq 'item contexts)
  7006. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7007. (goto-char pos))))
  7008. (defun org-get-local-variables ()
  7009. "Return a list of all local variables in an org-mode buffer."
  7010. (let (varlist)
  7011. (with-current-buffer (get-buffer-create "*Org tmp*")
  7012. (erase-buffer)
  7013. (org-mode)
  7014. (setq varlist (buffer-local-variables)))
  7015. (kill-buffer "*Org tmp*")
  7016. (delq nil
  7017. (mapcar
  7018. (lambda (x)
  7019. (setq x
  7020. (if (symbolp x)
  7021. (list x)
  7022. (list (car x) (list 'quote (cdr x)))))
  7023. (if (string-match
  7024. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7025. (symbol-name (car x)))
  7026. x nil))
  7027. varlist))))
  7028. ;;;###autoload
  7029. (defun org-run-like-in-org-mode (cmd)
  7030. (unless org-local-vars
  7031. (setq org-local-vars (org-get-local-variables)))
  7032. (eval (list 'let org-local-vars
  7033. (list 'call-interactively (list 'quote cmd)))))
  7034. ;;;; Archiving
  7035. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7036. (defun org-archive-subtree (&optional find-done)
  7037. "Move the current subtree to the archive.
  7038. The archive can be a certain top-level heading in the current file, or in
  7039. a different file. The tree will be moved to that location, the subtree
  7040. heading be marked DONE, and the current time will be added.
  7041. When called with prefix argument FIND-DONE, find whole trees without any
  7042. open TODO items and archive them (after getting confirmation from the user).
  7043. If the cursor is not at a headline when this comand is called, try all level
  7044. 1 trees. If the cursor is on a headline, only try the direct children of
  7045. this heading."
  7046. (interactive "P")
  7047. (if find-done
  7048. (org-archive-all-done)
  7049. ;; Save all relevant TODO keyword-relatex variables
  7050. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7051. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7052. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7053. (tr-org-done-keywords org-done-keywords)
  7054. (tr-org-todo-regexp org-todo-regexp)
  7055. (tr-org-todo-line-regexp org-todo-line-regexp)
  7056. (tr-org-odd-levels-only org-odd-levels-only)
  7057. (this-buffer (current-buffer))
  7058. (org-archive-location org-archive-location)
  7059. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7060. ;; start of variables that will be used for saving context
  7061. ;; The compiler complains about them - keep them anyway!
  7062. (file (abbreviate-file-name (buffer-file-name)))
  7063. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7064. (time (format-time-string
  7065. (substring (cdr org-time-stamp-formats) 1 -1)
  7066. (current-time)))
  7067. afile heading buffer level newfile-p
  7068. category todo priority
  7069. ;; start of variables that will be used for savind context
  7070. ltags itags prop)
  7071. ;; Try to find a local archive location
  7072. (save-excursion
  7073. (save-restriction
  7074. (widen)
  7075. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7076. (if (and prop (string-match "\\S-" prop))
  7077. (setq org-archive-location prop)
  7078. (if (or (re-search-backward re nil t)
  7079. (re-search-forward re nil t))
  7080. (setq org-archive-location (match-string 1))))))
  7081. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7082. (progn
  7083. (setq afile (format (match-string 1 org-archive-location)
  7084. (file-name-nondirectory buffer-file-name))
  7085. heading (match-string 2 org-archive-location)))
  7086. (error "Invalid `org-archive-location'"))
  7087. (if (> (length afile) 0)
  7088. (setq newfile-p (not (file-exists-p afile))
  7089. buffer (find-file-noselect afile))
  7090. (setq buffer (current-buffer)))
  7091. (unless buffer
  7092. (error "Cannot access file \"%s\"" afile))
  7093. (if (and (> (length heading) 0)
  7094. (string-match "^\\*+" heading))
  7095. (setq level (match-end 0))
  7096. (setq heading nil level 0))
  7097. (save-excursion
  7098. (org-back-to-heading t)
  7099. ;; Get context information that will be lost by moving the tree
  7100. (org-refresh-category-properties)
  7101. (setq category (org-get-category)
  7102. todo (and (looking-at org-todo-line-regexp)
  7103. (match-string 2))
  7104. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7105. ltags (org-get-tags)
  7106. itags (org-delete-all ltags (org-get-tags-at)))
  7107. (setq ltags (mapconcat 'identity ltags " ")
  7108. itags (mapconcat 'identity itags " "))
  7109. ;; We first only copy, in case something goes wrong
  7110. ;; we need to protect this-command, to avoid kill-region sets it,
  7111. ;; which would lead to duplication of subtrees
  7112. (let (this-command) (org-copy-subtree))
  7113. (set-buffer buffer)
  7114. ;; Enforce org-mode for the archive buffer
  7115. (if (not (org-mode-p))
  7116. ;; Force the mode for future visits.
  7117. (let ((org-insert-mode-line-in-empty-file t)
  7118. (org-inhibit-startup t))
  7119. (call-interactively 'org-mode)))
  7120. (when newfile-p
  7121. (goto-char (point-max))
  7122. (insert (format "\nArchived entries from file %s\n\n"
  7123. (buffer-file-name this-buffer))))
  7124. ;; Force the TODO keywords of the original buffer
  7125. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7126. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7127. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7128. (org-done-keywords tr-org-done-keywords)
  7129. (org-todo-regexp tr-org-todo-regexp)
  7130. (org-todo-line-regexp tr-org-todo-line-regexp)
  7131. (org-odd-levels-only
  7132. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7133. org-odd-levels-only
  7134. tr-org-odd-levels-only)))
  7135. (goto-char (point-min))
  7136. (if heading
  7137. (progn
  7138. (if (re-search-forward
  7139. (concat "^" (regexp-quote heading)
  7140. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7141. nil t)
  7142. (goto-char (match-end 0))
  7143. ;; Heading not found, just insert it at the end
  7144. (goto-char (point-max))
  7145. (or (bolp) (insert "\n"))
  7146. (insert "\n" heading "\n")
  7147. (end-of-line 0))
  7148. ;; Make the subtree visible
  7149. (show-subtree)
  7150. (org-end-of-subtree t)
  7151. (skip-chars-backward " \t\r\n")
  7152. (and (looking-at "[ \t\r\n]*")
  7153. (replace-match "\n\n")))
  7154. ;; No specific heading, just go to end of file.
  7155. (goto-char (point-max)) (insert "\n"))
  7156. ;; Paste
  7157. (org-paste-subtree (org-get-legal-level level 1))
  7158. ;; Mark the entry as done
  7159. (when (and org-archive-mark-done
  7160. (looking-at org-todo-line-regexp)
  7161. (or (not (match-end 2))
  7162. (not (member (match-string 2) org-done-keywords))))
  7163. (let (org-log-done org-todo-log-states)
  7164. (org-todo
  7165. (car (or (member org-archive-mark-done org-done-keywords)
  7166. org-done-keywords)))))
  7167. ;; Add the context info
  7168. (when org-archive-save-context-info
  7169. (let ((l org-archive-save-context-info) e n v)
  7170. (while (setq e (pop l))
  7171. (when (and (setq v (symbol-value e))
  7172. (stringp v) (string-match "\\S-" v))
  7173. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7174. (org-entry-put (point) n v)))))
  7175. ;; Save the buffer, if it is not the same buffer.
  7176. (if (not (eq this-buffer buffer)) (save-buffer))))
  7177. ;; Here we are back in the original buffer. Everything seems to have
  7178. ;; worked. So now cut the tree and finish up.
  7179. (let (this-command) (org-cut-subtree))
  7180. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7181. (message "Subtree archived %s"
  7182. (if (eq this-buffer buffer)
  7183. (concat "under heading: " heading)
  7184. (concat "in file: " (abbreviate-file-name afile)))))))
  7185. (defun org-refresh-category-properties ()
  7186. "Refresh category text properties in teh buffer."
  7187. (let ((def-cat (cond
  7188. ((null org-category)
  7189. (if buffer-file-name
  7190. (file-name-sans-extension
  7191. (file-name-nondirectory buffer-file-name))
  7192. "???"))
  7193. ((symbolp org-category) (symbol-name org-category))
  7194. (t org-category)))
  7195. beg end cat pos optionp)
  7196. (org-unmodified
  7197. (save-excursion
  7198. (save-restriction
  7199. (widen)
  7200. (goto-char (point-min))
  7201. (put-text-property (point) (point-max) 'org-category def-cat)
  7202. (while (re-search-forward
  7203. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7204. (setq pos (match-end 0)
  7205. optionp (equal (char-after (match-beginning 0)) ?#)
  7206. cat (org-trim (match-string 2)))
  7207. (if optionp
  7208. (setq beg (point-at-bol) end (point-max))
  7209. (org-back-to-heading t)
  7210. (setq beg (point) end (org-end-of-subtree t t)))
  7211. (put-text-property beg end 'org-category cat)
  7212. (goto-char pos)))))))
  7213. (defun org-archive-all-done (&optional tag)
  7214. "Archive sublevels of the current tree without open TODO items.
  7215. If the cursor is not on a headline, try all level 1 trees. If
  7216. it is on a headline, try all direct children.
  7217. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7218. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7219. (rea (concat ".*:" org-archive-tag ":"))
  7220. (begm (make-marker))
  7221. (endm (make-marker))
  7222. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7223. "Move subtree to archive (no open TODO items)? "))
  7224. beg end (cntarch 0))
  7225. (if (org-on-heading-p)
  7226. (progn
  7227. (setq re1 (concat "^" (regexp-quote
  7228. (make-string
  7229. (1+ (- (match-end 0) (match-beginning 0) 1))
  7230. ?*))
  7231. " "))
  7232. (move-marker begm (point))
  7233. (move-marker endm (org-end-of-subtree t)))
  7234. (setq re1 "^* ")
  7235. (move-marker begm (point-min))
  7236. (move-marker endm (point-max)))
  7237. (save-excursion
  7238. (goto-char begm)
  7239. (while (re-search-forward re1 endm t)
  7240. (setq beg (match-beginning 0)
  7241. end (save-excursion (org-end-of-subtree t) (point)))
  7242. (goto-char beg)
  7243. (if (re-search-forward re end t)
  7244. (goto-char end)
  7245. (goto-char beg)
  7246. (if (and (or (not tag) (not (looking-at rea)))
  7247. (y-or-n-p question))
  7248. (progn
  7249. (if tag
  7250. (org-toggle-tag org-archive-tag 'on)
  7251. (org-archive-subtree))
  7252. (setq cntarch (1+ cntarch)))
  7253. (goto-char end)))))
  7254. (message "%d trees archived" cntarch)))
  7255. (defun org-cycle-hide-drawers (state)
  7256. "Re-hide all drawers after a visibility state change."
  7257. (when (and (org-mode-p)
  7258. (not (memq state '(overview folded))))
  7259. (save-excursion
  7260. (let* ((globalp (memq state '(contents all)))
  7261. (beg (if globalp (point-min) (point)))
  7262. (end (if globalp (point-max) (org-end-of-subtree t))))
  7263. (goto-char beg)
  7264. (while (re-search-forward org-drawer-regexp end t)
  7265. (org-flag-drawer t))))))
  7266. (defun org-flag-drawer (flag)
  7267. (save-excursion
  7268. (beginning-of-line 1)
  7269. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7270. (let ((b (match-end 0))
  7271. (outline-regexp org-outline-regexp))
  7272. (if (re-search-forward
  7273. "^[ \t]*:END:"
  7274. (save-excursion (outline-next-heading) (point)) t)
  7275. (outline-flag-region b (point-at-eol) flag)
  7276. (error ":END: line missing"))))))
  7277. (defun org-cycle-hide-archived-subtrees (state)
  7278. "Re-hide all archived subtrees after a visibility state change."
  7279. (when (and (not org-cycle-open-archived-trees)
  7280. (not (memq state '(overview folded))))
  7281. (save-excursion
  7282. (let* ((globalp (memq state '(contents all)))
  7283. (beg (if globalp (point-min) (point)))
  7284. (end (if globalp (point-max) (org-end-of-subtree t))))
  7285. (org-hide-archived-subtrees beg end)
  7286. (goto-char beg)
  7287. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7288. (message "%s" (substitute-command-keys
  7289. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7290. (defun org-force-cycle-archived ()
  7291. "Cycle subtree even if it is archived."
  7292. (interactive)
  7293. (setq this-command 'org-cycle)
  7294. (let ((org-cycle-open-archived-trees t))
  7295. (call-interactively 'org-cycle)))
  7296. (defun org-hide-archived-subtrees (beg end)
  7297. "Re-hide all archived subtrees after a visibility state change."
  7298. (save-excursion
  7299. (let* ((re (concat ":" org-archive-tag ":")))
  7300. (goto-char beg)
  7301. (while (re-search-forward re end t)
  7302. (and (org-on-heading-p) (hide-subtree))
  7303. (org-end-of-subtree t)))))
  7304. (defun org-toggle-tag (tag &optional onoff)
  7305. "Toggle the tag TAG for the current line.
  7306. If ONOFF is `on' or `off', don't toggle but set to this state."
  7307. (unless (org-on-heading-p t) (error "Not on headling"))
  7308. (let (res current)
  7309. (save-excursion
  7310. (beginning-of-line)
  7311. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7312. (point-at-eol) t)
  7313. (progn
  7314. (setq current (match-string 1))
  7315. (replace-match ""))
  7316. (setq current ""))
  7317. (setq current (nreverse (org-split-string current ":")))
  7318. (cond
  7319. ((eq onoff 'on)
  7320. (setq res t)
  7321. (or (member tag current) (push tag current)))
  7322. ((eq onoff 'off)
  7323. (or (not (member tag current)) (setq current (delete tag current))))
  7324. (t (if (member tag current)
  7325. (setq current (delete tag current))
  7326. (setq res t)
  7327. (push tag current))))
  7328. (end-of-line 1)
  7329. (if current
  7330. (progn
  7331. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7332. (org-set-tags nil t))
  7333. (delete-horizontal-space))
  7334. (run-hooks 'org-after-tags-change-hook))
  7335. res))
  7336. (defun org-toggle-archive-tag (&optional arg)
  7337. "Toggle the archive tag for the current headline.
  7338. With prefix ARG, check all children of current headline and offer tagging
  7339. the children that do not contain any open TODO items."
  7340. (interactive "P")
  7341. (if arg
  7342. (org-archive-all-done 'tag)
  7343. (let (set)
  7344. (save-excursion
  7345. (org-back-to-heading t)
  7346. (setq set (org-toggle-tag org-archive-tag))
  7347. (when set (hide-subtree)))
  7348. (and set (beginning-of-line 1))
  7349. (message "Subtree %s" (if set "archived" "unarchived")))))
  7350. ;;;; Tables
  7351. ;;; The table editor
  7352. ;; Watch out: Here we are talking about two different kind of tables.
  7353. ;; Most of the code is for the tables created with the Org-mode table editor.
  7354. ;; Sometimes, we talk about tables created and edited with the table.el
  7355. ;; Emacs package. We call the former org-type tables, and the latter
  7356. ;; table.el-type tables.
  7357. (defun org-before-change-function (beg end)
  7358. "Every change indicates that a table might need an update."
  7359. (setq org-table-may-need-update t))
  7360. (defconst org-table-line-regexp "^[ \t]*|"
  7361. "Detects an org-type table line.")
  7362. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7363. "Detects an org-type table line.")
  7364. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7365. "Detects a table line marked for automatic recalculation.")
  7366. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7367. "Detects a table line marked for automatic recalculation.")
  7368. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7369. "Detects a table line marked for automatic recalculation.")
  7370. (defconst org-table-hline-regexp "^[ \t]*|-"
  7371. "Detects an org-type table hline.")
  7372. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7373. "Detects a table-type table hline.")
  7374. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7375. "Detects an org-type or table-type table.")
  7376. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7377. "Searching from within a table (any type) this finds the first line
  7378. outside the table.")
  7379. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7380. "Searching from within a table (any type) this finds the first line
  7381. outside the table.")
  7382. (defvar org-table-last-highlighted-reference nil)
  7383. (defvar org-table-formula-history nil)
  7384. (defvar org-table-column-names nil
  7385. "Alist with column names, derived from the `!' line.")
  7386. (defvar org-table-column-name-regexp nil
  7387. "Regular expression matching the current column names.")
  7388. (defvar org-table-local-parameters nil
  7389. "Alist with parameter names, derived from the `$' line.")
  7390. (defvar org-table-named-field-locations nil
  7391. "Alist with locations of named fields.")
  7392. (defvar org-table-current-line-types nil
  7393. "Table row types, non-nil only for the duration of a comand.")
  7394. (defvar org-table-current-begin-line nil
  7395. "Table begin line, non-nil only for the duration of a comand.")
  7396. (defvar org-table-current-begin-pos nil
  7397. "Table begin position, non-nil only for the duration of a comand.")
  7398. (defvar org-table-dlines nil
  7399. "Vector of data line line numbers in the current table.")
  7400. (defvar org-table-hlines nil
  7401. "Vector of hline line numbers in the current table.")
  7402. (defconst org-table-range-regexp
  7403. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7404. ;; 1 2 3 4 5
  7405. "Regular expression for matching ranges in formulas.")
  7406. (defconst org-table-range-regexp2
  7407. (concat
  7408. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7409. "\\.\\."
  7410. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7411. "Match a range for reference display.")
  7412. (defconst org-table-translate-regexp
  7413. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7414. "Match a reference that needs translation, for reference display.")
  7415. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7416. (defun org-table-create-with-table.el ()
  7417. "Use the table.el package to insert a new table.
  7418. If there is already a table at point, convert between Org-mode tables
  7419. and table.el tables."
  7420. (interactive)
  7421. (require 'table)
  7422. (cond
  7423. ((org-at-table.el-p)
  7424. (if (y-or-n-p "Convert table to Org-mode table? ")
  7425. (org-table-convert)))
  7426. ((org-at-table-p)
  7427. (if (y-or-n-p "Convert table to table.el table? ")
  7428. (org-table-convert)))
  7429. (t (call-interactively 'table-insert))))
  7430. (defun org-table-create-or-convert-from-region (arg)
  7431. "Convert region to table, or create an empty table.
  7432. If there is an active region, convert it to a table, using the function
  7433. `org-table-convert-region'. See the documentation of that function
  7434. to learn how the prefix argument is interpreted to determine the field
  7435. separator.
  7436. If there is no such region, create an empty table with `org-table-create'."
  7437. (interactive "P")
  7438. (if (org-region-active-p)
  7439. (org-table-convert-region (region-beginning) (region-end) arg)
  7440. (org-table-create arg)))
  7441. (defun org-table-create (&optional size)
  7442. "Query for a size and insert a table skeleton.
  7443. SIZE is a string Columns x Rows like for example \"3x2\"."
  7444. (interactive "P")
  7445. (unless size
  7446. (setq size (read-string
  7447. (concat "Table size Columns x Rows [e.g. "
  7448. org-table-default-size "]: ")
  7449. "" nil org-table-default-size)))
  7450. (let* ((pos (point))
  7451. (indent (make-string (current-column) ?\ ))
  7452. (split (org-split-string size " *x *"))
  7453. (rows (string-to-number (nth 1 split)))
  7454. (columns (string-to-number (car split)))
  7455. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7456. "\n")))
  7457. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7458. (point-at-bol) (point)))
  7459. (beginning-of-line 1)
  7460. (newline))
  7461. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7462. (dotimes (i rows) (insert line))
  7463. (goto-char pos)
  7464. (if (> rows 1)
  7465. ;; Insert a hline after the first row.
  7466. (progn
  7467. (end-of-line 1)
  7468. (insert "\n|-")
  7469. (goto-char pos)))
  7470. (org-table-align)))
  7471. (defun org-table-convert-region (beg0 end0 &optional separator)
  7472. "Convert region to a table.
  7473. The region goes from BEG0 to END0, but these borders will be moved
  7474. slightly, to make sure a beginning of line in the first line is included.
  7475. SEPARATOR specifies the field separator in the lines. It can have the
  7476. following values:
  7477. '(4) Use the comma as a field separator
  7478. '(16) Use a TAB as field separator
  7479. integer When a number, use that many spaces as field separator
  7480. nil When nil, the command tries to be smart and figure out the
  7481. separator in the following way:
  7482. - when each line contains a TAB, assume TAB-separated material
  7483. - when each line contains a comme, assume CSV material
  7484. - else, assume one or more SPACE charcters as separator."
  7485. (interactive "rP")
  7486. (let* ((beg (min beg0 end0))
  7487. (end (max beg0 end0))
  7488. re)
  7489. (goto-char beg)
  7490. (beginning-of-line 1)
  7491. (setq beg (move-marker (make-marker) (point)))
  7492. (goto-char end)
  7493. (if (bolp) (backward-char 1) (end-of-line 1))
  7494. (setq end (move-marker (make-marker) (point)))
  7495. ;; Get the right field separator
  7496. (unless separator
  7497. (goto-char beg)
  7498. (setq separator
  7499. (cond
  7500. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7501. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7502. (t 1))))
  7503. (setq re (cond
  7504. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7505. ((equal separator '(16)) "^\\|\t")
  7506. ((integerp separator)
  7507. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7508. (t (error "This should not happen"))))
  7509. (goto-char beg)
  7510. (while (re-search-forward re end t)
  7511. (replace-match "| " t t))
  7512. (goto-char beg)
  7513. (insert " ")
  7514. (org-table-align)))
  7515. (defun org-table-import (file arg)
  7516. "Import FILE as a table.
  7517. The file is assumed to be tab-separated. Such files can be produced by most
  7518. spreadsheet and database applications. If no tabs (at least one per line)
  7519. are found, lines will be split on whitespace into fields."
  7520. (interactive "f\nP")
  7521. (or (bolp) (newline))
  7522. (let ((beg (point))
  7523. (pm (point-max)))
  7524. (insert-file-contents file)
  7525. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7526. (defun org-table-export ()
  7527. "Export table as a tab-separated file.
  7528. Such a file can be imported into a spreadsheet program like Excel."
  7529. (interactive)
  7530. (let* ((beg (org-table-begin))
  7531. (end (org-table-end))
  7532. (table (buffer-substring beg end))
  7533. (file (read-file-name "Export table to: "))
  7534. buf)
  7535. (unless (or (not (file-exists-p file))
  7536. (y-or-n-p (format "Overwrite file %s? " file)))
  7537. (error "Abort"))
  7538. (with-current-buffer (find-file-noselect file)
  7539. (setq buf (current-buffer))
  7540. (erase-buffer)
  7541. (fundamental-mode)
  7542. (insert table)
  7543. (goto-char (point-min))
  7544. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7545. (replace-match "" t t)
  7546. (end-of-line 1))
  7547. (goto-char (point-min))
  7548. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7549. (replace-match "" t t)
  7550. (goto-char (min (1+ (point)) (point-max))))
  7551. (goto-char (point-min))
  7552. (while (re-search-forward "^-[-+]*$" nil t)
  7553. (replace-match "")
  7554. (if (looking-at "\n")
  7555. (delete-char 1)))
  7556. (goto-char (point-min))
  7557. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7558. (replace-match "\t" t t))
  7559. (save-buffer))
  7560. (kill-buffer buf)))
  7561. (defvar org-table-aligned-begin-marker (make-marker)
  7562. "Marker at the beginning of the table last aligned.
  7563. Used to check if cursor still is in that table, to minimize realignment.")
  7564. (defvar org-table-aligned-end-marker (make-marker)
  7565. "Marker at the end of the table last aligned.
  7566. Used to check if cursor still is in that table, to minimize realignment.")
  7567. (defvar org-table-last-alignment nil
  7568. "List of flags for flushright alignment, from the last re-alignment.
  7569. This is being used to correctly align a single field after TAB or RET.")
  7570. (defvar org-table-last-column-widths nil
  7571. "List of max width of fields in each column.
  7572. This is being used to correctly align a single field after TAB or RET.")
  7573. (defvar org-table-overlay-coordinates nil
  7574. "Overlay coordinates after each align of a table.")
  7575. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7576. (defvar org-last-recalc-line nil)
  7577. (defconst org-narrow-column-arrow "=>"
  7578. "Used as display property in narrowed table columns.")
  7579. (defun org-table-align ()
  7580. "Align the table at point by aligning all vertical bars."
  7581. (interactive)
  7582. (let* (
  7583. ;; Limits of table
  7584. (beg (org-table-begin))
  7585. (end (org-table-end))
  7586. ;; Current cursor position
  7587. (linepos (org-current-line))
  7588. (colpos (org-table-current-column))
  7589. (winstart (window-start))
  7590. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7591. lines (new "") lengths l typenums ty fields maxfields i
  7592. column
  7593. (indent "") cnt frac
  7594. rfmt hfmt
  7595. (spaces '(1 . 1))
  7596. (sp1 (car spaces))
  7597. (sp2 (cdr spaces))
  7598. (rfmt1 (concat
  7599. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7600. (hfmt1 (concat
  7601. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7602. emptystrings links dates emph narrow fmax f1 len c e)
  7603. (untabify beg end)
  7604. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7605. ;; Check if we have links or dates
  7606. (goto-char beg)
  7607. (setq links (re-search-forward org-bracket-link-regexp end t))
  7608. (goto-char beg)
  7609. (setq emph (and org-hide-emphasis-markers
  7610. (re-search-forward org-emph-re end t)))
  7611. (goto-char beg)
  7612. (setq dates (and org-display-custom-times
  7613. (re-search-forward org-ts-regexp-both end t)))
  7614. ;; Make sure the link properties are right
  7615. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7616. ;; Make sure the date properties are right
  7617. (when dates (goto-char beg) (while (org-activate-dates end)))
  7618. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7619. ;; Check if we are narrowing any columns
  7620. (goto-char beg)
  7621. (setq narrow (and org-format-transports-properties-p
  7622. (re-search-forward "<[0-9]+>" end t)))
  7623. ;; Get the rows
  7624. (setq lines (org-split-string
  7625. (buffer-substring beg end) "\n"))
  7626. ;; Store the indentation of the first line
  7627. (if (string-match "^ *" (car lines))
  7628. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7629. ;; Mark the hlines by setting the corresponding element to nil
  7630. ;; At the same time, we remove trailing space.
  7631. (setq lines (mapcar (lambda (l)
  7632. (if (string-match "^ *|-" l)
  7633. nil
  7634. (if (string-match "[ \t]+$" l)
  7635. (substring l 0 (match-beginning 0))
  7636. l)))
  7637. lines))
  7638. ;; Get the data fields by splitting the lines.
  7639. (setq fields (mapcar
  7640. (lambda (l)
  7641. (org-split-string l " *| *"))
  7642. (delq nil (copy-sequence lines))))
  7643. ;; How many fields in the longest line?
  7644. (condition-case nil
  7645. (setq maxfields (apply 'max (mapcar 'length fields)))
  7646. (error
  7647. (kill-region beg end)
  7648. (org-table-create org-table-default-size)
  7649. (error "Empty table - created default table")))
  7650. ;; A list of empty strings to fill any short rows on output
  7651. (setq emptystrings (make-list maxfields ""))
  7652. ;; Check for special formatting.
  7653. (setq i -1)
  7654. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7655. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7656. ;; Check if there is an explicit width specified
  7657. (when narrow
  7658. (setq c column fmax nil)
  7659. (while c
  7660. (setq e (pop c))
  7661. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7662. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7663. ;; Find fields that are wider than fmax, and shorten them
  7664. (when fmax
  7665. (loop for xx in column do
  7666. (when (and (stringp xx)
  7667. (> (org-string-width xx) fmax))
  7668. (org-add-props xx nil
  7669. 'help-echo
  7670. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7671. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7672. (unless (> f1 1)
  7673. (error "Cannot narrow field starting with wide link \"%s\""
  7674. (match-string 0 xx)))
  7675. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7676. (add-text-properties (- f1 2) f1
  7677. (list 'display org-narrow-column-arrow)
  7678. xx)))))
  7679. ;; Get the maximum width for each column
  7680. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7681. ;; Get the fraction of numbers, to decide about alignment of the column
  7682. (setq cnt 0 frac 0.0)
  7683. (loop for x in column do
  7684. (if (equal x "")
  7685. nil
  7686. (setq frac ( / (+ (* frac cnt)
  7687. (if (string-match org-table-number-regexp x) 1 0))
  7688. (setq cnt (1+ cnt))))))
  7689. (push (>= frac org-table-number-fraction) typenums))
  7690. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7691. ;; Store the alignment of this table, for later editing of single fields
  7692. (setq org-table-last-alignment typenums
  7693. org-table-last-column-widths lengths)
  7694. ;; With invisible characters, `format' does not get the field width right
  7695. ;; So we need to make these fields wide by hand.
  7696. (when (or links emph)
  7697. (loop for i from 0 upto (1- maxfields) do
  7698. (setq len (nth i lengths))
  7699. (loop for j from 0 upto (1- (length fields)) do
  7700. (setq c (nthcdr i (car (nthcdr j fields))))
  7701. (if (and (stringp (car c))
  7702. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7703. ; (string-match org-bracket-link-regexp (car c))
  7704. (< (org-string-width (car c)) len))
  7705. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7706. ;; Compute the formats needed for output of the table
  7707. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7708. (while (setq l (pop lengths))
  7709. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7710. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7711. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7712. (setq rfmt (concat rfmt "\n")
  7713. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7714. (setq new (mapconcat
  7715. (lambda (l)
  7716. (if l (apply 'format rfmt
  7717. (append (pop fields) emptystrings))
  7718. hfmt))
  7719. lines ""))
  7720. ;; Replace the old one
  7721. (delete-region beg end)
  7722. (move-marker end nil)
  7723. (move-marker org-table-aligned-begin-marker (point))
  7724. (insert new)
  7725. (move-marker org-table-aligned-end-marker (point))
  7726. (when (and orgtbl-mode (not (org-mode-p)))
  7727. (goto-char org-table-aligned-begin-marker)
  7728. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7729. ;; Try to move to the old location
  7730. (goto-line winstartline)
  7731. (setq winstart (point-at-bol))
  7732. (goto-line linepos)
  7733. (set-window-start (selected-window) winstart 'noforce)
  7734. (org-table-goto-column colpos)
  7735. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7736. (setq org-table-may-need-update nil)
  7737. ))
  7738. (defun org-string-width (s)
  7739. "Compute width of string, ignoring invisible characters.
  7740. This ignores character with invisibility property `org-link', and also
  7741. characters with property `org-cwidth', because these will become invisible
  7742. upon the next fontification round."
  7743. (let (b l)
  7744. (when (or (eq t buffer-invisibility-spec)
  7745. (assq 'org-link buffer-invisibility-spec))
  7746. (while (setq b (text-property-any 0 (length s)
  7747. 'invisible 'org-link s))
  7748. (setq s (concat (substring s 0 b)
  7749. (substring s (or (next-single-property-change
  7750. b 'invisible s) (length s)))))))
  7751. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7752. (setq s (concat (substring s 0 b)
  7753. (substring s (or (next-single-property-change
  7754. b 'org-cwidth s) (length s))))))
  7755. (setq l (string-width s) b -1)
  7756. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7757. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7758. l))
  7759. (defun org-table-begin (&optional table-type)
  7760. "Find the beginning of the table and return its position.
  7761. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7762. (save-excursion
  7763. (if (not (re-search-backward
  7764. (if table-type org-table-any-border-regexp
  7765. org-table-border-regexp)
  7766. nil t))
  7767. (progn (goto-char (point-min)) (point))
  7768. (goto-char (match-beginning 0))
  7769. (beginning-of-line 2)
  7770. (point))))
  7771. (defun org-table-end (&optional table-type)
  7772. "Find the end of the table and return its position.
  7773. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7774. (save-excursion
  7775. (if (not (re-search-forward
  7776. (if table-type org-table-any-border-regexp
  7777. org-table-border-regexp)
  7778. nil t))
  7779. (goto-char (point-max))
  7780. (goto-char (match-beginning 0)))
  7781. (point-marker)))
  7782. (defun org-table-justify-field-maybe (&optional new)
  7783. "Justify the current field, text to left, number to right.
  7784. Optional argument NEW may specify text to replace the current field content."
  7785. (cond
  7786. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7787. ((org-at-table-hline-p))
  7788. ((and (not new)
  7789. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7790. (current-buffer)))
  7791. (< (point) org-table-aligned-begin-marker)
  7792. (>= (point) org-table-aligned-end-marker)))
  7793. ;; This is not the same table, force a full re-align
  7794. (setq org-table-may-need-update t))
  7795. (t ;; realign the current field, based on previous full realign
  7796. (let* ((pos (point)) s
  7797. (col (org-table-current-column))
  7798. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7799. l f n o e)
  7800. (when (> col 0)
  7801. (skip-chars-backward "^|\n")
  7802. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7803. (progn
  7804. (setq s (match-string 1)
  7805. o (match-string 0)
  7806. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7807. e (not (= (match-beginning 2) (match-end 2))))
  7808. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7809. l (if e "|" (setq org-table-may-need-update t) ""))
  7810. n (format f s))
  7811. (if new
  7812. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7813. (setq n (format f new))
  7814. (setq n (concat new "|") org-table-may-need-update t)))
  7815. (or (equal n o)
  7816. (let (org-table-may-need-update)
  7817. (replace-match n t t))))
  7818. (setq org-table-may-need-update t))
  7819. (goto-char pos))))))
  7820. (defun org-table-next-field ()
  7821. "Go to the next field in the current table, creating new lines as needed.
  7822. Before doing so, re-align the table if necessary."
  7823. (interactive)
  7824. (org-table-maybe-eval-formula)
  7825. (org-table-maybe-recalculate-line)
  7826. (if (and org-table-automatic-realign
  7827. org-table-may-need-update)
  7828. (org-table-align))
  7829. (let ((end (org-table-end)))
  7830. (if (org-at-table-hline-p)
  7831. (end-of-line 1))
  7832. (condition-case nil
  7833. (progn
  7834. (re-search-forward "|" end)
  7835. (if (looking-at "[ \t]*$")
  7836. (re-search-forward "|" end))
  7837. (if (and (looking-at "-")
  7838. org-table-tab-jumps-over-hlines
  7839. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7840. (goto-char (match-beginning 1)))
  7841. (if (looking-at "-")
  7842. (progn
  7843. (beginning-of-line 0)
  7844. (org-table-insert-row 'below))
  7845. (if (looking-at " ") (forward-char 1))))
  7846. (error
  7847. (org-table-insert-row 'below)))))
  7848. (defun org-table-previous-field ()
  7849. "Go to the previous field in the table.
  7850. Before doing so, re-align the table if necessary."
  7851. (interactive)
  7852. (org-table-justify-field-maybe)
  7853. (org-table-maybe-recalculate-line)
  7854. (if (and org-table-automatic-realign
  7855. org-table-may-need-update)
  7856. (org-table-align))
  7857. (if (org-at-table-hline-p)
  7858. (end-of-line 1))
  7859. (re-search-backward "|" (org-table-begin))
  7860. (re-search-backward "|" (org-table-begin))
  7861. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7862. (re-search-backward "|" (org-table-begin)))
  7863. (if (looking-at "| ?")
  7864. (goto-char (match-end 0))))
  7865. (defun org-table-next-row ()
  7866. "Go to the next row (same column) in the current table.
  7867. Before doing so, re-align the table if necessary."
  7868. (interactive)
  7869. (org-table-maybe-eval-formula)
  7870. (org-table-maybe-recalculate-line)
  7871. (if (or (looking-at "[ \t]*$")
  7872. (save-excursion (skip-chars-backward " \t") (bolp)))
  7873. (newline)
  7874. (if (and org-table-automatic-realign
  7875. org-table-may-need-update)
  7876. (org-table-align))
  7877. (let ((col (org-table-current-column)))
  7878. (beginning-of-line 2)
  7879. (if (or (not (org-at-table-p))
  7880. (org-at-table-hline-p))
  7881. (progn
  7882. (beginning-of-line 0)
  7883. (org-table-insert-row 'below)))
  7884. (org-table-goto-column col)
  7885. (skip-chars-backward "^|\n\r")
  7886. (if (looking-at " ") (forward-char 1)))))
  7887. (defun org-table-copy-down (n)
  7888. "Copy a field down in the current column.
  7889. If the field at the cursor is empty, copy into it the content of the nearest
  7890. non-empty field above. With argument N, use the Nth non-empty field.
  7891. If the current field is not empty, it is copied down to the next row, and
  7892. the cursor is moved with it. Therefore, repeating this command causes the
  7893. column to be filled row-by-row.
  7894. If the variable `org-table-copy-increment' is non-nil and the field is an
  7895. integer or a timestamp, it will be incremented while copying. In the case of
  7896. a timestamp, if the cursor is on the year, change the year. If it is on the
  7897. month or the day, change that. Point will stay on the current date field
  7898. in order to easily repeat the interval."
  7899. (interactive "p")
  7900. (let* ((colpos (org-table-current-column))
  7901. (col (current-column))
  7902. (field (org-table-get-field))
  7903. (non-empty (string-match "[^ \t]" field))
  7904. (beg (org-table-begin))
  7905. txt)
  7906. (org-table-check-inside-data-field)
  7907. (if non-empty
  7908. (progn
  7909. (setq txt (org-trim field))
  7910. (org-table-next-row)
  7911. (org-table-blank-field))
  7912. (save-excursion
  7913. (setq txt
  7914. (catch 'exit
  7915. (while (progn (beginning-of-line 1)
  7916. (re-search-backward org-table-dataline-regexp
  7917. beg t))
  7918. (org-table-goto-column colpos t)
  7919. (if (and (looking-at
  7920. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7921. (= (setq n (1- n)) 0))
  7922. (throw 'exit (match-string 1))))))))
  7923. (if txt
  7924. (progn
  7925. (if (and org-table-copy-increment
  7926. (string-match "^[0-9]+$" txt))
  7927. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7928. (insert txt)
  7929. (move-to-column col)
  7930. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7931. (org-timestamp-up 1)
  7932. (org-table-maybe-recalculate-line))
  7933. (org-table-align)
  7934. (move-to-column col))
  7935. (error "No non-empty field found"))))
  7936. (defun org-table-check-inside-data-field ()
  7937. "Is point inside a table data field?
  7938. I.e. not on a hline or before the first or after the last column?
  7939. This actually throws an error, so it aborts the current command."
  7940. (if (or (not (org-at-table-p))
  7941. (= (org-table-current-column) 0)
  7942. (org-at-table-hline-p)
  7943. (looking-at "[ \t]*$"))
  7944. (error "Not in table data field")))
  7945. (defvar org-table-clip nil
  7946. "Clipboard for table regions.")
  7947. (defun org-table-blank-field ()
  7948. "Blank the current table field or active region."
  7949. (interactive)
  7950. (org-table-check-inside-data-field)
  7951. (if (and (interactive-p) (org-region-active-p))
  7952. (let (org-table-clip)
  7953. (org-table-cut-region (region-beginning) (region-end)))
  7954. (skip-chars-backward "^|")
  7955. (backward-char 1)
  7956. (if (looking-at "|[^|\n]+")
  7957. (let* ((pos (match-beginning 0))
  7958. (match (match-string 0))
  7959. (len (org-string-width match)))
  7960. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7961. (goto-char (+ 2 pos))
  7962. (substring match 1)))))
  7963. (defun org-table-get-field (&optional n replace)
  7964. "Return the value of the field in column N of current row.
  7965. N defaults to current field.
  7966. If REPLACE is a string, replace field with this value. The return value
  7967. is always the old value."
  7968. (and n (org-table-goto-column n))
  7969. (skip-chars-backward "^|\n")
  7970. (backward-char 1)
  7971. (if (looking-at "|[^|\r\n]*")
  7972. (let* ((pos (match-beginning 0))
  7973. (val (buffer-substring (1+ pos) (match-end 0))))
  7974. (if replace
  7975. (replace-match (concat "|" replace) t t))
  7976. (goto-char (min (point-at-eol) (+ 2 pos)))
  7977. val)
  7978. (forward-char 1) ""))
  7979. (defun org-table-field-info (arg)
  7980. "Show info about the current field, and highlight any reference at point."
  7981. (interactive "P")
  7982. (org-table-get-specials)
  7983. (save-excursion
  7984. (let* ((pos (point))
  7985. (col (org-table-current-column))
  7986. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7987. (name (car (rassoc (list (org-current-line) col)
  7988. org-table-named-field-locations)))
  7989. (eql (org-table-get-stored-formulas))
  7990. (dline (org-table-current-dline))
  7991. (ref (format "@%d$%d" dline col))
  7992. (ref1 (org-table-convert-refs-to-an ref))
  7993. (fequation (or (assoc name eql) (assoc ref eql)))
  7994. (cequation (assoc (int-to-string col) eql))
  7995. (eqn (or fequation cequation)))
  7996. (goto-char pos)
  7997. (condition-case nil
  7998. (org-table-show-reference 'local)
  7999. (error nil))
  8000. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8001. dline col
  8002. (if cname (concat " or $" cname) "")
  8003. dline col ref1
  8004. (if name (concat " or $" name) "")
  8005. ;; FIXME: formula info not correct if special table line
  8006. (if eqn
  8007. (concat ", formula: "
  8008. (org-table-formula-to-user
  8009. (concat
  8010. (if (string-match "^[$@]"(car eqn)) "" "$")
  8011. (car eqn) "=" (cdr eqn))))
  8012. "")))))
  8013. (defun org-table-current-column ()
  8014. "Find out which column we are in.
  8015. When called interactively, column is also displayed in echo area."
  8016. (interactive)
  8017. (if (interactive-p) (org-table-check-inside-data-field))
  8018. (save-excursion
  8019. (let ((cnt 0) (pos (point)))
  8020. (beginning-of-line 1)
  8021. (while (search-forward "|" pos t)
  8022. (setq cnt (1+ cnt)))
  8023. (if (interactive-p) (message "This is table column %d" cnt))
  8024. cnt)))
  8025. (defun org-table-current-dline ()
  8026. "Find out what table data line we are in.
  8027. Only datalins count for this."
  8028. (interactive)
  8029. (if (interactive-p) (org-table-check-inside-data-field))
  8030. (save-excursion
  8031. (let ((cnt 0) (pos (point)))
  8032. (goto-char (org-table-begin))
  8033. (while (<= (point) pos)
  8034. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8035. (beginning-of-line 2))
  8036. (if (interactive-p) (message "This is table line %d" cnt))
  8037. cnt)))
  8038. (defun org-table-goto-column (n &optional on-delim force)
  8039. "Move the cursor to the Nth column in the current table line.
  8040. With optional argument ON-DELIM, stop with point before the left delimiter
  8041. of the field.
  8042. If there are less than N fields, just go to after the last delimiter.
  8043. However, when FORCE is non-nil, create new columns if necessary."
  8044. (interactive "p")
  8045. (let ((pos (point-at-eol)))
  8046. (beginning-of-line 1)
  8047. (when (> n 0)
  8048. (while (and (> (setq n (1- n)) -1)
  8049. (or (search-forward "|" pos t)
  8050. (and force
  8051. (progn (end-of-line 1)
  8052. (skip-chars-backward "^|")
  8053. (insert " | "))))))
  8054. ; (backward-char 2) t)))))
  8055. (when (and force (not (looking-at ".*|")))
  8056. (save-excursion (end-of-line 1) (insert " | ")))
  8057. (if on-delim
  8058. (backward-char 1)
  8059. (if (looking-at " ") (forward-char 1))))))
  8060. (defun org-at-table-p (&optional table-type)
  8061. "Return t if the cursor is inside an org-type table.
  8062. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8063. (if org-enable-table-editor
  8064. (save-excursion
  8065. (beginning-of-line 1)
  8066. (looking-at (if table-type org-table-any-line-regexp
  8067. org-table-line-regexp)))
  8068. nil))
  8069. (defun org-at-table.el-p ()
  8070. "Return t if and only if we are at a table.el table."
  8071. (and (org-at-table-p 'any)
  8072. (save-excursion
  8073. (goto-char (org-table-begin 'any))
  8074. (looking-at org-table1-hline-regexp))))
  8075. (defun org-table-recognize-table.el ()
  8076. "If there is a table.el table nearby, recognize it and move into it."
  8077. (if org-table-tab-recognizes-table.el
  8078. (if (org-at-table.el-p)
  8079. (progn
  8080. (beginning-of-line 1)
  8081. (if (looking-at org-table-dataline-regexp)
  8082. nil
  8083. (if (looking-at org-table1-hline-regexp)
  8084. (progn
  8085. (beginning-of-line 2)
  8086. (if (looking-at org-table-any-border-regexp)
  8087. (beginning-of-line -1)))))
  8088. (if (re-search-forward "|" (org-table-end t) t)
  8089. (progn
  8090. (require 'table)
  8091. (if (table--at-cell-p (point))
  8092. t
  8093. (message "recognizing table.el table...")
  8094. (table-recognize-table)
  8095. (message "recognizing table.el table...done")))
  8096. (error "This should not happen..."))
  8097. t)
  8098. nil)
  8099. nil))
  8100. (defun org-at-table-hline-p ()
  8101. "Return t if the cursor is inside a hline in a table."
  8102. (if org-enable-table-editor
  8103. (save-excursion
  8104. (beginning-of-line 1)
  8105. (looking-at org-table-hline-regexp))
  8106. nil))
  8107. (defun org-table-insert-column ()
  8108. "Insert a new column into the table."
  8109. (interactive)
  8110. (if (not (org-at-table-p))
  8111. (error "Not at a table"))
  8112. (org-table-find-dataline)
  8113. (let* ((col (max 1 (org-table-current-column)))
  8114. (beg (org-table-begin))
  8115. (end (org-table-end))
  8116. ;; Current cursor position
  8117. (linepos (org-current-line))
  8118. (colpos col))
  8119. (goto-char beg)
  8120. (while (< (point) end)
  8121. (if (org-at-table-hline-p)
  8122. nil
  8123. (org-table-goto-column col t)
  8124. (insert "| "))
  8125. (beginning-of-line 2))
  8126. (move-marker end nil)
  8127. (goto-line linepos)
  8128. (org-table-goto-column colpos)
  8129. (org-table-align)
  8130. (org-table-fix-formulas "$" nil (1- col) 1)))
  8131. (defun org-table-find-dataline ()
  8132. "Find a dataline in the current table, which is needed for column commands."
  8133. (if (and (org-at-table-p)
  8134. (not (org-at-table-hline-p)))
  8135. t
  8136. (let ((col (current-column))
  8137. (end (org-table-end)))
  8138. (move-to-column col)
  8139. (while (and (< (point) end)
  8140. (or (not (= (current-column) col))
  8141. (org-at-table-hline-p)))
  8142. (beginning-of-line 2)
  8143. (move-to-column col))
  8144. (if (and (org-at-table-p)
  8145. (not (org-at-table-hline-p)))
  8146. t
  8147. (error
  8148. "Please position cursor in a data line for column operations")))))
  8149. (defun org-table-delete-column ()
  8150. "Delete a column from the table."
  8151. (interactive)
  8152. (if (not (org-at-table-p))
  8153. (error "Not at a table"))
  8154. (org-table-find-dataline)
  8155. (org-table-check-inside-data-field)
  8156. (let* ((col (org-table-current-column))
  8157. (beg (org-table-begin))
  8158. (end (org-table-end))
  8159. ;; Current cursor position
  8160. (linepos (org-current-line))
  8161. (colpos col))
  8162. (goto-char beg)
  8163. (while (< (point) end)
  8164. (if (org-at-table-hline-p)
  8165. nil
  8166. (org-table-goto-column col t)
  8167. (and (looking-at "|[^|\n]+|")
  8168. (replace-match "|")))
  8169. (beginning-of-line 2))
  8170. (move-marker end nil)
  8171. (goto-line linepos)
  8172. (org-table-goto-column colpos)
  8173. (org-table-align)
  8174. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8175. col -1 col)))
  8176. (defun org-table-move-column-right ()
  8177. "Move column to the right."
  8178. (interactive)
  8179. (org-table-move-column nil))
  8180. (defun org-table-move-column-left ()
  8181. "Move column to the left."
  8182. (interactive)
  8183. (org-table-move-column 'left))
  8184. (defun org-table-move-column (&optional left)
  8185. "Move the current column to the right. With arg LEFT, move to the left."
  8186. (interactive "P")
  8187. (if (not (org-at-table-p))
  8188. (error "Not at a table"))
  8189. (org-table-find-dataline)
  8190. (org-table-check-inside-data-field)
  8191. (let* ((col (org-table-current-column))
  8192. (col1 (if left (1- col) col))
  8193. (beg (org-table-begin))
  8194. (end (org-table-end))
  8195. ;; Current cursor position
  8196. (linepos (org-current-line))
  8197. (colpos (if left (1- col) (1+ col))))
  8198. (if (and left (= col 1))
  8199. (error "Cannot move column further left"))
  8200. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8201. (error "Cannot move column further right"))
  8202. (goto-char beg)
  8203. (while (< (point) end)
  8204. (if (org-at-table-hline-p)
  8205. nil
  8206. (org-table-goto-column col1 t)
  8207. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8208. (replace-match "|\\2|\\1|")))
  8209. (beginning-of-line 2))
  8210. (move-marker end nil)
  8211. (goto-line linepos)
  8212. (org-table-goto-column colpos)
  8213. (org-table-align)
  8214. (org-table-fix-formulas
  8215. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8216. (cons (number-to-string colpos) (number-to-string col))))))
  8217. (defun org-table-move-row-down ()
  8218. "Move table row down."
  8219. (interactive)
  8220. (org-table-move-row nil))
  8221. (defun org-table-move-row-up ()
  8222. "Move table row up."
  8223. (interactive)
  8224. (org-table-move-row 'up))
  8225. (defun org-table-move-row (&optional up)
  8226. "Move the current table line down. With arg UP, move it up."
  8227. (interactive "P")
  8228. (let* ((col (current-column))
  8229. (pos (point))
  8230. (hline1p (save-excursion (beginning-of-line 1)
  8231. (looking-at org-table-hline-regexp)))
  8232. (dline1 (org-table-current-dline))
  8233. (dline2 (+ dline1 (if up -1 1)))
  8234. (tonew (if up 0 2))
  8235. txt hline2p)
  8236. (beginning-of-line tonew)
  8237. (unless (org-at-table-p)
  8238. (goto-char pos)
  8239. (error "Cannot move row further"))
  8240. (setq hline2p (looking-at org-table-hline-regexp))
  8241. (goto-char pos)
  8242. (beginning-of-line 1)
  8243. (setq pos (point))
  8244. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8245. (delete-region (point) (1+ (point-at-eol)))
  8246. (beginning-of-line tonew)
  8247. (insert txt)
  8248. (beginning-of-line 0)
  8249. (move-to-column col)
  8250. (unless (or hline1p hline2p)
  8251. (org-table-fix-formulas
  8252. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8253. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8254. (defun org-table-insert-row (&optional arg)
  8255. "Insert a new row above the current line into the table.
  8256. With prefix ARG, insert below the current line."
  8257. (interactive "P")
  8258. (if (not (org-at-table-p))
  8259. (error "Not at a table"))
  8260. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8261. (new (org-table-clean-line line)))
  8262. ;; Fix the first field if necessary
  8263. (if (string-match "^[ \t]*| *[#$] *|" line)
  8264. (setq new (replace-match (match-string 0 line) t t new)))
  8265. (beginning-of-line (if arg 2 1))
  8266. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8267. (beginning-of-line 0)
  8268. (re-search-forward "| ?" (point-at-eol) t)
  8269. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8270. (org-table-align))
  8271. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8272. (defun org-table-insert-hline (&optional above)
  8273. "Insert a horizontal-line below the current line into the table.
  8274. With prefix ABOVE, insert above the current line."
  8275. (interactive "P")
  8276. (if (not (org-at-table-p))
  8277. (error "Not at a table"))
  8278. (let ((line (org-table-clean-line
  8279. (buffer-substring (point-at-bol) (point-at-eol))))
  8280. (col (current-column)))
  8281. (while (string-match "|\\( +\\)|" line)
  8282. (setq line (replace-match
  8283. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8284. ?-) "|") t t line)))
  8285. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8286. (beginning-of-line (if above 1 2))
  8287. (insert line "\n")
  8288. (beginning-of-line (if above 1 -1))
  8289. (move-to-column col)
  8290. (and org-table-overlay-coordinates (org-table-align))))
  8291. (defun org-table-hline-and-move (&optional same-column)
  8292. "Insert a hline and move to the row below that line."
  8293. (interactive "P")
  8294. (let ((col (org-table-current-column)))
  8295. (org-table-maybe-eval-formula)
  8296. (org-table-maybe-recalculate-line)
  8297. (org-table-insert-hline)
  8298. (end-of-line 2)
  8299. (if (looking-at "\n[ \t]*|-")
  8300. (progn (insert "\n|") (org-table-align))
  8301. (org-table-next-field))
  8302. (if same-column (org-table-goto-column col))))
  8303. (defun org-table-clean-line (s)
  8304. "Convert a table line S into a string with only \"|\" and space.
  8305. In particular, this does handle wide and invisible characters."
  8306. (if (string-match "^[ \t]*|-" s)
  8307. ;; It's a hline, just map the characters
  8308. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8309. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8310. (setq s (replace-match
  8311. (concat "|" (make-string (org-string-width (match-string 1 s))
  8312. ?\ ) "|")
  8313. t t s)))
  8314. s))
  8315. (defun org-table-kill-row ()
  8316. "Delete the current row or horizontal line from the table."
  8317. (interactive)
  8318. (if (not (org-at-table-p))
  8319. (error "Not at a table"))
  8320. (let ((col (current-column))
  8321. (dline (org-table-current-dline)))
  8322. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8323. (if (not (org-at-table-p)) (beginning-of-line 0))
  8324. (move-to-column col)
  8325. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8326. dline -1 dline)))
  8327. (defun org-table-sort-lines (with-case &optional sorting-type)
  8328. "Sort table lines according to the column at point.
  8329. The position of point indicates the column to be used for
  8330. sorting, and the range of lines is the range between the nearest
  8331. horizontal separator lines, or the entire table of no such lines
  8332. exist. If point is before the first column, you will be prompted
  8333. for the sorting column. If there is an active region, the mark
  8334. specifies the first line and the sorting column, while point
  8335. should be in the last line to be included into the sorting.
  8336. The command then prompts for the sorting type which can be
  8337. alphabetically, numerically, or by time (as given in a time stamp
  8338. in the field). Sorting in reverse order is also possible.
  8339. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8340. If SORTING-TYPE is specified when this function is called from a Lisp
  8341. program, no prompting will take place. SORTING-TYPE must be a character,
  8342. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8343. should be done in reverse order."
  8344. (interactive "P")
  8345. (let* ((thisline (org-current-line))
  8346. (thiscol (org-table-current-column))
  8347. beg end bcol ecol tend tbeg column lns pos)
  8348. (when (equal thiscol 0)
  8349. (if (interactive-p)
  8350. (setq thiscol
  8351. (string-to-number
  8352. (read-string "Use column N for sorting: ")))
  8353. (setq thiscol 1))
  8354. (org-table-goto-column thiscol))
  8355. (org-table-check-inside-data-field)
  8356. (if (org-region-active-p)
  8357. (progn
  8358. (setq beg (region-beginning) end (region-end))
  8359. (goto-char beg)
  8360. (setq column (org-table-current-column)
  8361. beg (point-at-bol))
  8362. (goto-char end)
  8363. (setq end (point-at-bol 2)))
  8364. (setq column (org-table-current-column)
  8365. pos (point)
  8366. tbeg (org-table-begin)
  8367. tend (org-table-end))
  8368. (if (re-search-backward org-table-hline-regexp tbeg t)
  8369. (setq beg (point-at-bol 2))
  8370. (goto-char tbeg)
  8371. (setq beg (point-at-bol 1)))
  8372. (goto-char pos)
  8373. (if (re-search-forward org-table-hline-regexp tend t)
  8374. (setq end (point-at-bol 1))
  8375. (goto-char tend)
  8376. (setq end (point-at-bol))))
  8377. (setq beg (move-marker (make-marker) beg)
  8378. end (move-marker (make-marker) end))
  8379. (untabify beg end)
  8380. (goto-char beg)
  8381. (org-table-goto-column column)
  8382. (skip-chars-backward "^|")
  8383. (setq bcol (current-column))
  8384. (org-table-goto-column (1+ column))
  8385. (skip-chars-backward "^|")
  8386. (setq ecol (1- (current-column)))
  8387. (org-table-goto-column column)
  8388. (setq lns (mapcar (lambda(x) (cons
  8389. (org-sort-remove-invisible
  8390. (nth (1- column)
  8391. (org-split-string x "[ \t]*|[ \t]*")))
  8392. x))
  8393. (org-split-string (buffer-substring beg end) "\n")))
  8394. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8395. (delete-region beg end)
  8396. (move-marker beg nil)
  8397. (move-marker end nil)
  8398. (insert (mapconcat 'cdr lns "\n") "\n")
  8399. (goto-line thisline)
  8400. (org-table-goto-column thiscol)
  8401. (message "%d lines sorted, based on column %d" (length lns) column)))
  8402. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8403. (defun org-sort-remove-invisible (s)
  8404. (remove-text-properties 0 (length s) org-rm-props s)
  8405. (while (string-match org-bracket-link-regexp s)
  8406. (setq s (replace-match (if (match-end 2)
  8407. (match-string 3 s)
  8408. (match-string 1 s)) t t s)))
  8409. s)
  8410. (defun org-table-cut-region (beg end)
  8411. "Copy region in table to the clipboard and blank all relevant fields."
  8412. (interactive "r")
  8413. (org-table-copy-region beg end 'cut))
  8414. (defun org-table-copy-region (beg end &optional cut)
  8415. "Copy rectangular region in table to clipboard.
  8416. A special clipboard is used which can only be accessed
  8417. with `org-table-paste-rectangle'."
  8418. (interactive "rP")
  8419. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8420. region cols
  8421. (rpl (if cut " " nil)))
  8422. (goto-char beg)
  8423. (org-table-check-inside-data-field)
  8424. (setq l01 (org-current-line)
  8425. c01 (org-table-current-column))
  8426. (goto-char end)
  8427. (org-table-check-inside-data-field)
  8428. (setq l02 (org-current-line)
  8429. c02 (org-table-current-column))
  8430. (setq l1 (min l01 l02) l2 (max l01 l02)
  8431. c1 (min c01 c02) c2 (max c01 c02))
  8432. (catch 'exit
  8433. (while t
  8434. (catch 'nextline
  8435. (if (> l1 l2) (throw 'exit t))
  8436. (goto-line l1)
  8437. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8438. (setq cols nil ic1 c1 ic2 c2)
  8439. (while (< ic1 (1+ ic2))
  8440. (push (org-table-get-field ic1 rpl) cols)
  8441. (setq ic1 (1+ ic1)))
  8442. (push (nreverse cols) region)
  8443. (setq l1 (1+ l1)))))
  8444. (setq org-table-clip (nreverse region))
  8445. (if cut (org-table-align))
  8446. org-table-clip))
  8447. (defun org-table-paste-rectangle ()
  8448. "Paste a rectangular region into a table.
  8449. The upper right corner ends up in the current field. All involved fields
  8450. will be overwritten. If the rectangle does not fit into the present table,
  8451. the table is enlarged as needed. The process ignores horizontal separator
  8452. lines."
  8453. (interactive)
  8454. (unless (and org-table-clip (listp org-table-clip))
  8455. (error "First cut/copy a region to paste!"))
  8456. (org-table-check-inside-data-field)
  8457. (let* ((clip org-table-clip)
  8458. (line (org-current-line))
  8459. (col (org-table-current-column))
  8460. (org-enable-table-editor t)
  8461. (org-table-automatic-realign nil)
  8462. c cols field)
  8463. (while (setq cols (pop clip))
  8464. (while (org-at-table-hline-p) (beginning-of-line 2))
  8465. (if (not (org-at-table-p))
  8466. (progn (end-of-line 0) (org-table-next-field)))
  8467. (setq c col)
  8468. (while (setq field (pop cols))
  8469. (org-table-goto-column c nil 'force)
  8470. (org-table-get-field nil field)
  8471. (setq c (1+ c)))
  8472. (beginning-of-line 2))
  8473. (goto-line line)
  8474. (org-table-goto-column col)
  8475. (org-table-align)))
  8476. (defun org-table-convert ()
  8477. "Convert from `org-mode' table to table.el and back.
  8478. Obviously, this only works within limits. When an Org-mode table is
  8479. converted to table.el, all horizontal separator lines get lost, because
  8480. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8481. A table.el table can be converted to an Org-mode table only if it does not
  8482. do row or column spanning. Multiline cells will become multiple cells.
  8483. Beware, Org-mode does not test if the table can be successfully converted - it
  8484. blindly applies a recipe that works for simple tables."
  8485. (interactive)
  8486. (require 'table)
  8487. (if (org-at-table.el-p)
  8488. ;; convert to Org-mode table
  8489. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8490. (end (move-marker (make-marker) (org-table-end t))))
  8491. (table-unrecognize-region beg end)
  8492. (goto-char beg)
  8493. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8494. (replace-match ""))
  8495. (goto-char beg))
  8496. (if (org-at-table-p)
  8497. ;; convert to table.el table
  8498. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8499. (end (move-marker (make-marker) (org-table-end))))
  8500. ;; first, get rid of all horizontal lines
  8501. (goto-char beg)
  8502. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8503. (replace-match ""))
  8504. ;; insert a hline before first
  8505. (goto-char beg)
  8506. (org-table-insert-hline 'above)
  8507. (beginning-of-line -1)
  8508. ;; insert a hline after each line
  8509. (while (progn (beginning-of-line 3) (< (point) end))
  8510. (org-table-insert-hline))
  8511. (goto-char beg)
  8512. (setq end (move-marker end (org-table-end)))
  8513. ;; replace "+" at beginning and ending of hlines
  8514. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8515. (replace-match "\\1+-"))
  8516. (goto-char beg)
  8517. (while (re-search-forward "-|[ \t]*$" end t)
  8518. (replace-match "-+"))
  8519. (goto-char beg)))))
  8520. (defun org-table-wrap-region (arg)
  8521. "Wrap several fields in a column like a paragraph.
  8522. This is useful if you'd like to spread the contents of a field over several
  8523. lines, in order to keep the table compact.
  8524. If there is an active region, and both point and mark are in the same column,
  8525. the text in the column is wrapped to minimum width for the given number of
  8526. lines. Generally, this makes the table more compact. A prefix ARG may be
  8527. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8528. formats the selected text to two lines. If the region was longer than two
  8529. lines, the remaining lines remain empty. A negative prefix argument reduces
  8530. the current number of lines by that amount. The wrapped text is pasted back
  8531. into the table. If you formatted it to more lines than it was before, fields
  8532. further down in the table get overwritten - so you might need to make space in
  8533. the table first.
  8534. If there is no region, the current field is split at the cursor position and
  8535. the text fragment to the right of the cursor is prepended to the field one
  8536. line down.
  8537. If there is no region, but you specify a prefix ARG, the current field gets
  8538. blank, and the content is appended to the field above."
  8539. (interactive "P")
  8540. (org-table-check-inside-data-field)
  8541. (if (org-region-active-p)
  8542. ;; There is a region: fill as a paragraph
  8543. (let* ((beg (region-beginning))
  8544. (cline (save-excursion (goto-char beg) (org-current-line)))
  8545. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8546. nlines)
  8547. (org-table-cut-region (region-beginning) (region-end))
  8548. (if (> (length (car org-table-clip)) 1)
  8549. (error "Region must be limited to single column"))
  8550. (setq nlines (if arg
  8551. (if (< arg 1)
  8552. (+ (length org-table-clip) arg)
  8553. arg)
  8554. (length org-table-clip)))
  8555. (setq org-table-clip
  8556. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8557. nil nlines)))
  8558. (goto-line cline)
  8559. (org-table-goto-column ccol)
  8560. (org-table-paste-rectangle))
  8561. ;; No region, split the current field at point
  8562. (if arg
  8563. ;; combine with field above
  8564. (let ((s (org-table-blank-field))
  8565. (col (org-table-current-column)))
  8566. (beginning-of-line 0)
  8567. (while (org-at-table-hline-p) (beginning-of-line 0))
  8568. (org-table-goto-column col)
  8569. (skip-chars-forward "^|")
  8570. (skip-chars-backward " ")
  8571. (insert " " (org-trim s))
  8572. (org-table-align))
  8573. ;; split field
  8574. (when (looking-at "\\([^|]+\\)+|")
  8575. (let ((s (match-string 1)))
  8576. (replace-match " |")
  8577. (goto-char (match-beginning 0))
  8578. (org-table-next-row)
  8579. (insert (org-trim s) " ")
  8580. (org-table-align))))))
  8581. (defvar org-field-marker nil)
  8582. (defun org-table-edit-field (arg)
  8583. "Edit table field in a different window.
  8584. This is mainly useful for fields that contain hidden parts.
  8585. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8586. it can be edited in place."
  8587. (interactive "P")
  8588. (if arg
  8589. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8590. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8591. (remove-text-properties b e '(org-cwidth t invisible t
  8592. display t intangible t))
  8593. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8594. (font-lock-fontify-block)))
  8595. (let ((pos (move-marker (make-marker) (point)))
  8596. (field (org-table-get-field))
  8597. (cw (current-window-configuration))
  8598. p)
  8599. (org-switch-to-buffer-other-window "*Org tmp*")
  8600. (erase-buffer)
  8601. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8602. (let ((org-inhibit-startup t)) (org-mode))
  8603. (goto-char (setq p (point-max)))
  8604. (insert (org-trim field))
  8605. (remove-text-properties p (point-max)
  8606. '(invisible t org-cwidth t display t
  8607. intangible t))
  8608. (goto-char p)
  8609. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8610. (org-set-local 'org-window-configuration cw)
  8611. (org-set-local 'org-field-marker pos)
  8612. (message "Edit and finish with C-c C-c"))))
  8613. (defun org-table-finish-edit-field ()
  8614. "Finish editing a table data field.
  8615. Remove all newline characters, insert the result into the table, realign
  8616. the table and kill the editing buffer."
  8617. (let ((pos org-field-marker)
  8618. (cw org-window-configuration)
  8619. (cb (current-buffer))
  8620. text)
  8621. (goto-char (point-min))
  8622. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8623. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8624. (replace-match " "))
  8625. (setq text (org-trim (buffer-string)))
  8626. (set-window-configuration cw)
  8627. (kill-buffer cb)
  8628. (select-window (get-buffer-window (marker-buffer pos)))
  8629. (goto-char pos)
  8630. (move-marker pos nil)
  8631. (org-table-check-inside-data-field)
  8632. (org-table-get-field nil text)
  8633. (org-table-align)
  8634. (message "New field value inserted")))
  8635. (defun org-trim (s)
  8636. "Remove whitespace at beginning and end of string."
  8637. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8638. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8639. s)
  8640. (defun org-wrap (string &optional width lines)
  8641. "Wrap string to either a number of lines, or a width in characters.
  8642. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8643. that costs. If there is a word longer than WIDTH, the text is actually
  8644. wrapped to the length of that word.
  8645. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8646. many lines, whatever width that takes.
  8647. The return value is a list of lines, without newlines at the end."
  8648. (let* ((words (org-split-string string "[ \t\n]+"))
  8649. (maxword (apply 'max (mapcar 'org-string-width words)))
  8650. w ll)
  8651. (cond (width
  8652. (org-do-wrap words (max maxword width)))
  8653. (lines
  8654. (setq w maxword)
  8655. (setq ll (org-do-wrap words maxword))
  8656. (if (<= (length ll) lines)
  8657. ll
  8658. (setq ll words)
  8659. (while (> (length ll) lines)
  8660. (setq w (1+ w))
  8661. (setq ll (org-do-wrap words w)))
  8662. ll))
  8663. (t (error "Cannot wrap this")))))
  8664. (defun org-do-wrap (words width)
  8665. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8666. (let (lines line)
  8667. (while words
  8668. (setq line (pop words))
  8669. (while (and words (< (+ (length line) (length (car words))) width))
  8670. (setq line (concat line " " (pop words))))
  8671. (setq lines (push line lines)))
  8672. (nreverse lines)))
  8673. (defun org-split-string (string &optional separators)
  8674. "Splits STRING into substrings at SEPARATORS.
  8675. No empty strings are returned if there are matches at the beginning
  8676. and end of string."
  8677. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8678. (start 0)
  8679. notfirst
  8680. (list nil))
  8681. (while (and (string-match rexp string
  8682. (if (and notfirst
  8683. (= start (match-beginning 0))
  8684. (< start (length string)))
  8685. (1+ start) start))
  8686. (< (match-beginning 0) (length string)))
  8687. (setq notfirst t)
  8688. (or (eq (match-beginning 0) 0)
  8689. (and (eq (match-beginning 0) (match-end 0))
  8690. (eq (match-beginning 0) start))
  8691. (setq list
  8692. (cons (substring string start (match-beginning 0))
  8693. list)))
  8694. (setq start (match-end 0)))
  8695. (or (eq start (length string))
  8696. (setq list
  8697. (cons (substring string start)
  8698. list)))
  8699. (nreverse list)))
  8700. (defun org-table-map-tables (function)
  8701. "Apply FUNCTION to the start of all tables in the buffer."
  8702. (save-excursion
  8703. (save-restriction
  8704. (widen)
  8705. (goto-char (point-min))
  8706. (while (re-search-forward org-table-any-line-regexp nil t)
  8707. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8708. (beginning-of-line 1)
  8709. (if (looking-at org-table-line-regexp)
  8710. (save-excursion (funcall function)))
  8711. (re-search-forward org-table-any-border-regexp nil 1))))
  8712. (message "Mapping tables: done"))
  8713. (defvar org-timecnt) ; dynamically scoped parameter
  8714. (defun org-table-sum (&optional beg end nlast)
  8715. "Sum numbers in region of current table column.
  8716. The result will be displayed in the echo area, and will be available
  8717. as kill to be inserted with \\[yank].
  8718. If there is an active region, it is interpreted as a rectangle and all
  8719. numbers in that rectangle will be summed. If there is no active
  8720. region and point is located in a table column, sum all numbers in that
  8721. column.
  8722. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8723. numbers are assumed to be times as well (in decimal hours) and the
  8724. numbers are added as such.
  8725. If NLAST is a number, only the NLAST fields will actually be summed."
  8726. (interactive)
  8727. (save-excursion
  8728. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8729. (cond
  8730. ((and beg end)) ; beg and end given explicitly
  8731. ((org-region-active-p)
  8732. (setq beg (region-beginning) end (region-end)))
  8733. (t
  8734. (setq col (org-table-current-column))
  8735. (goto-char (org-table-begin))
  8736. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8737. (error "No table data"))
  8738. (org-table-goto-column col)
  8739. (setq beg (point))
  8740. (goto-char (org-table-end))
  8741. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8742. (error "No table data"))
  8743. (org-table-goto-column col)
  8744. (setq end (point))))
  8745. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8746. (items1 (cond ((not nlast) items)
  8747. ((>= nlast (length items)) items)
  8748. (t (setq items (reverse items))
  8749. (setcdr (nthcdr (1- nlast) items) nil)
  8750. (nreverse items))))
  8751. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8752. items1)))
  8753. (res (apply '+ numbers))
  8754. (sres (if (= org-timecnt 0)
  8755. (format "%g" res)
  8756. (setq diff (* 3600 res)
  8757. h (floor (/ diff 3600)) diff (mod diff 3600)
  8758. m (floor (/ diff 60)) diff (mod diff 60)
  8759. s diff)
  8760. (format "%d:%02d:%02d" h m s))))
  8761. (kill-new sres)
  8762. (if (interactive-p)
  8763. (message "%s"
  8764. (substitute-command-keys
  8765. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8766. (length numbers) sres))))
  8767. sres))))
  8768. (defun org-table-get-number-for-summing (s)
  8769. (let (n)
  8770. (if (string-match "^ *|? *" s)
  8771. (setq s (replace-match "" nil nil s)))
  8772. (if (string-match " *|? *$" s)
  8773. (setq s (replace-match "" nil nil s)))
  8774. (setq n (string-to-number s))
  8775. (cond
  8776. ((and (string-match "0" s)
  8777. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8778. ((string-match "\\`[ \t]+\\'" s) nil)
  8779. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8780. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8781. (m (string-to-number (or (match-string 2 s) "0")))
  8782. (s (string-to-number (or (match-string 4 s) "0"))))
  8783. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8784. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8785. ((equal n 0) nil)
  8786. (t n))))
  8787. (defun org-table-current-field-formula (&optional key noerror)
  8788. "Return the formula active for the current field.
  8789. Assumes that specials are in place.
  8790. If KEY is given, return the key to this formula.
  8791. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8792. (let* ((name (car (rassoc (list (org-current-line)
  8793. (org-table-current-column))
  8794. org-table-named-field-locations)))
  8795. (col (org-table-current-column))
  8796. (scol (int-to-string col))
  8797. (ref (format "@%d$%d" (org-table-current-dline) col))
  8798. (stored-list (org-table-get-stored-formulas noerror))
  8799. (ass (or (assoc name stored-list)
  8800. (assoc ref stored-list)
  8801. (assoc scol stored-list))))
  8802. (if key
  8803. (car ass)
  8804. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8805. (cdr ass))))))
  8806. (defun org-table-get-formula (&optional equation named)
  8807. "Read a formula from the minibuffer, offer stored formula as default.
  8808. When NAMED is non-nil, look for a named equation."
  8809. (let* ((stored-list (org-table-get-stored-formulas))
  8810. (name (car (rassoc (list (org-current-line)
  8811. (org-table-current-column))
  8812. org-table-named-field-locations)))
  8813. (ref (format "@%d$%d" (org-table-current-dline)
  8814. (org-table-current-column)))
  8815. (refass (assoc ref stored-list))
  8816. (scol (if named
  8817. (if name name ref)
  8818. (int-to-string (org-table-current-column))))
  8819. (dummy (and (or name refass) (not named)
  8820. (not (y-or-n-p "Replace field formula with column formula? " ))
  8821. (error "Abort")))
  8822. (name (or name ref))
  8823. (org-table-may-need-update nil)
  8824. (stored (cdr (assoc scol stored-list)))
  8825. (eq (cond
  8826. ((and stored equation (string-match "^ *=? *$" equation))
  8827. stored)
  8828. ((stringp equation)
  8829. equation)
  8830. (t (org-table-formula-from-user
  8831. (read-string
  8832. (org-table-formula-to-user
  8833. (format "%s formula %s%s="
  8834. (if named "Field" "Column")
  8835. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8836. scol))
  8837. (if stored (org-table-formula-to-user stored) "")
  8838. 'org-table-formula-history
  8839. )))))
  8840. mustsave)
  8841. (when (not (string-match "\\S-" eq))
  8842. ;; remove formula
  8843. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8844. (org-table-store-formulas stored-list)
  8845. (error "Formula removed"))
  8846. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8847. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8848. (if (and name (not named))
  8849. ;; We set the column equation, delete the named one.
  8850. (setq stored-list (delq (assoc name stored-list) stored-list)
  8851. mustsave t))
  8852. (if stored
  8853. (setcdr (assoc scol stored-list) eq)
  8854. (setq stored-list (cons (cons scol eq) stored-list)))
  8855. (if (or mustsave (not (equal stored eq)))
  8856. (org-table-store-formulas stored-list))
  8857. eq))
  8858. (defun org-table-store-formulas (alist)
  8859. "Store the list of formulas below the current table."
  8860. (setq alist (sort alist 'org-table-formula-less-p))
  8861. (save-excursion
  8862. (goto-char (org-table-end))
  8863. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8864. (progn
  8865. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8866. (goto-char (match-beginning 2))
  8867. (delete-region (match-beginning 2) (match-end 0)))
  8868. (insert "#+TBLFM:"))
  8869. (insert " "
  8870. (mapconcat (lambda (x)
  8871. (concat
  8872. (if (equal (string-to-char (car x)) ?@) "" "$")
  8873. (car x) "=" (cdr x)))
  8874. alist "::")
  8875. "\n")))
  8876. (defsubst org-table-formula-make-cmp-string (a)
  8877. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8878. (concat
  8879. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8880. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8881. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8882. (defun org-table-formula-less-p (a b)
  8883. "Compare two formulas for sorting."
  8884. (let ((as (org-table-formula-make-cmp-string (car a)))
  8885. (bs (org-table-formula-make-cmp-string (car b))))
  8886. (and as bs (string< as bs))))
  8887. (defun org-table-get-stored-formulas (&optional noerror)
  8888. "Return an alist with the stored formulas directly after current table."
  8889. (interactive)
  8890. (let (scol eq eq-alist strings string seen)
  8891. (save-excursion
  8892. (goto-char (org-table-end))
  8893. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8894. (setq strings (org-split-string (match-string 2) " *:: *"))
  8895. (while (setq string (pop strings))
  8896. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8897. (setq scol (if (match-end 2)
  8898. (match-string 2 string)
  8899. (match-string 1 string))
  8900. eq (match-string 3 string)
  8901. eq-alist (cons (cons scol eq) eq-alist))
  8902. (if (member scol seen)
  8903. (if noerror
  8904. (progn
  8905. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8906. (ding)
  8907. (sit-for 2))
  8908. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8909. (push scol seen))))))
  8910. (nreverse eq-alist)))
  8911. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8912. "Modify the equations after the table structure has been edited.
  8913. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8914. For all numbers larger than LIMIT, shift them by DELTA."
  8915. (save-excursion
  8916. (goto-char (org-table-end))
  8917. (when (looking-at "#\\+TBLFM:")
  8918. (let ((re (concat key "\\([0-9]+\\)"))
  8919. (re2
  8920. (when remove
  8921. (if (equal key "$")
  8922. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8923. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8924. s n a)
  8925. (when remove
  8926. (while (re-search-forward re2 (point-at-eol) t)
  8927. (replace-match "")))
  8928. (while (re-search-forward re (point-at-eol) t)
  8929. (setq s (match-string 1) n (string-to-number s))
  8930. (cond
  8931. ((setq a (assoc s replace))
  8932. (replace-match (concat key (cdr a)) t t))
  8933. ((and limit (> n limit))
  8934. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8935. (defun org-table-get-specials ()
  8936. "Get the column names and local parameters for this table."
  8937. (save-excursion
  8938. (let ((beg (org-table-begin)) (end (org-table-end))
  8939. names name fields fields1 field cnt
  8940. c v l line col types dlines hlines)
  8941. (setq org-table-column-names nil
  8942. org-table-local-parameters nil
  8943. org-table-named-field-locations nil
  8944. org-table-current-begin-line nil
  8945. org-table-current-begin-pos nil
  8946. org-table-current-line-types nil)
  8947. (goto-char beg)
  8948. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8949. (setq names (org-split-string (match-string 1) " *| *")
  8950. cnt 1)
  8951. (while (setq name (pop names))
  8952. (setq cnt (1+ cnt))
  8953. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8954. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8955. (setq org-table-column-names (nreverse org-table-column-names))
  8956. (setq org-table-column-name-regexp
  8957. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8958. (goto-char beg)
  8959. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8960. (setq fields (org-split-string (match-string 1) " *| *"))
  8961. (while (setq field (pop fields))
  8962. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8963. (push (cons (match-string 1 field) (match-string 2 field))
  8964. org-table-local-parameters))))
  8965. (goto-char beg)
  8966. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8967. (setq c (match-string 1)
  8968. fields (org-split-string (match-string 2) " *| *"))
  8969. (save-excursion
  8970. (beginning-of-line (if (equal c "_") 2 0))
  8971. (setq line (org-current-line) col 1)
  8972. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8973. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8974. (while (and fields1 (setq field (pop fields)))
  8975. (setq v (pop fields1) col (1+ col))
  8976. (when (and (stringp field) (stringp v)
  8977. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8978. (push (cons field v) org-table-local-parameters)
  8979. (push (list field line col) org-table-named-field-locations))))
  8980. ;; Analyse the line types
  8981. (goto-char beg)
  8982. (setq org-table-current-begin-line (org-current-line)
  8983. org-table-current-begin-pos (point)
  8984. l org-table-current-begin-line)
  8985. (while (looking-at "[ \t]*|\\(-\\)?")
  8986. (push (if (match-end 1) 'hline 'dline) types)
  8987. (if (match-end 1) (push l hlines) (push l dlines))
  8988. (beginning-of-line 2)
  8989. (setq l (1+ l)))
  8990. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8991. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8992. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8993. (defun org-table-maybe-eval-formula ()
  8994. "Check if the current field starts with \"=\" or \":=\".
  8995. If yes, store the formula and apply it."
  8996. ;; We already know we are in a table. Get field will only return a formula
  8997. ;; when appropriate. It might return a separator line, but no problem.
  8998. (when org-table-formula-evaluate-inline
  8999. (let* ((field (org-trim (or (org-table-get-field) "")))
  9000. named eq)
  9001. (when (string-match "^:?=\\(.*\\)" field)
  9002. (setq named (equal (string-to-char field) ?:)
  9003. eq (match-string 1 field))
  9004. (if (or (fboundp 'calc-eval)
  9005. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9006. (org-table-eval-formula (if named '(4) nil)
  9007. (org-table-formula-from-user eq))
  9008. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9009. (defvar org-recalc-commands nil
  9010. "List of commands triggering the recalculation of a line.
  9011. Will be filled automatically during use.")
  9012. (defvar org-recalc-marks
  9013. '((" " . "Unmarked: no special line, no automatic recalculation")
  9014. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9015. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9016. ("!" . "Column name definition line. Reference in formula as $name.")
  9017. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9018. ("_" . "Names for values in row below this one.")
  9019. ("^" . "Names for values in row above this one.")))
  9020. (defun org-table-rotate-recalc-marks (&optional newchar)
  9021. "Rotate the recalculation mark in the first column.
  9022. If in any row, the first field is not consistent with a mark,
  9023. insert a new column for the markers.
  9024. When there is an active region, change all the lines in the region,
  9025. after prompting for the marking character.
  9026. After each change, a message will be displayed indicating the meaning
  9027. of the new mark."
  9028. (interactive)
  9029. (unless (org-at-table-p) (error "Not at a table"))
  9030. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9031. (beg (org-table-begin))
  9032. (end (org-table-end))
  9033. (l (org-current-line))
  9034. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9035. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9036. (have-col
  9037. (save-excursion
  9038. (goto-char beg)
  9039. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9040. (col (org-table-current-column))
  9041. (forcenew (car (assoc newchar org-recalc-marks)))
  9042. epos new)
  9043. (when l1
  9044. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9045. (setq newchar (char-to-string (read-char-exclusive))
  9046. forcenew (car (assoc newchar org-recalc-marks))))
  9047. (if (and newchar (not forcenew))
  9048. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9049. newchar))
  9050. (if l1 (goto-line l1))
  9051. (save-excursion
  9052. (beginning-of-line 1)
  9053. (unless (looking-at org-table-dataline-regexp)
  9054. (error "Not at a table data line")))
  9055. (unless have-col
  9056. (org-table-goto-column 1)
  9057. (org-table-insert-column)
  9058. (org-table-goto-column (1+ col)))
  9059. (setq epos (point-at-eol))
  9060. (save-excursion
  9061. (beginning-of-line 1)
  9062. (org-table-get-field
  9063. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9064. (concat " "
  9065. (setq new (or forcenew
  9066. (cadr (member (match-string 1) marks))))
  9067. " ")
  9068. " # ")))
  9069. (if (and l1 l2)
  9070. (progn
  9071. (goto-line l1)
  9072. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9073. (and (looking-at org-table-dataline-regexp)
  9074. (org-table-get-field 1 (concat " " new " "))))
  9075. (goto-line l1)))
  9076. (if (not (= epos (point-at-eol))) (org-table-align))
  9077. (goto-line l)
  9078. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9079. (defun org-table-maybe-recalculate-line ()
  9080. "Recompute the current line if marked for it, and if we haven't just done it."
  9081. (interactive)
  9082. (and org-table-allow-automatic-line-recalculation
  9083. (not (and (memq last-command org-recalc-commands)
  9084. (equal org-last-recalc-line (org-current-line))))
  9085. (save-excursion (beginning-of-line 1)
  9086. (looking-at org-table-auto-recalculate-regexp))
  9087. (org-table-recalculate) t))
  9088. (defvar org-table-formula-debug nil
  9089. "Non-nil means, debug table formulas.
  9090. When nil, simply write \"#ERROR\" in corrupted fields.")
  9091. (make-variable-buffer-local 'org-table-formula-debug)
  9092. (defvar modes)
  9093. (defsubst org-set-calc-mode (var &optional value)
  9094. (if (stringp var)
  9095. (setq var (assoc var '(("D" calc-angle-mode deg)
  9096. ("R" calc-angle-mode rad)
  9097. ("F" calc-prefer-frac t)
  9098. ("S" calc-symbolic-mode t)))
  9099. value (nth 2 var) var (nth 1 var)))
  9100. (if (memq var modes)
  9101. (setcar (cdr (memq var modes)) value)
  9102. (cons var (cons value modes)))
  9103. modes)
  9104. (defun org-table-eval-formula (&optional arg equation
  9105. suppress-align suppress-const
  9106. suppress-store suppress-analysis)
  9107. "Replace the table field value at the cursor by the result of a calculation.
  9108. This function makes use of Dave Gillespie's Calc package, in my view the
  9109. most exciting program ever written for GNU Emacs. So you need to have Calc
  9110. installed in order to use this function.
  9111. In a table, this command replaces the value in the current field with the
  9112. result of a formula. It also installs the formula as the \"current\" column
  9113. formula, by storing it in a special line below the table. When called
  9114. with a `C-u' prefix, the current field must ba a named field, and the
  9115. formula is installed as valid in only this specific field.
  9116. When called with two `C-u' prefixes, insert the active equation
  9117. for the field back into the current field, so that it can be
  9118. edited there. This is useful in order to use \\[org-table-show-reference]
  9119. to check the referenced fields.
  9120. When called, the command first prompts for a formula, which is read in
  9121. the minibuffer. Previously entered formulas are available through the
  9122. history list, and the last used formula is offered as a default.
  9123. These stored formulas are adapted correctly when moving, inserting, or
  9124. deleting columns with the corresponding commands.
  9125. The formula can be any algebraic expression understood by the Calc package.
  9126. For details, see the Org-mode manual.
  9127. This function can also be called from Lisp programs and offers
  9128. additional arguments: EQUATION can be the formula to apply. If this
  9129. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9130. used to speed-up recursive calls by by-passing unnecessary aligns.
  9131. SUPPRESS-CONST suppresses the interpretation of constants in the
  9132. formula, assuming that this has been done already outside the function.
  9133. SUPPRESS-STORE means the formula should not be stored, either because
  9134. it is already stored, or because it is a modified equation that should
  9135. not overwrite the stored one."
  9136. (interactive "P")
  9137. (org-table-check-inside-data-field)
  9138. (or suppress-analysis (org-table-get-specials))
  9139. (if (equal arg '(16))
  9140. (let ((eq (org-table-current-field-formula)))
  9141. (or eq (error "No equation active for current field"))
  9142. (org-table-get-field nil eq)
  9143. (org-table-align)
  9144. (setq org-table-may-need-update t))
  9145. (let* (fields
  9146. (ndown (if (integerp arg) arg 1))
  9147. (org-table-automatic-realign nil)
  9148. (case-fold-search nil)
  9149. (down (> ndown 1))
  9150. (formula (if (and equation suppress-store)
  9151. equation
  9152. (org-table-get-formula equation (equal arg '(4)))))
  9153. (n0 (org-table-current-column))
  9154. (modes (copy-sequence org-calc-default-modes))
  9155. (numbers nil) ; was a variable, now fixed default
  9156. (keep-empty nil)
  9157. n form form0 bw fmt x ev orig c lispp literal)
  9158. ;; Parse the format string. Since we have a lot of modes, this is
  9159. ;; a lot of work. However, I think calc still uses most of the time.
  9160. (if (string-match ";" formula)
  9161. (let ((tmp (org-split-string formula ";")))
  9162. (setq formula (car tmp)
  9163. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9164. (nth 1 tmp)))
  9165. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9166. (setq c (string-to-char (match-string 1 fmt))
  9167. n (string-to-number (match-string 2 fmt)))
  9168. (if (= c ?p)
  9169. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9170. (setq modes (org-set-calc-mode
  9171. 'calc-float-format
  9172. (list (cdr (assoc c '((?n . float) (?f . fix)
  9173. (?s . sci) (?e . eng))))
  9174. n))))
  9175. (setq fmt (replace-match "" t t fmt)))
  9176. (if (string-match "[NT]" fmt)
  9177. (setq numbers (equal (match-string 0 fmt) "N")
  9178. fmt (replace-match "" t t fmt)))
  9179. (if (string-match "L" fmt)
  9180. (setq literal t
  9181. fmt (replace-match "" t t fmt)))
  9182. (if (string-match "E" fmt)
  9183. (setq keep-empty t
  9184. fmt (replace-match "" t t fmt)))
  9185. (while (string-match "[DRFS]" fmt)
  9186. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9187. (setq fmt (replace-match "" t t fmt)))
  9188. (unless (string-match "\\S-" fmt)
  9189. (setq fmt nil))))
  9190. (if (and (not suppress-const) org-table-formula-use-constants)
  9191. (setq formula (org-table-formula-substitute-names formula)))
  9192. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9193. (while (> ndown 0)
  9194. (setq fields (org-split-string
  9195. (org-no-properties
  9196. (buffer-substring (point-at-bol) (point-at-eol)))
  9197. " *| *"))
  9198. (if (eq numbers t)
  9199. (setq fields (mapcar
  9200. (lambda (x) (number-to-string (string-to-number x)))
  9201. fields)))
  9202. (setq ndown (1- ndown))
  9203. (setq form (copy-sequence formula)
  9204. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9205. (if (and lispp literal) (setq lispp 'literal))
  9206. ;; Check for old vertical references
  9207. (setq form (org-rewrite-old-row-references form))
  9208. ;; Insert complex ranges
  9209. (while (string-match org-table-range-regexp form)
  9210. (setq form
  9211. (replace-match
  9212. (save-match-data
  9213. (org-table-make-reference
  9214. (org-table-get-range (match-string 0 form) nil n0)
  9215. keep-empty numbers lispp))
  9216. t t form)))
  9217. ;; Insert simple ranges
  9218. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9219. (setq form
  9220. (replace-match
  9221. (save-match-data
  9222. (org-table-make-reference
  9223. (org-sublist
  9224. fields (string-to-number (match-string 1 form))
  9225. (string-to-number (match-string 2 form)))
  9226. keep-empty numbers lispp))
  9227. t t form)))
  9228. (setq form0 form)
  9229. ;; Insert the references to fields in same row
  9230. (while (string-match "\\$\\([0-9]+\\)" form)
  9231. (setq n (string-to-number (match-string 1 form))
  9232. x (nth (1- (if (= n 0) n0 n)) fields))
  9233. (unless x (error "Invalid field specifier \"%s\""
  9234. (match-string 0 form)))
  9235. (setq form (replace-match
  9236. (save-match-data
  9237. (org-table-make-reference x nil numbers lispp))
  9238. t t form)))
  9239. (if lispp
  9240. (setq ev (condition-case nil
  9241. (eval (eval (read form)))
  9242. (error "#ERROR"))
  9243. ev (if (numberp ev) (number-to-string ev) ev))
  9244. (or (fboundp 'calc-eval)
  9245. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9246. (setq ev (calc-eval (cons form modes)
  9247. (if numbers 'num))))
  9248. (when org-table-formula-debug
  9249. (with-output-to-temp-buffer "*Substitution History*"
  9250. (princ (format "Substitution history of formula
  9251. Orig: %s
  9252. $xyz-> %s
  9253. @r$c-> %s
  9254. $1-> %s\n" orig formula form0 form))
  9255. (if (listp ev)
  9256. (princ (format " %s^\nError: %s"
  9257. (make-string (car ev) ?\-) (nth 1 ev)))
  9258. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9259. ev (or fmt "NONE")
  9260. (if fmt (format fmt (string-to-number ev)) ev)))))
  9261. (setq bw (get-buffer-window "*Substitution History*"))
  9262. (shrink-window-if-larger-than-buffer bw)
  9263. (unless (and (interactive-p) (not ndown))
  9264. (unless (let (inhibit-redisplay)
  9265. (y-or-n-p "Debugging Formula. Continue to next? "))
  9266. (org-table-align)
  9267. (error "Abort"))
  9268. (delete-window bw)
  9269. (message "")))
  9270. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9271. (org-table-justify-field-maybe
  9272. (if fmt (format fmt (string-to-number ev)) ev))
  9273. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9274. (call-interactively 'org-return)
  9275. (setq ndown 0)))
  9276. (and down (org-table-maybe-recalculate-line))
  9277. (or suppress-align (and org-table-may-need-update
  9278. (org-table-align))))))
  9279. (defun org-table-put-field-property (prop value)
  9280. (save-excursion
  9281. (put-text-property (progn (skip-chars-backward "^|") (point))
  9282. (progn (skip-chars-forward "^|") (point))
  9283. prop value)))
  9284. (defun org-table-get-range (desc &optional tbeg col highlight)
  9285. "Get a calc vector from a column, accorting to descriptor DESC.
  9286. Optional arguments TBEG and COL can give the beginning of the table and
  9287. the current column, to avoid unnecessary parsing.
  9288. HIGHLIGHT means, just highlight the range."
  9289. (if (not (equal (string-to-char desc) ?@))
  9290. (setq desc (concat "@" desc)))
  9291. (save-excursion
  9292. (or tbeg (setq tbeg (org-table-begin)))
  9293. (or col (setq col (org-table-current-column)))
  9294. (let ((thisline (org-current-line))
  9295. beg end c1 c2 r1 r2 rangep tmp)
  9296. (unless (string-match org-table-range-regexp desc)
  9297. (error "Invalid table range specifier `%s'" desc))
  9298. (setq rangep (match-end 3)
  9299. r1 (and (match-end 1) (match-string 1 desc))
  9300. r2 (and (match-end 4) (match-string 4 desc))
  9301. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9302. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9303. (and c1 (setq c1 (+ (string-to-number c1)
  9304. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9305. (and c2 (setq c2 (+ (string-to-number c2)
  9306. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9307. (if (equal r1 "") (setq r1 nil))
  9308. (if (equal r2 "") (setq r2 nil))
  9309. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9310. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9311. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9312. (if (not r1) (setq r1 thisline))
  9313. (if (not r2) (setq r2 thisline))
  9314. (if (not c1) (setq c1 col))
  9315. (if (not c2) (setq c2 col))
  9316. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9317. ;; just one field
  9318. (progn
  9319. (goto-line r1)
  9320. (while (not (looking-at org-table-dataline-regexp))
  9321. (beginning-of-line 2))
  9322. (prog1 (org-trim (org-table-get-field c1))
  9323. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9324. ;; A range, return a vector
  9325. ;; First sort the numbers to get a regular ractangle
  9326. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9327. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9328. (goto-line r1)
  9329. (while (not (looking-at org-table-dataline-regexp))
  9330. (beginning-of-line 2))
  9331. (org-table-goto-column c1)
  9332. (setq beg (point))
  9333. (goto-line r2)
  9334. (while (not (looking-at org-table-dataline-regexp))
  9335. (beginning-of-line 0))
  9336. (org-table-goto-column c2)
  9337. (setq end (point))
  9338. (if highlight
  9339. (org-table-highlight-rectangle
  9340. beg (progn (skip-chars-forward "^|\n") (point))))
  9341. ;; return string representation of calc vector
  9342. (mapcar 'org-trim
  9343. (apply 'append (org-table-copy-region beg end)))))))
  9344. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9345. "Analyze descriptor DESC and retrieve the corresponding line number.
  9346. The cursor is currently in line CLINE, the table begins in line BLINE,
  9347. and TABLE is a vector with line types."
  9348. (if (string-match "^[0-9]+$" desc)
  9349. (aref org-table-dlines (string-to-number desc))
  9350. (setq cline (or cline (org-current-line))
  9351. bline (or bline org-table-current-begin-line)
  9352. table (or table org-table-current-line-types))
  9353. (if (or
  9354. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9355. ;; 1 2 3 4 5 6
  9356. (and (not (match-end 3)) (not (match-end 6)))
  9357. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9358. (error "invalid row descriptor `%s'" desc))
  9359. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9360. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9361. (odir (and (match-end 5) (match-string 5 desc)))
  9362. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9363. (i (- cline bline))
  9364. (rel (and (match-end 6)
  9365. (or (and (match-end 1) (not (match-end 3)))
  9366. (match-end 5)))))
  9367. (if (and hn (not hdir))
  9368. (progn
  9369. (setq i 0 hdir "+")
  9370. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9371. (if (and (not hn) on (not odir))
  9372. (error "should never happen");;(aref org-table-dlines on)
  9373. (if (and hn (> hn 0))
  9374. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9375. (if on
  9376. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9377. (+ bline i)))))
  9378. (defun org-find-row-type (table i type backwards relative n)
  9379. (let ((l (length table)))
  9380. (while (> n 0)
  9381. (while (and (setq i (+ i (if backwards -1 1)))
  9382. (>= i 0) (< i l)
  9383. (not (eq (aref table i) type))
  9384. (if (and relative (eq (aref table i) 'hline))
  9385. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9386. t)))
  9387. (setq n (1- n)))
  9388. (if (or (< i 0) (>= i l))
  9389. (error "Row descriptior leads outside table")
  9390. i)))
  9391. (defun org-rewrite-old-row-references (s)
  9392. (if (string-match "&[-+0-9I]" s)
  9393. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9394. s))
  9395. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9396. "Convert list ELEMENTS to something appropriate to insert into formula.
  9397. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9398. NUMBERS indicates that everything should be converted to numbers.
  9399. LISPP means to return something appropriate for a Lisp list."
  9400. (if (stringp elements) ; just a single val
  9401. (if lispp
  9402. (if (eq lispp 'literal)
  9403. elements
  9404. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9405. (if (equal elements "") (setq elements "0"))
  9406. (if numbers (number-to-string (string-to-number elements)) elements))
  9407. (unless keep-empty
  9408. (setq elements
  9409. (delq nil
  9410. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9411. elements))))
  9412. (setq elements (or elements '("0")))
  9413. (if lispp
  9414. (mapconcat
  9415. (lambda (x)
  9416. (if (eq lispp 'literal)
  9417. x
  9418. (prin1-to-string (if numbers (string-to-number x) x))))
  9419. elements " ")
  9420. (concat "[" (mapconcat
  9421. (lambda (x)
  9422. (if numbers (number-to-string (string-to-number x)) x))
  9423. elements
  9424. ",") "]"))))
  9425. (defun org-table-recalculate (&optional all noalign)
  9426. "Recalculate the current table line by applying all stored formulas.
  9427. With prefix arg ALL, do this for all lines in the table."
  9428. (interactive "P")
  9429. (or (memq this-command org-recalc-commands)
  9430. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9431. (unless (org-at-table-p) (error "Not at a table"))
  9432. (if (equal all '(16))
  9433. (org-table-iterate)
  9434. (org-table-get-specials)
  9435. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9436. (lambda (a b) (string< (car a) (car b)))))
  9437. (inhibit-redisplay (not debug-on-error))
  9438. (line-re org-table-dataline-regexp)
  9439. (thisline (org-current-line))
  9440. (thiscol (org-table-current-column))
  9441. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9442. ;; Insert constants in all formulas
  9443. (setq eqlist
  9444. (mapcar (lambda (x)
  9445. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9446. x)
  9447. eqlist))
  9448. ;; Split the equation list
  9449. (while (setq eq (pop eqlist))
  9450. (if (<= (string-to-char (car eq)) ?9)
  9451. (push eq eqlnum)
  9452. (push eq eqlname)))
  9453. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9454. (if all
  9455. (progn
  9456. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9457. (goto-char (setq beg (org-table-begin)))
  9458. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9459. ;; This is a table with marked lines, compute selected lines
  9460. (setq line-re org-table-recalculate-regexp)
  9461. ;; Move forward to the first non-header line
  9462. (if (and (re-search-forward org-table-dataline-regexp end t)
  9463. (re-search-forward org-table-hline-regexp end t)
  9464. (re-search-forward org-table-dataline-regexp end t))
  9465. (setq beg (match-beginning 0))
  9466. nil))) ;; just leave beg where it is
  9467. (setq beg (point-at-bol)
  9468. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9469. (goto-char beg)
  9470. (and all (message "Re-applying formulas to full table..."))
  9471. ;; First find the named fields, and mark them untouchanble
  9472. (remove-text-properties beg end '(org-untouchable t))
  9473. (while (setq eq (pop eqlname))
  9474. (setq name (car eq)
  9475. a (assoc name org-table-named-field-locations))
  9476. (and (not a)
  9477. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9478. (setq a (list name
  9479. (aref org-table-dlines
  9480. (string-to-number (match-string 1 name)))
  9481. (string-to-number (match-string 2 name)))))
  9482. (when (and a (or all (equal (nth 1 a) thisline)))
  9483. (message "Re-applying formula to field: %s" name)
  9484. (goto-line (nth 1 a))
  9485. (org-table-goto-column (nth 2 a))
  9486. (push (append a (list (cdr eq))) eqlname1)
  9487. (org-table-put-field-property :org-untouchable t)))
  9488. ;; Now evauluate the column formulas, but skip fields covered by
  9489. ;; field formulas
  9490. (goto-char beg)
  9491. (while (re-search-forward line-re end t)
  9492. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9493. ;; Unprotected line, recalculate
  9494. (and all (message "Re-applying formulas to full table...(line %d)"
  9495. (setq cnt (1+ cnt))))
  9496. (setq org-last-recalc-line (org-current-line))
  9497. (setq eql eqlnum)
  9498. (while (setq entry (pop eql))
  9499. (goto-line org-last-recalc-line)
  9500. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9501. (unless (get-text-property (point) :org-untouchable)
  9502. (org-table-eval-formula nil (cdr entry)
  9503. 'noalign 'nocst 'nostore 'noanalysis)))))
  9504. ;; Now evaluate the field formulas
  9505. (while (setq eq (pop eqlname1))
  9506. (message "Re-applying formula to field: %s" (car eq))
  9507. (goto-line (nth 1 eq))
  9508. (org-table-goto-column (nth 2 eq))
  9509. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9510. 'nostore 'noanalysis))
  9511. (goto-line thisline)
  9512. (org-table-goto-column thiscol)
  9513. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9514. (or noalign (and org-table-may-need-update (org-table-align))
  9515. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9516. ;; back to initial position
  9517. (message "Re-applying formulas...done")
  9518. (goto-line thisline)
  9519. (org-table-goto-column thiscol)
  9520. (or noalign (and org-table-may-need-update (org-table-align))
  9521. (and all (message "Re-applying formulas...done"))))))
  9522. (defun org-table-iterate (&optional arg)
  9523. "Recalculate the table until it does not change anymore."
  9524. (interactive "P")
  9525. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9526. (i 0)
  9527. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9528. thistbl)
  9529. (catch 'exit
  9530. (while (< i imax)
  9531. (setq i (1+ i))
  9532. (org-table-recalculate 'all)
  9533. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9534. (if (not (string= lasttbl thistbl))
  9535. (setq lasttbl thistbl)
  9536. (if (> i 1)
  9537. (message "Convergence after %d iterations" i)
  9538. (message "Table was already stable"))
  9539. (throw 'exit t)))
  9540. (error "No convergence after %d iterations" i))))
  9541. (defun org-table-formula-substitute-names (f)
  9542. "Replace $const with values in string F."
  9543. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9544. ;; First, check for column names
  9545. (while (setq start (string-match org-table-column-name-regexp f start))
  9546. (setq start (1+ start))
  9547. (setq a (assoc (match-string 1 f) org-table-column-names))
  9548. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9549. ;; Parameters and constants
  9550. (setq start 0)
  9551. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9552. (setq start (1+ start))
  9553. (if (setq a (save-match-data
  9554. (org-table-get-constant (match-string 1 f))))
  9555. (setq f (replace-match
  9556. (concat (if pp "(") a (if pp ")")) t t f))))
  9557. (if org-table-formula-debug
  9558. (put-text-property 0 (length f) :orig-formula f1 f))
  9559. f))
  9560. (defun org-table-get-constant (const)
  9561. "Find the value for a parameter or constant in a formula.
  9562. Parameters get priority."
  9563. (or (cdr (assoc const org-table-local-parameters))
  9564. (cdr (assoc const org-table-formula-constants-local))
  9565. (cdr (assoc const org-table-formula-constants))
  9566. (and (fboundp 'constants-get) (constants-get const))
  9567. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9568. (org-entry-get nil (substring const 5) 'inherit))
  9569. "#UNDEFINED_NAME"))
  9570. (defvar org-table-fedit-map
  9571. (let ((map (make-sparse-keymap)))
  9572. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9573. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9574. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9575. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9576. (org-defkey map "\C-c?" 'org-table-show-reference)
  9577. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9578. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9579. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9580. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9581. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9582. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9583. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9584. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9585. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9586. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9587. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9588. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9589. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9590. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9591. map))
  9592. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9593. '("Edit-Formulas"
  9594. ["Finish and Install" org-table-fedit-finish t]
  9595. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9596. ["Abort" org-table-fedit-abort t]
  9597. "--"
  9598. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9599. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9600. "--"
  9601. "Shift Reference at Point"
  9602. ["Up" org-table-fedit-ref-up t]
  9603. ["Down" org-table-fedit-ref-down t]
  9604. ["Left" org-table-fedit-ref-left t]
  9605. ["Right" org-table-fedit-ref-right t]
  9606. "-"
  9607. "Change Test Row for Column Formulas"
  9608. ["Up" org-table-fedit-line-up t]
  9609. ["Down" org-table-fedit-line-down t]
  9610. "--"
  9611. ["Scroll Table Window" org-table-fedit-scroll t]
  9612. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9613. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9614. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9615. org-table-overlay-coordinates)]
  9616. "--"
  9617. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9618. :style toggle :selected org-table-buffer-is-an]))
  9619. (defvar org-pos)
  9620. (defun org-table-edit-formulas ()
  9621. "Edit the formulas of the current table in a separate buffer."
  9622. (interactive)
  9623. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9624. (beginning-of-line 0))
  9625. (unless (org-at-table-p) (error "Not at a table"))
  9626. (org-table-get-specials)
  9627. (let ((key (org-table-current-field-formula 'key 'noerror))
  9628. (eql (sort (org-table-get-stored-formulas 'noerror)
  9629. 'org-table-formula-less-p))
  9630. (pos (move-marker (make-marker) (point)))
  9631. (startline 1)
  9632. (wc (current-window-configuration))
  9633. (titles '((column . "# Column Formulas\n")
  9634. (field . "# Field Formulas\n")
  9635. (named . "# Named Field Formulas\n")))
  9636. entry s type title)
  9637. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9638. (erase-buffer)
  9639. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9640. (let ((font-lock-global-modes '(not fundamental-mode)))
  9641. (fundamental-mode))
  9642. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9643. (org-set-local 'org-pos pos)
  9644. (org-set-local 'org-window-configuration wc)
  9645. (use-local-map org-table-fedit-map)
  9646. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9647. (easy-menu-add org-table-fedit-menu)
  9648. (setq startline (org-current-line))
  9649. (while (setq entry (pop eql))
  9650. (setq type (cond
  9651. ((equal (string-to-char (car entry)) ?@) 'field)
  9652. ((string-match "^[0-9]" (car entry)) 'column)
  9653. (t 'named)))
  9654. (when (setq title (assq type titles))
  9655. (or (bobp) (insert "\n"))
  9656. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9657. (setq titles (delq title titles)))
  9658. (if (equal key (car entry)) (setq startline (org-current-line)))
  9659. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9660. (car entry) " = " (cdr entry) "\n"))
  9661. (remove-text-properties 0 (length s) '(face nil) s)
  9662. (insert s))
  9663. (if (eq org-table-use-standard-references t)
  9664. (org-table-fedit-toggle-ref-type))
  9665. (goto-line startline)
  9666. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9667. (defun org-table-fedit-post-command ()
  9668. (when (not (memq this-command '(lisp-complete-symbol)))
  9669. (let ((win (selected-window)))
  9670. (save-excursion
  9671. (condition-case nil
  9672. (org-table-show-reference)
  9673. (error nil))
  9674. (select-window win)))))
  9675. (defun org-table-formula-to-user (s)
  9676. "Convert a formula from internal to user representation."
  9677. (if (eq org-table-use-standard-references t)
  9678. (org-table-convert-refs-to-an s)
  9679. s))
  9680. (defun org-table-formula-from-user (s)
  9681. "Convert a formula from user to internal representation."
  9682. (if org-table-use-standard-references
  9683. (org-table-convert-refs-to-rc s)
  9684. s))
  9685. (defun org-table-convert-refs-to-rc (s)
  9686. "Convert spreadsheet references from AB7 to @7$28.
  9687. Works for single references, but also for entire formulas and even the
  9688. full TBLFM line."
  9689. (let ((start 0))
  9690. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9691. (cond
  9692. ((match-end 3)
  9693. ;; format match, just advance
  9694. (setq start (match-end 0)))
  9695. ((and (> (match-beginning 0) 0)
  9696. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9697. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9698. ;; 3.e5 or something like this.
  9699. (setq start (match-end 0)))
  9700. (t
  9701. (setq start (match-beginning 0)
  9702. s (replace-match
  9703. (if (equal (match-string 2 s) "&")
  9704. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9705. (format "@%d$%d"
  9706. (string-to-number (match-string 2 s))
  9707. (org-letters-to-number (match-string 1 s))))
  9708. t t s)))))
  9709. s))
  9710. (defun org-table-convert-refs-to-an (s)
  9711. "Convert spreadsheet references from to @7$28 to AB7.
  9712. Works for single references, but also for entire formulas and even the
  9713. full TBLFM line."
  9714. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9715. (setq s (replace-match
  9716. (format "%s%d"
  9717. (org-number-to-letters
  9718. (string-to-number (match-string 2 s)))
  9719. (string-to-number (match-string 1 s)))
  9720. t t s)))
  9721. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9722. (setq s (replace-match (concat "\\1"
  9723. (org-number-to-letters
  9724. (string-to-number (match-string 2 s))) "&")
  9725. t nil s)))
  9726. s)
  9727. (defun org-letters-to-number (s)
  9728. "Convert a base 26 number represented by letters into an integer.
  9729. For example: AB -> 28."
  9730. (let ((n 0))
  9731. (setq s (upcase s))
  9732. (while (> (length s) 0)
  9733. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9734. s (substring s 1)))
  9735. n))
  9736. (defun org-number-to-letters (n)
  9737. "Convert an integer into a base 26 number represented by letters.
  9738. For example: 28 -> AB."
  9739. (let ((s ""))
  9740. (while (> n 0)
  9741. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9742. n (/ (1- n) 26)))
  9743. s))
  9744. (defun org-table-fedit-convert-buffer (function)
  9745. "Convert all references in this buffer, using FUNTION."
  9746. (let ((line (org-current-line)))
  9747. (goto-char (point-min))
  9748. (while (not (eobp))
  9749. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9750. (delete-region (point) (point-at-eol))
  9751. (or (eobp) (forward-char 1)))
  9752. (goto-line line)))
  9753. (defun org-table-fedit-toggle-ref-type ()
  9754. "Convert all references in the buffer from B3 to @3$2 and back."
  9755. (interactive)
  9756. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9757. (org-table-fedit-convert-buffer
  9758. (if org-table-buffer-is-an
  9759. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9760. (message "Reference type switched to %s"
  9761. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9762. (defun org-table-fedit-ref-up ()
  9763. "Shift the reference at point one row/hline up."
  9764. (interactive)
  9765. (org-table-fedit-shift-reference 'up))
  9766. (defun org-table-fedit-ref-down ()
  9767. "Shift the reference at point one row/hline down."
  9768. (interactive)
  9769. (org-table-fedit-shift-reference 'down))
  9770. (defun org-table-fedit-ref-left ()
  9771. "Shift the reference at point one field to the left."
  9772. (interactive)
  9773. (org-table-fedit-shift-reference 'left))
  9774. (defun org-table-fedit-ref-right ()
  9775. "Shift the reference at point one field to the right."
  9776. (interactive)
  9777. (org-table-fedit-shift-reference 'right))
  9778. (defun org-table-fedit-shift-reference (dir)
  9779. (cond
  9780. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9781. (if (memq dir '(left right))
  9782. (org-rematch-and-replace 1 (eq dir 'left))
  9783. (error "Cannot shift reference in this direction")))
  9784. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9785. ;; A B3-like reference
  9786. (if (memq dir '(up down))
  9787. (org-rematch-and-replace 2 (eq dir 'up))
  9788. (org-rematch-and-replace 1 (eq dir 'left))))
  9789. ((org-at-regexp-p
  9790. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9791. ;; An internal reference
  9792. (if (memq dir '(up down))
  9793. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9794. (org-rematch-and-replace 5 (eq dir 'left))))))
  9795. (defun org-rematch-and-replace (n &optional decr hline)
  9796. "Re-match the group N, and replace it with the shifted refrence."
  9797. (or (match-end n) (error "Cannot shift reference in this direction"))
  9798. (goto-char (match-beginning n))
  9799. (and (looking-at (regexp-quote (match-string n)))
  9800. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9801. t t)))
  9802. (defun org-shift-refpart (ref &optional decr hline)
  9803. "Shift a refrence part REF.
  9804. If DECR is set, decrease the references row/column, else increase.
  9805. If HLINE is set, this may be a hline reference, it certainly is not
  9806. a translation reference."
  9807. (save-match-data
  9808. (let* ((sign (string-match "^[-+]" ref)) n)
  9809. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9810. (cond
  9811. ((and hline (string-match "^I+" ref))
  9812. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9813. (setq n (+ n (if decr -1 1)))
  9814. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9815. (if sign
  9816. (setq sign (if (< n 0) "-" "+") n (abs n))
  9817. (setq n (max 1 n)))
  9818. (concat sign (make-string n ?I)))
  9819. ((string-match "^[0-9]+" ref)
  9820. (setq n (string-to-number (concat sign ref)))
  9821. (setq n (+ n (if decr -1 1)))
  9822. (if sign
  9823. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9824. (number-to-string (max 1 n))))
  9825. ((string-match "^[a-zA-Z]+" ref)
  9826. (org-number-to-letters
  9827. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9828. (t (error "Cannot shift reference"))))))
  9829. (defun org-table-fedit-toggle-coordinates ()
  9830. "Toggle the display of coordinates in the refrenced table."
  9831. (interactive)
  9832. (let ((pos (marker-position org-pos)))
  9833. (with-current-buffer (marker-buffer org-pos)
  9834. (save-excursion
  9835. (goto-char pos)
  9836. (org-table-toggle-coordinate-overlays)))))
  9837. (defun org-table-fedit-finish (&optional arg)
  9838. "Parse the buffer for formula definitions and install them.
  9839. With prefix ARG, apply the new formulas to the table."
  9840. (interactive "P")
  9841. (org-table-remove-rectangle-highlight)
  9842. (if org-table-use-standard-references
  9843. (progn
  9844. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9845. (setq org-table-buffer-is-an nil)))
  9846. (let ((pos org-pos) eql var form)
  9847. (goto-char (point-min))
  9848. (while (re-search-forward
  9849. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9850. nil t)
  9851. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9852. form (match-string 3))
  9853. (setq form (org-trim form))
  9854. (when (not (equal form ""))
  9855. (while (string-match "[ \t]*\n[ \t]*" form)
  9856. (setq form (replace-match " " t t form)))
  9857. (when (assoc var eql)
  9858. (error "Double formulas for %s" var))
  9859. (push (cons var form) eql)))
  9860. (setq org-pos nil)
  9861. (set-window-configuration org-window-configuration)
  9862. (select-window (get-buffer-window (marker-buffer pos)))
  9863. (goto-char pos)
  9864. (unless (org-at-table-p)
  9865. (error "Lost table position - cannot install formulae"))
  9866. (org-table-store-formulas eql)
  9867. (move-marker pos nil)
  9868. (kill-buffer "*Edit Formulas*")
  9869. (if arg
  9870. (org-table-recalculate 'all)
  9871. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9872. (defun org-table-fedit-abort ()
  9873. "Abort editing formulas, without installing the changes."
  9874. (interactive)
  9875. (org-table-remove-rectangle-highlight)
  9876. (let ((pos org-pos))
  9877. (set-window-configuration org-window-configuration)
  9878. (select-window (get-buffer-window (marker-buffer pos)))
  9879. (goto-char pos)
  9880. (move-marker pos nil)
  9881. (message "Formula editing aborted without installing changes")))
  9882. (defun org-table-fedit-lisp-indent ()
  9883. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9884. (interactive)
  9885. (let ((pos (point)) beg end ind)
  9886. (beginning-of-line 1)
  9887. (cond
  9888. ((looking-at "[ \t]")
  9889. (goto-char pos)
  9890. (call-interactively 'lisp-indent-line))
  9891. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9892. ((not (fboundp 'pp-buffer))
  9893. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9894. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9895. (goto-char (- (match-end 0) 2))
  9896. (setq beg (point))
  9897. (setq ind (make-string (current-column) ?\ ))
  9898. (condition-case nil (forward-sexp 1)
  9899. (error
  9900. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9901. (setq end (point))
  9902. (save-restriction
  9903. (narrow-to-region beg end)
  9904. (if (eq last-command this-command)
  9905. (progn
  9906. (goto-char (point-min))
  9907. (setq this-command nil)
  9908. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9909. (replace-match " ")))
  9910. (pp-buffer)
  9911. (untabify (point-min) (point-max))
  9912. (goto-char (1+ (point-min)))
  9913. (while (re-search-forward "^." nil t)
  9914. (beginning-of-line 1)
  9915. (insert ind))
  9916. (goto-char (point-max))
  9917. (backward-delete-char 1)))
  9918. (goto-char beg))
  9919. (t nil))))
  9920. (defvar org-show-positions nil)
  9921. (defun org-table-show-reference (&optional local)
  9922. "Show the location/value of the $ expression at point."
  9923. (interactive)
  9924. (org-table-remove-rectangle-highlight)
  9925. (catch 'exit
  9926. (let ((pos (if local (point) org-pos))
  9927. (face2 'highlight)
  9928. (org-inhibit-highlight-removal t)
  9929. (win (selected-window))
  9930. (org-show-positions nil)
  9931. var name e what match dest)
  9932. (if local (org-table-get-specials))
  9933. (setq what (cond
  9934. ((or (org-at-regexp-p org-table-range-regexp2)
  9935. (org-at-regexp-p org-table-translate-regexp)
  9936. (org-at-regexp-p org-table-range-regexp))
  9937. (setq match
  9938. (save-match-data
  9939. (org-table-convert-refs-to-rc (match-string 0))))
  9940. 'range)
  9941. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9942. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9943. ((not local) nil)
  9944. (t (error "No reference at point")))
  9945. match (and what (or match (match-string 0))))
  9946. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9947. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9948. 'secondary-selection))
  9949. (org-add-hook 'before-change-functions
  9950. 'org-table-remove-rectangle-highlight)
  9951. (if (eq what 'name) (setq var (substring match 1)))
  9952. (when (eq what 'range)
  9953. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9954. (setq match (org-table-formula-substitute-names match)))
  9955. (unless local
  9956. (save-excursion
  9957. (end-of-line 1)
  9958. (re-search-backward "^\\S-" nil t)
  9959. (beginning-of-line 1)
  9960. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9961. (setq dest
  9962. (save-match-data
  9963. (org-table-convert-refs-to-rc (match-string 1))))
  9964. (org-table-add-rectangle-overlay
  9965. (match-beginning 1) (match-end 1) face2))))
  9966. (if (and (markerp pos) (marker-buffer pos))
  9967. (if (get-buffer-window (marker-buffer pos))
  9968. (select-window (get-buffer-window (marker-buffer pos)))
  9969. (org-switch-to-buffer-other-window (get-buffer-window
  9970. (marker-buffer pos)))))
  9971. (goto-char pos)
  9972. (org-table-force-dataline)
  9973. (when dest
  9974. (setq name (substring dest 1))
  9975. (cond
  9976. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9977. (setq e (assoc name org-table-named-field-locations))
  9978. (goto-line (nth 1 e))
  9979. (org-table-goto-column (nth 2 e)))
  9980. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9981. (let ((l (string-to-number (match-string 1 dest)))
  9982. (c (string-to-number (match-string 2 dest))))
  9983. (goto-line (aref org-table-dlines l))
  9984. (org-table-goto-column c)))
  9985. (t (org-table-goto-column (string-to-number name))))
  9986. (move-marker pos (point))
  9987. (org-table-highlight-rectangle nil nil face2))
  9988. (cond
  9989. ((equal dest match))
  9990. ((not match))
  9991. ((eq what 'range)
  9992. (condition-case nil
  9993. (save-excursion
  9994. (org-table-get-range match nil nil 'highlight))
  9995. (error nil)))
  9996. ((setq e (assoc var org-table-named-field-locations))
  9997. (goto-line (nth 1 e))
  9998. (org-table-goto-column (nth 2 e))
  9999. (org-table-highlight-rectangle (point) (point))
  10000. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10001. ((setq e (assoc var org-table-column-names))
  10002. (org-table-goto-column (string-to-number (cdr e)))
  10003. (org-table-highlight-rectangle (point) (point))
  10004. (goto-char (org-table-begin))
  10005. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10006. (org-table-end) t)
  10007. (progn
  10008. (goto-char (match-beginning 1))
  10009. (org-table-highlight-rectangle)
  10010. (message "Named column (column %s)" (cdr e)))
  10011. (error "Column name not found")))
  10012. ((eq what 'column)
  10013. ;; column number
  10014. (org-table-goto-column (string-to-number (substring match 1)))
  10015. (org-table-highlight-rectangle (point) (point))
  10016. (message "Column %s" (substring match 1)))
  10017. ((setq e (assoc var org-table-local-parameters))
  10018. (goto-char (org-table-begin))
  10019. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10020. (progn
  10021. (goto-char (match-beginning 1))
  10022. (org-table-highlight-rectangle)
  10023. (message "Local parameter."))
  10024. (error "Parameter not found")))
  10025. (t
  10026. (cond
  10027. ((not var) (error "No reference at point"))
  10028. ((setq e (assoc var org-table-formula-constants-local))
  10029. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10030. var (cdr e)))
  10031. ((setq e (assoc var org-table-formula-constants))
  10032. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10033. var (cdr e)))
  10034. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10035. (message "Constant: $%s=%s, from `constants.el'%s."
  10036. var e (format " (%s units)" constants-unit-system)))
  10037. (t (error "Undefined name $%s" var)))))
  10038. (goto-char pos)
  10039. (when (and org-show-positions
  10040. (not (memq this-command '(org-table-fedit-scroll
  10041. org-table-fedit-scroll-down))))
  10042. (push pos org-show-positions)
  10043. (push org-table-current-begin-pos org-show-positions)
  10044. (let ((min (apply 'min org-show-positions))
  10045. (max (apply 'max org-show-positions)))
  10046. (goto-char min) (recenter 0)
  10047. (goto-char max)
  10048. (or (pos-visible-in-window-p max) (recenter -1))))
  10049. (select-window win))))
  10050. (defun org-table-force-dataline ()
  10051. "Make sure the cursor is in a dataline in a table."
  10052. (unless (save-excursion
  10053. (beginning-of-line 1)
  10054. (looking-at org-table-dataline-regexp))
  10055. (let* ((re org-table-dataline-regexp)
  10056. (p1 (save-excursion (re-search-forward re nil 'move)))
  10057. (p2 (save-excursion (re-search-backward re nil 'move))))
  10058. (cond ((and p1 p2)
  10059. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10060. p1 p2)))
  10061. ((or p1 p2) (goto-char (or p1 p2)))
  10062. (t (error "No table dataline around here"))))))
  10063. (defun org-table-fedit-line-up ()
  10064. "Move cursor one line up in the window showing the table."
  10065. (interactive)
  10066. (org-table-fedit-move 'previous-line))
  10067. (defun org-table-fedit-line-down ()
  10068. "Move cursor one line down in the window showing the table."
  10069. (interactive)
  10070. (org-table-fedit-move 'next-line))
  10071. (defun org-table-fedit-move (command)
  10072. "Move the cursor in the window shoinw the table.
  10073. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10074. (let ((org-table-allow-automatic-line-recalculation nil)
  10075. (pos org-pos) (win (selected-window)) p)
  10076. (select-window (get-buffer-window (marker-buffer org-pos)))
  10077. (setq p (point))
  10078. (call-interactively command)
  10079. (while (and (org-at-table-p)
  10080. (org-at-table-hline-p))
  10081. (call-interactively command))
  10082. (or (org-at-table-p) (goto-char p))
  10083. (move-marker pos (point))
  10084. (select-window win)))
  10085. (defun org-table-fedit-scroll (N)
  10086. (interactive "p")
  10087. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10088. (scroll-other-window N)))
  10089. (defun org-table-fedit-scroll-down (N)
  10090. (interactive "p")
  10091. (org-table-fedit-scroll (- N)))
  10092. (defvar org-table-rectangle-overlays nil)
  10093. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10094. "Add a new overlay."
  10095. (let ((ov (org-make-overlay beg end)))
  10096. (org-overlay-put ov 'face (or face 'secondary-selection))
  10097. (push ov org-table-rectangle-overlays)))
  10098. (defun org-table-highlight-rectangle (&optional beg end face)
  10099. "Highlight rectangular region in a table."
  10100. (setq beg (or beg (point)) end (or end (point)))
  10101. (let ((b (min beg end))
  10102. (e (max beg end))
  10103. l1 c1 l2 c2 tmp)
  10104. (and (boundp 'org-show-positions)
  10105. (setq org-show-positions (cons b (cons e org-show-positions))))
  10106. (goto-char (min beg end))
  10107. (setq l1 (org-current-line)
  10108. c1 (org-table-current-column))
  10109. (goto-char (max beg end))
  10110. (setq l2 (org-current-line)
  10111. c2 (org-table-current-column))
  10112. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10113. (goto-line l1)
  10114. (beginning-of-line 1)
  10115. (loop for line from l1 to l2 do
  10116. (when (looking-at org-table-dataline-regexp)
  10117. (org-table-goto-column c1)
  10118. (skip-chars-backward "^|\n") (setq beg (point))
  10119. (org-table-goto-column c2)
  10120. (skip-chars-forward "^|\n") (setq end (point))
  10121. (org-table-add-rectangle-overlay beg end face))
  10122. (beginning-of-line 2))
  10123. (goto-char b))
  10124. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10125. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10126. "Remove the rectangle overlays."
  10127. (unless org-inhibit-highlight-removal
  10128. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10129. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10130. (setq org-table-rectangle-overlays nil)))
  10131. (defvar org-table-coordinate-overlays nil
  10132. "Collects the cooordinate grid overlays, so that they can be removed.")
  10133. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10134. (defun org-table-overlay-coordinates ()
  10135. "Add overlays to the table at point, to show row/column coordinates."
  10136. (interactive)
  10137. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10138. (setq org-table-coordinate-overlays nil)
  10139. (save-excursion
  10140. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10141. (goto-char (org-table-begin))
  10142. (while (org-at-table-p)
  10143. (setq eol (point-at-eol))
  10144. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10145. (push ov org-table-coordinate-overlays)
  10146. (setq hline (looking-at org-table-hline-regexp))
  10147. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10148. (format "%4d" (setq id (1+ id)))))
  10149. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10150. (when hline
  10151. (setq ic 0)
  10152. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10153. (setq beg (1+ (match-beginning 0))
  10154. ic (1+ ic)
  10155. s1 (concat "$" (int-to-string ic))
  10156. s2 (org-number-to-letters ic)
  10157. str (if (eq org-table-use-standard-references t) s2 s1))
  10158. (setq ov (org-make-overlay beg (+ beg (length str))))
  10159. (push ov org-table-coordinate-overlays)
  10160. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10161. (beginning-of-line 2)))))
  10162. (defun org-table-toggle-coordinate-overlays ()
  10163. "Toggle the display of Row/Column numbers in tables."
  10164. (interactive)
  10165. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10166. (message "Row/Column number display turned %s"
  10167. (if org-table-overlay-coordinates "on" "off"))
  10168. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10169. (org-table-align))
  10170. (unless org-table-overlay-coordinates
  10171. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10172. (setq org-table-coordinate-overlays nil)))
  10173. (defun org-table-toggle-formula-debugger ()
  10174. "Toggle the formula debugger in tables."
  10175. (interactive)
  10176. (setq org-table-formula-debug (not org-table-formula-debug))
  10177. (message "Formula debugging has been turned %s"
  10178. (if org-table-formula-debug "on" "off")))
  10179. ;;; The orgtbl minor mode
  10180. ;; Define a minor mode which can be used in other modes in order to
  10181. ;; integrate the org-mode table editor.
  10182. ;; This is really a hack, because the org-mode table editor uses several
  10183. ;; keys which normally belong to the major mode, for example the TAB and
  10184. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10185. ;; necessary to operate the table editor, but wraps the commands into a
  10186. ;; function which tests if the cursor is currently inside a table. If that
  10187. ;; is the case, the table editor command is executed. However, when any of
  10188. ;; those keys is used outside a table, the function uses `key-binding' to
  10189. ;; look up if the key has an associated command in another currently active
  10190. ;; keymap (minor modes, major mode, global), and executes that command.
  10191. ;; There might be problems if any of the keys used by the table editor is
  10192. ;; otherwise used as a prefix key.
  10193. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10194. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10195. ;; addresses this by checking explicitly for both bindings.
  10196. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10197. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10198. ;; Some modes bind other commands to simple characters, for example
  10199. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10200. ;; active, this binding is ignored inside tables and replaced with a
  10201. ;; modified self-insert.
  10202. (defvar orgtbl-mode nil
  10203. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10204. table editor in arbitrary modes.")
  10205. (make-variable-buffer-local 'orgtbl-mode)
  10206. (defvar orgtbl-mode-map (make-keymap)
  10207. "Keymap for `orgtbl-mode'.")
  10208. ;;;###autoload
  10209. (defun turn-on-orgtbl ()
  10210. "Unconditionally turn on `orgtbl-mode'."
  10211. (orgtbl-mode 1))
  10212. (defvar org-old-auto-fill-inhibit-regexp nil
  10213. "Local variable used by `orgtbl-mode'")
  10214. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10215. "Matches a line belonging to an orgtbl.")
  10216. (defconst orgtbl-extra-font-lock-keywords
  10217. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10218. 0 (quote 'org-table) 'prepend))
  10219. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10220. ;;;###autoload
  10221. (defun orgtbl-mode (&optional arg)
  10222. "The `org-mode' table editor as a minor mode for use in other modes."
  10223. (interactive)
  10224. (if (org-mode-p)
  10225. ;; Exit without error, in case some hook functions calls this
  10226. ;; by accident in org-mode.
  10227. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10228. (setq orgtbl-mode
  10229. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10230. (if orgtbl-mode
  10231. (progn
  10232. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10233. ;; Make sure we are first in minor-mode-map-alist
  10234. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10235. (and c (setq minor-mode-map-alist
  10236. (cons c (delq c minor-mode-map-alist)))))
  10237. (org-set-local (quote org-table-may-need-update) t)
  10238. (org-add-hook 'before-change-functions 'org-before-change-function
  10239. nil 'local)
  10240. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10241. auto-fill-inhibit-regexp)
  10242. (org-set-local 'auto-fill-inhibit-regexp
  10243. (if auto-fill-inhibit-regexp
  10244. (concat orgtbl-line-start-regexp "\\|"
  10245. auto-fill-inhibit-regexp)
  10246. orgtbl-line-start-regexp))
  10247. (org-add-to-invisibility-spec '(org-cwidth))
  10248. (when (fboundp 'font-lock-add-keywords)
  10249. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10250. (org-restart-font-lock))
  10251. (easy-menu-add orgtbl-mode-menu)
  10252. (run-hooks 'orgtbl-mode-hook))
  10253. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10254. (org-cleanup-narrow-column-properties)
  10255. (org-remove-from-invisibility-spec '(org-cwidth))
  10256. (remove-hook 'before-change-functions 'org-before-change-function t)
  10257. (when (fboundp 'font-lock-remove-keywords)
  10258. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10259. (org-restart-font-lock))
  10260. (easy-menu-remove orgtbl-mode-menu)
  10261. (force-mode-line-update 'all))))
  10262. (defun org-cleanup-narrow-column-properties ()
  10263. "Remove all properties related to narrow-column invisibility."
  10264. (let ((s 1))
  10265. (while (setq s (text-property-any s (point-max)
  10266. 'display org-narrow-column-arrow))
  10267. (remove-text-properties s (1+ s) '(display t)))
  10268. (setq s 1)
  10269. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10270. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10271. (setq s 1)
  10272. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10273. (remove-text-properties s (1+ s) '(invisible t)))))
  10274. ;; Install it as a minor mode.
  10275. (put 'orgtbl-mode :included t)
  10276. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10277. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10278. (defun orgtbl-make-binding (fun n &rest keys)
  10279. "Create a function for binding in the table minor mode.
  10280. FUN is the command to call inside a table. N is used to create a unique
  10281. command name. KEYS are keys that should be checked in for a command
  10282. to execute outside of tables."
  10283. (eval
  10284. (list 'defun
  10285. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10286. '(arg)
  10287. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10288. "Outside of tables, run the binding of `"
  10289. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10290. "'.")
  10291. '(interactive "p")
  10292. (list 'if
  10293. '(org-at-table-p)
  10294. (list 'call-interactively (list 'quote fun))
  10295. (list 'let '(orgtbl-mode)
  10296. (list 'call-interactively
  10297. (append '(or)
  10298. (mapcar (lambda (k)
  10299. (list 'key-binding k))
  10300. keys)
  10301. '('orgtbl-error))))))))
  10302. (defun orgtbl-error ()
  10303. "Error when there is no default binding for a table key."
  10304. (interactive)
  10305. (error "This key has no function outside tables"))
  10306. (defun orgtbl-setup ()
  10307. "Setup orgtbl keymaps."
  10308. (let ((nfunc 0)
  10309. (bindings
  10310. (list
  10311. '([(meta shift left)] org-table-delete-column)
  10312. '([(meta left)] org-table-move-column-left)
  10313. '([(meta right)] org-table-move-column-right)
  10314. '([(meta shift right)] org-table-insert-column)
  10315. '([(meta shift up)] org-table-kill-row)
  10316. '([(meta shift down)] org-table-insert-row)
  10317. '([(meta up)] org-table-move-row-up)
  10318. '([(meta down)] org-table-move-row-down)
  10319. '("\C-c\C-w" org-table-cut-region)
  10320. '("\C-c\M-w" org-table-copy-region)
  10321. '("\C-c\C-y" org-table-paste-rectangle)
  10322. '("\C-c-" org-table-insert-hline)
  10323. '("\C-c}" org-table-toggle-coordinate-overlays)
  10324. '("\C-c{" org-table-toggle-formula-debugger)
  10325. '("\C-m" org-table-next-row)
  10326. '([(shift return)] org-table-copy-down)
  10327. '("\C-c\C-q" org-table-wrap-region)
  10328. '("\C-c?" org-table-field-info)
  10329. '("\C-c " org-table-blank-field)
  10330. '("\C-c+" org-table-sum)
  10331. '("\C-c=" org-table-eval-formula)
  10332. '("\C-c'" org-table-edit-formulas)
  10333. '("\C-c`" org-table-edit-field)
  10334. '("\C-c*" org-table-recalculate)
  10335. '("\C-c|" org-table-create-or-convert-from-region)
  10336. '("\C-c^" org-table-sort-lines)
  10337. '([(control ?#)] org-table-rotate-recalc-marks)))
  10338. elt key fun cmd)
  10339. (while (setq elt (pop bindings))
  10340. (setq nfunc (1+ nfunc))
  10341. (setq key (org-key (car elt))
  10342. fun (nth 1 elt)
  10343. cmd (orgtbl-make-binding fun nfunc key))
  10344. (org-defkey orgtbl-mode-map key cmd))
  10345. ;; Special treatment needed for TAB and RET
  10346. (org-defkey orgtbl-mode-map [(return)]
  10347. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10348. (org-defkey orgtbl-mode-map "\C-m"
  10349. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10350. (org-defkey orgtbl-mode-map [(tab)]
  10351. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10352. (org-defkey orgtbl-mode-map "\C-i"
  10353. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10354. (org-defkey orgtbl-mode-map [(shift tab)]
  10355. (orgtbl-make-binding 'org-table-previous-field 104
  10356. [(shift tab)] [(tab)] "\C-i"))
  10357. (org-defkey orgtbl-mode-map "\M-\C-m"
  10358. (orgtbl-make-binding 'org-table-wrap-region 105
  10359. "\M-\C-m" [(meta return)]))
  10360. (org-defkey orgtbl-mode-map [(meta return)]
  10361. (orgtbl-make-binding 'org-table-wrap-region 106
  10362. [(meta return)] "\M-\C-m"))
  10363. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10364. (when orgtbl-optimized
  10365. ;; If the user wants maximum table support, we need to hijack
  10366. ;; some standard editing functions
  10367. (org-remap orgtbl-mode-map
  10368. 'self-insert-command 'orgtbl-self-insert-command
  10369. 'delete-char 'org-delete-char
  10370. 'delete-backward-char 'org-delete-backward-char)
  10371. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10372. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10373. '("OrgTbl"
  10374. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10375. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10376. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10377. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10378. "--"
  10379. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10380. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10381. ["Copy Field from Above"
  10382. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10383. "--"
  10384. ("Column"
  10385. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10386. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10387. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10388. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10389. ("Row"
  10390. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10391. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10392. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10393. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10394. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10395. "--"
  10396. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10397. ("Rectangle"
  10398. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10399. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10400. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10401. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10402. "--"
  10403. ("Radio tables"
  10404. ["Insert table template" orgtbl-insert-radio-table
  10405. (assq major-mode orgtbl-radio-table-templates)]
  10406. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10407. "--"
  10408. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10409. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10410. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10411. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10412. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10413. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10414. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10415. ["Sum Column/Rectangle" org-table-sum
  10416. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10417. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10418. ["Debug Formulas"
  10419. org-table-toggle-formula-debugger :active (org-at-table-p)
  10420. :keys "C-c {"
  10421. :style toggle :selected org-table-formula-debug]
  10422. ["Show Col/Row Numbers"
  10423. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10424. :keys "C-c }"
  10425. :style toggle :selected org-table-overlay-coordinates]
  10426. ))
  10427. t))
  10428. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10429. "If the cursor is inside a table, realign the table.
  10430. It it is a table to be sent away to a receiver, do it.
  10431. With prefix arg, also recompute table."
  10432. (interactive "P")
  10433. (let ((pos (point)) action)
  10434. (save-excursion
  10435. (beginning-of-line 1)
  10436. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10437. ((looking-at "[ \t]*|") pos)
  10438. ((looking-at "#\\+TBLFM:") 'recalc))))
  10439. (cond
  10440. ((integerp action)
  10441. (goto-char action)
  10442. (org-table-maybe-eval-formula)
  10443. (if arg
  10444. (call-interactively 'org-table-recalculate)
  10445. (org-table-maybe-recalculate-line))
  10446. (call-interactively 'org-table-align)
  10447. (orgtbl-send-table 'maybe))
  10448. ((eq action 'recalc)
  10449. (save-excursion
  10450. (beginning-of-line 1)
  10451. (skip-chars-backward " \r\n\t")
  10452. (if (org-at-table-p)
  10453. (org-call-with-arg 'org-table-recalculate t))))
  10454. (t (let (orgtbl-mode)
  10455. (call-interactively (key-binding "\C-c\C-c")))))))
  10456. (defun orgtbl-tab (arg)
  10457. "Justification and field motion for `orgtbl-mode'."
  10458. (interactive "P")
  10459. (if arg (org-table-edit-field t)
  10460. (org-table-justify-field-maybe)
  10461. (org-table-next-field)))
  10462. (defun orgtbl-ret ()
  10463. "Justification and field motion for `orgtbl-mode'."
  10464. (interactive)
  10465. (org-table-justify-field-maybe)
  10466. (org-table-next-row))
  10467. (defun orgtbl-self-insert-command (N)
  10468. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10469. If the cursor is in a table looking at whitespace, the whitespace is
  10470. overwritten, and the table is not marked as requiring realignment."
  10471. (interactive "p")
  10472. (if (and (org-at-table-p)
  10473. (or
  10474. (and org-table-auto-blank-field
  10475. (member last-command
  10476. '(orgtbl-hijacker-command-100
  10477. orgtbl-hijacker-command-101
  10478. orgtbl-hijacker-command-102
  10479. orgtbl-hijacker-command-103
  10480. orgtbl-hijacker-command-104
  10481. orgtbl-hijacker-command-105))
  10482. (org-table-blank-field))
  10483. t)
  10484. (eq N 1)
  10485. (looking-at "[^|\n]* +|"))
  10486. (let (org-table-may-need-update)
  10487. (goto-char (1- (match-end 0)))
  10488. (delete-backward-char 1)
  10489. (goto-char (match-beginning 0))
  10490. (self-insert-command N))
  10491. (setq org-table-may-need-update t)
  10492. (let (orgtbl-mode)
  10493. (call-interactively (key-binding (vector last-input-event))))))
  10494. (defun org-force-self-insert (N)
  10495. "Needed to enforce self-insert under remapping."
  10496. (interactive "p")
  10497. (self-insert-command N))
  10498. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10499. "Regula expression matching exponentials as produced by calc.")
  10500. (defvar org-table-clean-did-remove-column nil)
  10501. (defun orgtbl-export (table target)
  10502. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10503. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10504. org-table-last-alignment org-table-last-column-widths
  10505. maxcol column)
  10506. (if (not (fboundp func))
  10507. (error "Cannot export orgtbl table to %s" target))
  10508. (setq lines (org-table-clean-before-export lines))
  10509. (setq table
  10510. (mapcar
  10511. (lambda (x)
  10512. (if (string-match org-table-hline-regexp x)
  10513. 'hline
  10514. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10515. lines))
  10516. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10517. table)))
  10518. (loop for i from (1- maxcol) downto 0 do
  10519. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10520. (setq column (delq nil column))
  10521. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10522. (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))
  10523. (funcall func table nil)))
  10524. (defun orgtbl-send-table (&optional maybe)
  10525. "Send a tranformed version of this table to the receiver position.
  10526. With argument MAYBE, fail quietly if no transformation is defined for
  10527. this table."
  10528. (interactive)
  10529. (catch 'exit
  10530. (unless (org-at-table-p) (error "Not at a table"))
  10531. ;; when non-interactive, we assume align has just happened.
  10532. (when (interactive-p) (org-table-align))
  10533. (save-excursion
  10534. (goto-char (org-table-begin))
  10535. (beginning-of-line 0)
  10536. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10537. (if maybe
  10538. (throw 'exit nil)
  10539. (error "Don't know how to transform this table."))))
  10540. (let* ((name (match-string 1))
  10541. beg
  10542. (transform (intern (match-string 2)))
  10543. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10544. (skip (plist-get params :skip))
  10545. (skipcols (plist-get params :skipcols))
  10546. (txt (buffer-substring-no-properties
  10547. (org-table-begin) (org-table-end)))
  10548. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10549. (lines (org-table-clean-before-export lines))
  10550. (i0 (if org-table-clean-did-remove-column 2 1))
  10551. (table (mapcar
  10552. (lambda (x)
  10553. (if (string-match org-table-hline-regexp x)
  10554. 'hline
  10555. (org-remove-by-index
  10556. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10557. skipcols i0)))
  10558. lines))
  10559. (fun (if (= i0 2) 'cdr 'identity))
  10560. (org-table-last-alignment
  10561. (org-remove-by-index (funcall fun org-table-last-alignment)
  10562. skipcols i0))
  10563. (org-table-last-column-widths
  10564. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10565. skipcols i0)))
  10566. (unless (fboundp transform)
  10567. (error "No such transformation function %s" transform))
  10568. (setq txt (funcall transform table params))
  10569. ;; Find the insertion place
  10570. (save-excursion
  10571. (goto-char (point-min))
  10572. (unless (re-search-forward
  10573. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10574. (error "Don't know where to insert translated table"))
  10575. (goto-char (match-beginning 0))
  10576. (beginning-of-line 2)
  10577. (setq beg (point))
  10578. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10579. (error "Cannot find end of insertion region"))
  10580. (beginning-of-line 1)
  10581. (delete-region beg (point))
  10582. (goto-char beg)
  10583. (insert txt "\n"))
  10584. (message "Table converted and installed at receiver location"))))
  10585. (defun org-remove-by-index (list indices &optional i0)
  10586. "Remove the elements in LIST with indices in INDICES.
  10587. First element has index 0, or I0 if given."
  10588. (if (not indices)
  10589. list
  10590. (if (integerp indices) (setq indices (list indices)))
  10591. (setq i0 (1- (or i0 0)))
  10592. (delq :rm (mapcar (lambda (x)
  10593. (setq i0 (1+ i0))
  10594. (if (memq i0 indices) :rm x))
  10595. list))))
  10596. (defun orgtbl-toggle-comment ()
  10597. "Comment or uncomment the orgtbl at point."
  10598. (interactive)
  10599. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10600. (re2 (concat "^" orgtbl-line-start-regexp))
  10601. (commented (save-excursion (beginning-of-line 1)
  10602. (cond ((looking-at re1) t)
  10603. ((looking-at re2) nil)
  10604. (t (error "Not at an org table")))))
  10605. (re (if commented re1 re2))
  10606. beg end)
  10607. (save-excursion
  10608. (beginning-of-line 1)
  10609. (while (looking-at re) (beginning-of-line 0))
  10610. (beginning-of-line 2)
  10611. (setq beg (point))
  10612. (while (looking-at re) (beginning-of-line 2))
  10613. (setq end (point)))
  10614. (comment-region beg end (if commented '(4) nil))))
  10615. (defun orgtbl-insert-radio-table ()
  10616. "Insert a radio table template appropriate for this major mode."
  10617. (interactive)
  10618. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10619. (txt (nth 1 e))
  10620. name pos)
  10621. (unless e (error "No radio table setup defined for %s" major-mode))
  10622. (setq name (read-string "Table name: "))
  10623. (while (string-match "%n" txt)
  10624. (setq txt (replace-match name t t txt)))
  10625. (or (bolp) (insert "\n"))
  10626. (setq pos (point))
  10627. (insert txt)
  10628. (goto-char pos)))
  10629. (defun org-get-param (params header i sym &optional hsym)
  10630. "Get parameter value for symbol SYM.
  10631. If this is a header line, actually get the value for the symbol with an
  10632. additional \"h\" inserted after the colon.
  10633. If the value is a protperty list, get the element for the current column.
  10634. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10635. (let ((val (plist-get params sym)))
  10636. (and hsym header (setq val (or (plist-get params hsym) val)))
  10637. (if (consp val) (plist-get val i) val)))
  10638. (defun orgtbl-to-generic (table params)
  10639. "Convert the orgtbl-mode TABLE to some other format.
  10640. This generic routine can be used for many standard cases.
  10641. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10642. separator line, or a list of fields for that line.
  10643. PARAMS is a property list of parameters that can influence the conversion.
  10644. For the generic converter, some parameters are obligatory: You need to
  10645. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10646. :splice, you must have :tstart and :tend.
  10647. Valid parameters are
  10648. :tstart String to start the table. Ignored when :splice is t.
  10649. :tend String to end the table. Ignored when :splice is t.
  10650. :splice When set to t, return only table body lines, don't wrap
  10651. them into :tstart and :tend. Default is nil.
  10652. :hline String to be inserted on horizontal separation lines.
  10653. May be nil to ignore hlines.
  10654. :lstart String to start a new table line.
  10655. :lend String to end a table line
  10656. :sep Separator between two fields
  10657. :lfmt Format for entire line, with enough %s to capture all fields.
  10658. If this is present, :lstart, :lend, and :sep are ignored.
  10659. :fmt A format to be used to wrap the field, should contain
  10660. %s for the original field value. For example, to wrap
  10661. everything in dollars, you could use :fmt \"$%s$\".
  10662. This may also be a property list with column numbers and
  10663. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10664. :hlstart :hlend :hlsep :hlfmt :hfmt
  10665. Same as above, specific for the header lines in the table.
  10666. All lines before the first hline are treated as header.
  10667. If any of these is not present, the data line value is used.
  10668. :efmt Use this format to print numbers with exponentials.
  10669. The format should have %s twice for inserting mantissa
  10670. and exponent, for example \"%s\\\\times10^{%s}\". This
  10671. may also be a property list with column numbers and
  10672. formats. :fmt will still be applied after :efmt.
  10673. In addition to this, the parameters :skip and :skipcols are always handled
  10674. directly by `orgtbl-send-table'. See manual."
  10675. (interactive)
  10676. (let* ((p params)
  10677. (splicep (plist-get p :splice))
  10678. (hline (plist-get p :hline))
  10679. rtn line i fm efm lfmt h)
  10680. ;; Do we have a header?
  10681. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10682. (setq h t))
  10683. ;; Put header
  10684. (unless splicep
  10685. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10686. ;; Now loop over all lines
  10687. (while (setq line (pop table))
  10688. (if (eq line 'hline)
  10689. ;; A horizontal separator line
  10690. (progn (if hline (push hline rtn))
  10691. (setq h nil)) ; no longer in header
  10692. ;; A normal line. Convert the fields, push line onto the result list
  10693. (setq i 0)
  10694. (setq line
  10695. (mapcar
  10696. (lambda (f)
  10697. (setq i (1+ i)
  10698. fm (org-get-param p h i :fmt :hfmt)
  10699. efm (org-get-param p h i :efmt))
  10700. (if (and efm (string-match orgtbl-exp-regexp f))
  10701. (setq f (format
  10702. efm (match-string 1 f) (match-string 2 f))))
  10703. (if fm (setq f (format fm f)))
  10704. f)
  10705. line))
  10706. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10707. (push (apply 'format lfmt line) rtn)
  10708. (push (concat
  10709. (org-get-param p h i :lstart :hlstart)
  10710. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10711. (org-get-param p h i :lend :hlend))
  10712. rtn))))
  10713. (unless splicep
  10714. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10715. (mapconcat 'identity (nreverse rtn) "\n")))
  10716. (defun orgtbl-to-latex (table params)
  10717. "Convert the orgtbl-mode TABLE to LaTeX.
  10718. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10719. separator line, or a list of fields for that line.
  10720. PARAMS is a property list of parameters that can influence the conversion.
  10721. Supports all parameters from `orgtbl-to-generic'. Most important for
  10722. LaTeX are:
  10723. :splice When set to t, return only table body lines, don't wrap
  10724. them into a tabular environment. Default is nil.
  10725. :fmt A format to be used to wrap the field, should contain %s for the
  10726. original field value. For example, to wrap everything in dollars,
  10727. use :fmt \"$%s$\". This may also be a property list with column
  10728. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10729. :efmt Format for transforming numbers with exponentials. The format
  10730. should have %s twice for inserting mantissa and exponent, for
  10731. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10732. This may also be a property list with column numbers and formats.
  10733. The general parameters :skip and :skipcols have already been applied when
  10734. this function is called."
  10735. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10736. org-table-last-alignment ""))
  10737. (params2
  10738. (list
  10739. :tstart (concat "\\begin{tabular}{" alignment "}")
  10740. :tend "\\end{tabular}"
  10741. :lstart "" :lend " \\\\" :sep " & "
  10742. :efmt "%s\\,(%s)" :hline "\\hline")))
  10743. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10744. (defun orgtbl-to-html (table params)
  10745. "Convert the orgtbl-mode TABLE to LaTeX.
  10746. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10747. separator line, or a list of fields for that line.
  10748. PARAMS is a property list of parameters that can influence the conversion.
  10749. Currently this function recognizes the following parameters:
  10750. :splice When set to t, return only table body lines, don't wrap
  10751. them into a <table> environment. Default is nil.
  10752. The general parameters :skip and :skipcols have already been applied when
  10753. this function is called. The function does *not* use `orgtbl-to-generic',
  10754. so you cannot specify parameters for it."
  10755. (let* ((splicep (plist-get params :splice))
  10756. html)
  10757. ;; Just call the formatter we already have
  10758. ;; We need to make text lines for it, so put the fields back together.
  10759. (setq html (org-format-org-table-html
  10760. (mapcar
  10761. (lambda (x)
  10762. (if (eq x 'hline)
  10763. "|----+----|"
  10764. (concat "| " (mapconcat 'identity x " | ") " |")))
  10765. table)
  10766. splicep))
  10767. (if (string-match "\n+\\'" html)
  10768. (setq html (replace-match "" t t html)))
  10769. html))
  10770. (defun orgtbl-to-texinfo (table params)
  10771. "Convert the orgtbl-mode TABLE to TeXInfo.
  10772. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10773. separator line, or a list of fields for that line.
  10774. PARAMS is a property list of parameters that can influence the conversion.
  10775. Supports all parameters from `orgtbl-to-generic'. Most important for
  10776. TeXInfo are:
  10777. :splice nil/t When set to t, return only table body lines, don't wrap
  10778. them into a multitable environment. Default is nil.
  10779. :fmt fmt A format to be used to wrap the field, should contain
  10780. %s for the original field value. For example, to wrap
  10781. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10782. This may also be a property list with column numbers and
  10783. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10784. :cf \"f1 f2..\" The column fractions for the table. By default these
  10785. are computed automatically from the width of the columns
  10786. under org-mode.
  10787. The general parameters :skip and :skipcols have already been applied when
  10788. this function is called."
  10789. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10790. (colfrac (or (plist-get params :cf)
  10791. (mapconcat
  10792. (lambda (x) (format "%.3f" (/ (float x) total)))
  10793. org-table-last-column-widths " ")))
  10794. (params2
  10795. (list
  10796. :tstart (concat "@multitable @columnfractions " colfrac)
  10797. :tend "@end multitable"
  10798. :lstart "@item " :lend "" :sep " @tab "
  10799. :hlstart "@headitem ")))
  10800. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10801. ;;;; Link Stuff
  10802. ;;; Link abbreviations
  10803. (defun org-link-expand-abbrev (link)
  10804. "Apply replacements as defined in `org-link-abbrev-alist."
  10805. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10806. (let* ((key (match-string 1 link))
  10807. (as (or (assoc key org-link-abbrev-alist-local)
  10808. (assoc key org-link-abbrev-alist)))
  10809. (tag (and (match-end 2) (match-string 3 link)))
  10810. rpl)
  10811. (if (not as)
  10812. link
  10813. (setq rpl (cdr as))
  10814. (cond
  10815. ((symbolp rpl) (funcall rpl tag))
  10816. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10817. (t (concat rpl tag)))))
  10818. link))
  10819. ;;; Storing and inserting links
  10820. (defvar org-insert-link-history nil
  10821. "Minibuffer history for links inserted with `org-insert-link'.")
  10822. (defvar org-stored-links nil
  10823. "Contains the links stored with `org-store-link'.")
  10824. (defvar org-store-link-plist nil
  10825. "Plist with info about the most recently link created with `org-store-link'.")
  10826. (defvar org-link-protocols nil
  10827. "Link protocols added to Org-mode using `org-add-link-type'.")
  10828. (defvar org-store-link-functions nil
  10829. "List of functions that are called to create and store a link.
  10830. Each function will be called in turn until one returns a non-nil
  10831. value. Each function should check if it is responsible for creating
  10832. this link (for example by looking at the major mode).
  10833. If not, it must exit and return nil.
  10834. If yes, it should return a non-nil value after a calling
  10835. `org-store-link-props' with a list of properties and values.
  10836. Special properties are:
  10837. :type The link prefix. like \"http\". This must be given.
  10838. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10839. This is obligatory as well.
  10840. :description Optional default description for the second pair
  10841. of brackets in an Org-mode link. The user can still change
  10842. this when inserting this link into an Org-mode buffer.
  10843. In addition to these, any additional properties can be specified
  10844. and then used in remember templates.")
  10845. (defun org-add-link-type (type &optional follow publish)
  10846. "Add TYPE to the list of `org-link-types'.
  10847. Re-compute all regular expressions depending on `org-link-types'
  10848. FOLLOW and PUBLISH are two functions. Both take the link path as
  10849. an argument.
  10850. FOLLOW should do whatever is necessary to follow the link, for example
  10851. to find a file or display a mail message.
  10852. PUBLISH takes the path and retuns the string that should be used when
  10853. this document is published. FIMXE: This is actually not yet implemented."
  10854. (add-to-list 'org-link-types type t)
  10855. (org-make-link-regexps)
  10856. (add-to-list 'org-link-protocols
  10857. (list type follow publish)))
  10858. (defun org-add-agenda-custom-command (entry)
  10859. "Replace or add a command in `org-agenda-custom-commands'.
  10860. This is mostly for hacking and trying a new command - once the command
  10861. works you probably want to add it to `org-agenda-custom-commands' for good."
  10862. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10863. (if ass
  10864. (setcdr ass (cdr entry))
  10865. (push entry org-agenda-custom-commands))))
  10866. ;;;###autoload
  10867. (defun org-store-link (arg)
  10868. "\\<org-mode-map>Store an org-link to the current location.
  10869. This link is added to `org-stored-links' and can later be inserted
  10870. into an org-buffer with \\[org-insert-link].
  10871. For some link types, a prefix arg is interpreted:
  10872. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10873. For file links, arg negates `org-context-in-file-links'."
  10874. (interactive "P")
  10875. (setq org-store-link-plist nil) ; reset
  10876. (require 'org-irc)
  10877. (let (link cpltxt desc description search txt)
  10878. (cond
  10879. ((run-hook-with-args-until-success 'org-store-link-functions)
  10880. (setq link (plist-get org-store-link-plist :link)
  10881. desc (or (plist-get org-store-link-plist :description) link)))
  10882. ((eq major-mode 'bbdb-mode)
  10883. (let ((name (bbdb-record-name (bbdb-current-record)))
  10884. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10885. (setq cpltxt (concat "bbdb:" (or name company))
  10886. link (org-make-link cpltxt))
  10887. (org-store-link-props :type "bbdb" :name name :company company)))
  10888. ((eq major-mode 'Info-mode)
  10889. (setq link (org-make-link "info:"
  10890. (file-name-nondirectory Info-current-file)
  10891. ":" Info-current-node))
  10892. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10893. ":" Info-current-node))
  10894. (org-store-link-props :type "info" :file Info-current-file
  10895. :node Info-current-node))
  10896. ((eq major-mode 'calendar-mode)
  10897. (let ((cd (calendar-cursor-to-date)))
  10898. (setq link
  10899. (format-time-string
  10900. (car org-time-stamp-formats)
  10901. (apply 'encode-time
  10902. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10903. nil nil nil))))
  10904. (org-store-link-props :type "calendar" :date cd)))
  10905. ((or (eq major-mode 'vm-summary-mode)
  10906. (eq major-mode 'vm-presentation-mode))
  10907. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10908. (vm-follow-summary-cursor)
  10909. (save-excursion
  10910. (vm-select-folder-buffer)
  10911. (let* ((message (car vm-message-pointer))
  10912. (folder buffer-file-name)
  10913. (subject (vm-su-subject message))
  10914. (to (vm-get-header-contents message "To"))
  10915. (from (vm-get-header-contents message "From"))
  10916. (message-id (vm-su-message-id message)))
  10917. (org-store-link-props :type "vm" :from from :to to :subject subject
  10918. :message-id message-id)
  10919. (setq message-id (org-remove-angle-brackets message-id))
  10920. (setq folder (abbreviate-file-name folder))
  10921. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10922. folder)
  10923. (setq folder (replace-match "" t t folder)))
  10924. (setq cpltxt (org-email-link-description))
  10925. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10926. ((eq major-mode 'wl-summary-mode)
  10927. (let* ((msgnum (wl-summary-message-number))
  10928. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10929. msgnum 'message-id))
  10930. (wl-message-entity
  10931. (if (fboundp 'elmo-message-entity)
  10932. (elmo-message-entity
  10933. wl-summary-buffer-elmo-folder msgnum)
  10934. (elmo-msgdb-overview-get-entity
  10935. msgnum (wl-summary-buffer-msgdb))))
  10936. (from (wl-summary-line-from))
  10937. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  10938. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10939. (wl-summary-line-subject))))
  10940. (org-store-link-props :type "wl" :from from :to to
  10941. :subject subject :message-id message-id)
  10942. (setq message-id (org-remove-angle-brackets message-id))
  10943. (setq cpltxt (org-email-link-description))
  10944. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10945. "#" message-id))))
  10946. ((or (equal major-mode 'mh-folder-mode)
  10947. (equal major-mode 'mh-show-mode))
  10948. (let ((from (org-mhe-get-header "From:"))
  10949. (to (org-mhe-get-header "To:"))
  10950. (message-id (org-mhe-get-header "Message-Id:"))
  10951. (subject (org-mhe-get-header "Subject:")))
  10952. (org-store-link-props :type "mh" :from from :to to
  10953. :subject subject :message-id message-id)
  10954. (setq cpltxt (org-email-link-description))
  10955. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10956. (org-remove-angle-brackets message-id)))))
  10957. ((or (eq major-mode 'rmail-mode)
  10958. (eq major-mode 'rmail-summary-mode))
  10959. (save-window-excursion
  10960. (save-restriction
  10961. (when (eq major-mode 'rmail-summary-mode)
  10962. (rmail-show-message rmail-current-message))
  10963. (rmail-narrow-to-non-pruned-header)
  10964. (let ((folder buffer-file-name)
  10965. (message-id (mail-fetch-field "message-id"))
  10966. (from (mail-fetch-field "from"))
  10967. (to (mail-fetch-field "to"))
  10968. (subject (mail-fetch-field "subject")))
  10969. (org-store-link-props
  10970. :type "rmail" :from from :to to
  10971. :subject subject :message-id message-id)
  10972. (setq message-id (org-remove-angle-brackets message-id))
  10973. (setq cpltxt (org-email-link-description))
  10974. (setq link (org-make-link "rmail:" folder "#" message-id)))
  10975. (rmail-show-message rmail-current-message))))
  10976. ((eq major-mode 'gnus-group-mode)
  10977. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10978. (gnus-group-group-name)) ; version
  10979. ((fboundp 'gnus-group-name)
  10980. (gnus-group-name))
  10981. (t "???"))))
  10982. (unless group (error "Not on a group"))
  10983. (org-store-link-props :type "gnus" :group group)
  10984. (setq cpltxt (concat
  10985. (if (org-xor arg org-usenet-links-prefer-google)
  10986. "http://groups.google.com/groups?group="
  10987. "gnus:")
  10988. group)
  10989. link (org-make-link cpltxt))))
  10990. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10991. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10992. (let* ((group gnus-newsgroup-name)
  10993. (article (gnus-summary-article-number))
  10994. (header (gnus-summary-article-header article))
  10995. (from (mail-header-from header))
  10996. (message-id (mail-header-id header))
  10997. (date (mail-header-date header))
  10998. (subject (gnus-summary-subject-string)))
  10999. (org-store-link-props :type "gnus" :from from :subject subject
  11000. :message-id message-id :group group)
  11001. (setq cpltxt (org-email-link-description))
  11002. (if (org-xor arg org-usenet-links-prefer-google)
  11003. (setq link
  11004. (concat
  11005. cpltxt "\n "
  11006. (format "http://groups.google.com/groups?as_umsgid=%s"
  11007. (org-fixup-message-id-for-http message-id))))
  11008. (setq link (org-make-link "gnus:" group
  11009. "#" (number-to-string article))))))
  11010. ((eq major-mode 'w3-mode)
  11011. (setq cpltxt (url-view-url t)
  11012. link (org-make-link cpltxt))
  11013. (org-store-link-props :type "w3" :url (url-view-url t)))
  11014. ((eq major-mode 'w3m-mode)
  11015. (setq cpltxt (or w3m-current-title w3m-current-url)
  11016. link (org-make-link w3m-current-url))
  11017. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11018. ((setq search (run-hook-with-args-until-success
  11019. 'org-create-file-search-functions))
  11020. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11021. "::" search))
  11022. (setq cpltxt (or description link)))
  11023. ((eq major-mode 'image-mode)
  11024. (setq cpltxt (concat "file:"
  11025. (abbreviate-file-name buffer-file-name))
  11026. link (org-make-link cpltxt))
  11027. (org-store-link-props :type "image" :file buffer-file-name))
  11028. ((eq major-mode 'dired-mode)
  11029. ;; link to the file in the current line
  11030. (setq cpltxt (concat "file:"
  11031. (abbreviate-file-name
  11032. (expand-file-name
  11033. (dired-get-filename nil t))))
  11034. link (org-make-link cpltxt)))
  11035. ((and buffer-file-name (org-mode-p))
  11036. ;; Just link to current headline
  11037. (setq cpltxt (concat "file:"
  11038. (abbreviate-file-name buffer-file-name)))
  11039. ;; Add a context search string
  11040. (when (org-xor org-context-in-file-links arg)
  11041. ;; Check if we are on a target
  11042. (if (org-in-regexp "<<\\(.*?\\)>>")
  11043. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11044. (setq txt (cond
  11045. ((org-on-heading-p) nil)
  11046. ((org-region-active-p)
  11047. (buffer-substring (region-beginning) (region-end)))
  11048. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11049. (when (or (null txt) (string-match "\\S-" txt))
  11050. (setq cpltxt
  11051. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11052. desc "NONE"))))
  11053. (if (string-match "::\\'" cpltxt)
  11054. (setq cpltxt (substring cpltxt 0 -2)))
  11055. (setq link (org-make-link cpltxt)))
  11056. ((buffer-file-name (buffer-base-buffer))
  11057. ;; Just link to this file here.
  11058. (setq cpltxt (concat "file:"
  11059. (abbreviate-file-name
  11060. (buffer-file-name (buffer-base-buffer)))))
  11061. ;; Add a context string
  11062. (when (org-xor org-context-in-file-links arg)
  11063. (setq txt (if (org-region-active-p)
  11064. (buffer-substring (region-beginning) (region-end))
  11065. (buffer-substring (point-at-bol) (point-at-eol))))
  11066. ;; Only use search option if there is some text.
  11067. (when (string-match "\\S-" txt)
  11068. (setq cpltxt
  11069. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11070. desc "NONE")))
  11071. (setq link (org-make-link cpltxt)))
  11072. ((interactive-p)
  11073. (error "Cannot link to a buffer which is not visiting a file"))
  11074. (t (setq link nil)))
  11075. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11076. (setq link (or link cpltxt)
  11077. desc (or desc cpltxt))
  11078. (if (equal desc "NONE") (setq desc nil))
  11079. (if (and (interactive-p) link)
  11080. (progn
  11081. (setq org-stored-links
  11082. (cons (list link desc) org-stored-links))
  11083. (message "Stored: %s" (or desc link)))
  11084. (and link (org-make-link-string link desc)))))
  11085. (defun org-store-link-props (&rest plist)
  11086. "Store link properties, extract names and addresses."
  11087. (let (x adr)
  11088. (when (setq x (plist-get plist :from))
  11089. (setq adr (mail-extract-address-components x))
  11090. (plist-put plist :fromname (car adr))
  11091. (plist-put plist :fromaddress (nth 1 adr)))
  11092. (when (setq x (plist-get plist :to))
  11093. (setq adr (mail-extract-address-components x))
  11094. (plist-put plist :toname (car adr))
  11095. (plist-put plist :toaddress (nth 1 adr))))
  11096. (let ((from (plist-get plist :from))
  11097. (to (plist-get plist :to)))
  11098. (when (and from to org-from-is-user-regexp)
  11099. (plist-put plist :fromto
  11100. (if (string-match org-from-is-user-regexp from)
  11101. (concat "to %t")
  11102. (concat "from %f")))))
  11103. (setq org-store-link-plist plist))
  11104. (defun org-email-link-description (&optional fmt)
  11105. "Return the description part of an email link.
  11106. This takes information from `org-store-link-plist' and formats it
  11107. according to FMT (default from `org-email-link-description-format')."
  11108. (setq fmt (or fmt org-email-link-description-format))
  11109. (let* ((p org-store-link-plist)
  11110. (to (plist-get p :toaddress))
  11111. (from (plist-get p :fromaddress))
  11112. (table
  11113. (list
  11114. (cons "%c" (plist-get p :fromto))
  11115. (cons "%F" (plist-get p :from))
  11116. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11117. (cons "%T" (plist-get p :to))
  11118. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11119. (cons "%s" (plist-get p :subject))
  11120. (cons "%m" (plist-get p :message-id)))))
  11121. (when (string-match "%c" fmt)
  11122. ;; Check if the user wrote this message
  11123. (if (and org-from-is-user-regexp from to
  11124. (save-match-data (string-match org-from-is-user-regexp from)))
  11125. (setq fmt (replace-match "to %t" t t fmt))
  11126. (setq fmt (replace-match "from %f" t t fmt))))
  11127. (org-replace-escapes fmt table)))
  11128. (defun org-make-org-heading-search-string (&optional string heading)
  11129. "Make search string for STRING or current headline."
  11130. (interactive)
  11131. (let ((s (or string (org-get-heading))))
  11132. (unless (and string (not heading))
  11133. ;; We are using a headline, clean up garbage in there.
  11134. (if (string-match org-todo-regexp s)
  11135. (setq s (replace-match "" t t s)))
  11136. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11137. (setq s (replace-match "" t t s)))
  11138. (setq s (org-trim s))
  11139. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11140. org-comment-string "\\)") s)
  11141. (setq s (replace-match "" t t s)))
  11142. (while (string-match org-ts-regexp s)
  11143. (setq s (replace-match "" t t s))))
  11144. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11145. (setq s (replace-match " " t t s)))
  11146. (or string (setq s (concat "*" s))) ; Add * for headlines
  11147. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11148. (defun org-make-link (&rest strings)
  11149. "Concatenate STRINGS."
  11150. (apply 'concat strings))
  11151. (defun org-make-link-string (link &optional description)
  11152. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11153. (unless (string-match "\\S-" link)
  11154. (error "Empty link"))
  11155. (when (stringp description)
  11156. ;; Remove brackets from the description, they are fatal.
  11157. (while (string-match "\\[" description)
  11158. (setq description (replace-match "{" t t description)))
  11159. (while (string-match "\\]" description)
  11160. (setq description (replace-match "}" t t description))))
  11161. (when (equal (org-link-escape link) description)
  11162. ;; No description needed, it is identical
  11163. (setq description nil))
  11164. (when (and (not description)
  11165. (not (equal link (org-link-escape link))))
  11166. (setq description link))
  11167. (concat "[[" (org-link-escape link) "]"
  11168. (if description (concat "[" description "]") "")
  11169. "]"))
  11170. (defconst org-link-escape-chars
  11171. '((?\ . "%20")
  11172. (?\[ . "%5B")
  11173. (?\] . "%5D")
  11174. (?\340 . "%E0") ; `a
  11175. (?\342 . "%E2") ; ^a
  11176. (?\347 . "%E7") ; ,c
  11177. (?\350 . "%E8") ; `e
  11178. (?\351 . "%E9") ; 'e
  11179. (?\352 . "%EA") ; ^e
  11180. (?\356 . "%EE") ; ^i
  11181. (?\364 . "%F4") ; ^o
  11182. (?\371 . "%F9") ; `u
  11183. (?\373 . "%FB") ; ^u
  11184. (?\; . "%3B")
  11185. (?? . "%3F")
  11186. (?= . "%3D")
  11187. (?+ . "%2B")
  11188. )
  11189. "Association list of escapes for some characters problematic in links.
  11190. This is the list that is used for internal purposes.")
  11191. (defconst org-link-escape-chars-browser
  11192. '((?\ . "%20")) ; 32 for the SPC char
  11193. "Association list of escapes for some characters problematic in links.
  11194. This is the list that is used before handing over to the browser.")
  11195. (defun org-link-escape (text &optional table)
  11196. "Escape charaters in TEXT that are problematic for links."
  11197. (setq table (or table org-link-escape-chars))
  11198. (when text
  11199. (let ((re (mapconcat (lambda (x) (regexp-quote
  11200. (char-to-string (car x))))
  11201. table "\\|")))
  11202. (while (string-match re text)
  11203. (setq text
  11204. (replace-match
  11205. (cdr (assoc (string-to-char (match-string 0 text))
  11206. table))
  11207. t t text)))
  11208. text)))
  11209. (defun org-link-unescape (text &optional table)
  11210. "Reverse the action of `org-link-escape'."
  11211. (setq table (or table org-link-escape-chars))
  11212. (when text
  11213. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11214. table "\\|")))
  11215. (while (string-match re text)
  11216. (setq text
  11217. (replace-match
  11218. (char-to-string (car (rassoc (match-string 0 text) table)))
  11219. t t text)))
  11220. text)))
  11221. (defun org-xor (a b)
  11222. "Exclusive or."
  11223. (if a (not b) b))
  11224. (defun org-get-header (header)
  11225. "Find a header field in the current buffer."
  11226. (save-excursion
  11227. (goto-char (point-min))
  11228. (let ((case-fold-search t) s)
  11229. (cond
  11230. ((eq header 'from)
  11231. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11232. (setq s (match-string 1)))
  11233. (while (string-match "\"" s)
  11234. (setq s (replace-match "" t t s)))
  11235. (if (string-match "[<(].*" s)
  11236. (setq s (replace-match "" t t s))))
  11237. ((eq header 'message-id)
  11238. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11239. (setq s (match-string 1))))
  11240. ((eq header 'subject)
  11241. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11242. (setq s (match-string 1)))))
  11243. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11244. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11245. s)))
  11246. (defun org-fixup-message-id-for-http (s)
  11247. "Replace special characters in a message id, so it can be used in an http query."
  11248. (while (string-match "<" s)
  11249. (setq s (replace-match "%3C" t t s)))
  11250. (while (string-match ">" s)
  11251. (setq s (replace-match "%3E" t t s)))
  11252. (while (string-match "@" s)
  11253. (setq s (replace-match "%40" t t s)))
  11254. s)
  11255. ;;;###autoload
  11256. (defun org-insert-link-global ()
  11257. "Insert a link like Org-mode does.
  11258. This command can be called in any mode to insert a link in Org-mode syntax."
  11259. (interactive)
  11260. (org-run-like-in-org-mode 'org-insert-link))
  11261. (defun org-insert-link (&optional complete-file)
  11262. "Insert a link. At the prompt, enter the link.
  11263. Completion can be used to select a link previously stored with
  11264. `org-store-link'. When the empty string is entered (i.e. if you just
  11265. press RET at the prompt), the link defaults to the most recently
  11266. stored link. As SPC triggers completion in the minibuffer, you need to
  11267. use M-SPC or C-q SPC to force the insertion of a space character.
  11268. You will also be prompted for a description, and if one is given, it will
  11269. be displayed in the buffer instead of the link.
  11270. If there is already a link at point, this command will allow you to edit link
  11271. and description parts.
  11272. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11273. selected using completion. The path to the file will be relative to
  11274. the current directory if the file is in the current directory or a
  11275. subdirectory. Otherwise, the link will be the absolute path as
  11276. completed in the minibuffer (i.e. normally ~/path/to/file).
  11277. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11278. is in the current directory or below.
  11279. With three \\[universal-argument] prefixes, negate the meaning of
  11280. `org-keep-stored-link-after-insertion'."
  11281. (interactive "P")
  11282. (let* ((wcf (current-window-configuration))
  11283. (region (if (org-region-active-p)
  11284. (buffer-substring (region-beginning) (region-end))))
  11285. (remove (and region (list (region-beginning) (region-end))))
  11286. (desc region)
  11287. tmphist ; byte-compile incorrectly complains about this
  11288. link entry file)
  11289. (cond
  11290. ((org-in-regexp org-bracket-link-regexp 1)
  11291. ;; We do have a link at point, and we are going to edit it.
  11292. (setq remove (list (match-beginning 0) (match-end 0)))
  11293. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11294. (setq link (read-string "Link: "
  11295. (org-link-unescape
  11296. (org-match-string-no-properties 1)))))
  11297. ((or (org-in-regexp org-angle-link-re)
  11298. (org-in-regexp org-plain-link-re))
  11299. ;; Convert to bracket link
  11300. (setq remove (list (match-beginning 0) (match-end 0))
  11301. link (read-string "Link: "
  11302. (org-remove-angle-brackets (match-string 0)))))
  11303. ((equal complete-file '(4))
  11304. ;; Completing read for file names.
  11305. (setq file (read-file-name "File: "))
  11306. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11307. (pwd1 (file-name-as-directory (abbreviate-file-name
  11308. (expand-file-name ".")))))
  11309. (cond
  11310. ((equal complete-file '(16))
  11311. (setq link (org-make-link
  11312. "file:"
  11313. (abbreviate-file-name (expand-file-name file)))))
  11314. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11315. (setq link (org-make-link "file:" (match-string 1 file))))
  11316. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11317. (expand-file-name file))
  11318. (setq link (org-make-link
  11319. "file:" (match-string 1 (expand-file-name file)))))
  11320. (t (setq link (org-make-link "file:" file))))))
  11321. (t
  11322. ;; Read link, with completion for stored links.
  11323. (with-output-to-temp-buffer "*Org Links*"
  11324. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11325. (when org-stored-links
  11326. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11327. (princ (mapconcat
  11328. (lambda (x)
  11329. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11330. (reverse org-stored-links) "\n"))))
  11331. (let ((cw (selected-window)))
  11332. (select-window (get-buffer-window "*Org Links*"))
  11333. (shrink-window-if-larger-than-buffer)
  11334. (setq truncate-lines t)
  11335. (select-window cw))
  11336. ;; Fake a link history, containing the stored links.
  11337. (setq tmphist (append (mapcar 'car org-stored-links)
  11338. org-insert-link-history))
  11339. (unwind-protect
  11340. (setq link (org-completing-read
  11341. "Link: "
  11342. (append
  11343. (mapcar (lambda (x) (list (concat (car x) ":")))
  11344. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11345. (mapcar (lambda (x) (list (concat x ":")))
  11346. org-link-types))
  11347. nil nil nil
  11348. 'tmphist
  11349. (or (car (car org-stored-links)))))
  11350. (set-window-configuration wcf)
  11351. (kill-buffer "*Org Links*"))
  11352. (setq entry (assoc link org-stored-links))
  11353. (or entry (push link org-insert-link-history))
  11354. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11355. (not org-keep-stored-link-after-insertion))
  11356. (setq org-stored-links (delq (assoc link org-stored-links)
  11357. org-stored-links)))
  11358. (setq desc (or desc (nth 1 entry)))))
  11359. (if (string-match org-plain-link-re link)
  11360. ;; URL-like link, normalize the use of angular brackets.
  11361. (setq link (org-make-link (org-remove-angle-brackets link))))
  11362. ;; Check if we are linking to the current file with a search option
  11363. ;; If yes, simplify the link by using only the search option.
  11364. (when (and buffer-file-name
  11365. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11366. (let* ((path (match-string 1 link))
  11367. (case-fold-search nil)
  11368. (search (match-string 2 link)))
  11369. (save-match-data
  11370. (if (equal (file-truename buffer-file-name) (file-truename path))
  11371. ;; We are linking to this same file, with a search option
  11372. (setq link search)))))
  11373. ;; Check if we can/should use a relative path. If yes, simplify the link
  11374. (when (string-match "\\<file:\\(.*\\)" link)
  11375. (let* ((path (match-string 1 link))
  11376. (origpath path)
  11377. (case-fold-search nil))
  11378. (cond
  11379. ((eq org-link-file-path-type 'absolute)
  11380. (setq path (abbreviate-file-name (expand-file-name path))))
  11381. ((eq org-link-file-path-type 'noabbrev)
  11382. (setq path (expand-file-name path)))
  11383. ((eq org-link-file-path-type 'relative)
  11384. (setq path (file-relative-name path)))
  11385. (t
  11386. (save-match-data
  11387. (if (string-match (concat "^" (regexp-quote
  11388. (file-name-as-directory
  11389. (expand-file-name "."))))
  11390. (expand-file-name path))
  11391. ;; We are linking a file with relative path name.
  11392. (setq path (substring (expand-file-name path)
  11393. (match-end 0)))))))
  11394. (setq link (concat "file:" path))
  11395. (if (equal desc origpath)
  11396. (setq desc path))))
  11397. (setq desc (read-string "Description: " desc))
  11398. (unless (string-match "\\S-" desc) (setq desc nil))
  11399. (if remove (apply 'delete-region remove))
  11400. (insert (org-make-link-string link desc))))
  11401. (defun org-completing-read (&rest args)
  11402. (let ((minibuffer-local-completion-map
  11403. (copy-keymap minibuffer-local-completion-map)))
  11404. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11405. (apply 'completing-read args)))
  11406. ;;; Opening/following a link
  11407. (defvar org-link-search-failed nil)
  11408. (defun org-next-link ()
  11409. "Move forward to the next link.
  11410. If the link is in hidden text, expose it."
  11411. (interactive)
  11412. (when (and org-link-search-failed (eq this-command last-command))
  11413. (goto-char (point-min))
  11414. (message "Link search wrapped back to beginning of buffer"))
  11415. (setq org-link-search-failed nil)
  11416. (let* ((pos (point))
  11417. (ct (org-context))
  11418. (a (assoc :link ct)))
  11419. (if a (goto-char (nth 2 a)))
  11420. (if (re-search-forward org-any-link-re nil t)
  11421. (progn
  11422. (goto-char (match-beginning 0))
  11423. (if (org-invisible-p) (org-show-context)))
  11424. (goto-char pos)
  11425. (setq org-link-search-failed t)
  11426. (error "No further link found"))))
  11427. (defun org-previous-link ()
  11428. "Move backward to the previous link.
  11429. If the link is in hidden text, expose it."
  11430. (interactive)
  11431. (when (and org-link-search-failed (eq this-command last-command))
  11432. (goto-char (point-max))
  11433. (message "Link search wrapped back to end of buffer"))
  11434. (setq org-link-search-failed nil)
  11435. (let* ((pos (point))
  11436. (ct (org-context))
  11437. (a (assoc :link ct)))
  11438. (if a (goto-char (nth 1 a)))
  11439. (if (re-search-backward org-any-link-re nil t)
  11440. (progn
  11441. (goto-char (match-beginning 0))
  11442. (if (org-invisible-p) (org-show-context)))
  11443. (goto-char pos)
  11444. (setq org-link-search-failed t)
  11445. (error "No further link found"))))
  11446. (defun org-find-file-at-mouse (ev)
  11447. "Open file link or URL at mouse."
  11448. (interactive "e")
  11449. (mouse-set-point ev)
  11450. (org-open-at-point 'in-emacs))
  11451. (defun org-open-at-mouse (ev)
  11452. "Open file link or URL at mouse."
  11453. (interactive "e")
  11454. (mouse-set-point ev)
  11455. (org-open-at-point))
  11456. (defvar org-window-config-before-follow-link nil
  11457. "The window configuration before following a link.
  11458. This is saved in case the need arises to restore it.")
  11459. (defvar org-open-link-marker (make-marker)
  11460. "Marker pointing to the location where `org-open-at-point; was called.")
  11461. ;;;###autoload
  11462. (defun org-open-at-point-global ()
  11463. "Follow a link like Org-mode does.
  11464. This command can be called in any mode to follow a link that has
  11465. Org-mode syntax."
  11466. (interactive)
  11467. (org-run-like-in-org-mode 'org-open-at-point))
  11468. (defun org-open-at-point (&optional in-emacs)
  11469. "Open link at or after point.
  11470. If there is no link at point, this function will search forward up to
  11471. the end of the current subtree.
  11472. Normally, files will be opened by an appropriate application. If the
  11473. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11474. (interactive "P")
  11475. (catch 'abort
  11476. (move-marker org-open-link-marker (point))
  11477. (setq org-window-config-before-follow-link (current-window-configuration))
  11478. (org-remove-occur-highlights nil nil t)
  11479. (if (org-at-timestamp-p t)
  11480. (org-follow-timestamp-link)
  11481. (let (type path link line search (pos (point)))
  11482. (catch 'match
  11483. (save-excursion
  11484. (skip-chars-forward "^]\n\r")
  11485. (when (org-in-regexp org-bracket-link-regexp)
  11486. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11487. (while (string-match " *\n *" link)
  11488. (setq link (replace-match " " t t link)))
  11489. (setq link (org-link-expand-abbrev link))
  11490. (if (string-match org-link-re-with-space2 link)
  11491. (setq type (match-string 1 link) path (match-string 2 link))
  11492. (setq type "thisfile" path link))
  11493. (throw 'match t)))
  11494. (when (get-text-property (point) 'org-linked-text)
  11495. (setq type "thisfile"
  11496. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11497. (1+ (point)) (point))
  11498. path (buffer-substring
  11499. (previous-single-property-change pos 'org-linked-text)
  11500. (next-single-property-change pos 'org-linked-text)))
  11501. (throw 'match t))
  11502. (save-excursion
  11503. (when (or (org-in-regexp org-angle-link-re)
  11504. (org-in-regexp org-plain-link-re))
  11505. (setq type (match-string 1) path (match-string 2))
  11506. (throw 'match t)))
  11507. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11508. (setq type "tree-match"
  11509. path (match-string 1))
  11510. (throw 'match t))
  11511. (save-excursion
  11512. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11513. (setq type "tags"
  11514. path (match-string 1))
  11515. (while (string-match ":" path)
  11516. (setq path (replace-match "+" t t path)))
  11517. (throw 'match t))))
  11518. (unless path
  11519. (error "No link found"))
  11520. ;; Remove any trailing spaces in path
  11521. (if (string-match " +\\'" path)
  11522. (setq path (replace-match "" t t path)))
  11523. (cond
  11524. ((assoc type org-link-protocols)
  11525. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11526. ((equal type "mailto")
  11527. (let ((cmd (car org-link-mailto-program))
  11528. (args (cdr org-link-mailto-program)) args1
  11529. (address path) (subject "") a)
  11530. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11531. (setq address (match-string 1 path)
  11532. subject (org-link-escape (match-string 2 path))))
  11533. (while args
  11534. (cond
  11535. ((not (stringp (car args))) (push (pop args) args1))
  11536. (t (setq a (pop args))
  11537. (if (string-match "%a" a)
  11538. (setq a (replace-match address t t a)))
  11539. (if (string-match "%s" a)
  11540. (setq a (replace-match subject t t a)))
  11541. (push a args1))))
  11542. (apply cmd (nreverse args1))))
  11543. ((member type '("http" "https" "ftp" "news"))
  11544. (browse-url (concat type ":" (org-link-escape
  11545. path org-link-escape-chars-browser))))
  11546. ((member type '("message"))
  11547. (browse-url (concat type ":" path)))
  11548. ((string= type "tags")
  11549. (org-tags-view in-emacs path))
  11550. ((string= type "thisfile")
  11551. (if in-emacs
  11552. (switch-to-buffer-other-window
  11553. (org-get-buffer-for-internal-link (current-buffer)))
  11554. (org-mark-ring-push))
  11555. (let ((cmd `(org-link-search
  11556. ,path
  11557. ,(cond ((equal in-emacs '(4)) 'occur)
  11558. ((equal in-emacs '(16)) 'org-occur)
  11559. (t nil))
  11560. ,pos)))
  11561. (condition-case nil (eval cmd)
  11562. (error (progn (widen) (eval cmd))))))
  11563. ((string= type "tree-match")
  11564. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11565. ((string= type "file")
  11566. (if (string-match "::\\([0-9]+\\)\\'" path)
  11567. (setq line (string-to-number (match-string 1 path))
  11568. path (substring path 0 (match-beginning 0)))
  11569. (if (string-match "::\\(.+\\)\\'" path)
  11570. (setq search (match-string 1 path)
  11571. path (substring path 0 (match-beginning 0)))))
  11572. (if (string-match "[*?{]" (file-name-nondirectory path))
  11573. (dired path)
  11574. (org-open-file path in-emacs line search)))
  11575. ((string= type "news")
  11576. (org-follow-gnus-link path))
  11577. ((string= type "bbdb")
  11578. (org-follow-bbdb-link path))
  11579. ((string= type "info")
  11580. (org-follow-info-link path))
  11581. ((string= type "gnus")
  11582. (let (group article)
  11583. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11584. (error "Error in Gnus link"))
  11585. (setq group (match-string 1 path)
  11586. article (match-string 3 path))
  11587. (org-follow-gnus-link group article)))
  11588. ((string= type "vm")
  11589. (let (folder article)
  11590. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11591. (error "Error in VM link"))
  11592. (setq folder (match-string 1 path)
  11593. article (match-string 3 path))
  11594. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11595. (org-follow-vm-link folder article in-emacs)))
  11596. ((string= type "wl")
  11597. (let (folder article)
  11598. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11599. (error "Error in Wanderlust link"))
  11600. (setq folder (match-string 1 path)
  11601. article (match-string 3 path))
  11602. (org-follow-wl-link folder article)))
  11603. ((string= type "mhe")
  11604. (let (folder article)
  11605. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11606. (error "Error in MHE link"))
  11607. (setq folder (match-string 1 path)
  11608. article (match-string 3 path))
  11609. (org-follow-mhe-link folder article)))
  11610. ((string= type "rmail")
  11611. (let (folder article)
  11612. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11613. (error "Error in RMAIL link"))
  11614. (setq folder (match-string 1 path)
  11615. article (match-string 3 path))
  11616. (org-follow-rmail-link folder article)))
  11617. ((string= type "shell")
  11618. (let ((cmd path))
  11619. (if (or (not org-confirm-shell-link-function)
  11620. (funcall org-confirm-shell-link-function
  11621. (format "Execute \"%s\" in shell? "
  11622. (org-add-props cmd nil
  11623. 'face 'org-warning))))
  11624. (progn
  11625. (message "Executing %s" cmd)
  11626. (shell-command cmd))
  11627. (error "Abort"))))
  11628. ((string= type "elisp")
  11629. (let ((cmd path))
  11630. (if (or (not org-confirm-elisp-link-function)
  11631. (funcall org-confirm-elisp-link-function
  11632. (format "Execute \"%s\" as elisp? "
  11633. (org-add-props cmd nil
  11634. 'face 'org-warning))))
  11635. (message "%s => %s" cmd (eval (read cmd)))
  11636. (error "Abort"))))
  11637. (t
  11638. (browse-url-at-point)))))
  11639. (move-marker org-open-link-marker nil)))
  11640. ;;; File search
  11641. (defvar org-create-file-search-functions nil
  11642. "List of functions to construct the right search string for a file link.
  11643. These functions are called in turn with point at the location to
  11644. which the link should point.
  11645. A function in the hook should first test if it would like to
  11646. handle this file type, for example by checking the major-mode or
  11647. the file extension. If it decides not to handle this file, it
  11648. should just return nil to give other functions a chance. If it
  11649. does handle the file, it must return the search string to be used
  11650. when following the link. The search string will be part of the
  11651. file link, given after a double colon, and `org-open-at-point'
  11652. will automatically search for it. If special measures must be
  11653. taken to make the search successful, another function should be
  11654. added to the companion hook `org-execute-file-search-functions',
  11655. which see.
  11656. A function in this hook may also use `setq' to set the variable
  11657. `description' to provide a suggestion for the descriptive text to
  11658. be used for this link when it gets inserted into an Org-mode
  11659. buffer with \\[org-insert-link].")
  11660. (defvar org-execute-file-search-functions nil
  11661. "List of functions to execute a file search triggered by a link.
  11662. Functions added to this hook must accept a single argument, the
  11663. search string that was part of the file link, the part after the
  11664. double colon. The function must first check if it would like to
  11665. handle this search, for example by checking the major-mode or the
  11666. file extension. If it decides not to handle this search, it
  11667. should just return nil to give other functions a chance. If it
  11668. does handle the search, it must return a non-nil value to keep
  11669. other functions from trying.
  11670. Each function can access the current prefix argument through the
  11671. variable `current-prefix-argument'. Note that a single prefix is
  11672. used to force opening a link in Emacs, so it may be good to only
  11673. use a numeric or double prefix to guide the search function.
  11674. In case this is needed, a function in this hook can also restore
  11675. the window configuration before `org-open-at-point' was called using:
  11676. (set-window-configuration org-window-config-before-follow-link)")
  11677. (defun org-link-search (s &optional type avoid-pos)
  11678. "Search for a link search option.
  11679. If S is surrounded by forward slashes, it is interpreted as a
  11680. regular expression. In org-mode files, this will create an `org-occur'
  11681. sparse tree. In ordinary files, `occur' will be used to list matches.
  11682. If the current buffer is in `dired-mode', grep will be used to search
  11683. in all files. If AVOID-POS is given, ignore matches near that position."
  11684. (let ((case-fold-search t)
  11685. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11686. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11687. (append '(("") (" ") ("\t") ("\n"))
  11688. org-emphasis-alist)
  11689. "\\|") "\\)"))
  11690. (pos (point))
  11691. (pre "") (post "")
  11692. words re0 re1 re2 re3 re4 re5 re2a reall)
  11693. (cond
  11694. ;; First check if there are any special
  11695. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11696. ;; Now try the builtin stuff
  11697. ((save-excursion
  11698. (goto-char (point-min))
  11699. (and
  11700. (re-search-forward
  11701. (concat "<<" (regexp-quote s0) ">>") nil t)
  11702. (setq pos (match-beginning 0))))
  11703. ;; There is an exact target for this
  11704. (goto-char pos))
  11705. ((string-match "^/\\(.*\\)/$" s)
  11706. ;; A regular expression
  11707. (cond
  11708. ((org-mode-p)
  11709. (org-occur (match-string 1 s)))
  11710. ;;((eq major-mode 'dired-mode)
  11711. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11712. (t (org-do-occur (match-string 1 s)))))
  11713. (t
  11714. ;; A normal search strings
  11715. (when (equal (string-to-char s) ?*)
  11716. ;; Anchor on headlines, post may include tags.
  11717. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11718. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11719. s (substring s 1)))
  11720. (remove-text-properties
  11721. 0 (length s)
  11722. '(face nil mouse-face nil keymap nil fontified nil) s)
  11723. ;; Make a series of regular expressions to find a match
  11724. (setq words (org-split-string s "[ \n\r\t]+")
  11725. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11726. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11727. "\\)" markers)
  11728. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11729. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11730. re1 (concat pre re2 post)
  11731. re3 (concat pre re4 post)
  11732. re5 (concat pre ".*" re4)
  11733. re2 (concat pre re2)
  11734. re2a (concat pre re2a)
  11735. re4 (concat pre re4)
  11736. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11737. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11738. re5 "\\)"
  11739. ))
  11740. (cond
  11741. ((eq type 'org-occur) (org-occur reall))
  11742. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11743. (t (goto-char (point-min))
  11744. (if (or (org-search-not-self 1 re0 nil t)
  11745. (org-search-not-self 1 re1 nil t)
  11746. (org-search-not-self 1 re2 nil t)
  11747. (org-search-not-self 1 re2a nil t)
  11748. (org-search-not-self 1 re3 nil t)
  11749. (org-search-not-self 1 re4 nil t)
  11750. (org-search-not-self 1 re5 nil t)
  11751. )
  11752. (goto-char (match-beginning 1))
  11753. (goto-char pos)
  11754. (error "No match")))))
  11755. (t
  11756. ;; Normal string-search
  11757. (goto-char (point-min))
  11758. (if (search-forward s nil t)
  11759. (goto-char (match-beginning 0))
  11760. (error "No match"))))
  11761. (and (org-mode-p) (org-show-context 'link-search))))
  11762. (defun org-search-not-self (group &rest args)
  11763. "Execute `re-search-forward', but only accept matches that do not
  11764. enclose the position of `org-open-link-marker'."
  11765. (let ((m org-open-link-marker))
  11766. (catch 'exit
  11767. (while (apply 're-search-forward args)
  11768. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11769. (goto-char (match-end group))
  11770. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11771. (> (match-beginning 0) (marker-position m))
  11772. (< (match-end 0) (marker-position m)))
  11773. (save-match-data
  11774. (or (not (org-in-regexp
  11775. org-bracket-link-analytic-regexp 1))
  11776. (not (match-end 4)) ; no description
  11777. (and (<= (match-beginning 4) (point))
  11778. (>= (match-end 4) (point))))))
  11779. (throw 'exit (point))))))))
  11780. (defun org-get-buffer-for-internal-link (buffer)
  11781. "Return a buffer to be used for displaying the link target of internal links."
  11782. (cond
  11783. ((not org-display-internal-link-with-indirect-buffer)
  11784. buffer)
  11785. ((string-match "(Clone)$" (buffer-name buffer))
  11786. (message "Buffer is already a clone, not making another one")
  11787. ;; we also do not modify visibility in this case
  11788. buffer)
  11789. (t ; make a new indirect buffer for displaying the link
  11790. (let* ((bn (buffer-name buffer))
  11791. (ibn (concat bn "(Clone)"))
  11792. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11793. (with-current-buffer ib (org-overview))
  11794. ib))))
  11795. (defun org-do-occur (regexp &optional cleanup)
  11796. "Call the Emacs command `occur'.
  11797. If CLEANUP is non-nil, remove the printout of the regular expression
  11798. in the *Occur* buffer. This is useful if the regex is long and not useful
  11799. to read."
  11800. (occur regexp)
  11801. (when cleanup
  11802. (let ((cwin (selected-window)) win beg end)
  11803. (when (setq win (get-buffer-window "*Occur*"))
  11804. (select-window win))
  11805. (goto-char (point-min))
  11806. (when (re-search-forward "match[a-z]+" nil t)
  11807. (setq beg (match-end 0))
  11808. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11809. (setq end (1- (match-beginning 0)))))
  11810. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11811. (goto-char (point-min))
  11812. (select-window cwin))))
  11813. ;;; The mark ring for links jumps
  11814. (defvar org-mark-ring nil
  11815. "Mark ring for positions before jumps in Org-mode.")
  11816. (defvar org-mark-ring-last-goto nil
  11817. "Last position in the mark ring used to go back.")
  11818. ;; Fill and close the ring
  11819. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11820. (loop for i from 1 to org-mark-ring-length do
  11821. (push (make-marker) org-mark-ring))
  11822. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11823. org-mark-ring)
  11824. (defun org-mark-ring-push (&optional pos buffer)
  11825. "Put the current position or POS into the mark ring and rotate it."
  11826. (interactive)
  11827. (setq pos (or pos (point)))
  11828. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11829. (move-marker (car org-mark-ring)
  11830. (or pos (point))
  11831. (or buffer (current-buffer)))
  11832. (message "%s"
  11833. (substitute-command-keys
  11834. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11835. (defun org-mark-ring-goto (&optional n)
  11836. "Jump to the previous position in the mark ring.
  11837. With prefix arg N, jump back that many stored positions. When
  11838. called several times in succession, walk through the entire ring.
  11839. Org-mode commands jumping to a different position in the current file,
  11840. or to another Org-mode file, automatically push the old position
  11841. onto the ring."
  11842. (interactive "p")
  11843. (let (p m)
  11844. (if (eq last-command this-command)
  11845. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11846. (setq p org-mark-ring))
  11847. (setq org-mark-ring-last-goto p)
  11848. (setq m (car p))
  11849. (switch-to-buffer (marker-buffer m))
  11850. (goto-char m)
  11851. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11852. (defun org-remove-angle-brackets (s)
  11853. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11854. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11855. s)
  11856. (defun org-add-angle-brackets (s)
  11857. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11858. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11859. s)
  11860. ;;; Following specific links
  11861. (defun org-follow-timestamp-link ()
  11862. (cond
  11863. ((org-at-date-range-p t)
  11864. (let ((org-agenda-start-on-weekday)
  11865. (t1 (match-string 1))
  11866. (t2 (match-string 2)))
  11867. (setq t1 (time-to-days (org-time-string-to-time t1))
  11868. t2 (time-to-days (org-time-string-to-time t2)))
  11869. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11870. ((org-at-timestamp-p t)
  11871. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11872. (substring (match-string 1) 0 10)))
  11873. 1))
  11874. (t (error "This should not happen"))))
  11875. (defun org-follow-bbdb-link (name)
  11876. "Follow a BBDB link to NAME."
  11877. (require 'bbdb)
  11878. (let ((inhibit-redisplay (not debug-on-error))
  11879. (bbdb-electric-p nil))
  11880. (catch 'exit
  11881. ;; Exact match on name
  11882. (bbdb-name (concat "\\`" name "\\'") nil)
  11883. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11884. ;; Exact match on name
  11885. (bbdb-company (concat "\\`" name "\\'") nil)
  11886. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11887. ;; Partial match on name
  11888. (bbdb-name name nil)
  11889. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11890. ;; Partial match on company
  11891. (bbdb-company name nil)
  11892. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11893. ;; General match including network address and notes
  11894. (bbdb name nil)
  11895. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11896. (delete-window (get-buffer-window "*BBDB*"))
  11897. (error "No matching BBDB record")))))
  11898. (defun org-follow-info-link (name)
  11899. "Follow an info file & node link to NAME."
  11900. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11901. (string-match "\\(.*\\)" name))
  11902. (progn
  11903. (require 'info)
  11904. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11905. (Info-find-node (match-string 1 name) (match-string 2 name))
  11906. (Info-find-node (match-string 1 name) "Top")))
  11907. (message "Could not open: %s" name)))
  11908. (defun org-follow-gnus-link (&optional group article)
  11909. "Follow a Gnus link to GROUP and ARTICLE."
  11910. (require 'gnus)
  11911. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11912. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11913. (cond ((and group article)
  11914. (gnus-group-read-group 1 nil group)
  11915. (gnus-summary-goto-article (string-to-number article) nil t))
  11916. (group (gnus-group-jump-to-group group))))
  11917. (defun org-follow-vm-link (&optional folder article readonly)
  11918. "Follow a VM link to FOLDER and ARTICLE."
  11919. (require 'vm)
  11920. (setq article (org-add-angle-brackets article))
  11921. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11922. ;; ange-ftp or efs or tramp access
  11923. (let ((user (or (match-string 1 folder) (user-login-name)))
  11924. (host (match-string 2 folder))
  11925. (file (match-string 3 folder)))
  11926. (cond
  11927. ((featurep 'tramp)
  11928. ;; use tramp to access the file
  11929. (if (featurep 'xemacs)
  11930. (setq folder (format "[%s@%s]%s" user host file))
  11931. (setq folder (format "/%s@%s:%s" user host file))))
  11932. (t
  11933. ;; use ange-ftp or efs
  11934. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11935. (setq folder (format "/%s@%s:%s" user host file))))))
  11936. (when folder
  11937. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11938. (sit-for 0.1)
  11939. (when article
  11940. (vm-select-folder-buffer)
  11941. (widen)
  11942. (let ((case-fold-search t))
  11943. (goto-char (point-min))
  11944. (if (not (re-search-forward
  11945. (concat "^" "message-id: *" (regexp-quote article))))
  11946. (error "Could not find the specified message in this folder"))
  11947. (vm-isearch-update)
  11948. (vm-isearch-narrow)
  11949. (vm-beginning-of-message)
  11950. (vm-summarize)))))
  11951. (defun org-follow-wl-link (folder article)
  11952. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11953. (if (and (string= folder "%")
  11954. article
  11955. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11956. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11957. ;; Thus, we recompose folder and article ids.
  11958. (setq folder (format "%s#%s" folder (match-string 1 article))
  11959. article (match-string 3 article)))
  11960. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11961. (error "No such folder: %s" folder))
  11962. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11963. (and article
  11964. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11965. (wl-summary-redisplay)))
  11966. (defun org-follow-rmail-link (folder article)
  11967. "Follow an RMAIL link to FOLDER and ARTICLE."
  11968. (setq article (org-add-angle-brackets article))
  11969. (let (message-number)
  11970. (save-excursion
  11971. (save-window-excursion
  11972. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11973. (setq message-number
  11974. (save-restriction
  11975. (widen)
  11976. (goto-char (point-max))
  11977. (if (re-search-backward
  11978. (concat "^Message-ID:\\s-+" (regexp-quote
  11979. (or article "")))
  11980. nil t)
  11981. (rmail-what-message))))))
  11982. (if message-number
  11983. (progn
  11984. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11985. (rmail-show-message message-number)
  11986. message-number)
  11987. (error "Message not found"))))
  11988. ;;; mh-e integration based on planner-mode
  11989. (defun org-mhe-get-message-real-folder ()
  11990. "Return the name of the current message real folder, so if you use
  11991. sequences, it will now work."
  11992. (save-excursion
  11993. (let* ((folder
  11994. (if (equal major-mode 'mh-folder-mode)
  11995. mh-current-folder
  11996. ;; Refer to the show buffer
  11997. mh-show-folder-buffer))
  11998. (end-index
  11999. (if (boundp 'mh-index-folder)
  12000. (min (length mh-index-folder) (length folder))))
  12001. )
  12002. ;; a simple test on mh-index-data does not work, because
  12003. ;; mh-index-data is always nil in a show buffer.
  12004. (if (and (boundp 'mh-index-folder)
  12005. (string= mh-index-folder (substring folder 0 end-index)))
  12006. (if (equal major-mode 'mh-show-mode)
  12007. (save-window-excursion
  12008. (let (pop-up-frames)
  12009. (when (buffer-live-p (get-buffer folder))
  12010. (progn
  12011. (pop-to-buffer folder)
  12012. (org-mhe-get-message-folder-from-index)
  12013. )
  12014. )))
  12015. (org-mhe-get-message-folder-from-index)
  12016. )
  12017. folder
  12018. )
  12019. )))
  12020. (defun org-mhe-get-message-folder-from-index ()
  12021. "Returns the name of the message folder in a index folder buffer."
  12022. (save-excursion
  12023. (mh-index-previous-folder)
  12024. (re-search-forward "^\\(+.*\\)$" nil t)
  12025. (message "%s" (match-string 1))))
  12026. (defun org-mhe-get-message-folder ()
  12027. "Return the name of the current message folder. Be careful if you
  12028. use sequences."
  12029. (save-excursion
  12030. (if (equal major-mode 'mh-folder-mode)
  12031. mh-current-folder
  12032. ;; Refer to the show buffer
  12033. mh-show-folder-buffer)))
  12034. (defun org-mhe-get-message-num ()
  12035. "Return the number of the current message. Be careful if you
  12036. use sequences."
  12037. (save-excursion
  12038. (if (equal major-mode 'mh-folder-mode)
  12039. (mh-get-msg-num nil)
  12040. ;; Refer to the show buffer
  12041. (mh-show-buffer-message-number))))
  12042. (defun org-mhe-get-header (header)
  12043. "Return a header of the message in folder mode. This will create a
  12044. show buffer for the corresponding message. If you have a more clever
  12045. idea..."
  12046. (let* ((folder (org-mhe-get-message-folder))
  12047. (num (org-mhe-get-message-num))
  12048. (buffer (get-buffer-create (concat "show-" folder)))
  12049. (header-field))
  12050. (with-current-buffer buffer
  12051. (mh-display-msg num folder)
  12052. (if (equal major-mode 'mh-folder-mode)
  12053. (mh-header-display)
  12054. (mh-show-header-display))
  12055. (set-buffer buffer)
  12056. (setq header-field (mh-get-header-field header))
  12057. (if (equal major-mode 'mh-folder-mode)
  12058. (mh-show)
  12059. (mh-show-show))
  12060. header-field)))
  12061. (defun org-follow-mhe-link (folder article)
  12062. "Follow an MHE link to FOLDER and ARTICLE.
  12063. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12064. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12065. ARTICLE is searched in all folders. Indexed searches (swish++,
  12066. namazu, and others supported by MH-E) will always search in all
  12067. folders."
  12068. (require 'mh-e)
  12069. (require 'mh-search)
  12070. (require 'mh-utils)
  12071. (mh-find-path)
  12072. (if (not article)
  12073. (mh-visit-folder (mh-normalize-folder-name folder))
  12074. (setq article (org-add-angle-brackets article))
  12075. (mh-search-choose)
  12076. (if (equal mh-searcher 'pick)
  12077. (progn
  12078. (mh-search folder (list "--message-id" article))
  12079. (when (and org-mhe-search-all-folders
  12080. (not (org-mhe-get-message-real-folder)))
  12081. (kill-this-buffer)
  12082. (mh-search "+" (list "--message-id" article))))
  12083. (mh-search "+" article))
  12084. (if (org-mhe-get-message-real-folder)
  12085. (mh-show-msg 1)
  12086. (kill-this-buffer)
  12087. (error "Message not found"))))
  12088. ;;; BibTeX links
  12089. ;; Use the custom search meachnism to construct and use search strings for
  12090. ;; file links to BibTeX database entries.
  12091. (defun org-create-file-search-in-bibtex ()
  12092. "Create the search string and description for a BibTeX database entry."
  12093. (when (eq major-mode 'bibtex-mode)
  12094. ;; yes, we want to construct this search string.
  12095. ;; Make a good description for this entry, using names, year and the title
  12096. ;; Put it into the `description' variable which is dynamically scoped.
  12097. (let ((bibtex-autokey-names 1)
  12098. (bibtex-autokey-names-stretch 1)
  12099. (bibtex-autokey-name-case-convert-function 'identity)
  12100. (bibtex-autokey-name-separator " & ")
  12101. (bibtex-autokey-additional-names " et al.")
  12102. (bibtex-autokey-year-length 4)
  12103. (bibtex-autokey-name-year-separator " ")
  12104. (bibtex-autokey-titlewords 3)
  12105. (bibtex-autokey-titleword-separator " ")
  12106. (bibtex-autokey-titleword-case-convert-function 'identity)
  12107. (bibtex-autokey-titleword-length 'infty)
  12108. (bibtex-autokey-year-title-separator ": "))
  12109. (setq description (bibtex-generate-autokey)))
  12110. ;; Now parse the entry, get the key and return it.
  12111. (save-excursion
  12112. (bibtex-beginning-of-entry)
  12113. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12114. (defun org-execute-file-search-in-bibtex (s)
  12115. "Find the link search string S as a key for a database entry."
  12116. (when (eq major-mode 'bibtex-mode)
  12117. ;; Yes, we want to do the search in this file.
  12118. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12119. (goto-char (point-min))
  12120. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12121. (regexp-quote s) "[ \t\n]*,") nil t)
  12122. (goto-char (match-beginning 0)))
  12123. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12124. ;; Use double prefix to indicate that any web link should be browsed
  12125. (let ((b (current-buffer)) (p (point)))
  12126. ;; Restore the window configuration because we just use the web link
  12127. (set-window-configuration org-window-config-before-follow-link)
  12128. (save-excursion (set-buffer b) (goto-char p)
  12129. (bibtex-url)))
  12130. (recenter 0)) ; Move entry start to beginning of window
  12131. ;; return t to indicate that the search is done.
  12132. t))
  12133. ;; Finally add the functions to the right hooks.
  12134. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12135. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12136. ;; end of Bibtex link setup
  12137. ;;; Following file links
  12138. (defun org-open-file (path &optional in-emacs line search)
  12139. "Open the file at PATH.
  12140. First, this expands any special file name abbreviations. Then the
  12141. configuration variable `org-file-apps' is checked if it contains an
  12142. entry for this file type, and if yes, the corresponding command is launched.
  12143. If no application is found, Emacs simply visits the file.
  12144. With optional argument IN-EMACS, Emacs will visit the file.
  12145. Optional LINE specifies a line to go to, optional SEARCH a string to
  12146. search for. If LINE or SEARCH is given, the file will always be
  12147. opened in Emacs.
  12148. If the file does not exist, an error is thrown."
  12149. (setq in-emacs (or in-emacs line search))
  12150. (let* ((file (if (equal path "")
  12151. buffer-file-name
  12152. (substitute-in-file-name (expand-file-name path))))
  12153. (apps (append org-file-apps (org-default-apps)))
  12154. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12155. (dirp (if remp nil (file-directory-p file)))
  12156. (dfile (downcase file))
  12157. (old-buffer (current-buffer))
  12158. (old-pos (point))
  12159. (old-mode major-mode)
  12160. ext cmd)
  12161. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12162. (setq ext (match-string 1 dfile))
  12163. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12164. (setq ext (match-string 1 dfile))))
  12165. (if in-emacs
  12166. (setq cmd 'emacs)
  12167. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12168. (and dirp (cdr (assoc 'directory apps)))
  12169. (cdr (assoc ext apps))
  12170. (cdr (assoc t apps)))))
  12171. (when (eq cmd 'mailcap)
  12172. (require 'mailcap)
  12173. (mailcap-parse-mailcaps)
  12174. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12175. (command (mailcap-mime-info mime-type)))
  12176. (if (stringp command)
  12177. (setq cmd command)
  12178. (setq cmd 'emacs))))
  12179. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12180. (not (file-exists-p file))
  12181. (not org-open-non-existing-files))
  12182. (error "No such file: %s" file))
  12183. (cond
  12184. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12185. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12186. (while (string-match "['\"]%s['\"]" cmd)
  12187. (setq cmd (replace-match "%s" t t cmd)))
  12188. (while (string-match "%s" cmd)
  12189. (setq cmd (replace-match
  12190. (save-match-data (shell-quote-argument file))
  12191. t t cmd)))
  12192. (save-window-excursion
  12193. (start-process-shell-command cmd nil cmd)))
  12194. ((or (stringp cmd)
  12195. (eq cmd 'emacs))
  12196. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12197. (widen)
  12198. (if line (goto-line line)
  12199. (if search (org-link-search search))))
  12200. ((consp cmd)
  12201. (eval cmd))
  12202. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12203. (and (org-mode-p) (eq old-mode 'org-mode)
  12204. (or (not (equal old-buffer (current-buffer)))
  12205. (not (equal old-pos (point))))
  12206. (org-mark-ring-push old-pos old-buffer))))
  12207. (defun org-default-apps ()
  12208. "Return the default applications for this operating system."
  12209. (cond
  12210. ((eq system-type 'darwin)
  12211. org-file-apps-defaults-macosx)
  12212. ((eq system-type 'windows-nt)
  12213. org-file-apps-defaults-windowsnt)
  12214. (t org-file-apps-defaults-gnu)))
  12215. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12216. (defun org-file-remote-p (file)
  12217. "Test whether FILE specifies a location on a remote system.
  12218. Return non-nil if the location is indeed remote.
  12219. For example, the filename \"/user@host:/foo\" specifies a location
  12220. on the system \"/user@host:\"."
  12221. (cond ((fboundp 'file-remote-p)
  12222. (file-remote-p file))
  12223. ((fboundp 'tramp-handle-file-remote-p)
  12224. (tramp-handle-file-remote-p file))
  12225. ((and (boundp 'ange-ftp-name-format)
  12226. (string-match (car ange-ftp-name-format) file))
  12227. t)
  12228. (t nil)))
  12229. ;;;; Hooks for remember.el, and refiling
  12230. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12231. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12232. ;;;###autoload
  12233. (defun org-remember-insinuate ()
  12234. "Setup remember.el for use wiht Org-mode."
  12235. (require 'remember)
  12236. (setq remember-annotation-functions '(org-remember-annotation))
  12237. (setq remember-handler-functions '(org-remember-handler))
  12238. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12239. ;;;###autoload
  12240. (defun org-remember-annotation ()
  12241. "Return a link to the current location as an annotation for remember.el.
  12242. If you are using Org-mode files as target for data storage with
  12243. remember.el, then the annotations should include a link compatible with the
  12244. conventions in Org-mode. This function returns such a link."
  12245. (org-store-link nil))
  12246. (defconst org-remember-help
  12247. "Select a destination location for the note.
  12248. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12249. RET on headline -> Store as sublevel entry to current headline
  12250. RET at beg-of-buf -> Append to file as level 2 headline
  12251. <left>/<right> -> before/after current headline, same headings level")
  12252. (defvar org-remember-previous-location nil)
  12253. (defvar org-force-remember-template-char) ;; dynamically scoped
  12254. (defun org-select-remember-template (&optional use-char)
  12255. (when org-remember-templates
  12256. (let* ((templates (mapcar (lambda (x)
  12257. (if (stringp (car x))
  12258. (append (list (nth 1 x) (car x)) (cddr x))
  12259. (append (list (car x) "") (cdr x))))
  12260. org-remember-templates))
  12261. (char (or use-char
  12262. (cond
  12263. ((= (length templates) 1)
  12264. (caar templates))
  12265. ((and (boundp 'org-force-remember-template-char)
  12266. org-force-remember-template-char)
  12267. (if (stringp org-force-remember-template-char)
  12268. (string-to-char org-force-remember-template-char)
  12269. org-force-remember-template-char))
  12270. (t
  12271. (message "Select template: %s"
  12272. (mapconcat
  12273. (lambda (x)
  12274. (cond
  12275. ((not (string-match "\\S-" (nth 1 x)))
  12276. (format "[%c]" (car x)))
  12277. ((equal (downcase (car x))
  12278. (downcase (aref (nth 1 x) 0)))
  12279. (format "[%c]%s" (car x)
  12280. (substring (nth 1 x) 1)))
  12281. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12282. templates " "))
  12283. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12284. (when (equal char0 ?\C-g)
  12285. (jump-to-register remember-register)
  12286. (kill-buffer remember-buffer))
  12287. char0))))))
  12288. (cddr (assoc char templates)))))
  12289. (defvar x-last-selected-text)
  12290. (defvar x-last-selected-text-primary)
  12291. ;;;###autoload
  12292. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12293. "Initialize *remember* buffer with template, invoke `org-mode'.
  12294. This function should be placed into `remember-mode-hook' and in fact requires
  12295. to be run from that hook to function properly."
  12296. (if org-remember-templates
  12297. (let* ((entry (org-select-remember-template use-char))
  12298. (tpl (car entry))
  12299. (plist-p (if org-store-link-plist t nil))
  12300. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12301. (string-match "\\S-" (nth 1 entry)))
  12302. (nth 1 entry)
  12303. org-default-notes-file))
  12304. (headline (nth 2 entry))
  12305. (v-c (or (and (eq window-system 'x)
  12306. (fboundp 'x-cut-buffer-or-selection-value)
  12307. (x-cut-buffer-or-selection-value))
  12308. (org-bound-and-true-p x-last-selected-text)
  12309. (org-bound-and-true-p x-last-selected-text-primary)
  12310. (and (> (length kill-ring) 0) (current-kill 0))))
  12311. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12312. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12313. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12314. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12315. ;; `initial' and `annotation' are bound in `remember'
  12316. (v-i (if (boundp 'initial) initial))
  12317. (v-a (if (and (boundp 'annotation) annotation)
  12318. (if (equal annotation "[[]]") "" annotation)
  12319. ""))
  12320. (v-A (if (and v-a
  12321. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12322. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12323. v-a))
  12324. (v-n user-full-name)
  12325. (org-startup-folded nil)
  12326. org-time-was-given org-end-time-was-given x
  12327. prompt completions char time pos default histvar)
  12328. (setq org-store-link-plist
  12329. (append (list :annotation v-a :initial v-i)
  12330. org-store-link-plist))
  12331. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12332. (erase-buffer)
  12333. (insert (substitute-command-keys
  12334. (format
  12335. "## Filing location: Select interactively, default, or last used:
  12336. ## %s to select file and header location interactively.
  12337. ## %s \"%s\" -> \"* %s\"
  12338. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12339. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12340. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12341. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12342. (abbreviate-file-name (or file org-default-notes-file))
  12343. (or headline "")
  12344. (or (car org-remember-previous-location) "???")
  12345. (or (cdr org-remember-previous-location) "???"))))
  12346. (insert tpl) (goto-char (point-min))
  12347. ;; Simple %-escapes
  12348. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12349. (when (and initial (equal (match-string 0) "%i"))
  12350. (save-match-data
  12351. (let* ((lead (buffer-substring
  12352. (point-at-bol) (match-beginning 0))))
  12353. (setq v-i (mapconcat 'identity
  12354. (org-split-string initial "\n")
  12355. (concat "\n" lead))))))
  12356. (replace-match
  12357. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12358. t t))
  12359. ;; %[] Insert contents of a file.
  12360. (goto-char (point-min))
  12361. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12362. (let ((start (match-beginning 0))
  12363. (end (match-end 0))
  12364. (filename (expand-file-name (match-string 1))))
  12365. (goto-char start)
  12366. (delete-region start end)
  12367. (condition-case error
  12368. (insert-file-contents filename)
  12369. (error (insert (format "%%![Couldn't insert %s: %s]"
  12370. filename error))))))
  12371. ;; %() embedded elisp
  12372. (goto-char (point-min))
  12373. (while (re-search-forward "%\\((.+)\\)" nil t)
  12374. (goto-char (match-beginning 0))
  12375. (let ((template-start (point)))
  12376. (forward-char 1)
  12377. (let ((result
  12378. (condition-case error
  12379. (eval (read (current-buffer)))
  12380. (error (format "%%![Error: %s]" error)))))
  12381. (delete-region template-start (point))
  12382. (insert result))))
  12383. ;; From the property list
  12384. (when plist-p
  12385. (goto-char (point-min))
  12386. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12387. (and (setq x (or (plist-get org-store-link-plist
  12388. (intern (match-string 1))) ""))
  12389. (replace-match x t t))))
  12390. ;; Turn on org-mode in the remember buffer, set local variables
  12391. (org-mode)
  12392. (org-set-local 'org-finish-function 'org-remember-finalize)
  12393. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12394. (org-set-local 'org-default-notes-file file))
  12395. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12396. (org-set-local 'org-remember-default-headline headline))
  12397. ;; Interactive template entries
  12398. (goto-char (point-min))
  12399. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12400. (setq char (if (match-end 3) (match-string 3))
  12401. prompt (if (match-end 2) (match-string 2)))
  12402. (goto-char (match-beginning 0))
  12403. (replace-match "")
  12404. (setq completions nil default nil)
  12405. (when prompt
  12406. (setq completions (org-split-string prompt "|")
  12407. prompt (pop completions)
  12408. default (car completions)
  12409. histvar (intern (concat
  12410. "org-remember-template-prompt-history::"
  12411. (or prompt "")))
  12412. completions (mapcar 'list completions)))
  12413. (cond
  12414. ((member char '("G" "g"))
  12415. (let* ((org-last-tags-completion-table
  12416. (org-global-tags-completion-table
  12417. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12418. (org-add-colon-after-tag-completion t)
  12419. (ins (completing-read
  12420. (if prompt (concat prompt ": ") "Tags: ")
  12421. 'org-tags-completion-function nil nil nil
  12422. 'org-tags-history)))
  12423. (setq ins (mapconcat 'identity
  12424. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12425. ":"))
  12426. (when (string-match "\\S-" ins)
  12427. (or (equal (char-before) ?:) (insert ":"))
  12428. (insert ins)
  12429. (or (equal (char-after) ?:) (insert ":")))))
  12430. (char
  12431. (setq org-time-was-given (equal (upcase char) char))
  12432. (setq time (org-read-date (equal (upcase char) "U") t nil
  12433. prompt))
  12434. (org-insert-time-stamp time org-time-was-given
  12435. (member char '("u" "U"))
  12436. nil nil (list org-end-time-was-given)))
  12437. (t
  12438. (insert (org-completing-read
  12439. (concat (if prompt prompt "Enter string")
  12440. (if default (concat " [" default "]"))
  12441. ": ")
  12442. completions nil nil nil histvar default)))))
  12443. (goto-char (point-min))
  12444. (if (re-search-forward "%\\?" nil t)
  12445. (replace-match "")
  12446. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12447. (org-mode)
  12448. (org-set-local 'org-finish-function 'org-remember-finalize))
  12449. (when (save-excursion
  12450. (goto-char (point-min))
  12451. (re-search-forward "%!" nil t))
  12452. (replace-match "")
  12453. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12454. (defun org-remember-finish-immediately ()
  12455. "File remember note immediately.
  12456. This should be run in `post-command-hook' and will remove itself
  12457. from that hook."
  12458. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12459. (when org-finish-function
  12460. (funcall org-finish-function)))
  12461. (defvar org-clock-marker) ; Defined below
  12462. (defun org-remember-finalize ()
  12463. "Finalize the remember process."
  12464. (unless (fboundp 'remember-finalize)
  12465. (defalias 'remember-finalize 'remember-buffer))
  12466. (when (and org-clock-marker
  12467. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12468. ;; FIXME: test this, this is w/o notetaking!
  12469. (let (org-log-note-clock-out) (org-clock-out)))
  12470. (when buffer-file-name
  12471. (save-buffer)
  12472. (setq buffer-file-name nil))
  12473. (remember-finalize))
  12474. ;;;###autoload
  12475. (defun org-remember (&optional goto org-force-remember-template-char)
  12476. "Call `remember'. If this is already a remember buffer, re-apply template.
  12477. If there is an active region, make sure remember uses it as initial content
  12478. of the remember buffer.
  12479. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12480. anything, just go to the file/headline where the selected template usually
  12481. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12482. note stored by remember.
  12483. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12484. associated with a template in `org-remember-templates'."
  12485. (interactive "P")
  12486. (cond
  12487. ((equal goto '(4)) (org-go-to-remember-target))
  12488. ((equal goto '(16)) (org-remember-goto-last-stored))
  12489. (t
  12490. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12491. (progn
  12492. (when (< (length org-remember-templates) 2)
  12493. (error "No other template available"))
  12494. (erase-buffer)
  12495. (let ((annotation (plist-get org-store-link-plist :annotation))
  12496. (initial (plist-get org-store-link-plist :initial)))
  12497. (org-remember-apply-template))
  12498. (message "Press C-c C-c to remember data"))
  12499. (if (org-region-active-p)
  12500. (remember (buffer-substring (point) (mark)))
  12501. (call-interactively 'remember))))))
  12502. (defun org-remember-goto-last-stored ()
  12503. "Go to the location where the last remember note was stored."
  12504. (interactive)
  12505. (bookmark-jump "org-remember-last-stored")
  12506. (message "This is the last note stored by remember"))
  12507. (defun org-go-to-remember-target (&optional template-key)
  12508. "Go to the target location of a remember template.
  12509. The user is queried for the template."
  12510. (interactive)
  12511. (let* ((entry (org-select-remember-template template-key))
  12512. (file (nth 1 entry))
  12513. (heading (nth 2 entry))
  12514. visiting)
  12515. (unless (and file (stringp file) (string-match "\\S-" file))
  12516. (setq file org-default-notes-file))
  12517. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12518. (setq heading org-remember-default-headline))
  12519. (setq visiting (org-find-base-buffer-visiting file))
  12520. (if (not visiting) (find-file-noselect file))
  12521. (switch-to-buffer (or visiting (get-file-buffer file)))
  12522. (widen)
  12523. (goto-char (point-min))
  12524. (if (re-search-forward
  12525. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12526. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12527. nil t)
  12528. (goto-char (match-beginning 0))
  12529. (error "Target headline not found: %s" heading))))
  12530. (defvar org-note-abort nil) ; dynamically scoped
  12531. ;;;###autoload
  12532. (defun org-remember-handler ()
  12533. "Store stuff from remember.el into an org file.
  12534. First prompts for an org file. If the user just presses return, the value
  12535. of `org-default-notes-file' is used.
  12536. Then the command offers the headings tree of the selected file in order to
  12537. file the text at a specific location.
  12538. You can either immediately press RET to get the note appended to the
  12539. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12540. find a better place. Then press RET or <left> or <right> in insert the note.
  12541. Key Cursor position Note gets inserted
  12542. -----------------------------------------------------------------------------
  12543. RET buffer-start as level 1 heading at end of file
  12544. RET on headline as sublevel of the heading at cursor
  12545. RET no heading at cursor position, level taken from context.
  12546. Or use prefix arg to specify level manually.
  12547. <left> on headline as same level, before current heading
  12548. <right> on headline as same level, after current heading
  12549. So the fastest way to store the note is to press RET RET to append it to
  12550. the default file. This way your current train of thought is not
  12551. interrupted, in accordance with the principles of remember.el.
  12552. You can also get the fast execution without prompting by using
  12553. C-u C-c C-c to exit the remember buffer. See also the variable
  12554. `org-remember-store-without-prompt'.
  12555. Before being stored away, the function ensures that the text has a
  12556. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12557. is constructed from the current date and some additional data.
  12558. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12559. also indented so that it starts in the same column as the headline
  12560. \(i.e. after the stars).
  12561. See also the variable `org-reverse-note-order'."
  12562. (goto-char (point-min))
  12563. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12564. (replace-match ""))
  12565. (goto-char (point-max))
  12566. (beginning-of-line 1)
  12567. (while (looking-at "[ \t]*$\\|##.*")
  12568. (delete-region (1- (point)) (point-max))
  12569. (beginning-of-line 1))
  12570. (catch 'quit
  12571. (if org-note-abort (throw 'quit nil))
  12572. (let* ((txt (buffer-substring (point-min) (point-max)))
  12573. (fastp (org-xor (equal current-prefix-arg '(4))
  12574. org-remember-store-without-prompt))
  12575. (file (cond
  12576. (fastp org-default-notes-file)
  12577. ((and (eq org-remember-interactive-interface 'refile)
  12578. org-refile-targets)
  12579. org-default-notes-file)
  12580. ((not (and (equal current-prefix-arg '(16))
  12581. org-remember-previous-location))
  12582. (org-get-org-file))))
  12583. (heading org-remember-default-headline)
  12584. (visiting (and file (org-find-base-buffer-visiting file)))
  12585. (org-startup-folded nil)
  12586. (org-startup-align-all-tables nil)
  12587. (org-goto-start-pos 1)
  12588. spos exitcmd level indent reversed)
  12589. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12590. (setq file (car org-remember-previous-location)
  12591. heading (cdr org-remember-previous-location)
  12592. fastp t))
  12593. (setq current-prefix-arg nil)
  12594. (if (string-match "[ \t\n]+\\'" txt)
  12595. (setq txt (replace-match "" t t txt)))
  12596. ;; Modify text so that it becomes a nice subtree which can be inserted
  12597. ;; into an org tree.
  12598. (let* ((lines (split-string txt "\n"))
  12599. first)
  12600. (setq first (car lines) lines (cdr lines))
  12601. (if (string-match "^\\*+ " first)
  12602. ;; Is already a headline
  12603. (setq indent nil)
  12604. ;; We need to add a headline: Use time and first buffer line
  12605. (setq lines (cons first lines)
  12606. first (concat "* " (current-time-string)
  12607. " (" (remember-buffer-desc) ")")
  12608. indent " "))
  12609. (if (and org-adapt-indentation indent)
  12610. (setq lines (mapcar
  12611. (lambda (x)
  12612. (if (string-match "\\S-" x)
  12613. (concat indent x) x))
  12614. lines)))
  12615. (setq txt (concat first "\n"
  12616. (mapconcat 'identity lines "\n"))))
  12617. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12618. (setq txt (replace-match "\n\n" t t txt))
  12619. (if (string-match "[ \t\n]*\\'" txt)
  12620. (setq txt (replace-match "\n" t t txt))))
  12621. ;; Put the modified text back into the remember buffer, for refile.
  12622. (erase-buffer)
  12623. (insert txt)
  12624. (goto-char (point-min))
  12625. (when (and (eq org-remember-interactive-interface 'refile)
  12626. (not fastp))
  12627. (org-refile nil (or visiting (find-file-noselect file)))
  12628. (throw 'quit t))
  12629. ;; Find the file
  12630. (if (not visiting) (find-file-noselect file))
  12631. (with-current-buffer (or visiting (get-file-buffer file))
  12632. (unless (org-mode-p)
  12633. (error "Target files for remember notes must be in Org-mode"))
  12634. (save-excursion
  12635. (save-restriction
  12636. (widen)
  12637. (and (goto-char (point-min))
  12638. (not (re-search-forward "^\\* " nil t))
  12639. (insert "\n* " (or heading "Notes") "\n"))
  12640. (setq reversed (org-notes-order-reversed-p))
  12641. ;; Find the default location
  12642. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12643. (goto-char (point-min))
  12644. (if (re-search-forward
  12645. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12646. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12647. nil t)
  12648. (setq org-goto-start-pos (match-beginning 0))
  12649. (when fastp
  12650. (goto-char (point-max))
  12651. (unless (bolp) (newline))
  12652. (insert "* " heading "\n")
  12653. (setq org-goto-start-pos (point-at-bol 0)))))
  12654. ;; Ask the User for a location, using the appropriate interface
  12655. (cond
  12656. (fastp (setq spos org-goto-start-pos
  12657. exitcmd 'return))
  12658. ((eq org-remember-interactive-interface 'outline)
  12659. (setq spos (org-get-location (current-buffer)
  12660. org-remember-help)
  12661. exitcmd (cdr spos)
  12662. spos (car spos)))
  12663. ((eq org-remember-interactive-interface 'outline-path-completion)
  12664. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12665. (org-refile-use-outline-path t))
  12666. (setq spos (org-refile-get-location "Heading: ")
  12667. exitcmd 'return
  12668. spos (nth 3 spos))))
  12669. (t (error "this should not hapen")))
  12670. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12671. ; not handle this note
  12672. (goto-char spos)
  12673. (cond ((org-on-heading-p t)
  12674. (org-back-to-heading t)
  12675. (setq level (funcall outline-level))
  12676. (cond
  12677. ((eq exitcmd 'return)
  12678. ;; sublevel of current
  12679. (setq org-remember-previous-location
  12680. (cons (abbreviate-file-name file)
  12681. (org-get-heading 'notags)))
  12682. (if reversed
  12683. (outline-next-heading)
  12684. (org-end-of-subtree t)
  12685. (if (not (bolp))
  12686. (if (looking-at "[ \t]*\n")
  12687. (beginning-of-line 2)
  12688. (end-of-line 1)
  12689. (insert "\n"))))
  12690. (bookmark-set "org-remember-last-stored")
  12691. (org-paste-subtree (org-get-legal-level level 1) txt))
  12692. ((eq exitcmd 'left)
  12693. ;; before current
  12694. (bookmark-set "org-remember-last-stored")
  12695. (org-paste-subtree level txt))
  12696. ((eq exitcmd 'right)
  12697. ;; after current
  12698. (org-end-of-subtree t)
  12699. (bookmark-set "org-remember-last-stored")
  12700. (org-paste-subtree level txt))
  12701. (t (error "This should not happen"))))
  12702. ((and (bobp) (not reversed))
  12703. ;; Put it at the end, one level below level 1
  12704. (save-restriction
  12705. (widen)
  12706. (goto-char (point-max))
  12707. (if (not (bolp)) (newline))
  12708. (bookmark-set "org-remember-last-stored")
  12709. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12710. ((and (bobp) reversed)
  12711. ;; Put it at the start, as level 1
  12712. (save-restriction
  12713. (widen)
  12714. (goto-char (point-min))
  12715. (re-search-forward "^\\*+ " nil t)
  12716. (beginning-of-line 1)
  12717. (bookmark-set "org-remember-last-stored")
  12718. (org-paste-subtree 1 txt)))
  12719. (t
  12720. ;; Put it right there, with automatic level determined by
  12721. ;; org-paste-subtree or from prefix arg
  12722. (bookmark-set "org-remember-last-stored")
  12723. (org-paste-subtree
  12724. (if (numberp current-prefix-arg) current-prefix-arg)
  12725. txt)))
  12726. (when remember-save-after-remembering
  12727. (save-buffer)
  12728. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12729. t) ;; return t to indicate that we took care of this note.
  12730. (defun org-get-org-file ()
  12731. "Read a filename, with default directory `org-directory'."
  12732. (let ((default (or org-default-notes-file remember-data-file)))
  12733. (read-file-name (format "File name [%s]: " default)
  12734. (file-name-as-directory org-directory)
  12735. default)))
  12736. (defun org-notes-order-reversed-p ()
  12737. "Check if the current file should receive notes in reversed order."
  12738. (cond
  12739. ((not org-reverse-note-order) nil)
  12740. ((eq t org-reverse-note-order) t)
  12741. ((not (listp org-reverse-note-order)) nil)
  12742. (t (catch 'exit
  12743. (let ((all org-reverse-note-order)
  12744. entry)
  12745. (while (setq entry (pop all))
  12746. (if (string-match (car entry) buffer-file-name)
  12747. (throw 'exit (cdr entry))))
  12748. nil)))))
  12749. ;;; Refiling
  12750. (defvar org-refile-target-table nil
  12751. "The list of refile targets, created by `org-refile'.")
  12752. (defvar org-agenda-new-buffers nil
  12753. "Buffers created to visit agenda files.")
  12754. (defun org-get-refile-targets (&optional default-buffer)
  12755. "Produce a table with refile targets."
  12756. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12757. targets txt re files f desc descre)
  12758. (with-current-buffer (or default-buffer (current-buffer))
  12759. (while (setq entry (pop entries))
  12760. (setq files (car entry) desc (cdr entry))
  12761. (cond
  12762. ((null files) (setq files (list (current-buffer))))
  12763. ((eq files 'org-agenda-files)
  12764. (setq files (org-agenda-files 'unrestricted)))
  12765. ((and (symbolp files) (fboundp files))
  12766. (setq files (funcall files)))
  12767. ((and (symbolp files) (boundp files))
  12768. (setq files (symbol-value files))))
  12769. (if (stringp files) (setq files (list files)))
  12770. (cond
  12771. ((eq (car desc) :tag)
  12772. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12773. ((eq (car desc) :todo)
  12774. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12775. ((eq (car desc) :regexp)
  12776. (setq descre (cdr desc)))
  12777. ((eq (car desc) :level)
  12778. (setq descre (concat "^\\*\\{" (number-to-string
  12779. (if org-odd-levels-only
  12780. (1- (* 2 (cdr desc)))
  12781. (cdr desc)))
  12782. "\\}[ \t]")))
  12783. ((eq (car desc) :maxlevel)
  12784. (setq descre (concat "^\\*\\{1," (number-to-string
  12785. (if org-odd-levels-only
  12786. (1- (* 2 (cdr desc)))
  12787. (cdr desc)))
  12788. "\\}[ \t]")))
  12789. (t (error "Bad refiling target description %s" desc)))
  12790. (while (setq f (pop files))
  12791. (save-excursion
  12792. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12793. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12794. (save-excursion
  12795. (save-restriction
  12796. (widen)
  12797. (goto-char (point-min))
  12798. (while (re-search-forward descre nil t)
  12799. (goto-char (point-at-bol))
  12800. (when (looking-at org-complex-heading-regexp)
  12801. (setq txt (match-string 4)
  12802. re (concat "^" (regexp-quote
  12803. (buffer-substring (match-beginning 1)
  12804. (match-end 4)))))
  12805. (if (match-end 5) (setq re (concat re "[ \t]+"
  12806. (regexp-quote
  12807. (match-string 5)))))
  12808. (setq re (concat re "[ \t]*$"))
  12809. (when org-refile-use-outline-path
  12810. (setq txt (mapconcat 'identity
  12811. (append
  12812. (if (eq org-refile-use-outline-path 'file)
  12813. (list (file-name-nondirectory
  12814. (buffer-file-name (buffer-base-buffer))))
  12815. (if (eq org-refile-use-outline-path 'full-file-path)
  12816. (list (buffer-file-name (buffer-base-buffer)))))
  12817. (org-get-outline-path)
  12818. (list txt))
  12819. "/")))
  12820. (push (list txt f re (point)) targets))
  12821. (goto-char (point-at-eol))))))))
  12822. (nreverse targets))))
  12823. (defun org-get-outline-path ()
  12824. "Return the outline path to the current entry, as a list."
  12825. (let (rtn)
  12826. (save-excursion
  12827. (while (org-up-heading-safe)
  12828. (when (looking-at org-complex-heading-regexp)
  12829. (push (org-match-string-no-properties 4) rtn)))
  12830. rtn)))
  12831. (defvar org-refile-history nil
  12832. "History for refiling operations.")
  12833. (defun org-refile (&optional goto default-buffer)
  12834. "Move the entry at point to another heading.
  12835. The list of target headings is compiled using the information in
  12836. `org-refile-targets', which see. This list is created upon first use, and
  12837. you can update it by calling this command with a double prefix (`C-u C-u').
  12838. FIXME: Can we find a better way of updating?
  12839. At the target location, the entry is filed as a subitem of the target heading.
  12840. Depending on `org-reverse-note-order', the new subitem will either be the
  12841. first of the last subitem.
  12842. With prefix arg GOTO, the command will only visit the target location,
  12843. not actually move anything.
  12844. With a double prefix `C-c C-c', go to the location where the last refiling
  12845. operation has put the subtree.
  12846. With a double prefix argument, the command can be used to jump to any
  12847. heading in the current buffer."
  12848. (interactive "P")
  12849. (let* ((cbuf (current-buffer))
  12850. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12851. pos it nbuf file re level reversed)
  12852. (if (equal goto '(16))
  12853. (org-refile-goto-last-stored)
  12854. (when (setq it (org-refile-get-location
  12855. (if goto "Goto: " "Refile to: ") default-buffer))
  12856. (setq file (nth 1 it)
  12857. re (nth 2 it)
  12858. pos (nth 3 it))
  12859. (setq nbuf (or (find-buffer-visiting file)
  12860. (find-file-noselect file)))
  12861. (if goto
  12862. (progn
  12863. (switch-to-buffer nbuf)
  12864. (goto-char pos)
  12865. (org-show-context 'org-goto))
  12866. (org-copy-special)
  12867. (save-excursion
  12868. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12869. (find-file-noselect file))))
  12870. (setq reversed (org-notes-order-reversed-p))
  12871. (save-excursion
  12872. (save-restriction
  12873. (widen)
  12874. (goto-char pos)
  12875. (looking-at outline-regexp)
  12876. (setq level (org-get-legal-level (funcall outline-level) 1))
  12877. (goto-char
  12878. (if reversed
  12879. (outline-next-heading)
  12880. (or (save-excursion (outline-get-next-sibling))
  12881. (org-end-of-subtree t t)
  12882. (point-max))))
  12883. (bookmark-set "org-refile-last-stored")
  12884. (org-paste-subtree level))))
  12885. (org-cut-special)
  12886. (message "Entry refiled to \"%s\"" (car it)))))))
  12887. (defun org-refile-goto-last-stored ()
  12888. "Go to the location where the last refile was stored."
  12889. (interactive)
  12890. (bookmark-jump "org-refile-last-stored")
  12891. (message "This is the location of the last refile"))
  12892. (defun org-refile-get-location (&optional prompt default-buffer)
  12893. "Prompt the user for a refile location, using PROMPT."
  12894. (let ((org-refile-targets org-refile-targets)
  12895. (org-refile-use-outline-path org-refile-use-outline-path))
  12896. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12897. (unless org-refile-target-table
  12898. (error "No refile targets"))
  12899. (let* ((cbuf (current-buffer))
  12900. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12901. (fname (and filename (file-truename filename)))
  12902. (tbl (mapcar
  12903. (lambda (x)
  12904. (if (not (equal fname (file-truename (nth 1 x))))
  12905. (cons (concat (car x) " (" (file-name-nondirectory
  12906. (nth 1 x)) ")")
  12907. (cdr x))
  12908. x))
  12909. org-refile-target-table))
  12910. (completion-ignore-case t))
  12911. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12912. tbl)))
  12913. ;;;; Dynamic blocks
  12914. (defun org-find-dblock (name)
  12915. "Find the first dynamic block with name NAME in the buffer.
  12916. If not found, stay at current position and return nil."
  12917. (let (pos)
  12918. (save-excursion
  12919. (goto-char (point-min))
  12920. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12921. nil t)
  12922. (match-beginning 0))))
  12923. (if pos (goto-char pos))
  12924. pos))
  12925. (defconst org-dblock-start-re
  12926. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12927. "Matches the startline of a dynamic block, with parameters.")
  12928. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12929. "Matches the end of a dyhamic block.")
  12930. (defun org-create-dblock (plist)
  12931. "Create a dynamic block section, with parameters taken from PLIST.
  12932. PLIST must containe a :name entry which is used as name of the block."
  12933. (unless (bolp) (newline))
  12934. (let ((name (plist-get plist :name)))
  12935. (insert "#+BEGIN: " name)
  12936. (while plist
  12937. (if (eq (car plist) :name)
  12938. (setq plist (cddr plist))
  12939. (insert " " (prin1-to-string (pop plist)))))
  12940. (insert "\n\n#+END:\n")
  12941. (beginning-of-line -2)))
  12942. (defun org-prepare-dblock ()
  12943. "Prepare dynamic block for refresh.
  12944. This empties the block, puts the cursor at the insert position and returns
  12945. the property list including an extra property :name with the block name."
  12946. (unless (looking-at org-dblock-start-re)
  12947. (error "Not at a dynamic block"))
  12948. (let* ((begdel (1+ (match-end 0)))
  12949. (name (org-no-properties (match-string 1)))
  12950. (params (append (list :name name)
  12951. (read (concat "(" (match-string 3) ")")))))
  12952. (unless (re-search-forward org-dblock-end-re nil t)
  12953. (error "Dynamic block not terminated"))
  12954. (delete-region begdel (match-beginning 0))
  12955. (goto-char begdel)
  12956. (open-line 1)
  12957. params))
  12958. (defun org-map-dblocks (&optional command)
  12959. "Apply COMMAND to all dynamic blocks in the current buffer.
  12960. If COMMAND is not given, use `org-update-dblock'."
  12961. (let ((cmd (or command 'org-update-dblock))
  12962. pos)
  12963. (save-excursion
  12964. (goto-char (point-min))
  12965. (while (re-search-forward org-dblock-start-re nil t)
  12966. (goto-char (setq pos (match-beginning 0)))
  12967. (condition-case nil
  12968. (funcall cmd)
  12969. (error (message "Error during update of dynamic block")))
  12970. (goto-char pos)
  12971. (unless (re-search-forward org-dblock-end-re nil t)
  12972. (error "Dynamic block not terminated"))))))
  12973. (defun org-dblock-update (&optional arg)
  12974. "User command for updating dynamic blocks.
  12975. Update the dynamic block at point. With prefix ARG, update all dynamic
  12976. blocks in the buffer."
  12977. (interactive "P")
  12978. (if arg
  12979. (org-update-all-dblocks)
  12980. (or (looking-at org-dblock-start-re)
  12981. (org-beginning-of-dblock))
  12982. (org-update-dblock)))
  12983. (defun org-update-dblock ()
  12984. "Update the dynamic block at point
  12985. This means to empty the block, parse for parameters and then call
  12986. the correct writing function."
  12987. (save-window-excursion
  12988. (let* ((pos (point))
  12989. (line (org-current-line))
  12990. (params (org-prepare-dblock))
  12991. (name (plist-get params :name))
  12992. (cmd (intern (concat "org-dblock-write:" name))))
  12993. (message "Updating dynamic block `%s' at line %d..." name line)
  12994. (funcall cmd params)
  12995. (message "Updating dynamic block `%s' at line %d...done" name line)
  12996. (goto-char pos))))
  12997. (defun org-beginning-of-dblock ()
  12998. "Find the beginning of the dynamic block at point.
  12999. Error if there is no scuh block at point."
  13000. (let ((pos (point))
  13001. beg)
  13002. (end-of-line 1)
  13003. (if (and (re-search-backward org-dblock-start-re nil t)
  13004. (setq beg (match-beginning 0))
  13005. (re-search-forward org-dblock-end-re nil t)
  13006. (> (match-end 0) pos))
  13007. (goto-char beg)
  13008. (goto-char pos)
  13009. (error "Not in a dynamic block"))))
  13010. (defun org-update-all-dblocks ()
  13011. "Update all dynamic blocks in the buffer.
  13012. This function can be used in a hook."
  13013. (when (org-mode-p)
  13014. (org-map-dblocks 'org-update-dblock)))
  13015. ;;;; Completion
  13016. (defconst org-additional-option-like-keywords
  13017. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13018. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13019. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13020. (defun org-complete (&optional arg)
  13021. "Perform completion on word at point.
  13022. At the beginning of a headline, this completes TODO keywords as given in
  13023. `org-todo-keywords'.
  13024. If the current word is preceded by a backslash, completes the TeX symbols
  13025. that are supported for HTML support.
  13026. If the current word is preceded by \"#+\", completes special words for
  13027. setting file options.
  13028. In the line after \"#+STARTUP:, complete valid keywords.\"
  13029. At all other locations, this simply calls the value of
  13030. `org-completion-fallback-command'."
  13031. (interactive "P")
  13032. (org-without-partial-completion
  13033. (catch 'exit
  13034. (let* ((end (point))
  13035. (beg1 (save-excursion
  13036. (skip-chars-backward (org-re "[:alnum:]_@"))
  13037. (point)))
  13038. (beg (save-excursion
  13039. (skip-chars-backward "a-zA-Z0-9_:$")
  13040. (point)))
  13041. (confirm (lambda (x) (stringp (car x))))
  13042. (searchhead (equal (char-before beg) ?*))
  13043. (tag (and (equal (char-before beg1) ?:)
  13044. (equal (char-after (point-at-bol)) ?*)))
  13045. (prop (and (equal (char-before beg1) ?:)
  13046. (not (equal (char-after (point-at-bol)) ?*))))
  13047. (texp (equal (char-before beg) ?\\))
  13048. (link (equal (char-before beg) ?\[))
  13049. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13050. beg)
  13051. "#+"))
  13052. (startup (string-match "^#\\+STARTUP:.*"
  13053. (buffer-substring (point-at-bol) (point))))
  13054. (completion-ignore-case opt)
  13055. (type nil)
  13056. (tbl nil)
  13057. (table (cond
  13058. (opt
  13059. (setq type :opt)
  13060. (append
  13061. (mapcar
  13062. (lambda (x)
  13063. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13064. (cons (match-string 2 x) (match-string 1 x)))
  13065. (org-split-string (org-get-current-options) "\n"))
  13066. (mapcar 'list org-additional-option-like-keywords)))
  13067. (startup
  13068. (setq type :startup)
  13069. org-startup-options)
  13070. (link (append org-link-abbrev-alist-local
  13071. org-link-abbrev-alist))
  13072. (texp
  13073. (setq type :tex)
  13074. org-html-entities)
  13075. ((string-match "\\`\\*+[ \t]+\\'"
  13076. (buffer-substring (point-at-bol) beg))
  13077. (setq type :todo)
  13078. (mapcar 'list org-todo-keywords-1))
  13079. (searchhead
  13080. (setq type :searchhead)
  13081. (save-excursion
  13082. (goto-char (point-min))
  13083. (while (re-search-forward org-todo-line-regexp nil t)
  13084. (push (list
  13085. (org-make-org-heading-search-string
  13086. (match-string 3) t))
  13087. tbl)))
  13088. tbl)
  13089. (tag (setq type :tag beg beg1)
  13090. (or org-tag-alist (org-get-buffer-tags)))
  13091. (prop (setq type :prop beg beg1)
  13092. (mapcar 'list (org-buffer-property-keys nil t t)))
  13093. (t (progn
  13094. (call-interactively org-completion-fallback-command)
  13095. (throw 'exit nil)))))
  13096. (pattern (buffer-substring-no-properties beg end))
  13097. (completion (try-completion pattern table confirm)))
  13098. (cond ((eq completion t)
  13099. (if (not (assoc (upcase pattern) table))
  13100. (message "Already complete")
  13101. (if (equal type :opt)
  13102. (insert (substring (cdr (assoc (upcase pattern) table))
  13103. (length pattern)))
  13104. (if (memq type '(:tag :prop)) (insert ":")))))
  13105. ((null completion)
  13106. (message "Can't find completion for \"%s\"" pattern)
  13107. (ding))
  13108. ((not (string= pattern completion))
  13109. (delete-region beg end)
  13110. (if (string-match " +$" completion)
  13111. (setq completion (replace-match "" t t completion)))
  13112. (insert completion)
  13113. (if (get-buffer-window "*Completions*")
  13114. (delete-window (get-buffer-window "*Completions*")))
  13115. (if (assoc completion table)
  13116. (if (eq type :todo) (insert " ")
  13117. (if (memq type '(:tag :prop)) (insert ":"))))
  13118. (if (and (equal type :opt) (assoc completion table))
  13119. (message "%s" (substitute-command-keys
  13120. "Press \\[org-complete] again to insert example settings"))))
  13121. (t
  13122. (message "Making completion list...")
  13123. (let ((list (sort (all-completions pattern table confirm)
  13124. 'string<)))
  13125. (with-output-to-temp-buffer "*Completions*"
  13126. (condition-case nil
  13127. ;; Protection needed for XEmacs and emacs 21
  13128. (display-completion-list list pattern)
  13129. (error (display-completion-list list)))))
  13130. (message "Making completion list...%s" "done")))))))
  13131. ;;;; TODO, DEADLINE, Comments
  13132. (defun org-toggle-comment ()
  13133. "Change the COMMENT state of an entry."
  13134. (interactive)
  13135. (save-excursion
  13136. (org-back-to-heading)
  13137. (let (case-fold-search)
  13138. (if (looking-at (concat outline-regexp
  13139. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13140. (replace-match "" t t nil 1)
  13141. (if (looking-at outline-regexp)
  13142. (progn
  13143. (goto-char (match-end 0))
  13144. (insert org-comment-string " ")))))))
  13145. (defvar org-last-todo-state-is-todo nil
  13146. "This is non-nil when the last TODO state change led to a TODO state.
  13147. If the last change removed the TODO tag or switched to DONE, then
  13148. this is nil.")
  13149. (defvar org-setting-tags nil) ; dynamically skiped
  13150. ;; FIXME: better place
  13151. (defun org-property-or-variable-value (var &optional inherit)
  13152. "Check if there is a property fixing the value of VAR.
  13153. If yes, return this value. If not, return the current value of the variable."
  13154. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13155. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13156. (read prop)
  13157. (symbol-value var))))
  13158. (defun org-parse-local-options (string var)
  13159. "Parse STRING for startup setting relevant for variable VAR."
  13160. (let ((rtn (symbol-value var))
  13161. e opts)
  13162. (save-match-data
  13163. (if (or (not string) (not (string-match "\\S-" string)))
  13164. rtn
  13165. (setq opts (delq nil (mapcar (lambda (x)
  13166. (setq e (assoc x org-startup-options))
  13167. (if (eq (nth 1 e) var) e nil))
  13168. (org-split-string string "[ \t]+"))))
  13169. (if (not opts)
  13170. rtn
  13171. (setq rtn nil)
  13172. (while (setq e (pop opts))
  13173. (if (not (nth 3 e))
  13174. (setq rtn (nth 2 e))
  13175. (if (not (listp rtn)) (setq rtn nil))
  13176. (push (nth 2 e) rtn)))
  13177. rtn)))))
  13178. (defvar org-blocker-hook nil
  13179. "Hook for functions that are allowed to block a state change.
  13180. Each function gets as its single argument a property list, see
  13181. `org-trigger-hook' for more information about this list.
  13182. If any of the functions in this hook returns nil, the state change
  13183. is blocked.")
  13184. (defvar org-trigger-hook nil
  13185. "Hook for functions that are triggered by a state change.
  13186. Each function gets as its single argument a property list with at least
  13187. the following elements:
  13188. (:type type-of-change :position pos-at-entry-start
  13189. :from old-state :to new-state)
  13190. Depending on the type, more properties may be present.
  13191. This mechanism is currently implemented for:
  13192. TODO state changes
  13193. ------------------
  13194. :type todo-state-change
  13195. :from previous state (keyword as a string), or nil
  13196. :to new state (keyword as a string), or nil")
  13197. (defun org-todo (&optional arg)
  13198. "Change the TODO state of an item.
  13199. The state of an item is given by a keyword at the start of the heading,
  13200. like
  13201. *** TODO Write paper
  13202. *** DONE Call mom
  13203. The different keywords are specified in the variable `org-todo-keywords'.
  13204. By default the available states are \"TODO\" and \"DONE\".
  13205. So for this example: when the item starts with TODO, it is changed to DONE.
  13206. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13207. DONE are present, add TODO at the beginning of the heading.
  13208. With C-u prefix arg, use completion to determine the new state.
  13209. With numeric prefix arg, switch to that state.
  13210. For calling through lisp, arg is also interpreted in the following way:
  13211. 'none -> empty state
  13212. \"\"(empty string) -> switch to empty state
  13213. 'done -> switch to DONE
  13214. 'nextset -> switch to the next set of keywords
  13215. 'previousset -> switch to the previous set of keywords
  13216. \"WAITING\" -> switch to the specified keyword, but only if it
  13217. really is a member of `org-todo-keywords'."
  13218. (interactive "P")
  13219. (save-excursion
  13220. (catch 'exit
  13221. (org-back-to-heading)
  13222. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13223. (or (looking-at (concat " +" org-todo-regexp " *"))
  13224. (looking-at " *"))
  13225. (let* ((match-data (match-data))
  13226. (startpos (point-at-bol))
  13227. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13228. (org-log-done org-log-done)
  13229. (org-log-repeat org-log-repeat)
  13230. (org-todo-log-states org-todo-log-states)
  13231. (this (match-string 1))
  13232. (hl-pos (match-beginning 0))
  13233. (head (org-get-todo-sequence-head this))
  13234. (ass (assoc head org-todo-kwd-alist))
  13235. (interpret (nth 1 ass))
  13236. (done-word (nth 3 ass))
  13237. (final-done-word (nth 4 ass))
  13238. (last-state (or this ""))
  13239. (completion-ignore-case t)
  13240. (member (member this org-todo-keywords-1))
  13241. (tail (cdr member))
  13242. (state (cond
  13243. ((and org-todo-key-trigger
  13244. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13245. (and (not arg) org-use-fast-todo-selection
  13246. (not (eq org-use-fast-todo-selection 'prefix)))))
  13247. ;; Use fast selection
  13248. (org-fast-todo-selection))
  13249. ((and (equal arg '(4))
  13250. (or (not org-use-fast-todo-selection)
  13251. (not org-todo-key-trigger)))
  13252. ;; Read a state with completion
  13253. (completing-read "State: " (mapcar (lambda(x) (list x))
  13254. org-todo-keywords-1)
  13255. nil t))
  13256. ((eq arg 'right)
  13257. (if this
  13258. (if tail (car tail) nil)
  13259. (car org-todo-keywords-1)))
  13260. ((eq arg 'left)
  13261. (if (equal member org-todo-keywords-1)
  13262. nil
  13263. (if this
  13264. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13265. org-todo-keywords-1)
  13266. (org-last org-todo-keywords-1))))
  13267. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13268. (setq arg nil))) ; hack to fall back to cycling
  13269. (arg
  13270. ;; user or caller requests a specific state
  13271. (cond
  13272. ((equal arg "") nil)
  13273. ((eq arg 'none) nil)
  13274. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13275. ((eq arg 'nextset)
  13276. (or (car (cdr (member head org-todo-heads)))
  13277. (car org-todo-heads)))
  13278. ((eq arg 'previousset)
  13279. (let ((org-todo-heads (reverse org-todo-heads)))
  13280. (or (car (cdr (member head org-todo-heads)))
  13281. (car org-todo-heads))))
  13282. ((car (member arg org-todo-keywords-1)))
  13283. ((nth (1- (prefix-numeric-value arg))
  13284. org-todo-keywords-1))))
  13285. ((null member) (or head (car org-todo-keywords-1)))
  13286. ((equal this final-done-word) nil) ;; -> make empty
  13287. ((null tail) nil) ;; -> first entry
  13288. ((eq interpret 'sequence)
  13289. (car tail))
  13290. ((memq interpret '(type priority))
  13291. (if (eq this-command last-command)
  13292. (car tail)
  13293. (if (> (length tail) 0)
  13294. (or done-word (car org-done-keywords))
  13295. nil)))
  13296. (t nil)))
  13297. (next (if state (concat " " state " ") " "))
  13298. (change-plist (list :type 'todo-state-change :from this :to state
  13299. :position startpos))
  13300. dolog now-done-p)
  13301. (when org-blocker-hook
  13302. (unless (save-excursion
  13303. (save-match-data
  13304. (run-hook-with-args-until-failure
  13305. 'org-blocker-hook change-plist)))
  13306. (if (interactive-p)
  13307. (error "TODO state change from %s to %s blocked" this state)
  13308. ;; fail silently
  13309. (message "TODO state change from %s to %s blocked" this state)
  13310. (throw 'exit nil))))
  13311. (store-match-data match-data)
  13312. (replace-match next t t)
  13313. (unless (pos-visible-in-window-p hl-pos)
  13314. (message "TODO state changed to %s" (org-trim next)))
  13315. (unless head
  13316. (setq head (org-get-todo-sequence-head state)
  13317. ass (assoc head org-todo-kwd-alist)
  13318. interpret (nth 1 ass)
  13319. done-word (nth 3 ass)
  13320. final-done-word (nth 4 ass)))
  13321. (when (memq arg '(nextset previousset))
  13322. (message "Keyword-Set %d/%d: %s"
  13323. (- (length org-todo-sets) -1
  13324. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13325. (length org-todo-sets)
  13326. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13327. (setq org-last-todo-state-is-todo
  13328. (not (member state org-done-keywords)))
  13329. (setq now-done-p (and (member state org-done-keywords)
  13330. (not (member this org-done-keywords))))
  13331. (and logging (org-local-logging logging))
  13332. (when (and (or org-todo-log-states org-log-done)
  13333. (not (memq arg '(nextset previousset))))
  13334. ;; we need to look at recording a time and note
  13335. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13336. (nth 2 (assoc this org-todo-log-states))))
  13337. (when (and state
  13338. (member state org-not-done-keywords)
  13339. (not (member this org-not-done-keywords)))
  13340. ;; This is now a todo state and was not one before
  13341. ;; If there was a CLOSED time stamp, get rid of it.
  13342. (org-add-planning-info nil nil 'closed))
  13343. (when (and now-done-p org-log-done)
  13344. ;; It is now done, and it was not done before
  13345. (org-add-planning-info 'closed (org-current-time))
  13346. (if (and (not dolog) (eq 'note org-log-done))
  13347. (org-add-log-maybe 'done state 'findpos 'note)))
  13348. (when (and state dolog)
  13349. ;; This is a non-nil state, and we need to log it
  13350. (org-add-log-maybe 'state state 'findpos dolog)))
  13351. ;; Fixup tag positioning
  13352. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13353. (run-hooks 'org-after-todo-state-change-hook)
  13354. (if (and arg (not (member state org-done-keywords)))
  13355. (setq head (org-get-todo-sequence-head state)))
  13356. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13357. ;; Do we need to trigger a repeat?
  13358. (when now-done-p (org-auto-repeat-maybe state))
  13359. ;; Fixup cursor location if close to the keyword
  13360. (if (and (outline-on-heading-p)
  13361. (not (bolp))
  13362. (save-excursion (beginning-of-line 1)
  13363. (looking-at org-todo-line-regexp))
  13364. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13365. (progn
  13366. (goto-char (or (match-end 2) (match-end 1)))
  13367. (just-one-space)))
  13368. (when org-trigger-hook
  13369. (save-excursion
  13370. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13371. (defun org-local-logging (value)
  13372. "Get logging settings from a property VALUE."
  13373. (let* (words w a)
  13374. ;; directly set the variables, they are already local.
  13375. (setq org-log-done nil
  13376. org-log-repeat nil
  13377. org-todo-log-states nil)
  13378. (setq words (org-split-string value))
  13379. (while (setq w (pop words))
  13380. (cond
  13381. ((setq a (assoc w org-startup-options))
  13382. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13383. (set (nth 1 a) (nth 2 a))))
  13384. ((setq a (org-extract-log-state-settings w))
  13385. (and (member (car a) org-todo-keywords-1)
  13386. (push a org-todo-log-states)))))))
  13387. (defun org-get-todo-sequence-head (kwd)
  13388. "Return the head of the TODO sequence to which KWD belongs.
  13389. If KWD is not set, check if there is a text property remembering the
  13390. right sequence."
  13391. (let (p)
  13392. (cond
  13393. ((not kwd)
  13394. (or (get-text-property (point-at-bol) 'org-todo-head)
  13395. (progn
  13396. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13397. nil (point-at-eol)))
  13398. (get-text-property p 'org-todo-head))))
  13399. ((not (member kwd org-todo-keywords-1))
  13400. (car org-todo-keywords-1))
  13401. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13402. (defun org-fast-todo-selection ()
  13403. "Fast TODO keyword selection with single keys.
  13404. Returns the new TODO keyword, or nil if no state change should occur."
  13405. (let* ((fulltable org-todo-key-alist)
  13406. (done-keywords org-done-keywords) ;; needed for the faces.
  13407. (maxlen (apply 'max (mapcar
  13408. (lambda (x)
  13409. (if (stringp (car x)) (string-width (car x)) 0))
  13410. fulltable)))
  13411. (expert nil)
  13412. (fwidth (+ maxlen 3 1 3))
  13413. (ncol (/ (- (window-width) 4) fwidth))
  13414. tg cnt e c tbl
  13415. groups ingroup)
  13416. (save-window-excursion
  13417. (if expert
  13418. (set-buffer (get-buffer-create " *Org todo*"))
  13419. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13420. (erase-buffer)
  13421. (org-set-local 'org-done-keywords done-keywords)
  13422. (setq tbl fulltable cnt 0)
  13423. (while (setq e (pop tbl))
  13424. (cond
  13425. ((equal e '(:startgroup))
  13426. (push '() groups) (setq ingroup t)
  13427. (when (not (= cnt 0))
  13428. (setq cnt 0)
  13429. (insert "\n"))
  13430. (insert "{ "))
  13431. ((equal e '(:endgroup))
  13432. (setq ingroup nil cnt 0)
  13433. (insert "}\n"))
  13434. (t
  13435. (setq tg (car e) c (cdr e))
  13436. (if ingroup (push tg (car groups)))
  13437. (setq tg (org-add-props tg nil 'face
  13438. (org-get-todo-face tg)))
  13439. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13440. (insert "[" c "] " tg (make-string
  13441. (- fwidth 4 (length tg)) ?\ ))
  13442. (when (= (setq cnt (1+ cnt)) ncol)
  13443. (insert "\n")
  13444. (if ingroup (insert " "))
  13445. (setq cnt 0)))))
  13446. (insert "\n")
  13447. (goto-char (point-min))
  13448. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13449. (fit-window-to-buffer))
  13450. (message "[a-z..]:Set [SPC]:clear")
  13451. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13452. (cond
  13453. ((or (= c ?\C-g)
  13454. (and (= c ?q) (not (rassoc c fulltable))))
  13455. (setq quit-flag t))
  13456. ((= c ?\ ) nil)
  13457. ((setq e (rassoc c fulltable) tg (car e))
  13458. tg)
  13459. (t (setq quit-flag t))))))
  13460. (defun org-get-repeat ()
  13461. "Check if tere is a deadline/schedule with repeater in this entry."
  13462. (save-match-data
  13463. (save-excursion
  13464. (org-back-to-heading t)
  13465. (if (re-search-forward
  13466. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13467. (match-string 1)))))
  13468. (defvar org-last-changed-timestamp)
  13469. (defvar org-log-post-message)
  13470. (defvar org-log-note-purpose)
  13471. (defun org-auto-repeat-maybe (done-word)
  13472. "Check if the current headline contains a repeated deadline/schedule.
  13473. If yes, set TODO state back to what it was and change the base date
  13474. of repeating deadline/scheduled time stamps to new date.
  13475. This function should be run in the `org-after-todo-state-change-hook'."
  13476. ;; last-state is dynamically scoped into this function
  13477. (let* ((repeat (org-get-repeat))
  13478. (aa (assoc last-state org-todo-kwd-alist))
  13479. (interpret (nth 1 aa))
  13480. (head (nth 2 aa))
  13481. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13482. (msg "Entry repeats: ")
  13483. (org-log-done nil)
  13484. re type n what ts)
  13485. (when repeat
  13486. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13487. (org-todo (if (eq interpret 'type) last-state head))
  13488. (when (and org-log-repeat
  13489. (or (not (memq 'org-add-log-note
  13490. (default-value 'post-command-hook)))
  13491. (eq org-log-note-purpose 'done)))
  13492. ;; Make sure a note is taken;
  13493. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13494. 'findpos org-log-repeat))
  13495. (org-back-to-heading t)
  13496. (org-add-planning-info nil nil 'closed)
  13497. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13498. org-deadline-time-regexp "\\)\\|\\("
  13499. org-ts-regexp "\\)"))
  13500. (while (re-search-forward
  13501. re (save-excursion (outline-next-heading) (point)) t)
  13502. (setq type (if (match-end 1) org-scheduled-string
  13503. (if (match-end 3) org-deadline-string "Plain:"))
  13504. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13505. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13506. (setq n (string-to-number (match-string 1 ts))
  13507. what (match-string 2 ts))
  13508. (if (equal what "w") (setq n (* n 7) what "d"))
  13509. (org-timestamp-change n (cdr (assoc what whata)))
  13510. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13511. (setq org-log-post-message msg)
  13512. (message "%s" msg))))
  13513. (defun org-show-todo-tree (arg)
  13514. "Make a compact tree which shows all headlines marked with TODO.
  13515. The tree will show the lines where the regexp matches, and all higher
  13516. headlines above the match.
  13517. With \\[universal-argument] prefix, also show the DONE entries.
  13518. With a numeric prefix N, construct a sparse tree for the Nth element
  13519. of `org-todo-keywords-1'."
  13520. (interactive "P")
  13521. (let ((case-fold-search nil)
  13522. (kwd-re
  13523. (cond ((null arg) org-not-done-regexp)
  13524. ((equal arg '(4))
  13525. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13526. (mapcar 'list org-todo-keywords-1))))
  13527. (concat "\\("
  13528. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13529. "\\)\\>")))
  13530. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13531. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13532. org-todo-keywords-1)))
  13533. (t (error "Invalid prefix argument: %s" arg)))))
  13534. (message "%d TODO entries found"
  13535. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13536. (defun org-deadline (&optional remove)
  13537. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13538. With argument REMOVE, remove any deadline from the item."
  13539. (interactive "P")
  13540. (if remove
  13541. (progn
  13542. (org-remove-timestamp-with-keyword org-deadline-string)
  13543. (message "Item no longer has a deadline."))
  13544. (org-add-planning-info 'deadline nil 'closed)))
  13545. (defun org-schedule (&optional remove)
  13546. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13547. With argument REMOVE, remove any scheduling date from the item."
  13548. (interactive "P")
  13549. (if remove
  13550. (progn
  13551. (org-remove-timestamp-with-keyword org-scheduled-string)
  13552. (message "Item is no longer scheduled."))
  13553. (org-add-planning-info 'scheduled nil 'closed)))
  13554. (defun org-remove-timestamp-with-keyword (keyword)
  13555. "Remove all time stamps with KEYWORD in the current entry."
  13556. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13557. beg)
  13558. (save-excursion
  13559. (org-back-to-heading t)
  13560. (setq beg (point))
  13561. (org-end-of-subtree t t)
  13562. (while (re-search-backward re beg t)
  13563. (replace-match "")
  13564. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13565. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13566. (defun org-add-planning-info (what &optional time &rest remove)
  13567. "Insert new timestamp with keyword in the line directly after the headline.
  13568. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13569. If non is given, the user is prompted for a date.
  13570. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13571. be removed."
  13572. (interactive)
  13573. (let (org-time-was-given org-end-time-was-given)
  13574. (when what (setq time (or time (org-read-date nil 'to-time))))
  13575. (when (and org-insert-labeled-timestamps-at-point
  13576. (member what '(scheduled deadline)))
  13577. (insert
  13578. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13579. (org-insert-time-stamp time org-time-was-given
  13580. nil nil nil (list org-end-time-was-given))
  13581. (setq what nil))
  13582. (save-excursion
  13583. (save-restriction
  13584. (let (col list elt ts buffer-invisibility-spec)
  13585. (org-back-to-heading t)
  13586. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13587. (goto-char (match-end 1))
  13588. (setq col (current-column))
  13589. (goto-char (match-end 0))
  13590. (if (eobp) (insert "\n") (forward-char 1))
  13591. (if (and (not (looking-at outline-regexp))
  13592. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13593. "[^\r\n]*"))
  13594. (not (equal (match-string 1) org-clock-string)))
  13595. (narrow-to-region (match-beginning 0) (match-end 0))
  13596. (insert-before-markers "\n")
  13597. (backward-char 1)
  13598. (narrow-to-region (point) (point))
  13599. (indent-to-column col))
  13600. ;; Check if we have to remove something.
  13601. (setq list (cons what remove))
  13602. (while list
  13603. (setq elt (pop list))
  13604. (goto-char (point-min))
  13605. (when (or (and (eq elt 'scheduled)
  13606. (re-search-forward org-scheduled-time-regexp nil t))
  13607. (and (eq elt 'deadline)
  13608. (re-search-forward org-deadline-time-regexp nil t))
  13609. (and (eq elt 'closed)
  13610. (re-search-forward org-closed-time-regexp nil t)))
  13611. (replace-match "")
  13612. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13613. (if (looking-at " +") (replace-match ""))))
  13614. (goto-char (point-max))
  13615. (when what
  13616. (insert
  13617. (if (not (equal (char-before) ?\ )) " " "")
  13618. (cond ((eq what 'scheduled) org-scheduled-string)
  13619. ((eq what 'deadline) org-deadline-string)
  13620. ((eq what 'closed) org-closed-string))
  13621. " ")
  13622. (setq ts (org-insert-time-stamp
  13623. time
  13624. (or org-time-was-given
  13625. (and (eq what 'closed) org-log-done-with-time))
  13626. (eq what 'closed)
  13627. nil nil (list org-end-time-was-given)))
  13628. (end-of-line 1))
  13629. (goto-char (point-min))
  13630. (widen)
  13631. (if (looking-at "[ \t]+\r?\n")
  13632. (replace-match ""))
  13633. ts)))))
  13634. (defvar org-log-note-marker (make-marker))
  13635. (defvar org-log-note-purpose nil)
  13636. (defvar org-log-note-state nil)
  13637. (defvar org-log-note-how nil)
  13638. (defvar org-log-note-window-configuration nil)
  13639. (defvar org-log-note-return-to (make-marker))
  13640. (defvar org-log-post-message nil
  13641. "Message to be displayed after a log note has been stored.
  13642. The auto-repeater uses this.")
  13643. (defun org-add-log-maybe (&optional purpose state findpos how)
  13644. "Set up the post command hook to take a note.
  13645. If this is about to TODO state change, the new state is expected in STATE.
  13646. When FINDPOS is non-nil, find the correct position for the note in
  13647. the current entry. If not, assume that it can be inserted at point."
  13648. (save-excursion
  13649. (when findpos
  13650. (org-back-to-heading t)
  13651. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13652. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13653. "[^\r\n]*\\)?"))
  13654. (goto-char (match-end 0))
  13655. (unless org-log-states-order-reversed
  13656. (and (= (char-after) ?\n) (forward-char 1))
  13657. (org-skip-over-state-notes)
  13658. (skip-chars-backward " \t\n\r")))
  13659. (move-marker org-log-note-marker (point))
  13660. (setq org-log-note-purpose purpose
  13661. org-log-note-state state
  13662. org-log-note-how how)
  13663. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13664. (defun org-skip-over-state-notes ()
  13665. "Skip past the list of State notes in an entry."
  13666. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13667. (while (looking-at "[ \t]*- State")
  13668. (condition-case nil
  13669. (org-next-item)
  13670. (error (org-end-of-item)))))
  13671. (defun org-add-log-note (&optional purpose)
  13672. "Pop up a window for taking a note, and add this note later at point."
  13673. (remove-hook 'post-command-hook 'org-add-log-note)
  13674. (setq org-log-note-window-configuration (current-window-configuration))
  13675. (delete-other-windows)
  13676. (move-marker org-log-note-return-to (point))
  13677. (switch-to-buffer (marker-buffer org-log-note-marker))
  13678. (goto-char org-log-note-marker)
  13679. (org-switch-to-buffer-other-window "*Org Note*")
  13680. (erase-buffer)
  13681. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13682. (org-store-log-note)
  13683. (let ((org-inhibit-startup t)) (org-mode))
  13684. (insert (format "# Insert note for %s.
  13685. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13686. (cond
  13687. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13688. ((eq org-log-note-purpose 'done) "closed todo item")
  13689. ((eq org-log-note-purpose 'state)
  13690. (format "state change to \"%s\"" org-log-note-state))
  13691. (t (error "This should not happen")))))
  13692. (org-set-local 'org-finish-function 'org-store-log-note)))
  13693. (defun org-store-log-note ()
  13694. "Finish taking a log note, and insert it to where it belongs."
  13695. (let ((txt (buffer-string))
  13696. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13697. lines ind)
  13698. (kill-buffer (current-buffer))
  13699. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13700. (setq txt (replace-match "" t t txt)))
  13701. (if (string-match "\\s-+\\'" txt)
  13702. (setq txt (replace-match "" t t txt)))
  13703. (setq lines (org-split-string txt "\n"))
  13704. (when (and note (string-match "\\S-" note))
  13705. (setq note
  13706. (org-replace-escapes
  13707. note
  13708. (list (cons "%u" (user-login-name))
  13709. (cons "%U" user-full-name)
  13710. (cons "%t" (format-time-string
  13711. (org-time-stamp-format 'long 'inactive)
  13712. (current-time)))
  13713. (cons "%s" (if org-log-note-state
  13714. (concat "\"" org-log-note-state "\"")
  13715. "")))))
  13716. (if lines (setq note (concat note " \\\\")))
  13717. (push note lines))
  13718. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13719. (when lines
  13720. (save-excursion
  13721. (set-buffer (marker-buffer org-log-note-marker))
  13722. (save-excursion
  13723. (goto-char org-log-note-marker)
  13724. (move-marker org-log-note-marker nil)
  13725. (end-of-line 1)
  13726. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13727. (indent-relative nil)
  13728. (insert "- " (pop lines))
  13729. (org-indent-line-function)
  13730. (beginning-of-line 1)
  13731. (looking-at "[ \t]*")
  13732. (setq ind (concat (match-string 0) " "))
  13733. (end-of-line 1)
  13734. (while lines (insert "\n" ind (pop lines)))))))
  13735. (set-window-configuration org-log-note-window-configuration)
  13736. (with-current-buffer (marker-buffer org-log-note-return-to)
  13737. (goto-char org-log-note-return-to))
  13738. (move-marker org-log-note-return-to nil)
  13739. (and org-log-post-message (message "%s" org-log-post-message)))
  13740. ;; FIXME: what else would be useful?
  13741. ;; - priority
  13742. ;; - date
  13743. (defun org-sparse-tree (&optional arg)
  13744. "Create a sparse tree, prompt for the details.
  13745. This command can create sparse trees. You first need to select the type
  13746. of match used to create the tree:
  13747. t Show entries with a specific TODO keyword.
  13748. T Show entries selected by a tags match.
  13749. p Enter a property name and its value (both with completion on existing
  13750. names/values) and show entries with that property.
  13751. r Show entries matching a regular expression
  13752. d Show deadlines due within `org-deadline-warning-days'."
  13753. (interactive "P")
  13754. (let (ans kwd value)
  13755. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13756. (setq ans (read-char-exclusive))
  13757. (cond
  13758. ((equal ans ?d)
  13759. (call-interactively 'org-check-deadlines))
  13760. ((equal ans ?b)
  13761. (call-interactively 'org-check-before-date))
  13762. ((equal ans ?t)
  13763. (org-show-todo-tree '(4)))
  13764. ((equal ans ?T)
  13765. (call-interactively 'org-tags-sparse-tree))
  13766. ((member ans '(?p ?P))
  13767. (setq kwd (completing-read "Property: "
  13768. (mapcar 'list (org-buffer-property-keys))))
  13769. (setq value (completing-read "Value: "
  13770. (mapcar 'list (org-property-values kwd))))
  13771. (unless (string-match "\\`{.*}\\'" value)
  13772. (setq value (concat "\"" value "\"")))
  13773. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13774. ((member ans '(?r ?R ?/))
  13775. (call-interactively 'org-occur))
  13776. (t (error "No such sparse tree command \"%c\"" ans)))))
  13777. (defvar org-occur-highlights nil)
  13778. (make-variable-buffer-local 'org-occur-highlights)
  13779. (defun org-occur (regexp &optional keep-previous callback)
  13780. "Make a compact tree which shows all matches of REGEXP.
  13781. The tree will show the lines where the regexp matches, and all higher
  13782. headlines above the match. It will also show the heading after the match,
  13783. to make sure editing the matching entry is easy.
  13784. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13785. call to `org-occur' will be kept, to allow stacking of calls to this
  13786. command.
  13787. If CALLBACK is non-nil, it is a function which is called to confirm
  13788. that the match should indeed be shown."
  13789. (interactive "sRegexp: \nP")
  13790. (or keep-previous (org-remove-occur-highlights nil nil t))
  13791. (let ((cnt 0))
  13792. (save-excursion
  13793. (goto-char (point-min))
  13794. (if (or (not keep-previous) ; do not want to keep
  13795. (not org-occur-highlights)) ; no previous matches
  13796. ;; hide everything
  13797. (org-overview))
  13798. (while (re-search-forward regexp nil t)
  13799. (when (or (not callback)
  13800. (save-match-data (funcall callback)))
  13801. (setq cnt (1+ cnt))
  13802. (when org-highlight-sparse-tree-matches
  13803. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13804. (org-show-context 'occur-tree))))
  13805. (when org-remove-highlights-with-change
  13806. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13807. nil 'local))
  13808. (unless org-sparse-tree-open-archived-trees
  13809. (org-hide-archived-subtrees (point-min) (point-max)))
  13810. (run-hooks 'org-occur-hook)
  13811. (if (interactive-p)
  13812. (message "%d match(es) for regexp %s" cnt regexp))
  13813. cnt))
  13814. (defun org-show-context (&optional key)
  13815. "Make sure point and context and visible.
  13816. How much context is shown depends upon the variables
  13817. `org-show-hierarchy-above', `org-show-following-heading'. and
  13818. `org-show-siblings'."
  13819. (let ((heading-p (org-on-heading-p t))
  13820. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13821. (following-p (org-get-alist-option org-show-following-heading key))
  13822. (entry-p (org-get-alist-option org-show-entry-below key))
  13823. (siblings-p (org-get-alist-option org-show-siblings key)))
  13824. (catch 'exit
  13825. ;; Show heading or entry text
  13826. (if (and heading-p (not entry-p))
  13827. (org-flag-heading nil) ; only show the heading
  13828. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13829. (org-show-hidden-entry))) ; show entire entry
  13830. (when following-p
  13831. ;; Show next sibling, or heading below text
  13832. (save-excursion
  13833. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13834. (org-flag-heading nil))))
  13835. (when siblings-p (org-show-siblings))
  13836. (when hierarchy-p
  13837. ;; show all higher headings, possibly with siblings
  13838. (save-excursion
  13839. (while (and (condition-case nil
  13840. (progn (org-up-heading-all 1) t)
  13841. (error nil))
  13842. (not (bobp)))
  13843. (org-flag-heading nil)
  13844. (when siblings-p (org-show-siblings))))))))
  13845. (defun org-reveal (&optional siblings)
  13846. "Show current entry, hierarchy above it, and the following headline.
  13847. This can be used to show a consistent set of context around locations
  13848. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13849. not t for the search context.
  13850. With optional argument SIBLINGS, on each level of the hierarchy all
  13851. siblings are shown. This repairs the tree structure to what it would
  13852. look like when opened with hierarchical calls to `org-cycle'."
  13853. (interactive "P")
  13854. (let ((org-show-hierarchy-above t)
  13855. (org-show-following-heading t)
  13856. (org-show-siblings (if siblings t org-show-siblings)))
  13857. (org-show-context nil)))
  13858. (defun org-highlight-new-match (beg end)
  13859. "Highlight from BEG to END and mark the highlight is an occur headline."
  13860. (let ((ov (org-make-overlay beg end)))
  13861. (org-overlay-put ov 'face 'secondary-selection)
  13862. (push ov org-occur-highlights)))
  13863. (defun org-remove-occur-highlights (&optional beg end noremove)
  13864. "Remove the occur highlights from the buffer.
  13865. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13866. from the `before-change-functions' in the current buffer."
  13867. (interactive)
  13868. (unless org-inhibit-highlight-removal
  13869. (mapc 'org-delete-overlay org-occur-highlights)
  13870. (setq org-occur-highlights nil)
  13871. (unless noremove
  13872. (remove-hook 'before-change-functions
  13873. 'org-remove-occur-highlights 'local))))
  13874. ;;;; Priorities
  13875. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13876. "Regular expression matching the priority indicator.")
  13877. (defvar org-remove-priority-next-time nil)
  13878. (defun org-priority-up ()
  13879. "Increase the priority of the current item."
  13880. (interactive)
  13881. (org-priority 'up))
  13882. (defun org-priority-down ()
  13883. "Decrease the priority of the current item."
  13884. (interactive)
  13885. (org-priority 'down))
  13886. (defun org-priority (&optional action)
  13887. "Change the priority of an item by ARG.
  13888. ACTION can be `set', `up', `down', or a character."
  13889. (interactive)
  13890. (setq action (or action 'set))
  13891. (let (current new news have remove)
  13892. (save-excursion
  13893. (org-back-to-heading)
  13894. (if (looking-at org-priority-regexp)
  13895. (setq current (string-to-char (match-string 2))
  13896. have t)
  13897. (setq current org-default-priority))
  13898. (cond
  13899. ((or (eq action 'set) (integerp action))
  13900. (if (integerp action)
  13901. (setq new action)
  13902. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13903. (setq new (read-char-exclusive)))
  13904. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13905. (= (upcase org-lowest-priority) org-lowest-priority))
  13906. (setq new (upcase new)))
  13907. (cond ((equal new ?\ ) (setq remove t))
  13908. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13909. (error "Priority must be between `%c' and `%c'"
  13910. org-highest-priority org-lowest-priority))))
  13911. ((eq action 'up)
  13912. (if (and (not have) (eq last-command this-command))
  13913. (setq new org-lowest-priority)
  13914. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13915. org-default-priority (1- current)))))
  13916. ((eq action 'down)
  13917. (if (and (not have) (eq last-command this-command))
  13918. (setq new org-highest-priority)
  13919. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13920. org-default-priority (1+ current)))))
  13921. (t (error "Invalid action")))
  13922. (if (or (< (upcase new) org-highest-priority)
  13923. (> (upcase new) org-lowest-priority))
  13924. (setq remove t))
  13925. (setq news (format "%c" new))
  13926. (if have
  13927. (if remove
  13928. (replace-match "" t t nil 1)
  13929. (replace-match news t t nil 2))
  13930. (if remove
  13931. (error "No priority cookie found in line")
  13932. (looking-at org-todo-line-regexp)
  13933. (if (match-end 2)
  13934. (progn
  13935. (goto-char (match-end 2))
  13936. (insert " [#" news "]"))
  13937. (goto-char (match-beginning 3))
  13938. (insert "[#" news "] ")))))
  13939. (org-preserve-lc (org-set-tags nil 'align))
  13940. (if remove
  13941. (message "Priority removed")
  13942. (message "Priority of current item set to %s" news))))
  13943. (defun org-get-priority (s)
  13944. "Find priority cookie and return priority."
  13945. (save-match-data
  13946. (if (not (string-match org-priority-regexp s))
  13947. (* 1000 (- org-lowest-priority org-default-priority))
  13948. (* 1000 (- org-lowest-priority
  13949. (string-to-char (match-string 2 s)))))))
  13950. ;;;; Tags
  13951. (defun org-scan-tags (action matcher &optional todo-only)
  13952. "Scan headline tags with inheritance and produce output ACTION.
  13953. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13954. evaluated, testing if a given set of tags qualifies a headline for
  13955. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13956. are included in the output."
  13957. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13958. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13959. (org-re
  13960. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13961. (props (list 'face nil
  13962. 'done-face 'org-done
  13963. 'undone-face nil
  13964. 'mouse-face 'highlight
  13965. 'org-not-done-regexp org-not-done-regexp
  13966. 'org-todo-regexp org-todo-regexp
  13967. 'keymap org-agenda-keymap
  13968. 'help-echo
  13969. (format "mouse-2 or RET jump to org file %s"
  13970. (abbreviate-file-name
  13971. (or (buffer-file-name (buffer-base-buffer))
  13972. (buffer-name (buffer-base-buffer)))))))
  13973. (case-fold-search nil)
  13974. lspos
  13975. tags tags-list tags-alist (llast 0) rtn level category i txt
  13976. todo marker entry priority)
  13977. (save-excursion
  13978. (goto-char (point-min))
  13979. (when (eq action 'sparse-tree)
  13980. (org-overview)
  13981. (org-remove-occur-highlights))
  13982. (while (re-search-forward re nil t)
  13983. (catch :skip
  13984. (setq todo (if (match-end 1) (match-string 2))
  13985. tags (if (match-end 4) (match-string 4)))
  13986. (goto-char (setq lspos (1+ (match-beginning 0))))
  13987. (setq level (org-reduced-level (funcall outline-level))
  13988. category (org-get-category))
  13989. (setq i llast llast level)
  13990. ;; remove tag lists from same and sublevels
  13991. (while (>= i level)
  13992. (when (setq entry (assoc i tags-alist))
  13993. (setq tags-alist (delete entry tags-alist)))
  13994. (setq i (1- i)))
  13995. ;; add the nex tags
  13996. (when tags
  13997. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13998. tags-alist
  13999. (cons (cons level tags) tags-alist)))
  14000. ;; compile tags for current headline
  14001. (setq tags-list
  14002. (if org-use-tag-inheritance
  14003. (apply 'append (mapcar 'cdr tags-alist))
  14004. tags))
  14005. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14006. (eval matcher)
  14007. (or (not org-agenda-skip-archived-trees)
  14008. (not (member org-archive-tag tags-list))))
  14009. (and (eq action 'agenda) (org-agenda-skip))
  14010. ;; list this headline
  14011. (if (eq action 'sparse-tree)
  14012. (progn
  14013. (and org-highlight-sparse-tree-matches
  14014. (org-get-heading) (match-end 0)
  14015. (org-highlight-new-match
  14016. (match-beginning 0) (match-beginning 1)))
  14017. (org-show-context 'tags-tree))
  14018. (setq txt (org-format-agenda-item
  14019. ""
  14020. (concat
  14021. (if org-tags-match-list-sublevels
  14022. (make-string (1- level) ?.) "")
  14023. (org-get-heading))
  14024. category tags-list)
  14025. priority (org-get-priority txt))
  14026. (goto-char lspos)
  14027. (setq marker (org-agenda-new-marker))
  14028. (org-add-props txt props
  14029. 'org-marker marker 'org-hd-marker marker 'org-category category
  14030. 'priority priority 'type "tagsmatch")
  14031. (push txt rtn))
  14032. ;; if we are to skip sublevels, jump to end of subtree
  14033. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14034. (when (and (eq action 'sparse-tree)
  14035. (not org-sparse-tree-open-archived-trees))
  14036. (org-hide-archived-subtrees (point-min) (point-max)))
  14037. (nreverse rtn)))
  14038. (defvar todo-only) ;; dynamically scoped
  14039. (defun org-tags-sparse-tree (&optional todo-only match)
  14040. "Create a sparse tree according to tags string MATCH.
  14041. MATCH can contain positive and negative selection of tags, like
  14042. \"+WORK+URGENT-WITHBOSS\".
  14043. If optional argument TODO_ONLY is non-nil, only select lines that are
  14044. also TODO lines."
  14045. (interactive "P")
  14046. (org-prepare-agenda-buffers (list (current-buffer)))
  14047. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14048. (defvar org-cached-props nil)
  14049. (defun org-cached-entry-get (pom property)
  14050. (if (or (eq t org-use-property-inheritance)
  14051. (member property org-use-property-inheritance))
  14052. ;; Caching is not possible, check it directly
  14053. (org-entry-get pom property 'inherit)
  14054. ;; Get all properties, so that we can do complicated checks easily
  14055. (cdr (assoc property (or org-cached-props
  14056. (setq org-cached-props
  14057. (org-entry-properties pom)))))))
  14058. (defun org-global-tags-completion-table (&optional files)
  14059. "Return the list of all tags in all agenda buffer/files."
  14060. (save-excursion
  14061. (org-uniquify
  14062. (delq nil
  14063. (apply 'append
  14064. (mapcar
  14065. (lambda (file)
  14066. (set-buffer (find-file-noselect file))
  14067. (append (org-get-buffer-tags)
  14068. (mapcar (lambda (x) (if (stringp (car-safe x))
  14069. (list (car-safe x)) nil))
  14070. org-tag-alist)))
  14071. (if (and files (car files))
  14072. files
  14073. (org-agenda-files))))))))
  14074. (defun org-make-tags-matcher (match)
  14075. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14076. ;; todo-only is scoped dynamically into this function, and the function
  14077. ;; may change it it the matcher asksk for it.
  14078. (unless match
  14079. ;; Get a new match request, with completion
  14080. (let ((org-last-tags-completion-table
  14081. (org-global-tags-completion-table)))
  14082. (setq match (completing-read
  14083. "Match: " 'org-tags-completion-function nil nil nil
  14084. 'org-tags-history))))
  14085. ;; Parse the string and create a lisp form
  14086. (let ((match0 match)
  14087. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14088. minus tag mm
  14089. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14090. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14091. (if (string-match "/+" match)
  14092. ;; match contains also a todo-matching request
  14093. (progn
  14094. (setq tagsmatch (substring match 0 (match-beginning 0))
  14095. todomatch (substring match (match-end 0)))
  14096. (if (string-match "^!" todomatch)
  14097. (setq todo-only t todomatch (substring todomatch 1)))
  14098. (if (string-match "^\\s-*$" todomatch)
  14099. (setq todomatch nil)))
  14100. ;; only matching tags
  14101. (setq tagsmatch match todomatch nil))
  14102. ;; Make the tags matcher
  14103. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14104. (setq tagsmatcher t)
  14105. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14106. (while (setq term (pop orterms))
  14107. (while (and (equal (substring term -1) "\\") orterms)
  14108. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14109. (while (string-match re term)
  14110. (setq minus (and (match-end 1)
  14111. (equal (match-string 1 term) "-"))
  14112. tag (match-string 2 term)
  14113. re-p (equal (string-to-char tag) ?{)
  14114. level-p (match-end 3)
  14115. prop-p (match-end 4)
  14116. mm (cond
  14117. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14118. (level-p `(= level ,(string-to-number
  14119. (match-string 3 term))))
  14120. (prop-p
  14121. (setq pn (match-string 4 term)
  14122. pv (match-string 5 term)
  14123. cat-p (equal pn "CATEGORY")
  14124. re-p (equal (string-to-char pv) ?{)
  14125. pv (substring pv 1 -1))
  14126. (if (equal pn "CATEGORY")
  14127. (setq gv '(get-text-property (point) 'org-category))
  14128. (setq gv `(org-cached-entry-get nil ,pn)))
  14129. (if re-p
  14130. `(string-match ,pv (or ,gv ""))
  14131. `(equal ,pv (or ,gv ""))))
  14132. (t `(member ,(downcase tag) tags-list)))
  14133. mm (if minus (list 'not mm) mm)
  14134. term (substring term (match-end 0)))
  14135. (push mm tagsmatcher))
  14136. (push (if (> (length tagsmatcher) 1)
  14137. (cons 'and tagsmatcher)
  14138. (car tagsmatcher))
  14139. orlist)
  14140. (setq tagsmatcher nil))
  14141. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14142. (setq tagsmatcher
  14143. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14144. ;; Make the todo matcher
  14145. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14146. (setq todomatcher t)
  14147. (setq orterms (org-split-string todomatch "|") orlist nil)
  14148. (while (setq term (pop orterms))
  14149. (while (string-match re term)
  14150. (setq minus (and (match-end 1)
  14151. (equal (match-string 1 term) "-"))
  14152. kwd (match-string 2 term)
  14153. re-p (equal (string-to-char kwd) ?{)
  14154. term (substring term (match-end 0))
  14155. mm (if re-p
  14156. `(string-match ,(substring kwd 1 -1) todo)
  14157. (list 'equal 'todo kwd))
  14158. mm (if minus (list 'not mm) mm))
  14159. (push mm todomatcher))
  14160. (push (if (> (length todomatcher) 1)
  14161. (cons 'and todomatcher)
  14162. (car todomatcher))
  14163. orlist)
  14164. (setq todomatcher nil))
  14165. (setq todomatcher (if (> (length orlist) 1)
  14166. (cons 'or orlist) (car orlist))))
  14167. ;; Return the string and lisp forms of the matcher
  14168. (setq matcher (if todomatcher
  14169. (list 'and tagsmatcher todomatcher)
  14170. tagsmatcher))
  14171. (cons match0 matcher)))
  14172. (defun org-match-any-p (re list)
  14173. "Does re match any element of list?"
  14174. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14175. (delq nil list))
  14176. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14177. (defvar org-tags-overlay (org-make-overlay 1 1))
  14178. (org-detach-overlay org-tags-overlay)
  14179. (defun org-align-tags-here (to-col)
  14180. ;; Assumes that this is a headline
  14181. (let ((pos (point)) (col (current-column)) tags)
  14182. (beginning-of-line 1)
  14183. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14184. (< pos (match-beginning 2)))
  14185. (progn
  14186. (setq tags (match-string 2))
  14187. (goto-char (match-beginning 1))
  14188. (insert " ")
  14189. (delete-region (point) (1+ (match-end 0)))
  14190. (backward-char 1)
  14191. (move-to-column
  14192. (max (1+ (current-column))
  14193. (1+ col)
  14194. (if (> to-col 0)
  14195. to-col
  14196. (- (abs to-col) (length tags))))
  14197. t)
  14198. (insert tags)
  14199. (move-to-column (min (current-column) col) t))
  14200. (goto-char pos))))
  14201. (defun org-set-tags (&optional arg just-align)
  14202. "Set the tags for the current headline.
  14203. With prefix ARG, realign all tags in headings in the current buffer."
  14204. (interactive "P")
  14205. (let* ((re (concat "^" outline-regexp))
  14206. (current (org-get-tags-string))
  14207. (col (current-column))
  14208. (org-setting-tags t)
  14209. table current-tags inherited-tags ; computed below when needed
  14210. tags p0 c0 c1 rpl)
  14211. (if arg
  14212. (save-excursion
  14213. (goto-char (point-min))
  14214. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14215. (while (re-search-forward re nil t)
  14216. (org-set-tags nil t)
  14217. (end-of-line 1)))
  14218. (message "All tags realigned to column %d" org-tags-column))
  14219. (if just-align
  14220. (setq tags current)
  14221. ;; Get a new set of tags from the user
  14222. (save-excursion
  14223. (setq table (or org-tag-alist (org-get-buffer-tags))
  14224. org-last-tags-completion-table table
  14225. current-tags (org-split-string current ":")
  14226. inherited-tags (nreverse
  14227. (nthcdr (length current-tags)
  14228. (nreverse (org-get-tags-at))))
  14229. tags
  14230. (if (or (eq t org-use-fast-tag-selection)
  14231. (and org-use-fast-tag-selection
  14232. (delq nil (mapcar 'cdr table))))
  14233. (org-fast-tag-selection
  14234. current-tags inherited-tags table
  14235. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14236. (let ((org-add-colon-after-tag-completion t))
  14237. (org-trim
  14238. (org-without-partial-completion
  14239. (completing-read "Tags: " 'org-tags-completion-function
  14240. nil nil current 'org-tags-history)))))))
  14241. (while (string-match "[-+&]+" tags)
  14242. ;; No boolean logic, just a list
  14243. (setq tags (replace-match ":" t t tags))))
  14244. (if (string-match "\\`[\t ]*\\'" tags)
  14245. (setq tags "")
  14246. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14247. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14248. ;; Insert new tags at the correct column
  14249. (beginning-of-line 1)
  14250. (cond
  14251. ((and (equal current "") (equal tags "")))
  14252. ((re-search-forward
  14253. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14254. (point-at-eol) t)
  14255. (if (equal tags "")
  14256. (setq rpl "")
  14257. (goto-char (match-beginning 0))
  14258. (setq c0 (current-column) p0 (point)
  14259. c1 (max (1+ c0) (if (> org-tags-column 0)
  14260. org-tags-column
  14261. (- (- org-tags-column) (length tags))))
  14262. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14263. (replace-match rpl t t)
  14264. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14265. tags)
  14266. (t (error "Tags alignment failed")))
  14267. (move-to-column col)
  14268. (unless just-align
  14269. (run-hooks 'org-after-tags-change-hook)))))
  14270. (defun org-change-tag-in-region (beg end tag off)
  14271. "Add or remove TAG for each entry in the region.
  14272. This works in the agenda, and also in an org-mode buffer."
  14273. (interactive
  14274. (list (region-beginning) (region-end)
  14275. (let ((org-last-tags-completion-table
  14276. (if (org-mode-p)
  14277. (org-get-buffer-tags)
  14278. (org-global-tags-completion-table))))
  14279. (completing-read
  14280. "Tag: " 'org-tags-completion-function nil nil nil
  14281. 'org-tags-history))
  14282. (progn
  14283. (message "[s]et or [r]emove? ")
  14284. (equal (read-char-exclusive) ?r))))
  14285. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14286. (let ((agendap (equal major-mode 'org-agenda-mode))
  14287. l1 l2 m buf pos newhead (cnt 0))
  14288. (goto-char end)
  14289. (setq l2 (1- (org-current-line)))
  14290. (goto-char beg)
  14291. (setq l1 (org-current-line))
  14292. (loop for l from l1 to l2 do
  14293. (goto-line l)
  14294. (setq m (get-text-property (point) 'org-hd-marker))
  14295. (when (or (and (org-mode-p) (org-on-heading-p))
  14296. (and agendap m))
  14297. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14298. pos (if agendap m (point)))
  14299. (with-current-buffer buf
  14300. (save-excursion
  14301. (save-restriction
  14302. (goto-char pos)
  14303. (setq cnt (1+ cnt))
  14304. (org-toggle-tag tag (if off 'off 'on))
  14305. (setq newhead (org-get-heading)))))
  14306. (and agendap (org-agenda-change-all-lines newhead m))))
  14307. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14308. (defun org-tags-completion-function (string predicate &optional flag)
  14309. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14310. (confirm (lambda (x) (stringp (car x)))))
  14311. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14312. (setq s1 (match-string 1 string)
  14313. s2 (match-string 2 string))
  14314. (setq s1 "" s2 string))
  14315. (cond
  14316. ((eq flag nil)
  14317. ;; try completion
  14318. (setq rtn (try-completion s2 ctable confirm))
  14319. (if (stringp rtn)
  14320. (setq rtn
  14321. (concat s1 s2 (substring rtn (length s2))
  14322. (if (and org-add-colon-after-tag-completion
  14323. (assoc rtn ctable))
  14324. ":" ""))))
  14325. rtn)
  14326. ((eq flag t)
  14327. ;; all-completions
  14328. (all-completions s2 ctable confirm)
  14329. )
  14330. ((eq flag 'lambda)
  14331. ;; exact match?
  14332. (assoc s2 ctable)))
  14333. ))
  14334. (defun org-fast-tag-insert (kwd tags face &optional end)
  14335. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14336. (insert (format "%-12s" (concat kwd ":"))
  14337. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14338. (or end "")))
  14339. (defun org-fast-tag-show-exit (flag)
  14340. (save-excursion
  14341. (goto-line 3)
  14342. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14343. (replace-match ""))
  14344. (when flag
  14345. (end-of-line 1)
  14346. (move-to-column (- (window-width) 19) t)
  14347. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14348. (defun org-set-current-tags-overlay (current prefix)
  14349. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14350. (if (featurep 'xemacs)
  14351. (org-overlay-display org-tags-overlay (concat prefix s)
  14352. 'secondary-selection)
  14353. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14354. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14355. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14356. "Fast tag selection with single keys.
  14357. CURRENT is the current list of tags in the headline, INHERITED is the
  14358. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14359. possibly with grouping information. TODO-TABLE is a similar table with
  14360. TODO keywords, should these have keys assigned to them.
  14361. If the keys are nil, a-z are automatically assigned.
  14362. Returns the new tags string, or nil to not change the current settings."
  14363. (let* ((fulltable (append table todo-table))
  14364. (maxlen (apply 'max (mapcar
  14365. (lambda (x)
  14366. (if (stringp (car x)) (string-width (car x)) 0))
  14367. fulltable)))
  14368. (buf (current-buffer))
  14369. (expert (eq org-fast-tag-selection-single-key 'expert))
  14370. (buffer-tags nil)
  14371. (fwidth (+ maxlen 3 1 3))
  14372. (ncol (/ (- (window-width) 4) fwidth))
  14373. (i-face 'org-done)
  14374. (c-face 'org-todo)
  14375. tg cnt e c char c1 c2 ntable tbl rtn
  14376. ov-start ov-end ov-prefix
  14377. (exit-after-next org-fast-tag-selection-single-key)
  14378. (done-keywords org-done-keywords)
  14379. groups ingroup)
  14380. (save-excursion
  14381. (beginning-of-line 1)
  14382. (if (looking-at
  14383. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14384. (setq ov-start (match-beginning 1)
  14385. ov-end (match-end 1)
  14386. ov-prefix "")
  14387. (setq ov-start (1- (point-at-eol))
  14388. ov-end (1+ ov-start))
  14389. (skip-chars-forward "^\n\r")
  14390. (setq ov-prefix
  14391. (concat
  14392. (buffer-substring (1- (point)) (point))
  14393. (if (> (current-column) org-tags-column)
  14394. " "
  14395. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14396. (org-move-overlay org-tags-overlay ov-start ov-end)
  14397. (save-window-excursion
  14398. (if expert
  14399. (set-buffer (get-buffer-create " *Org tags*"))
  14400. (delete-other-windows)
  14401. (split-window-vertically)
  14402. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14403. (erase-buffer)
  14404. (org-set-local 'org-done-keywords done-keywords)
  14405. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14406. (org-fast-tag-insert "Current" current c-face "\n\n")
  14407. (org-fast-tag-show-exit exit-after-next)
  14408. (org-set-current-tags-overlay current ov-prefix)
  14409. (setq tbl fulltable char ?a cnt 0)
  14410. (while (setq e (pop tbl))
  14411. (cond
  14412. ((equal e '(:startgroup))
  14413. (push '() groups) (setq ingroup t)
  14414. (when (not (= cnt 0))
  14415. (setq cnt 0)
  14416. (insert "\n"))
  14417. (insert "{ "))
  14418. ((equal e '(:endgroup))
  14419. (setq ingroup nil cnt 0)
  14420. (insert "}\n"))
  14421. (t
  14422. (setq tg (car e) c2 nil)
  14423. (if (cdr e)
  14424. (setq c (cdr e))
  14425. ;; automatically assign a character.
  14426. (setq c1 (string-to-char
  14427. (downcase (substring
  14428. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14429. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14430. (while (or (rassoc char ntable) (rassoc char table))
  14431. (setq char (1+ char)))
  14432. (setq c2 c1))
  14433. (setq c (or c2 char)))
  14434. (if ingroup (push tg (car groups)))
  14435. (setq tg (org-add-props tg nil 'face
  14436. (cond
  14437. ((not (assoc tg table))
  14438. (org-get-todo-face tg))
  14439. ((member tg current) c-face)
  14440. ((member tg inherited) i-face)
  14441. (t nil))))
  14442. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14443. (insert "[" c "] " tg (make-string
  14444. (- fwidth 4 (length tg)) ?\ ))
  14445. (push (cons tg c) ntable)
  14446. (when (= (setq cnt (1+ cnt)) ncol)
  14447. (insert "\n")
  14448. (if ingroup (insert " "))
  14449. (setq cnt 0)))))
  14450. (setq ntable (nreverse ntable))
  14451. (insert "\n")
  14452. (goto-char (point-min))
  14453. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14454. (fit-window-to-buffer))
  14455. (setq rtn
  14456. (catch 'exit
  14457. (while t
  14458. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14459. (if groups " [!] no groups" " [!]groups")
  14460. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14461. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14462. (cond
  14463. ((= c ?\r) (throw 'exit t))
  14464. ((= c ?!)
  14465. (setq groups (not groups))
  14466. (goto-char (point-min))
  14467. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14468. ((= c ?\C-c)
  14469. (if (not expert)
  14470. (org-fast-tag-show-exit
  14471. (setq exit-after-next (not exit-after-next)))
  14472. (setq expert nil)
  14473. (delete-other-windows)
  14474. (split-window-vertically)
  14475. (org-switch-to-buffer-other-window " *Org tags*")
  14476. (and (fboundp 'fit-window-to-buffer)
  14477. (fit-window-to-buffer))))
  14478. ((or (= c ?\C-g)
  14479. (and (= c ?q) (not (rassoc c ntable))))
  14480. (org-detach-overlay org-tags-overlay)
  14481. (setq quit-flag t))
  14482. ((= c ?\ )
  14483. (setq current nil)
  14484. (if exit-after-next (setq exit-after-next 'now)))
  14485. ((= c ?\t)
  14486. (condition-case nil
  14487. (setq tg (completing-read
  14488. "Tag: "
  14489. (or buffer-tags
  14490. (with-current-buffer buf
  14491. (org-get-buffer-tags)))))
  14492. (quit (setq tg "")))
  14493. (when (string-match "\\S-" tg)
  14494. (add-to-list 'buffer-tags (list tg))
  14495. (if (member tg current)
  14496. (setq current (delete tg current))
  14497. (push tg current)))
  14498. (if exit-after-next (setq exit-after-next 'now)))
  14499. ((setq e (rassoc c todo-table) tg (car e))
  14500. (with-current-buffer buf
  14501. (save-excursion (org-todo tg)))
  14502. (if exit-after-next (setq exit-after-next 'now)))
  14503. ((setq e (rassoc c ntable) tg (car e))
  14504. (if (member tg current)
  14505. (setq current (delete tg current))
  14506. (loop for g in groups do
  14507. (if (member tg g)
  14508. (mapc (lambda (x)
  14509. (setq current (delete x current)))
  14510. g)))
  14511. (push tg current))
  14512. (if exit-after-next (setq exit-after-next 'now))))
  14513. ;; Create a sorted list
  14514. (setq current
  14515. (sort current
  14516. (lambda (a b)
  14517. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14518. (if (eq exit-after-next 'now) (throw 'exit t))
  14519. (goto-char (point-min))
  14520. (beginning-of-line 2)
  14521. (delete-region (point) (point-at-eol))
  14522. (org-fast-tag-insert "Current" current c-face)
  14523. (org-set-current-tags-overlay current ov-prefix)
  14524. (while (re-search-forward
  14525. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14526. (setq tg (match-string 1))
  14527. (add-text-properties
  14528. (match-beginning 1) (match-end 1)
  14529. (list 'face
  14530. (cond
  14531. ((member tg current) c-face)
  14532. ((member tg inherited) i-face)
  14533. (t (get-text-property (match-beginning 1) 'face))))))
  14534. (goto-char (point-min)))))
  14535. (org-detach-overlay org-tags-overlay)
  14536. (if rtn
  14537. (mapconcat 'identity current ":")
  14538. nil))))
  14539. (defun org-get-tags-string ()
  14540. "Get the TAGS string in the current headline."
  14541. (unless (org-on-heading-p t)
  14542. (error "Not on a heading"))
  14543. (save-excursion
  14544. (beginning-of-line 1)
  14545. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14546. (org-match-string-no-properties 1)
  14547. "")))
  14548. (defun org-get-tags ()
  14549. "Get the list of tags specified in the current headline."
  14550. (org-split-string (org-get-tags-string) ":"))
  14551. (defun org-get-buffer-tags ()
  14552. "Get a table of all tags used in the buffer, for completion."
  14553. (let (tags)
  14554. (save-excursion
  14555. (goto-char (point-min))
  14556. (while (re-search-forward
  14557. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14558. (when (equal (char-after (point-at-bol 0)) ?*)
  14559. (mapc (lambda (x) (add-to-list 'tags x))
  14560. (org-split-string (org-match-string-no-properties 1) ":")))))
  14561. (mapcar 'list tags)))
  14562. ;;;; Properties
  14563. ;;; Setting and retrieving properties
  14564. (defconst org-special-properties
  14565. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14566. "TIMESTAMP" "TIMESTAMP_IA")
  14567. "The special properties valid in Org-mode.
  14568. These are properties that are not defined in the property drawer,
  14569. but in some other way.")
  14570. (defconst org-default-properties
  14571. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14572. "LOCATION" "LOGGING" "COLUMNS")
  14573. "Some properties that are used by Org-mode for various purposes.
  14574. Being in this list makes sure that they are offered for completion.")
  14575. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14576. "Regular expression matching the first line of a property drawer.")
  14577. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14578. "Regular expression matching the first line of a property drawer.")
  14579. (defun org-property-action ()
  14580. "Do an action on properties."
  14581. (interactive)
  14582. (let (c)
  14583. (org-at-property-p)
  14584. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14585. (setq c (read-char-exclusive))
  14586. (cond
  14587. ((equal c ?s)
  14588. (call-interactively 'org-set-property))
  14589. ((equal c ?d)
  14590. (call-interactively 'org-delete-property))
  14591. ((equal c ?D)
  14592. (call-interactively 'org-delete-property-globally))
  14593. ((equal c ?c)
  14594. (call-interactively 'org-compute-property-at-point))
  14595. (t (error "No such property action %c" c)))))
  14596. (defun org-at-property-p ()
  14597. "Is the cursor in a property line?"
  14598. ;; FIXME: Does not check if we are actually in the drawer.
  14599. ;; FIXME: also returns true on any drawers.....
  14600. ;; This is used by C-c C-c for property action.
  14601. (save-excursion
  14602. (beginning-of-line 1)
  14603. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14604. (defmacro org-with-point-at (pom &rest body)
  14605. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14606. (declare (indent 1) (debug t))
  14607. `(save-excursion
  14608. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14609. (save-excursion
  14610. (goto-char (or pom (point)))
  14611. ,@body)))
  14612. (defun org-get-property-block (&optional beg end force)
  14613. "Return the (beg . end) range of the body of the property drawer.
  14614. BEG and END can be beginning and end of subtree, if not given
  14615. they will be found.
  14616. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14617. (catch 'exit
  14618. (save-excursion
  14619. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14620. (end (or end (progn (outline-next-heading) (point)))))
  14621. (goto-char beg)
  14622. (if (re-search-forward org-property-start-re end t)
  14623. (setq beg (1+ (match-end 0)))
  14624. (if force
  14625. (save-excursion
  14626. (org-insert-property-drawer)
  14627. (setq end (progn (outline-next-heading) (point))))
  14628. (throw 'exit nil))
  14629. (goto-char beg)
  14630. (if (re-search-forward org-property-start-re end t)
  14631. (setq beg (1+ (match-end 0)))))
  14632. (if (re-search-forward org-property-end-re end t)
  14633. (setq end (match-beginning 0))
  14634. (or force (throw 'exit nil))
  14635. (goto-char beg)
  14636. (setq end beg)
  14637. (org-indent-line-function)
  14638. (insert ":END:\n"))
  14639. (cons beg end)))))
  14640. (defun org-entry-properties (&optional pom which)
  14641. "Get all properties of the entry at point-or-marker POM.
  14642. This includes the TODO keyword, the tags, time strings for deadline,
  14643. scheduled, and clocking, and any additional properties defined in the
  14644. entry. The return value is an alist, keys may occur multiple times
  14645. if the property key was used several times.
  14646. POM may also be nil, in which case the current entry is used.
  14647. If WHICH is nil or `all', get all properties. If WHICH is
  14648. `special' or `standard', only get that subclass."
  14649. (setq which (or which 'all))
  14650. (org-with-point-at pom
  14651. (let ((clockstr (substring org-clock-string 0 -1))
  14652. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14653. beg end range props sum-props key value string clocksum)
  14654. (save-excursion
  14655. (when (condition-case nil (org-back-to-heading t) (error nil))
  14656. (setq beg (point))
  14657. (setq sum-props (get-text-property (point) 'org-summaries))
  14658. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14659. (outline-next-heading)
  14660. (setq end (point))
  14661. (when (memq which '(all special))
  14662. ;; Get the special properties, like TODO and tags
  14663. (goto-char beg)
  14664. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14665. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14666. (when (looking-at org-priority-regexp)
  14667. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14668. (when (and (setq value (org-get-tags-string))
  14669. (string-match "\\S-" value))
  14670. (push (cons "TAGS" value) props))
  14671. (when (setq value (org-get-tags-at))
  14672. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14673. props))
  14674. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14675. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14676. string (if (equal key clockstr)
  14677. (org-no-properties
  14678. (org-trim
  14679. (buffer-substring
  14680. (match-beginning 3) (goto-char (point-at-eol)))))
  14681. (substring (org-match-string-no-properties 3) 1 -1)))
  14682. (unless key
  14683. (if (= (char-after (match-beginning 3)) ?\[)
  14684. (setq key "TIMESTAMP_IA")
  14685. (setq key "TIMESTAMP")))
  14686. (when (or (equal key clockstr) (not (assoc key props)))
  14687. (push (cons key string) props)))
  14688. )
  14689. (when (memq which '(all standard))
  14690. ;; Get the standard properties, like :PORP: ...
  14691. (setq range (org-get-property-block beg end))
  14692. (when range
  14693. (goto-char (car range))
  14694. (while (re-search-forward
  14695. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14696. (cdr range) t)
  14697. (setq key (org-match-string-no-properties 1)
  14698. value (org-trim (or (org-match-string-no-properties 2) "")))
  14699. (unless (member key excluded)
  14700. (push (cons key (or value "")) props)))))
  14701. (if clocksum
  14702. (push (cons "CLOCKSUM"
  14703. (org-column-number-to-string (/ (float clocksum) 60.)
  14704. 'add_times))
  14705. props))
  14706. (append sum-props (nreverse props)))))))
  14707. (defun org-entry-get (pom property &optional inherit)
  14708. "Get value of PROPERTY for entry at point-or-marker POM.
  14709. If INHERIT is non-nil and the entry does not have the property,
  14710. then also check higher levels of the hierarchy.
  14711. If the property is present but empty, the return value is the empty string.
  14712. If the property is not present at all, nil is returned."
  14713. (org-with-point-at pom
  14714. (if inherit
  14715. (org-entry-get-with-inheritance property)
  14716. (if (member property org-special-properties)
  14717. ;; We need a special property. Use brute force, get all properties.
  14718. (cdr (assoc property (org-entry-properties nil 'special)))
  14719. (let ((range (org-get-property-block)))
  14720. (if (and range
  14721. (goto-char (car range))
  14722. (re-search-forward
  14723. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14724. (cdr range) t))
  14725. ;; Found the property, return it.
  14726. (if (match-end 1)
  14727. (org-match-string-no-properties 1)
  14728. "")))))))
  14729. (defun org-entry-delete (pom property)
  14730. "Delete the property PROPERTY from entry at point-or-marker POM."
  14731. (org-with-point-at pom
  14732. (if (member property org-special-properties)
  14733. nil ; cannot delete these properties.
  14734. (let ((range (org-get-property-block)))
  14735. (if (and range
  14736. (goto-char (car range))
  14737. (re-search-forward
  14738. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14739. (cdr range) t))
  14740. (progn
  14741. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14742. t)
  14743. nil)))))
  14744. ;; Multi-values properties are properties that contain multiple values
  14745. ;; These values are assumed to be single words, separated by whitespace.
  14746. (defun org-entry-add-to-multivalued-property (pom property value)
  14747. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14748. (let* ((old (org-entry-get pom property))
  14749. (values (and old (org-split-string old "[ \t]"))))
  14750. (unless (member value values)
  14751. (setq values (cons value values))
  14752. (org-entry-put pom property
  14753. (mapconcat 'identity values " ")))))
  14754. (defun org-entry-remove-from-multivalued-property (pom property value)
  14755. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14756. (let* ((old (org-entry-get pom property))
  14757. (values (and old (org-split-string old "[ \t]"))))
  14758. (when (member value values)
  14759. (setq values (delete value values))
  14760. (org-entry-put pom property
  14761. (mapconcat 'identity values " ")))))
  14762. (defun org-entry-member-in-multivalued-property (pom property value)
  14763. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14764. (let* ((old (org-entry-get pom property))
  14765. (values (and old (org-split-string old "[ \t]"))))
  14766. (member value values)))
  14767. (defvar org-entry-property-inherited-from (make-marker))
  14768. (defun org-entry-get-with-inheritance (property)
  14769. "Get entry property, and search higher levels if not present."
  14770. (let (tmp)
  14771. (save-excursion
  14772. (save-restriction
  14773. (widen)
  14774. (catch 'ex
  14775. (while t
  14776. (when (setq tmp (org-entry-get nil property))
  14777. (org-back-to-heading t)
  14778. (move-marker org-entry-property-inherited-from (point))
  14779. (throw 'ex tmp))
  14780. (or (org-up-heading-safe) (throw 'ex nil)))))
  14781. (or tmp (cdr (assoc property org-local-properties))
  14782. (cdr (assoc property org-global-properties))))))
  14783. (defun org-entry-put (pom property value)
  14784. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14785. (org-with-point-at pom
  14786. (org-back-to-heading t)
  14787. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14788. range)
  14789. (cond
  14790. ((equal property "TODO")
  14791. (when (and (stringp value) (string-match "\\S-" value)
  14792. (not (member value org-todo-keywords-1)))
  14793. (error "\"%s\" is not a valid TODO state" value))
  14794. (if (or (not value)
  14795. (not (string-match "\\S-" value)))
  14796. (setq value 'none))
  14797. (org-todo value)
  14798. (org-set-tags nil 'align))
  14799. ((equal property "PRIORITY")
  14800. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14801. (string-to-char value) ?\ ))
  14802. (org-set-tags nil 'align))
  14803. ((equal property "SCHEDULED")
  14804. (if (re-search-forward org-scheduled-time-regexp end t)
  14805. (cond
  14806. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14807. ((eq value 'later) (org-timestamp-change 1 'day))
  14808. (t (call-interactively 'org-schedule)))
  14809. (call-interactively 'org-schedule)))
  14810. ((equal property "DEADLINE")
  14811. (if (re-search-forward org-deadline-time-regexp end t)
  14812. (cond
  14813. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14814. ((eq value 'later) (org-timestamp-change 1 'day))
  14815. (t (call-interactively 'org-deadline)))
  14816. (call-interactively 'org-deadline)))
  14817. ((member property org-special-properties)
  14818. (error "The %s property can not yet be set with `org-entry-put'"
  14819. property))
  14820. (t ; a non-special property
  14821. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14822. (setq range (org-get-property-block beg end 'force))
  14823. (goto-char (car range))
  14824. (if (re-search-forward
  14825. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14826. (progn
  14827. (delete-region (match-beginning 1) (match-end 1))
  14828. (goto-char (match-beginning 1)))
  14829. (goto-char (cdr range))
  14830. (insert "\n")
  14831. (backward-char 1)
  14832. (org-indent-line-function)
  14833. (insert ":" property ":"))
  14834. (and value (insert " " value))
  14835. (org-indent-line-function)))))))
  14836. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14837. "Get all property keys in the current buffer.
  14838. With INCLUDE-SPECIALS, also list the special properties that relect things
  14839. like tags and TODO state.
  14840. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14841. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14842. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14843. formats in the current buffer."
  14844. (let (rtn range cfmt cols s p)
  14845. (save-excursion
  14846. (save-restriction
  14847. (widen)
  14848. (goto-char (point-min))
  14849. (while (re-search-forward org-property-start-re nil t)
  14850. (setq range (org-get-property-block))
  14851. (goto-char (car range))
  14852. (while (re-search-forward
  14853. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14854. (cdr range) t)
  14855. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14856. (outline-next-heading))))
  14857. (when include-specials
  14858. (setq rtn (append org-special-properties rtn)))
  14859. (when include-defaults
  14860. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14861. (when include-columns
  14862. (save-excursion
  14863. (save-restriction
  14864. (widen)
  14865. (goto-char (point-min))
  14866. (while (re-search-forward
  14867. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14868. nil t)
  14869. (setq cfmt (match-string 2) s 0)
  14870. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14871. cfmt s)
  14872. (setq s (match-end 0)
  14873. p (match-string 1 cfmt))
  14874. (unless (or (equal p "ITEM")
  14875. (member p org-special-properties))
  14876. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14877. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14878. (defun org-property-values (key)
  14879. "Return a list of all values of property KEY."
  14880. (save-excursion
  14881. (save-restriction
  14882. (widen)
  14883. (goto-char (point-min))
  14884. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14885. values)
  14886. (while (re-search-forward re nil t)
  14887. (add-to-list 'values (org-trim (match-string 1))))
  14888. (delete "" values)))))
  14889. (defun org-insert-property-drawer ()
  14890. "Insert a property drawer into the current entry."
  14891. (interactive)
  14892. (org-back-to-heading t)
  14893. (looking-at outline-regexp)
  14894. (let ((indent (- (match-end 0)(match-beginning 0)))
  14895. (beg (point))
  14896. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14897. end hiddenp)
  14898. (outline-next-heading)
  14899. (setq end (point))
  14900. (goto-char beg)
  14901. (while (re-search-forward re end t))
  14902. (setq hiddenp (org-invisible-p))
  14903. (end-of-line 1)
  14904. (and (equal (char-after) ?\n) (forward-char 1))
  14905. (org-skip-over-state-notes)
  14906. (skip-chars-backward " \t\n\r")
  14907. (if (eq (char-before) ?*) (forward-char 1))
  14908. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14909. (beginning-of-line 0)
  14910. (indent-to-column indent)
  14911. (beginning-of-line 2)
  14912. (indent-to-column indent)
  14913. (beginning-of-line 0)
  14914. (if hiddenp
  14915. (save-excursion
  14916. (org-back-to-heading t)
  14917. (hide-entry))
  14918. (org-flag-drawer t))))
  14919. (defun org-set-property (property value)
  14920. "In the current entry, set PROPERTY to VALUE.
  14921. When called interactively, this will prompt for a property name, offering
  14922. completion on existing and default properties. And then it will prompt
  14923. for a value, offering competion either on allowed values (via an inherited
  14924. xxx_ALL property) or on existing values in other instances of this property
  14925. in the current file."
  14926. (interactive
  14927. (let* ((prop (completing-read
  14928. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  14929. (cur (org-entry-get nil prop))
  14930. (allowed (org-property-get-allowed-values nil prop 'table))
  14931. (existing (mapcar 'list (org-property-values prop)))
  14932. (val (if allowed
  14933. (completing-read "Value: " allowed nil 'req-match)
  14934. (completing-read
  14935. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14936. (concat "[" cur "]") "")
  14937. ": ")
  14938. existing nil nil "" nil cur))))
  14939. (list prop (if (equal val "") cur val))))
  14940. (unless (equal (org-entry-get nil property) value)
  14941. (org-entry-put nil property value)))
  14942. (defun org-delete-property (property)
  14943. "In the current entry, delete PROPERTY."
  14944. (interactive
  14945. (let* ((prop (completing-read
  14946. "Property: " (org-entry-properties nil 'standard))))
  14947. (list prop)))
  14948. (message "Property %s %s" property
  14949. (if (org-entry-delete nil property)
  14950. "deleted"
  14951. "was not present in the entry")))
  14952. (defun org-delete-property-globally (property)
  14953. "Remove PROPERTY globally, from all entries."
  14954. (interactive
  14955. (let* ((prop (completing-read
  14956. "Globally remove property: "
  14957. (mapcar 'list (org-buffer-property-keys)))))
  14958. (list prop)))
  14959. (save-excursion
  14960. (save-restriction
  14961. (widen)
  14962. (goto-char (point-min))
  14963. (let ((cnt 0))
  14964. (while (re-search-forward
  14965. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14966. nil t)
  14967. (setq cnt (1+ cnt))
  14968. (replace-match ""))
  14969. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14970. (defvar org-columns-current-fmt-compiled) ; defined below
  14971. (defun org-compute-property-at-point ()
  14972. "Compute the property at point.
  14973. This looks for an enclosing column format, extracts the operator and
  14974. then applies it to the proerty in the column format's scope."
  14975. (interactive)
  14976. (unless (org-at-property-p)
  14977. (error "Not at a property"))
  14978. (let ((prop (org-match-string-no-properties 2)))
  14979. (org-columns-get-format-and-top-level)
  14980. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14981. (error "No operator defined for property %s" prop))
  14982. (org-columns-compute prop)))
  14983. (defun org-property-get-allowed-values (pom property &optional table)
  14984. "Get allowed values for the property PROPERTY.
  14985. When TABLE is non-nil, return an alist that can directly be used for
  14986. completion."
  14987. (let (vals)
  14988. (cond
  14989. ((equal property "TODO")
  14990. (setq vals (org-with-point-at pom
  14991. (append org-todo-keywords-1 '("")))))
  14992. ((equal property "PRIORITY")
  14993. (let ((n org-lowest-priority))
  14994. (while (>= n org-highest-priority)
  14995. (push (char-to-string n) vals)
  14996. (setq n (1- n)))))
  14997. ((member property org-special-properties))
  14998. (t
  14999. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15000. (when (and vals (string-match "\\S-" vals))
  15001. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15002. (setq vals (mapcar (lambda (x)
  15003. (cond ((stringp x) x)
  15004. ((numberp x) (number-to-string x))
  15005. ((symbolp x) (symbol-name x))
  15006. (t "???")))
  15007. vals)))))
  15008. (if table (mapcar 'list vals) vals)))
  15009. (defun org-property-previous-allowed-value (&optional previous)
  15010. "Switch to the next allowed value for this property."
  15011. (interactive)
  15012. (org-property-next-allowed-value t))
  15013. (defun org-property-next-allowed-value (&optional previous)
  15014. "Switch to the next allowed value for this property."
  15015. (interactive)
  15016. (unless (org-at-property-p)
  15017. (error "Not at a property"))
  15018. (let* ((key (match-string 2))
  15019. (value (match-string 3))
  15020. (allowed (or (org-property-get-allowed-values (point) key)
  15021. (and (member value '("[ ]" "[-]" "[X]"))
  15022. '("[ ]" "[X]"))))
  15023. nval)
  15024. (unless allowed
  15025. (error "Allowed values for this property have not been defined"))
  15026. (if previous (setq allowed (reverse allowed)))
  15027. (if (member value allowed)
  15028. (setq nval (car (cdr (member value allowed)))))
  15029. (setq nval (or nval (car allowed)))
  15030. (if (equal nval value)
  15031. (error "Only one allowed value for this property"))
  15032. (org-at-property-p)
  15033. (replace-match (concat " :" key ": " nval) t t)
  15034. (org-indent-line-function)
  15035. (beginning-of-line 1)
  15036. (skip-chars-forward " \t")))
  15037. (defun org-find-entry-with-id (ident)
  15038. "Locate the entry that contains the ID property with exact value IDENT.
  15039. IDENT can be a string, a symbol or a number, this function will search for
  15040. the string representation of it.
  15041. Return the position where this entry starts, or nil if there is no such entry."
  15042. (let ((id (cond
  15043. ((stringp ident) ident)
  15044. ((symbol-name ident) (symbol-name ident))
  15045. ((numberp ident) (number-to-string ident))
  15046. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15047. (case-fold-search nil))
  15048. (save-excursion
  15049. (save-restriction
  15050. (goto-char (point-min))
  15051. (when (re-search-forward
  15052. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15053. nil t)
  15054. (org-back-to-heading)
  15055. (point))))))
  15056. ;;; Column View
  15057. (defvar org-columns-overlays nil
  15058. "Holds the list of current column overlays.")
  15059. (defvar org-columns-current-fmt nil
  15060. "Local variable, holds the currently active column format.")
  15061. (defvar org-columns-current-fmt-compiled nil
  15062. "Local variable, holds the currently active column format.
  15063. This is the compiled version of the format.")
  15064. (defvar org-columns-current-widths nil
  15065. "Loval variable, holds the currently widths of fields.")
  15066. (defvar org-columns-current-maxwidths nil
  15067. "Loval variable, holds the currently active maximum column widths.")
  15068. (defvar org-columns-begin-marker (make-marker)
  15069. "Points to the position where last a column creation command was called.")
  15070. (defvar org-columns-top-level-marker (make-marker)
  15071. "Points to the position where current columns region starts.")
  15072. (defvar org-columns-map (make-sparse-keymap)
  15073. "The keymap valid in column display.")
  15074. (defun org-columns-content ()
  15075. "Switch to contents view while in columns view."
  15076. (interactive)
  15077. (org-overview)
  15078. (org-content))
  15079. (org-defkey org-columns-map "c" 'org-columns-content)
  15080. (org-defkey org-columns-map "o" 'org-overview)
  15081. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15082. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15083. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15084. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15085. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15086. (org-defkey org-columns-map "q" 'org-columns-quit)
  15087. (org-defkey org-columns-map "r" 'org-columns-redo)
  15088. (org-defkey org-columns-map "g" 'org-columns-redo)
  15089. (org-defkey org-columns-map [left] 'backward-char)
  15090. (org-defkey org-columns-map "\M-b" 'backward-char)
  15091. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15092. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15093. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15094. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15095. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15096. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15097. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15098. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15099. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15100. (org-defkey org-columns-map ">" 'org-columns-widen)
  15101. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15102. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15103. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15104. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15105. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15106. '("Column"
  15107. ["Edit property" org-columns-edit-value t]
  15108. ["Next allowed value" org-columns-next-allowed-value t]
  15109. ["Previous allowed value" org-columns-previous-allowed-value t]
  15110. ["Show full value" org-columns-show-value t]
  15111. ["Edit allowed values" org-columns-edit-allowed t]
  15112. "--"
  15113. ["Edit column attributes" org-columns-edit-attributes t]
  15114. ["Increase column width" org-columns-widen t]
  15115. ["Decrease column width" org-columns-narrow t]
  15116. "--"
  15117. ["Move column right" org-columns-move-right t]
  15118. ["Move column left" org-columns-move-left t]
  15119. ["Add column" org-columns-new t]
  15120. ["Delete column" org-columns-delete t]
  15121. "--"
  15122. ["CONTENTS" org-columns-content t]
  15123. ["OVERVIEW" org-overview t]
  15124. ["Refresh columns display" org-columns-redo t]
  15125. "--"
  15126. ["Open link" org-columns-open-link t]
  15127. "--"
  15128. ["Quit" org-columns-quit t]))
  15129. (defun org-columns-new-overlay (beg end &optional string face)
  15130. "Create a new column overlay and add it to the list."
  15131. (let ((ov (org-make-overlay beg end)))
  15132. (org-overlay-put ov 'face (or face 'secondary-selection))
  15133. (org-overlay-display ov string face)
  15134. (push ov org-columns-overlays)
  15135. ov))
  15136. (defun org-columns-display-here (&optional props)
  15137. "Overlay the current line with column display."
  15138. (interactive)
  15139. (let* ((fmt org-columns-current-fmt-compiled)
  15140. (beg (point-at-bol))
  15141. (level-face (save-excursion
  15142. (beginning-of-line 1)
  15143. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15144. (org-get-level-face 2))))
  15145. (color (list :foreground
  15146. (face-attribute (or level-face 'default) :foreground)))
  15147. props pom property ass width f string ov column val modval)
  15148. ;; Check if the entry is in another buffer.
  15149. (unless props
  15150. (if (eq major-mode 'org-agenda-mode)
  15151. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15152. (get-text-property (point) 'org-marker))
  15153. props (if pom (org-entry-properties pom) nil))
  15154. (setq props (org-entry-properties nil))))
  15155. ;; Walk the format
  15156. (while (setq column (pop fmt))
  15157. (setq property (car column)
  15158. ass (if (equal property "ITEM")
  15159. (cons "ITEM"
  15160. (save-match-data
  15161. (org-no-properties
  15162. (org-remove-tabs
  15163. (buffer-substring-no-properties
  15164. (point-at-bol) (point-at-eol))))))
  15165. (assoc property props))
  15166. width (or (cdr (assoc property org-columns-current-maxwidths))
  15167. (nth 2 column)
  15168. (length property))
  15169. f (format "%%-%d.%ds | " width width)
  15170. val (or (cdr ass) "")
  15171. modval (if (equal property "ITEM")
  15172. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15173. string (format f (or modval val)))
  15174. ;; Create the overlay
  15175. (org-unmodified
  15176. (setq ov (org-columns-new-overlay
  15177. beg (setq beg (1+ beg)) string
  15178. (list color 'org-column)))
  15179. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15180. (org-overlay-put ov 'keymap org-columns-map)
  15181. (org-overlay-put ov 'org-columns-key property)
  15182. (org-overlay-put ov 'org-columns-value (cdr ass))
  15183. (org-overlay-put ov 'org-columns-value-modified modval)
  15184. (org-overlay-put ov 'org-columns-pom pom)
  15185. (org-overlay-put ov 'org-columns-format f))
  15186. (if (or (not (char-after beg))
  15187. (equal (char-after beg) ?\n))
  15188. (let ((inhibit-read-only t))
  15189. (save-excursion
  15190. (goto-char beg)
  15191. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15192. ;; Make the rest of the line disappear.
  15193. (org-unmodified
  15194. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15195. (org-overlay-put ov 'invisible t)
  15196. (org-overlay-put ov 'keymap org-columns-map)
  15197. (org-overlay-put ov 'intangible t)
  15198. (push ov org-columns-overlays)
  15199. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15200. (org-overlay-put ov 'keymap org-columns-map)
  15201. (push ov org-columns-overlays)
  15202. (let ((inhibit-read-only t))
  15203. (put-text-property (max (point-min) (1- (point-at-bol)))
  15204. (min (point-max) (1+ (point-at-eol)))
  15205. 'read-only "Type `e' to edit property")))))
  15206. (defvar org-previous-header-line-format nil
  15207. "The header line format before column view was turned on.")
  15208. (defvar org-columns-inhibit-recalculation nil
  15209. "Inhibit recomputing of columns on column view startup.")
  15210. (defvar header-line-format)
  15211. (defun org-columns-display-here-title ()
  15212. "Overlay the newline before the current line with the table title."
  15213. (interactive)
  15214. (let ((fmt org-columns-current-fmt-compiled)
  15215. string (title "")
  15216. property width f column str widths)
  15217. (while (setq column (pop fmt))
  15218. (setq property (car column)
  15219. str (or (nth 1 column) property)
  15220. width (or (cdr (assoc property org-columns-current-maxwidths))
  15221. (nth 2 column)
  15222. (length str))
  15223. widths (push width widths)
  15224. f (format "%%-%d.%ds | " width width)
  15225. string (format f str)
  15226. title (concat title string)))
  15227. (setq title (concat
  15228. (org-add-props " " nil 'display '(space :align-to 0))
  15229. (org-add-props title nil 'face '(:weight bold :underline t))))
  15230. (org-set-local 'org-previous-header-line-format header-line-format)
  15231. (org-set-local 'org-columns-current-widths (nreverse widths))
  15232. (setq header-line-format title)))
  15233. (defun org-columns-remove-overlays ()
  15234. "Remove all currently active column overlays."
  15235. (interactive)
  15236. (when (marker-buffer org-columns-begin-marker)
  15237. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15238. (when (local-variable-p 'org-previous-header-line-format)
  15239. (setq header-line-format org-previous-header-line-format)
  15240. (kill-local-variable 'org-previous-header-line-format))
  15241. (move-marker org-columns-begin-marker nil)
  15242. (move-marker org-columns-top-level-marker nil)
  15243. (org-unmodified
  15244. (mapc 'org-delete-overlay org-columns-overlays)
  15245. (setq org-columns-overlays nil)
  15246. (let ((inhibit-read-only t))
  15247. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15248. (defun org-columns-cleanup-item (item fmt)
  15249. "Remove from ITEM what is a column in the format FMT."
  15250. (if (not org-complex-heading-regexp)
  15251. item
  15252. (when (string-match org-complex-heading-regexp item)
  15253. (concat
  15254. (org-add-props (concat (match-string 1 item) " ") nil
  15255. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15256. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15257. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15258. " " (match-string 4 item)
  15259. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15260. (defun org-columns-show-value ()
  15261. "Show the full value of the property."
  15262. (interactive)
  15263. (let ((value (get-char-property (point) 'org-columns-value)))
  15264. (message "Value is: %s" (or value ""))))
  15265. (defun org-columns-quit ()
  15266. "Remove the column overlays and in this way exit column editing."
  15267. (interactive)
  15268. (org-unmodified
  15269. (org-columns-remove-overlays)
  15270. (let ((inhibit-read-only t))
  15271. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15272. (when (eq major-mode 'org-agenda-mode)
  15273. (message
  15274. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15275. (defun org-columns-check-computed ()
  15276. "Check if this column value is computed.
  15277. If yes, throw an error indicating that changing it does not make sense."
  15278. (let ((val (get-char-property (point) 'org-columns-value)))
  15279. (when (and (stringp val)
  15280. (get-char-property 0 'org-computed val))
  15281. (error "This value is computed from the entry's children"))))
  15282. (defun org-columns-todo (&optional arg)
  15283. "Change the TODO state during column view."
  15284. (interactive "P")
  15285. (org-columns-edit-value "TODO"))
  15286. (defun org-columns-set-tags-or-toggle (&optional arg)
  15287. "Toggle checkbox at point, or set tags for current headline."
  15288. (interactive "P")
  15289. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15290. (get-char-property (point) 'org-columns-value))
  15291. (org-columns-next-allowed-value)
  15292. (org-columns-edit-value "TAGS")))
  15293. (defun org-columns-edit-value (&optional key)
  15294. "Edit the value of the property at point in column view.
  15295. Where possible, use the standard interface for changing this line."
  15296. (interactive)
  15297. (org-columns-check-computed)
  15298. (let* ((external-key key)
  15299. (col (current-column))
  15300. (key (or key (get-char-property (point) 'org-columns-key)))
  15301. (value (get-char-property (point) 'org-columns-value))
  15302. (bol (point-at-bol)) (eol (point-at-eol))
  15303. (pom (or (get-text-property bol 'org-hd-marker)
  15304. (point))) ; keep despite of compiler waring
  15305. (line-overlays
  15306. (delq nil (mapcar (lambda (x)
  15307. (and (eq (overlay-buffer x) (current-buffer))
  15308. (>= (overlay-start x) bol)
  15309. (<= (overlay-start x) eol)
  15310. x))
  15311. org-columns-overlays)))
  15312. nval eval allowed)
  15313. (cond
  15314. ((equal key "CLOCKSUM")
  15315. (error "This special column cannot be edited"))
  15316. ((equal key "ITEM")
  15317. (setq eval '(org-with-point-at pom
  15318. (org-edit-headline))))
  15319. ((equal key "TODO")
  15320. (setq eval '(org-with-point-at pom
  15321. (let ((current-prefix-arg
  15322. (if external-key current-prefix-arg '(4))))
  15323. (call-interactively 'org-todo)))))
  15324. ((equal key "PRIORITY")
  15325. (setq eval '(org-with-point-at pom
  15326. (call-interactively 'org-priority))))
  15327. ((equal key "TAGS")
  15328. (setq eval '(org-with-point-at pom
  15329. (let ((org-fast-tag-selection-single-key
  15330. (if (eq org-fast-tag-selection-single-key 'expert)
  15331. t org-fast-tag-selection-single-key)))
  15332. (call-interactively 'org-set-tags)))))
  15333. ((equal key "DEADLINE")
  15334. (setq eval '(org-with-point-at pom
  15335. (call-interactively 'org-deadline))))
  15336. ((equal key "SCHEDULED")
  15337. (setq eval '(org-with-point-at pom
  15338. (call-interactively 'org-schedule))))
  15339. (t
  15340. (setq allowed (org-property-get-allowed-values pom key 'table))
  15341. (if allowed
  15342. (setq nval (completing-read "Value: " allowed nil t))
  15343. (setq nval (read-string "Edit: " value)))
  15344. (setq nval (org-trim nval))
  15345. (when (not (equal nval value))
  15346. (setq eval '(org-entry-put pom key nval)))))
  15347. (when eval
  15348. (let ((inhibit-read-only t))
  15349. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15350. (unwind-protect
  15351. (progn
  15352. (setq org-columns-overlays
  15353. (org-delete-all line-overlays org-columns-overlays))
  15354. (mapc 'org-delete-overlay line-overlays)
  15355. (org-columns-eval eval))
  15356. (org-columns-display-here))))
  15357. (move-to-column col)
  15358. (if (and (org-mode-p)
  15359. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15360. (org-columns-update key))))
  15361. (defun org-edit-headline () ; FIXME: this is not columns specific
  15362. "Edit the current headline, the part without TODO keyword, TAGS."
  15363. (org-back-to-heading)
  15364. (when (looking-at org-todo-line-regexp)
  15365. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15366. (txt (match-string 3))
  15367. (post "")
  15368. txt2)
  15369. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15370. (setq post (match-string 0 txt)
  15371. txt (substring txt 0 (match-beginning 0))))
  15372. (setq txt2 (read-string "Edit: " txt))
  15373. (when (not (equal txt txt2))
  15374. (beginning-of-line 1)
  15375. (insert pre txt2 post)
  15376. (delete-region (point) (point-at-eol))
  15377. (org-set-tags nil t)))))
  15378. (defun org-columns-edit-allowed ()
  15379. "Edit the list of allowed values for the current property."
  15380. (interactive)
  15381. (let* ((key (get-char-property (point) 'org-columns-key))
  15382. (key1 (concat key "_ALL"))
  15383. (allowed (org-entry-get (point) key1 t))
  15384. nval)
  15385. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15386. (setq nval (read-string "Allowed: " allowed))
  15387. (org-entry-put
  15388. (cond ((marker-position org-entry-property-inherited-from)
  15389. org-entry-property-inherited-from)
  15390. ((marker-position org-columns-top-level-marker)
  15391. org-columns-top-level-marker))
  15392. key1 nval)))
  15393. (defmacro org-no-warnings (&rest body)
  15394. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15395. (defun org-columns-eval (form)
  15396. (let (hidep)
  15397. (save-excursion
  15398. (beginning-of-line 1)
  15399. ;; `next-line' is needed here, because it skips invisible line.
  15400. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15401. (setq hidep (org-on-heading-p 1)))
  15402. (eval form)
  15403. (and hidep (hide-entry))))
  15404. (defun org-columns-previous-allowed-value ()
  15405. "Switch to the previous allowed value for this column."
  15406. (interactive)
  15407. (org-columns-next-allowed-value t))
  15408. (defun org-columns-next-allowed-value (&optional previous)
  15409. "Switch to the next allowed value for this column."
  15410. (interactive)
  15411. (org-columns-check-computed)
  15412. (let* ((col (current-column))
  15413. (key (get-char-property (point) 'org-columns-key))
  15414. (value (get-char-property (point) 'org-columns-value))
  15415. (bol (point-at-bol)) (eol (point-at-eol))
  15416. (pom (or (get-text-property bol 'org-hd-marker)
  15417. (point))) ; keep despite of compiler waring
  15418. (line-overlays
  15419. (delq nil (mapcar (lambda (x)
  15420. (and (eq (overlay-buffer x) (current-buffer))
  15421. (>= (overlay-start x) bol)
  15422. (<= (overlay-start x) eol)
  15423. x))
  15424. org-columns-overlays)))
  15425. (allowed (or (org-property-get-allowed-values pom key)
  15426. (and (memq
  15427. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15428. '(checkbox checkbox-n-of-m checkbox-percent))
  15429. '("[ ]" "[X]"))))
  15430. nval)
  15431. (when (equal key "ITEM")
  15432. (error "Cannot edit item headline from here"))
  15433. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15434. (error "Allowed values for this property have not been defined"))
  15435. (if (member key '("SCHEDULED" "DEADLINE"))
  15436. (setq nval (if previous 'earlier 'later))
  15437. (if previous (setq allowed (reverse allowed)))
  15438. (if (member value allowed)
  15439. (setq nval (car (cdr (member value allowed)))))
  15440. (setq nval (or nval (car allowed)))
  15441. (if (equal nval value)
  15442. (error "Only one allowed value for this property")))
  15443. (let ((inhibit-read-only t))
  15444. (remove-text-properties (1- bol) eol '(read-only t))
  15445. (unwind-protect
  15446. (progn
  15447. (setq org-columns-overlays
  15448. (org-delete-all line-overlays org-columns-overlays))
  15449. (mapc 'org-delete-overlay line-overlays)
  15450. (org-columns-eval '(org-entry-put pom key nval)))
  15451. (org-columns-display-here)))
  15452. (move-to-column col)
  15453. (if (and (org-mode-p)
  15454. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15455. (org-columns-update key))))
  15456. (defun org-verify-version (task)
  15457. (cond
  15458. ((eq task 'columns)
  15459. (if (or (featurep 'xemacs)
  15460. (< emacs-major-version 22))
  15461. (error "Emacs 22 is required for the columns feature")))))
  15462. (defun org-columns-open-link (&optional arg)
  15463. (interactive "P")
  15464. (let ((value (get-char-property (point) 'org-columns-value)))
  15465. (org-open-link-from-string value arg)))
  15466. (defun org-open-link-from-string (s &optional arg)
  15467. "Open a link in the string S, as if it was in Org-mode."
  15468. (interactive)
  15469. (with-temp-buffer
  15470. (let ((org-inhibit-startup t))
  15471. (org-mode)
  15472. (insert s)
  15473. (goto-char (point-min))
  15474. (org-open-at-point arg))))
  15475. (defun org-columns-get-format-and-top-level ()
  15476. (let (fmt)
  15477. (when (condition-case nil (org-back-to-heading) (error nil))
  15478. (move-marker org-entry-property-inherited-from nil)
  15479. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15480. (setq fmt (or fmt org-columns-default-format))
  15481. (org-set-local 'org-columns-current-fmt fmt)
  15482. (org-columns-compile-format fmt)
  15483. (if (marker-position org-entry-property-inherited-from)
  15484. (move-marker org-columns-top-level-marker
  15485. org-entry-property-inherited-from)
  15486. (move-marker org-columns-top-level-marker (point)))
  15487. fmt))
  15488. (defun org-columns ()
  15489. "Turn on column view on an org-mode file."
  15490. (interactive)
  15491. (org-verify-version 'columns)
  15492. (org-columns-remove-overlays)
  15493. (move-marker org-columns-begin-marker (point))
  15494. (let (beg end fmt cache maxwidths)
  15495. (setq fmt (org-columns-get-format-and-top-level))
  15496. (save-excursion
  15497. (goto-char org-columns-top-level-marker)
  15498. (setq beg (point))
  15499. (unless org-columns-inhibit-recalculation
  15500. (org-columns-compute-all))
  15501. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15502. (point-max)))
  15503. ;; Get and cache the properties
  15504. (goto-char beg)
  15505. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15506. (save-excursion
  15507. (save-restriction
  15508. (narrow-to-region beg end)
  15509. (org-clock-sum))))
  15510. (while (re-search-forward (concat "^" outline-regexp) end t)
  15511. (push (cons (org-current-line) (org-entry-properties)) cache))
  15512. (when cache
  15513. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15514. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15515. (org-columns-display-here-title)
  15516. (mapc (lambda (x)
  15517. (goto-line (car x))
  15518. (org-columns-display-here (cdr x)))
  15519. cache)))))
  15520. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15521. "Insert a new column, to the leeft o the current column."
  15522. (interactive)
  15523. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15524. cell)
  15525. (setq prop (completing-read
  15526. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15527. nil nil prop))
  15528. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15529. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15530. (if (string-match "\\S-" width)
  15531. (setq width (string-to-number width))
  15532. (setq width nil))
  15533. (setq fmt (completing-read "Summary [none]: "
  15534. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15535. nil t))
  15536. (if (string-match "\\S-" fmt)
  15537. (setq fmt (intern fmt))
  15538. (setq fmt nil))
  15539. (if (eq fmt 'none) (setq fmt nil))
  15540. (if editp
  15541. (progn
  15542. (setcar editp prop)
  15543. (setcdr editp (list title width nil fmt)))
  15544. (setq cell (nthcdr (1- (current-column))
  15545. org-columns-current-fmt-compiled))
  15546. (setcdr cell (cons (list prop title width nil fmt)
  15547. (cdr cell))))
  15548. (org-columns-store-format)
  15549. (org-columns-redo)))
  15550. (defun org-columns-delete ()
  15551. "Delete the column at point from columns view."
  15552. (interactive)
  15553. (let* ((n (current-column))
  15554. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15555. (when (y-or-n-p
  15556. (format "Are you sure you want to remove column \"%s\"? " title))
  15557. (setq org-columns-current-fmt-compiled
  15558. (delq (nth n org-columns-current-fmt-compiled)
  15559. org-columns-current-fmt-compiled))
  15560. (org-columns-store-format)
  15561. (org-columns-redo)
  15562. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15563. (backward-char 1)))))
  15564. (defun org-columns-edit-attributes ()
  15565. "Edit the attributes of the current column."
  15566. (interactive)
  15567. (let* ((n (current-column))
  15568. (info (nth n org-columns-current-fmt-compiled)))
  15569. (apply 'org-columns-new info)))
  15570. (defun org-columns-widen (arg)
  15571. "Make the column wider by ARG characters."
  15572. (interactive "p")
  15573. (let* ((n (current-column))
  15574. (entry (nth n org-columns-current-fmt-compiled))
  15575. (width (or (nth 2 entry)
  15576. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15577. (setq width (max 1 (+ width arg)))
  15578. (setcar (nthcdr 2 entry) width)
  15579. (org-columns-store-format)
  15580. (org-columns-redo)))
  15581. (defun org-columns-narrow (arg)
  15582. "Make the column nrrower by ARG characters."
  15583. (interactive "p")
  15584. (org-columns-widen (- arg)))
  15585. (defun org-columns-move-right ()
  15586. "Swap this column with the one to the right."
  15587. (interactive)
  15588. (let* ((n (current-column))
  15589. (cell (nthcdr n org-columns-current-fmt-compiled))
  15590. e)
  15591. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15592. (error "Cannot shift this column further to the right"))
  15593. (setq e (car cell))
  15594. (setcar cell (car (cdr cell)))
  15595. (setcdr cell (cons e (cdr (cdr cell))))
  15596. (org-columns-store-format)
  15597. (org-columns-redo)
  15598. (forward-char 1)))
  15599. (defun org-columns-move-left ()
  15600. "Swap this column with the one to the left."
  15601. (interactive)
  15602. (let* ((n (current-column)))
  15603. (when (= n 0)
  15604. (error "Cannot shift this column further to the left"))
  15605. (backward-char 1)
  15606. (org-columns-move-right)
  15607. (backward-char 1)))
  15608. (defun org-columns-store-format ()
  15609. "Store the text version of the current columns format in appropriate place.
  15610. This is either in the COLUMNS property of the node starting the current column
  15611. display, or in the #+COLUMNS line of the current buffer."
  15612. (let (fmt (cnt 0))
  15613. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15614. (org-set-local 'org-columns-current-fmt fmt)
  15615. (if (marker-position org-columns-top-level-marker)
  15616. (save-excursion
  15617. (goto-char org-columns-top-level-marker)
  15618. (if (and (org-at-heading-p)
  15619. (org-entry-get nil "COLUMNS"))
  15620. (org-entry-put nil "COLUMNS" fmt)
  15621. (goto-char (point-min))
  15622. ;; Overwrite all #+COLUMNS lines....
  15623. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15624. (setq cnt (1+ cnt))
  15625. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15626. (unless (> cnt 0)
  15627. (goto-char (point-min))
  15628. (or (org-on-heading-p t) (outline-next-heading))
  15629. (let ((inhibit-read-only t))
  15630. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15631. (org-set-local 'org-columns-default-format fmt))))))
  15632. (defvar org-overriding-columns-format nil
  15633. "When set, overrides any other definition.")
  15634. (defvar org-agenda-view-columns-initially nil
  15635. "When set, switch to columns view immediately after creating the agenda.")
  15636. (defun org-agenda-columns ()
  15637. "Turn on column view in the agenda."
  15638. (interactive)
  15639. (org-verify-version 'columns)
  15640. (org-columns-remove-overlays)
  15641. (move-marker org-columns-begin-marker (point))
  15642. (let (fmt cache maxwidths m)
  15643. (cond
  15644. ((and (local-variable-p 'org-overriding-columns-format)
  15645. org-overriding-columns-format)
  15646. (setq fmt org-overriding-columns-format))
  15647. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15648. (setq fmt (org-entry-get m "COLUMNS" t)))
  15649. ((and (boundp 'org-columns-current-fmt)
  15650. (local-variable-p 'org-columns-current-fmt)
  15651. org-columns-current-fmt)
  15652. (setq fmt org-columns-current-fmt))
  15653. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15654. (setq m (get-text-property m 'org-hd-marker))
  15655. (setq fmt (org-entry-get m "COLUMNS" t))))
  15656. (setq fmt (or fmt org-columns-default-format))
  15657. (org-set-local 'org-columns-current-fmt fmt)
  15658. (org-columns-compile-format fmt)
  15659. (save-excursion
  15660. ;; Get and cache the properties
  15661. (goto-char (point-min))
  15662. (while (not (eobp))
  15663. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15664. (get-text-property (point) 'org-marker)))
  15665. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15666. (beginning-of-line 2))
  15667. (when cache
  15668. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15669. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15670. (org-columns-display-here-title)
  15671. (mapc (lambda (x)
  15672. (goto-line (car x))
  15673. (org-columns-display-here (cdr x)))
  15674. cache)))))
  15675. (defun org-columns-get-autowidth-alist (s cache)
  15676. "Derive the maximum column widths from the format and the cache."
  15677. (let ((start 0) rtn)
  15678. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15679. (push (cons (match-string 1 s) 1) rtn)
  15680. (setq start (match-end 0)))
  15681. (mapc (lambda (x)
  15682. (setcdr x (apply 'max
  15683. (mapcar
  15684. (lambda (y)
  15685. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15686. cache))))
  15687. rtn)
  15688. rtn))
  15689. (defun org-columns-compute-all ()
  15690. "Compute all columns that have operators defined."
  15691. (org-unmodified
  15692. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15693. (let ((columns org-columns-current-fmt-compiled) col)
  15694. (while (setq col (pop columns))
  15695. (when (nth 3 col)
  15696. (save-excursion
  15697. (org-columns-compute (car col)))))))
  15698. (defun org-columns-update (property)
  15699. "Recompute PROPERTY, and update the columns display for it."
  15700. (org-columns-compute property)
  15701. (let (fmt val pos)
  15702. (save-excursion
  15703. (mapc (lambda (ov)
  15704. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15705. (setq pos (org-overlay-start ov))
  15706. (goto-char pos)
  15707. (when (setq val (cdr (assoc property
  15708. (get-text-property
  15709. (point-at-bol) 'org-summaries))))
  15710. (setq fmt (org-overlay-get ov 'org-columns-format))
  15711. (org-overlay-put ov 'org-columns-value val)
  15712. (org-overlay-put ov 'display (format fmt val)))))
  15713. org-columns-overlays))))
  15714. (defun org-columns-compute (property)
  15715. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15716. (interactive)
  15717. (let* ((re (concat "^" outline-regexp))
  15718. (lmax 30) ; Does anyone use deeper levels???
  15719. (lsum (make-vector lmax 0))
  15720. (lflag (make-vector lmax nil))
  15721. (level 0)
  15722. (ass (assoc property org-columns-current-fmt-compiled))
  15723. (format (nth 4 ass))
  15724. (printf (nth 5 ass))
  15725. (beg org-columns-top-level-marker)
  15726. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15727. (save-excursion
  15728. ;; Find the region to compute
  15729. (goto-char beg)
  15730. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15731. (goto-char end)
  15732. ;; Walk the tree from the back and do the computations
  15733. (while (re-search-backward re beg t)
  15734. (setq sumpos (match-beginning 0)
  15735. last-level level
  15736. level (org-outline-level)
  15737. val (org-entry-get nil property)
  15738. valflag (and val (string-match "\\S-" val)))
  15739. (cond
  15740. ((< level last-level)
  15741. ;; put the sum of lower levels here as a property
  15742. (setq sum (aref lsum last-level) ; current sum
  15743. flag (aref lflag last-level) ; any valid entries from children?
  15744. str (org-column-number-to-string sum format printf)
  15745. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15746. useval (if flag str1 (if valflag val ""))
  15747. sum-alist (get-text-property sumpos 'org-summaries))
  15748. (if (assoc property sum-alist)
  15749. (setcdr (assoc property sum-alist) useval)
  15750. (push (cons property useval) sum-alist)
  15751. (org-unmodified
  15752. (add-text-properties sumpos (1+ sumpos)
  15753. (list 'org-summaries sum-alist))))
  15754. (when val
  15755. (org-entry-put nil property (if flag str val)))
  15756. ;; add current to current level accumulator
  15757. (when (or flag valflag)
  15758. (aset lsum level (+ (aref lsum level)
  15759. (if flag sum (org-column-string-to-number
  15760. (if flag str val) format))))
  15761. (aset lflag level t))
  15762. ;; clear accumulators for deeper levels
  15763. (loop for l from (1+ level) to (1- lmax) do
  15764. (aset lsum l 0)
  15765. (aset lflag l nil)))
  15766. ((>= level last-level)
  15767. ;; add what we have here to the accumulator for this level
  15768. (aset lsum level (+ (aref lsum level)
  15769. (org-column-string-to-number (or val "0") format)))
  15770. (and valflag (aset lflag level t)))
  15771. (t (error "This should not happen")))))))
  15772. (defun org-columns-redo ()
  15773. "Construct the column display again."
  15774. (interactive)
  15775. (message "Recomputing columns...")
  15776. (save-excursion
  15777. (if (marker-position org-columns-begin-marker)
  15778. (goto-char org-columns-begin-marker))
  15779. (org-columns-remove-overlays)
  15780. (if (org-mode-p)
  15781. (call-interactively 'org-columns)
  15782. (call-interactively 'org-agenda-columns)))
  15783. (message "Recomputing columns...done"))
  15784. (defun org-columns-not-in-agenda ()
  15785. (if (eq major-mode 'org-agenda-mode)
  15786. (error "This command is only allowed in Org-mode buffers")))
  15787. (defun org-string-to-number (s)
  15788. "Convert string to number, and interpret hh:mm:ss."
  15789. (if (not (string-match ":" s))
  15790. (string-to-number s)
  15791. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15792. (while l
  15793. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15794. sum)))
  15795. (defun org-column-number-to-string (n fmt &optional printf)
  15796. "Convert a computed column number to a string value, according to FMT."
  15797. (cond
  15798. ((eq fmt 'add_times)
  15799. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15800. (format "%d:%02d" h m)))
  15801. ((eq fmt 'checkbox)
  15802. (cond ((= n (floor n)) "[X]")
  15803. ((> n 1.) "[-]")
  15804. (t "[ ]")))
  15805. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15806. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15807. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15808. (printf (format printf n))
  15809. ((eq fmt 'currency)
  15810. (format "%.2f" n))
  15811. (t (number-to-string n))))
  15812. (defun org-nofm-to-completion (n m &optional percent)
  15813. (if (not percent)
  15814. (format "[%d/%d]" n m)
  15815. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15816. (defun org-column-string-to-number (s fmt)
  15817. "Convert a column value to a number that can be used for column computing."
  15818. (cond
  15819. ((string-match ":" s)
  15820. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15821. (while l
  15822. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15823. sum))
  15824. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15825. (if (equal s "[X]") 1. 0.000001))
  15826. (t (string-to-number s))))
  15827. (defun org-columns-uncompile-format (cfmt)
  15828. "Turn the compiled columns format back into a string representation."
  15829. (let ((rtn "") e s prop title op width fmt printf)
  15830. (while (setq e (pop cfmt))
  15831. (setq prop (car e)
  15832. title (nth 1 e)
  15833. width (nth 2 e)
  15834. op (nth 3 e)
  15835. fmt (nth 4 e)
  15836. printf (nth 5 e))
  15837. (cond
  15838. ((eq fmt 'add_times) (setq op ":"))
  15839. ((eq fmt 'checkbox) (setq op "X"))
  15840. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15841. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15842. ((eq fmt 'add_numbers) (setq op "+"))
  15843. ((eq fmt 'currency) (setq op "$")))
  15844. (if (and op printf) (setq op (concat op ";" printf)))
  15845. (if (equal title prop) (setq title nil))
  15846. (setq s (concat "%" (if width (number-to-string width))
  15847. prop
  15848. (if title (concat "(" title ")"))
  15849. (if op (concat "{" op "}"))))
  15850. (setq rtn (concat rtn " " s)))
  15851. (org-trim rtn)))
  15852. (defun org-columns-compile-format (fmt)
  15853. "Turn a column format string into an alist of specifications.
  15854. The alist has one entry for each column in the format. The elements of
  15855. that list are:
  15856. property the property
  15857. title the title field for the columns
  15858. width the column width in characters, can be nil for automatic
  15859. operator the operator if any
  15860. format the output format for computed results, derived from operator
  15861. printf a printf format for computed values"
  15862. (let ((start 0) width prop title op f printf)
  15863. (setq org-columns-current-fmt-compiled nil)
  15864. (while (string-match
  15865. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15866. fmt start)
  15867. (setq start (match-end 0)
  15868. width (match-string 1 fmt)
  15869. prop (match-string 2 fmt)
  15870. title (or (match-string 3 fmt) prop)
  15871. op (match-string 4 fmt)
  15872. f nil
  15873. printf nil)
  15874. (if width (setq width (string-to-number width)))
  15875. (when (and op (string-match ";" op))
  15876. (setq printf (substring op (match-end 0))
  15877. op (substring op 0 (match-beginning 0))))
  15878. (cond
  15879. ((equal op "+") (setq f 'add_numbers))
  15880. ((equal op "$") (setq f 'currency))
  15881. ((equal op ":") (setq f 'add_times))
  15882. ((equal op "X") (setq f 'checkbox))
  15883. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15884. ((equal op "X%") (setq f 'checkbox-percent))
  15885. )
  15886. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15887. (setq org-columns-current-fmt-compiled
  15888. (nreverse org-columns-current-fmt-compiled))))
  15889. ;;; Dynamic block for Column view
  15890. (defun org-columns-capture-view ()
  15891. "Get the column view of the current buffer and return it as a list.
  15892. The list will contains the title row and all other rows. Each row is
  15893. a list of fields."
  15894. (save-excursion
  15895. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15896. (n (length title)) row tbl)
  15897. (goto-char (point-min))
  15898. (while (re-search-forward "^\\*+ " nil t)
  15899. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15900. (setq row nil)
  15901. (loop for i from 0 to (1- n) do
  15902. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15903. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15904. "")
  15905. row))
  15906. (setq row (nreverse row))
  15907. (push row tbl)))
  15908. (append (list title 'hline) (nreverse tbl)))))
  15909. (defun org-dblock-write:columnview (params)
  15910. "Write the column view table.
  15911. PARAMS is a property list of parameters:
  15912. :width enforce same column widths with <N> specifiers.
  15913. :id the :ID: property of the entry where the columns view
  15914. should be built, as a string. When `local', call locally.
  15915. When `global' call column view with the cursor at the beginning
  15916. of the buffer (usually this means that the whole buffer switches
  15917. to column view).
  15918. :hlines When t, insert a hline before each item. When a number, insert
  15919. a hline before each level <= that number.
  15920. :vlines When t, make each column a colgroup to enforce vertical lines."
  15921. (let ((pos (move-marker (make-marker) (point)))
  15922. (hlines (plist-get params :hlines))
  15923. (vlines (plist-get params :vlines))
  15924. tbl id idpos nfields tmp)
  15925. (save-excursion
  15926. (save-restriction
  15927. (when (setq id (plist-get params :id))
  15928. (cond ((not id) nil)
  15929. ((eq id 'global) (goto-char (point-min)))
  15930. ((eq id 'local) nil)
  15931. ((setq idpos (org-find-entry-with-id id))
  15932. (goto-char idpos))
  15933. (t (error "Cannot find entry with :ID: %s" id))))
  15934. (org-columns)
  15935. (setq tbl (org-columns-capture-view))
  15936. (setq nfields (length (car tbl)))
  15937. (org-columns-quit)))
  15938. (goto-char pos)
  15939. (move-marker pos nil)
  15940. (when tbl
  15941. (when (plist-get params :hlines)
  15942. (setq tmp nil)
  15943. (while tbl
  15944. (if (eq (car tbl) 'hline)
  15945. (push (pop tbl) tmp)
  15946. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15947. (if (and (not (eq (car tmp) 'hline))
  15948. (or (eq hlines t)
  15949. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15950. (push 'hline tmp)))
  15951. (push (pop tbl) tmp)))
  15952. (setq tbl (nreverse tmp)))
  15953. (when vlines
  15954. (setq tbl (mapcar (lambda (x)
  15955. (if (eq 'hline x) x (cons "" x)))
  15956. tbl))
  15957. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15958. (setq pos (point))
  15959. (insert (org-listtable-to-string tbl))
  15960. (when (plist-get params :width)
  15961. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15962. org-columns-current-widths "|")))
  15963. (goto-char pos)
  15964. (org-table-align))))
  15965. (defun org-listtable-to-string (tbl)
  15966. "Convert a listtable TBL to a string that contains the Org-mode table.
  15967. The table still need to be alligned. The resulting string has no leading
  15968. and tailing newline characters."
  15969. (mapconcat
  15970. (lambda (x)
  15971. (cond
  15972. ((listp x)
  15973. (concat "|" (mapconcat 'identity x "|") "|"))
  15974. ((eq x 'hline) "|-|")
  15975. (t (error "Garbage in listtable: %s" x))))
  15976. tbl "\n"))
  15977. (defun org-insert-columns-dblock ()
  15978. "Create a dynamic block capturing a column view table."
  15979. (interactive)
  15980. (let ((defaults '(:name "columnview" :hlines 1))
  15981. (id (completing-read
  15982. "Capture columns (local, global, entry with :ID: property) [local]: "
  15983. (append '(("global") ("local"))
  15984. (mapcar 'list (org-property-values "ID"))))))
  15985. (if (equal id "") (setq id 'local))
  15986. (if (equal id "global") (setq id 'global))
  15987. (setq defaults (append defaults (list :id id)))
  15988. (org-create-dblock defaults)
  15989. (org-update-dblock)))
  15990. ;;;; Timestamps
  15991. (defvar org-last-changed-timestamp nil)
  15992. (defvar org-time-was-given) ; dynamically scoped parameter
  15993. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15994. (defvar org-ts-what) ; dynamically scoped parameter
  15995. (defun org-time-stamp (arg)
  15996. "Prompt for a date/time and insert a time stamp.
  15997. If the user specifies a time like HH:MM, or if this command is called
  15998. with a prefix argument, the time stamp will contain date and time.
  15999. Otherwise, only the date will be included. All parts of a date not
  16000. specified by the user will be filled in from the current date/time.
  16001. So if you press just return without typing anything, the time stamp
  16002. will represent the current date/time. If there is already a timestamp
  16003. at the cursor, it will be modified."
  16004. (interactive "P")
  16005. (let* ((ts nil)
  16006. (default-time
  16007. ;; Default time is either today, or, when entering a range,
  16008. ;; the range start.
  16009. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16010. (save-excursion
  16011. (re-search-backward
  16012. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16013. (- (point) 20) t)))
  16014. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16015. (current-time)))
  16016. (default-input (and ts (org-get-compact-tod ts)))
  16017. org-time-was-given org-end-time-was-given time)
  16018. (cond
  16019. ((and (org-at-timestamp-p)
  16020. (eq last-command 'org-time-stamp)
  16021. (eq this-command 'org-time-stamp))
  16022. (insert "--")
  16023. (setq time (let ((this-command this-command))
  16024. (org-read-date arg 'totime nil nil default-time default-input)))
  16025. (org-insert-time-stamp time (or org-time-was-given arg)))
  16026. ((org-at-timestamp-p)
  16027. (setq time (let ((this-command this-command))
  16028. (org-read-date arg 'totime nil nil default-time default-input)))
  16029. (when (org-at-timestamp-p) ; just to get the match data
  16030. (replace-match "")
  16031. (setq org-last-changed-timestamp
  16032. (org-insert-time-stamp
  16033. time (or org-time-was-given arg)
  16034. nil nil nil (list org-end-time-was-given))))
  16035. (message "Timestamp updated"))
  16036. (t
  16037. (setq time (let ((this-command this-command))
  16038. (org-read-date arg 'totime nil nil default-time default-input)))
  16039. (org-insert-time-stamp time (or org-time-was-given arg)
  16040. nil nil nil (list org-end-time-was-given))))))
  16041. ;; FIXME: can we use this for something else????
  16042. ;; like computing time differences?????
  16043. (defun org-get-compact-tod (s)
  16044. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16045. (let* ((t1 (match-string 1 s))
  16046. (h1 (string-to-number (match-string 2 s)))
  16047. (m1 (string-to-number (match-string 3 s)))
  16048. (t2 (and (match-end 4) (match-string 5 s)))
  16049. (h2 (and t2 (string-to-number (match-string 6 s))))
  16050. (m2 (and t2 (string-to-number (match-string 7 s))))
  16051. dh dm)
  16052. (if (not t2)
  16053. t1
  16054. (setq dh (- h2 h1) dm (- m2 m1))
  16055. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16056. (concat t1 "+" (number-to-string dh)
  16057. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16058. (defun org-time-stamp-inactive (&optional arg)
  16059. "Insert an inactive time stamp.
  16060. An inactive time stamp is enclosed in square brackets instead of angle
  16061. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16062. does not link to the calendar and cannot be changed with the S-cursor keys.
  16063. So these are more for recording a certain time/date."
  16064. (interactive "P")
  16065. (let (org-time-was-given org-end-time-was-given time)
  16066. (setq time (org-read-date arg 'totime))
  16067. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16068. nil nil (list org-end-time-was-given))))
  16069. (defvar org-date-ovl (org-make-overlay 1 1))
  16070. (org-overlay-put org-date-ovl 'face 'org-warning)
  16071. (org-detach-overlay org-date-ovl)
  16072. (defvar org-ans1) ; dynamically scoped parameter
  16073. (defvar org-ans2) ; dynamically scoped parameter
  16074. (defvar org-plain-time-of-day-regexp) ; defined below
  16075. (defvar org-read-date-overlay nil)
  16076. (defvar org-dcst nil) ; dynamically scoped
  16077. (defun org-read-date (&optional with-time to-time from-string prompt
  16078. default-time default-input)
  16079. "Read a date, possibly a time, and make things smooth for the user.
  16080. The prompt will suggest to enter an ISO date, but you can also enter anything
  16081. which will at least partially be understood by `parse-time-string'.
  16082. Unrecognized parts of the date will default to the current day, month, year,
  16083. hour and minute. If this command is called to replace a timestamp at point,
  16084. of to enter the second timestamp of a range, the default time is taken from the
  16085. existing stamp. For example,
  16086. 3-2-5 --> 2003-02-05
  16087. feb 15 --> currentyear-02-15
  16088. sep 12 9 --> 2009-09-12
  16089. 12:45 --> today 12:45
  16090. 22 sept 0:34 --> currentyear-09-22 0:34
  16091. 12 --> currentyear-currentmonth-12
  16092. Fri --> nearest Friday (today or later)
  16093. etc.
  16094. Furthermore you can specify a relative date by giving, as the *first* thing
  16095. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16096. change in days weeks, months, years.
  16097. With a single plus or minus, the date is relative to today. With a double
  16098. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16099. +4d --> four days from today
  16100. +4 --> same as above
  16101. +2w --> two weeks from today
  16102. ++5 --> five days from default date
  16103. The function understands only English month and weekday abbreviations,
  16104. but this can be configured with the variables `parse-time-months' and
  16105. `parse-time-weekdays'.
  16106. While prompting, a calendar is popped up - you can also select the
  16107. date with the mouse (button 1). The calendar shows a period of three
  16108. months. To scroll it to other months, use the keys `>' and `<'.
  16109. If you don't like the calendar, turn it off with
  16110. \(setq org-read-date-popup-calendar nil)
  16111. With optional argument TO-TIME, the date will immediately be converted
  16112. to an internal time.
  16113. With an optional argument WITH-TIME, the prompt will suggest to also
  16114. insert a time. Note that when WITH-TIME is not set, you can still
  16115. enter a time, and this function will inform the calling routine about
  16116. this change. The calling routine may then choose to change the format
  16117. used to insert the time stamp into the buffer to include the time.
  16118. With optional argument FROM-STRING, read from this string instead from
  16119. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16120. the time/date that is used for everything that is not specified by the
  16121. user."
  16122. (require 'parse-time)
  16123. (let* ((org-time-stamp-rounding-minutes
  16124. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16125. (org-dcst org-display-custom-times)
  16126. (ct (org-current-time))
  16127. (def (or default-time ct))
  16128. (defdecode (decode-time def))
  16129. (dummy (progn
  16130. (when (< (nth 2 defdecode) org-extend-today-until)
  16131. (setcar (nthcdr 2 defdecode) -1)
  16132. (setcar (nthcdr 1 defdecode) 59)
  16133. (setq def (apply 'encode-time defdecode)
  16134. defdecode (decode-time def)))))
  16135. (calendar-move-hook nil)
  16136. (view-diary-entries-initially nil)
  16137. (view-calendar-holidays-initially nil)
  16138. (timestr (format-time-string
  16139. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16140. (prompt (concat (if prompt (concat prompt " ") "")
  16141. (format "Date+time [%s]: " timestr)))
  16142. ans (org-ans0 "") org-ans1 org-ans2 final)
  16143. (cond
  16144. (from-string (setq ans from-string))
  16145. (org-read-date-popup-calendar
  16146. (save-excursion
  16147. (save-window-excursion
  16148. (calendar)
  16149. (calendar-forward-day (- (time-to-days def)
  16150. (calendar-absolute-from-gregorian
  16151. (calendar-current-date))))
  16152. (org-eval-in-calendar nil t)
  16153. (let* ((old-map (current-local-map))
  16154. (map (copy-keymap calendar-mode-map))
  16155. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16156. (org-defkey map (kbd "RET") 'org-calendar-select)
  16157. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16158. 'org-calendar-select-mouse)
  16159. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16160. 'org-calendar-select-mouse)
  16161. (org-defkey minibuffer-local-map [(meta shift left)]
  16162. (lambda () (interactive)
  16163. (org-eval-in-calendar '(calendar-backward-month 1))))
  16164. (org-defkey minibuffer-local-map [(meta shift right)]
  16165. (lambda () (interactive)
  16166. (org-eval-in-calendar '(calendar-forward-month 1))))
  16167. (org-defkey minibuffer-local-map [(meta shift up)]
  16168. (lambda () (interactive)
  16169. (org-eval-in-calendar '(calendar-backward-year 1))))
  16170. (org-defkey minibuffer-local-map [(meta shift down)]
  16171. (lambda () (interactive)
  16172. (org-eval-in-calendar '(calendar-forward-year 1))))
  16173. (org-defkey minibuffer-local-map [(shift up)]
  16174. (lambda () (interactive)
  16175. (org-eval-in-calendar '(calendar-backward-week 1))))
  16176. (org-defkey minibuffer-local-map [(shift down)]
  16177. (lambda () (interactive)
  16178. (org-eval-in-calendar '(calendar-forward-week 1))))
  16179. (org-defkey minibuffer-local-map [(shift left)]
  16180. (lambda () (interactive)
  16181. (org-eval-in-calendar '(calendar-backward-day 1))))
  16182. (org-defkey minibuffer-local-map [(shift right)]
  16183. (lambda () (interactive)
  16184. (org-eval-in-calendar '(calendar-forward-day 1))))
  16185. (org-defkey minibuffer-local-map ">"
  16186. (lambda () (interactive)
  16187. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16188. (org-defkey minibuffer-local-map "<"
  16189. (lambda () (interactive)
  16190. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16191. (unwind-protect
  16192. (progn
  16193. (use-local-map map)
  16194. (add-hook 'post-command-hook 'org-read-date-display)
  16195. (setq org-ans0 (read-string prompt default-input nil nil))
  16196. ;; org-ans0: from prompt
  16197. ;; org-ans1: from mouse click
  16198. ;; org-ans2: from calendar motion
  16199. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16200. (remove-hook 'post-command-hook 'org-read-date-display)
  16201. (use-local-map old-map)
  16202. (when org-read-date-overlay
  16203. (org-delete-overlay org-read-date-overlay)
  16204. (setq org-read-date-overlay nil)))))))
  16205. (t ; Naked prompt only
  16206. (unwind-protect
  16207. (setq ans (read-string prompt default-input nil timestr))
  16208. (when org-read-date-overlay
  16209. (org-delete-overlay org-read-date-overlay)
  16210. (setq org-read-date-overlay nil)))))
  16211. (setq final (org-read-date-analyze ans def defdecode))
  16212. (if to-time
  16213. (apply 'encode-time final)
  16214. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16215. (format "%04d-%02d-%02d %02d:%02d"
  16216. (nth 5 final) (nth 4 final) (nth 3 final)
  16217. (nth 2 final) (nth 1 final))
  16218. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16219. (defvar def)
  16220. (defvar defdecode)
  16221. (defvar with-time)
  16222. (defun org-read-date-display ()
  16223. "Display the currrent date prompt interpretation in the minibuffer."
  16224. (when org-read-date-display-live
  16225. (when org-read-date-overlay
  16226. (org-delete-overlay org-read-date-overlay))
  16227. (let ((p (point)))
  16228. (end-of-line 1)
  16229. (while (not (equal (buffer-substring
  16230. (max (point-min) (- (point) 4)) (point))
  16231. " "))
  16232. (insert " "))
  16233. (goto-char p))
  16234. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16235. " " (or org-ans1 org-ans2)))
  16236. (org-end-time-was-given nil)
  16237. (f (org-read-date-analyze ans def defdecode))
  16238. (fmts (if org-dcst
  16239. org-time-stamp-custom-formats
  16240. org-time-stamp-formats))
  16241. (fmt (if (or with-time
  16242. (and (boundp 'org-time-was-given) org-time-was-given))
  16243. (cdr fmts)
  16244. (car fmts)))
  16245. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16246. (when (and org-end-time-was-given
  16247. (string-match org-plain-time-of-day-regexp txt))
  16248. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16249. org-end-time-was-given
  16250. (substring txt (match-end 0)))))
  16251. (setq org-read-date-overlay
  16252. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16253. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16254. (defun org-read-date-analyze (ans def defdecode)
  16255. "Analyze the combined answer of the date prompt."
  16256. ;; FIXME: cleanup and comment
  16257. (let (delta deltan deltaw deltadef year month day
  16258. hour minute second wday pm h2 m2 tl wday1)
  16259. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16260. (setq ans (replace-match "" t t ans)
  16261. deltan (car delta)
  16262. deltaw (nth 1 delta)
  16263. deltadef (nth 2 delta)))
  16264. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16265. (when (string-match
  16266. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16267. (setq year (if (match-end 2)
  16268. (string-to-number (match-string 2 ans))
  16269. (string-to-number (format-time-string "%Y")))
  16270. month (string-to-number (match-string 3 ans))
  16271. day (string-to-number (match-string 4 ans)))
  16272. (if (< year 100) (setq year (+ 2000 year)))
  16273. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16274. t nil ans)))
  16275. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16276. ;; If there is a time with am/pm, and *no* time without it, we convert
  16277. ;; so that matching will be successful.
  16278. (loop for i from 1 to 2 do ; twice, for end time as well
  16279. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16280. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16281. (setq hour (string-to-number (match-string 1 ans))
  16282. minute (if (match-end 3)
  16283. (string-to-number (match-string 3 ans))
  16284. 0)
  16285. pm (equal ?p
  16286. (string-to-char (downcase (match-string 4 ans)))))
  16287. (if (and (= hour 12) (not pm))
  16288. (setq hour 0)
  16289. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16290. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16291. t t ans))))
  16292. ;; Check if a time range is given as a duration
  16293. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16294. (setq hour (string-to-number (match-string 1 ans))
  16295. h2 (+ hour (string-to-number (match-string 3 ans)))
  16296. minute (string-to-number (match-string 2 ans))
  16297. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16298. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16299. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16300. ;; Check if there is a time range
  16301. (when (boundp 'org-end-time-was-given)
  16302. (setq org-time-was-given nil)
  16303. (when (and (string-match org-plain-time-of-day-regexp ans)
  16304. (match-end 8))
  16305. (setq org-end-time-was-given (match-string 8 ans))
  16306. (setq ans (concat (substring ans 0 (match-beginning 7))
  16307. (substring ans (match-end 7))))))
  16308. (setq tl (parse-time-string ans)
  16309. day (or (nth 3 tl) (nth 3 defdecode))
  16310. month (or (nth 4 tl)
  16311. (if (and org-read-date-prefer-future
  16312. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16313. (1+ (nth 4 defdecode))
  16314. (nth 4 defdecode)))
  16315. year (or (nth 5 tl)
  16316. (if (and org-read-date-prefer-future
  16317. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16318. (1+ (nth 5 defdecode))
  16319. (nth 5 defdecode)))
  16320. hour (or (nth 2 tl) (nth 2 defdecode))
  16321. minute (or (nth 1 tl) (nth 1 defdecode))
  16322. second (or (nth 0 tl) 0)
  16323. wday (nth 6 tl))
  16324. (when deltan
  16325. (unless deltadef
  16326. (let ((now (decode-time (current-time))))
  16327. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16328. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16329. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16330. ((equal deltaw "m") (setq month (+ month deltan)))
  16331. ((equal deltaw "y") (setq year (+ year deltan)))))
  16332. (when (and wday (not (nth 3 tl)))
  16333. ;; Weekday was given, but no day, so pick that day in the week
  16334. ;; on or after the derived date.
  16335. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16336. (unless (equal wday wday1)
  16337. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16338. (if (and (boundp 'org-time-was-given)
  16339. (nth 2 tl))
  16340. (setq org-time-was-given t))
  16341. (if (< year 100) (setq year (+ 2000 year)))
  16342. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16343. (list second minute hour day month year)))
  16344. (defvar parse-time-weekdays)
  16345. (defun org-read-date-get-relative (s today default)
  16346. "Check string S for special relative date string.
  16347. TODAY and DEFAULT are internal times, for today and for a default.
  16348. Return shift list (N what def-flag)
  16349. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16350. N is the number of WHATs to shift.
  16351. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16352. the DEFAULT date rather than TODAY."
  16353. (when (string-match
  16354. (concat
  16355. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16356. "\\([0-9]+\\)?"
  16357. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16358. "\\([ \t]\\|$\\)") s)
  16359. (let* ((dir (if (match-end 1)
  16360. (string-to-char (substring (match-string 1 s) -1))
  16361. ?+))
  16362. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16363. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16364. (what (if (match-end 3) (match-string 3 s) "d"))
  16365. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16366. (date (if rel default today))
  16367. (wday (nth 6 (decode-time date)))
  16368. delta)
  16369. (if wday1
  16370. (progn
  16371. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16372. (if (= dir ?-) (setq delta (- delta 7)))
  16373. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16374. (list delta "d" rel))
  16375. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16376. (defun org-eval-in-calendar (form &optional keepdate)
  16377. "Eval FORM in the calendar window and return to current window.
  16378. Also, store the cursor date in variable org-ans2."
  16379. (let ((sw (selected-window)))
  16380. (select-window (get-buffer-window "*Calendar*"))
  16381. (eval form)
  16382. (when (and (not keepdate) (calendar-cursor-to-date))
  16383. (let* ((date (calendar-cursor-to-date))
  16384. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16385. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16386. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16387. (select-window sw)))
  16388. ; ;; Update the prompt to show new default date
  16389. ; (save-excursion
  16390. ; (goto-char (point-min))
  16391. ; (when (and org-ans2
  16392. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16393. ; (get-text-property (match-end 0) 'field))
  16394. ; (let ((inhibit-read-only t))
  16395. ; (replace-match (concat "[" org-ans2 "]") t t)
  16396. ; (add-text-properties (point-min) (1+ (match-end 0))
  16397. ; (text-properties-at (1+ (point-min)))))))))
  16398. (defun org-calendar-select ()
  16399. "Return to `org-read-date' with the date currently selected.
  16400. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16401. (interactive)
  16402. (when (calendar-cursor-to-date)
  16403. (let* ((date (calendar-cursor-to-date))
  16404. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16405. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16406. (if (active-minibuffer-window) (exit-minibuffer))))
  16407. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16408. "Insert a date stamp for the date given by the internal TIME.
  16409. WITH-HM means, use the stamp format that includes the time of the day.
  16410. INACTIVE means use square brackets instead of angular ones, so that the
  16411. stamp will not contribute to the agenda.
  16412. PRE and POST are optional strings to be inserted before and after the
  16413. stamp.
  16414. The command returns the inserted time stamp."
  16415. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16416. stamp)
  16417. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16418. (insert-before-markers (or pre ""))
  16419. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16420. (when (listp extra)
  16421. (setq extra (car extra))
  16422. (if (and (stringp extra)
  16423. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16424. (setq extra (format "-%02d:%02d"
  16425. (string-to-number (match-string 1 extra))
  16426. (string-to-number (match-string 2 extra))))
  16427. (setq extra nil)))
  16428. (when extra
  16429. (backward-char 1)
  16430. (insert-before-markers extra)
  16431. (forward-char 1))
  16432. (insert-before-markers (or post ""))
  16433. stamp))
  16434. (defun org-toggle-time-stamp-overlays ()
  16435. "Toggle the use of custom time stamp formats."
  16436. (interactive)
  16437. (setq org-display-custom-times (not org-display-custom-times))
  16438. (unless org-display-custom-times
  16439. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16440. (while (setq p (next-single-property-change p 'display))
  16441. (if (and (get-text-property p 'display)
  16442. (eq (get-text-property p 'face) 'org-date))
  16443. (remove-text-properties
  16444. p (setq p (next-single-property-change p 'display))
  16445. '(display t))))
  16446. (set-buffer-modified-p bmp)))
  16447. (if (featurep 'xemacs)
  16448. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16449. (org-restart-font-lock)
  16450. (setq org-table-may-need-update t)
  16451. (if org-display-custom-times
  16452. (message "Time stamps are overlayed with custom format")
  16453. (message "Time stamp overlays removed")))
  16454. (defun org-display-custom-time (beg end)
  16455. "Overlay modified time stamp format over timestamp between BED and END."
  16456. (let* ((ts (buffer-substring beg end))
  16457. t1 w1 with-hm tf time str w2 (off 0))
  16458. (save-match-data
  16459. (setq t1 (org-parse-time-string ts t))
  16460. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16461. (setq off (- (match-end 0) (match-beginning 0)))))
  16462. (setq end (- end off))
  16463. (setq w1 (- end beg)
  16464. with-hm (and (nth 1 t1) (nth 2 t1))
  16465. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16466. time (org-fix-decoded-time t1)
  16467. str (org-add-props
  16468. (format-time-string
  16469. (substring tf 1 -1) (apply 'encode-time time))
  16470. nil 'mouse-face 'highlight)
  16471. w2 (length str))
  16472. (if (not (= w2 w1))
  16473. (add-text-properties (1+ beg) (+ 2 beg)
  16474. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16475. (if (featurep 'xemacs)
  16476. (progn
  16477. (put-text-property beg end 'invisible t)
  16478. (put-text-property beg end 'end-glyph (make-glyph str)))
  16479. (put-text-property beg end 'display str))))
  16480. (defun org-translate-time (string)
  16481. "Translate all timestamps in STRING to custom format.
  16482. But do this only if the variable `org-display-custom-times' is set."
  16483. (when org-display-custom-times
  16484. (save-match-data
  16485. (let* ((start 0)
  16486. (re org-ts-regexp-both)
  16487. t1 with-hm inactive tf time str beg end)
  16488. (while (setq start (string-match re string start))
  16489. (setq beg (match-beginning 0)
  16490. end (match-end 0)
  16491. t1 (save-match-data
  16492. (org-parse-time-string (substring string beg end) t))
  16493. with-hm (and (nth 1 t1) (nth 2 t1))
  16494. inactive (equal (substring string beg (1+ beg)) "[")
  16495. tf (funcall (if with-hm 'cdr 'car)
  16496. org-time-stamp-custom-formats)
  16497. time (org-fix-decoded-time t1)
  16498. str (format-time-string
  16499. (concat
  16500. (if inactive "[" "<") (substring tf 1 -1)
  16501. (if inactive "]" ">"))
  16502. (apply 'encode-time time))
  16503. string (replace-match str t t string)
  16504. start (+ start (length str)))))))
  16505. string)
  16506. (defun org-fix-decoded-time (time)
  16507. "Set 0 instead of nil for the first 6 elements of time.
  16508. Don't touch the rest."
  16509. (let ((n 0))
  16510. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16511. (defun org-days-to-time (timestamp-string)
  16512. "Difference between TIMESTAMP-STRING and now in days."
  16513. (- (time-to-days (org-time-string-to-time timestamp-string))
  16514. (time-to-days (current-time))))
  16515. (defun org-deadline-close (timestamp-string &optional ndays)
  16516. "Is the time in TIMESTAMP-STRING close to the current date?"
  16517. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16518. (and (< (org-days-to-time timestamp-string) ndays)
  16519. (not (org-entry-is-done-p))))
  16520. (defun org-get-wdays (ts)
  16521. "Get the deadline lead time appropriate for timestring TS."
  16522. (cond
  16523. ((<= org-deadline-warning-days 0)
  16524. ;; 0 or negative, enforce this value no matter what
  16525. (- org-deadline-warning-days))
  16526. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16527. ;; lead time is specified.
  16528. (floor (* (string-to-number (match-string 1 ts))
  16529. (cdr (assoc (match-string 2 ts)
  16530. '(("d" . 1) ("w" . 7)
  16531. ("m" . 30.4) ("y" . 365.25)))))))
  16532. ;; go for the default.
  16533. (t org-deadline-warning-days)))
  16534. (defun org-calendar-select-mouse (ev)
  16535. "Return to `org-read-date' with the date currently selected.
  16536. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16537. (interactive "e")
  16538. (mouse-set-point ev)
  16539. (when (calendar-cursor-to-date)
  16540. (let* ((date (calendar-cursor-to-date))
  16541. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16542. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16543. (if (active-minibuffer-window) (exit-minibuffer))))
  16544. (defun org-check-deadlines (ndays)
  16545. "Check if there are any deadlines due or past due.
  16546. A deadline is considered due if it happens within `org-deadline-warning-days'
  16547. days from today's date. If the deadline appears in an entry marked DONE,
  16548. it is not shown. The prefix arg NDAYS can be used to test that many
  16549. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16550. (interactive "P")
  16551. (let* ((org-warn-days
  16552. (cond
  16553. ((equal ndays '(4)) 100000)
  16554. (ndays (prefix-numeric-value ndays))
  16555. (t (abs org-deadline-warning-days))))
  16556. (case-fold-search nil)
  16557. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16558. (callback
  16559. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16560. (message "%d deadlines past-due or due within %d days"
  16561. (org-occur regexp nil callback)
  16562. org-warn-days)))
  16563. (defun org-check-before-date (date)
  16564. "Check if there are deadlines or scheduled entries before DATE."
  16565. (interactive (list (org-read-date)))
  16566. (let ((case-fold-search nil)
  16567. (regexp (concat "\\<\\(" org-deadline-string
  16568. "\\|" org-scheduled-string
  16569. "\\) *<\\([^>]+\\)>"))
  16570. (callback
  16571. (lambda () (time-less-p
  16572. (org-time-string-to-time (match-string 2))
  16573. (org-time-string-to-time date)))))
  16574. (message "%d entries before %s"
  16575. (org-occur regexp nil callback) date)))
  16576. (defun org-evaluate-time-range (&optional to-buffer)
  16577. "Evaluate a time range by computing the difference between start and end.
  16578. Normally the result is just printed in the echo area, but with prefix arg
  16579. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16580. If the time range is actually in a table, the result is inserted into the
  16581. next column.
  16582. For time difference computation, a year is assumed to be exactly 365
  16583. days in order to avoid rounding problems."
  16584. (interactive "P")
  16585. (or
  16586. (org-clock-update-time-maybe)
  16587. (save-excursion
  16588. (unless (org-at-date-range-p t)
  16589. (goto-char (point-at-bol))
  16590. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16591. (if (not (org-at-date-range-p t))
  16592. (error "Not at a time-stamp range, and none found in current line")))
  16593. (let* ((ts1 (match-string 1))
  16594. (ts2 (match-string 2))
  16595. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16596. (match-end (match-end 0))
  16597. (time1 (org-time-string-to-time ts1))
  16598. (time2 (org-time-string-to-time ts2))
  16599. (t1 (time-to-seconds time1))
  16600. (t2 (time-to-seconds time2))
  16601. (diff (abs (- t2 t1)))
  16602. (negative (< (- t2 t1) 0))
  16603. ;; (ys (floor (* 365 24 60 60)))
  16604. (ds (* 24 60 60))
  16605. (hs (* 60 60))
  16606. (fy "%dy %dd %02d:%02d")
  16607. (fy1 "%dy %dd")
  16608. (fd "%dd %02d:%02d")
  16609. (fd1 "%dd")
  16610. (fh "%02d:%02d")
  16611. y d h m align)
  16612. (if havetime
  16613. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16614. y 0
  16615. d (floor (/ diff ds)) diff (mod diff ds)
  16616. h (floor (/ diff hs)) diff (mod diff hs)
  16617. m (floor (/ diff 60)))
  16618. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16619. y 0
  16620. d (floor (+ (/ diff ds) 0.5))
  16621. h 0 m 0))
  16622. (if (not to-buffer)
  16623. (message "%s" (org-make-tdiff-string y d h m))
  16624. (if (org-at-table-p)
  16625. (progn
  16626. (goto-char match-end)
  16627. (setq align t)
  16628. (and (looking-at " *|") (goto-char (match-end 0))))
  16629. (goto-char match-end))
  16630. (if (looking-at
  16631. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16632. (replace-match ""))
  16633. (if negative (insert " -"))
  16634. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16635. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16636. (insert " " (format fh h m))))
  16637. (if align (org-table-align))
  16638. (message "Time difference inserted")))))
  16639. (defun org-make-tdiff-string (y d h m)
  16640. (let ((fmt "")
  16641. (l nil))
  16642. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16643. l (push y l)))
  16644. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16645. l (push d l)))
  16646. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16647. l (push h l)))
  16648. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16649. l (push m l)))
  16650. (apply 'format fmt (nreverse l))))
  16651. (defun org-time-string-to-time (s)
  16652. (apply 'encode-time (org-parse-time-string s)))
  16653. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16654. "Convert a time stamp to an absolute day number.
  16655. If there is a specifyer for a cyclic time stamp, get the closest date to
  16656. DAYNR."
  16657. (cond
  16658. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16659. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16660. daynr
  16661. (+ daynr 1000)))
  16662. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16663. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16664. (time-to-days (current-time))) (match-string 0 s)
  16665. prefer))
  16666. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16667. (defun org-time-from-absolute (d)
  16668. "Return the time corresponding to date D.
  16669. D may be an absolute day number, or a calendar-type list (month day year)."
  16670. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16671. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16672. (defun org-calendar-holiday ()
  16673. "List of holidays, for Diary display in Org-mode."
  16674. (require 'holidays)
  16675. (let ((hl (funcall
  16676. (if (fboundp 'calendar-check-holidays)
  16677. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16678. (if hl (mapconcat 'identity hl "; "))))
  16679. (defun org-diary-sexp-entry (sexp entry date)
  16680. "Process a SEXP diary ENTRY for DATE."
  16681. (require 'diary-lib)
  16682. (let ((result (if calendar-debug-sexp
  16683. (let ((stack-trace-on-error t))
  16684. (eval (car (read-from-string sexp))))
  16685. (condition-case nil
  16686. (eval (car (read-from-string sexp)))
  16687. (error
  16688. (beep)
  16689. (message "Bad sexp at line %d in %s: %s"
  16690. (org-current-line)
  16691. (buffer-file-name) sexp)
  16692. (sleep-for 2))))))
  16693. (cond ((stringp result) result)
  16694. ((and (consp result)
  16695. (stringp (cdr result))) (cdr result))
  16696. (result entry)
  16697. (t nil))))
  16698. (defun org-diary-to-ical-string (frombuf)
  16699. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16700. This uses the icalendar.el library."
  16701. (let* ((tmpdir (if (featurep 'xemacs)
  16702. (temp-directory)
  16703. temporary-file-directory))
  16704. (tmpfile (make-temp-name
  16705. (expand-file-name "orgics" tmpdir)))
  16706. buf rtn b e)
  16707. (save-excursion
  16708. (set-buffer frombuf)
  16709. (icalendar-export-region (point-min) (point-max) tmpfile)
  16710. (setq buf (find-buffer-visiting tmpfile))
  16711. (set-buffer buf)
  16712. (goto-char (point-min))
  16713. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16714. (setq b (match-beginning 0)))
  16715. (goto-char (point-max))
  16716. (if (re-search-backward "^END:VEVENT" nil t)
  16717. (setq e (match-end 0)))
  16718. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16719. (kill-buffer buf)
  16720. (kill-buffer frombuf)
  16721. (delete-file tmpfile)
  16722. rtn))
  16723. (defun org-closest-date (start current change prefer)
  16724. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16725. When PREFER is `past' return a date that is either CURRENT or past.
  16726. When PREFER is `future', return a date that is either CURRENT or future."
  16727. ;; Make the proper lists from the dates
  16728. (catch 'exit
  16729. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16730. dn dw sday cday n1 n2
  16731. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16732. (setq start (org-date-to-gregorian start)
  16733. current (org-date-to-gregorian
  16734. (if org-agenda-repeating-timestamp-show-all
  16735. current
  16736. (time-to-days (current-time))))
  16737. sday (calendar-absolute-from-gregorian start)
  16738. cday (calendar-absolute-from-gregorian current))
  16739. (if (<= cday sday) (throw 'exit sday))
  16740. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16741. (setq dn (string-to-number (match-string 1 change))
  16742. dw (cdr (assoc (match-string 2 change) a1)))
  16743. (error "Invalid change specifyer: %s" change))
  16744. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16745. (cond
  16746. ((eq dw 'day)
  16747. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16748. n2 (+ n1 dn)))
  16749. ((eq dw 'year)
  16750. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16751. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16752. (setq date1 (list m d y1)
  16753. n1 (calendar-absolute-from-gregorian date1)
  16754. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16755. n2 (calendar-absolute-from-gregorian date2)))
  16756. ((eq dw 'month)
  16757. ;; approx number of month between the tow dates
  16758. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16759. ;; How often does dn fit in there?
  16760. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16761. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16762. m (+ m nm)
  16763. ny (floor (/ m 12))
  16764. y (+ y ny)
  16765. m (- m (* ny 12)))
  16766. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16767. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16768. (setq m2 (+ m dn) y2 y)
  16769. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16770. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16771. (while (< n2 cday)
  16772. (setq n1 n2 m m2 y y2)
  16773. (setq m2 (+ m dn) y2 y)
  16774. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16775. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16776. (if org-agenda-repeating-timestamp-show-all
  16777. (cond
  16778. ((eq prefer 'past) n1)
  16779. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16780. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16781. (cond
  16782. ((eq prefer 'past) n1)
  16783. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16784. (t (if (= cday n1) n1 n2)))))))
  16785. (defun org-date-to-gregorian (date)
  16786. "Turn any specification of DATE into a gregorian date for the calendar."
  16787. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16788. ((and (listp date) (= (length date) 3)) date)
  16789. ((stringp date)
  16790. (setq date (org-parse-time-string date))
  16791. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16792. ((listp date)
  16793. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16794. (defun org-parse-time-string (s &optional nodefault)
  16795. "Parse the standard Org-mode time string.
  16796. This should be a lot faster than the normal `parse-time-string'.
  16797. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16798. hour and minute fields will be nil if not given."
  16799. (if (string-match org-ts-regexp0 s)
  16800. (list 0
  16801. (if (or (match-beginning 8) (not nodefault))
  16802. (string-to-number (or (match-string 8 s) "0")))
  16803. (if (or (match-beginning 7) (not nodefault))
  16804. (string-to-number (or (match-string 7 s) "0")))
  16805. (string-to-number (match-string 4 s))
  16806. (string-to-number (match-string 3 s))
  16807. (string-to-number (match-string 2 s))
  16808. nil nil nil)
  16809. (make-list 9 0)))
  16810. (defun org-timestamp-up (&optional arg)
  16811. "Increase the date item at the cursor by one.
  16812. If the cursor is on the year, change the year. If it is on the month or
  16813. the day, change that.
  16814. With prefix ARG, change by that many units."
  16815. (interactive "p")
  16816. (org-timestamp-change (prefix-numeric-value arg)))
  16817. (defun org-timestamp-down (&optional arg)
  16818. "Decrease the date item at the cursor by one.
  16819. If the cursor is on the year, change the year. If it is on the month or
  16820. the day, change that.
  16821. With prefix ARG, change by that many units."
  16822. (interactive "p")
  16823. (org-timestamp-change (- (prefix-numeric-value arg))))
  16824. (defun org-timestamp-up-day (&optional arg)
  16825. "Increase the date in the time stamp by one day.
  16826. With prefix ARG, change that many days."
  16827. (interactive "p")
  16828. (if (and (not (org-at-timestamp-p t))
  16829. (org-on-heading-p))
  16830. (org-todo 'up)
  16831. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16832. (defun org-timestamp-down-day (&optional arg)
  16833. "Decrease the date in the time stamp by one day.
  16834. With prefix ARG, change that many days."
  16835. (interactive "p")
  16836. (if (and (not (org-at-timestamp-p t))
  16837. (org-on-heading-p))
  16838. (org-todo 'down)
  16839. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16840. (defsubst org-pos-in-match-range (pos n)
  16841. (and (match-beginning n)
  16842. (<= (match-beginning n) pos)
  16843. (>= (match-end n) pos)))
  16844. (defun org-at-timestamp-p (&optional inactive-ok)
  16845. "Determine if the cursor is in or at a timestamp."
  16846. (interactive)
  16847. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16848. (pos (point))
  16849. (ans (or (looking-at tsr)
  16850. (save-excursion
  16851. (skip-chars-backward "^[<\n\r\t")
  16852. (if (> (point) (point-min)) (backward-char 1))
  16853. (and (looking-at tsr)
  16854. (> (- (match-end 0) pos) -1))))))
  16855. (and ans
  16856. (boundp 'org-ts-what)
  16857. (setq org-ts-what
  16858. (cond
  16859. ((= pos (match-beginning 0)) 'bracket)
  16860. ((= pos (1- (match-end 0))) 'bracket)
  16861. ((org-pos-in-match-range pos 2) 'year)
  16862. ((org-pos-in-match-range pos 3) 'month)
  16863. ((org-pos-in-match-range pos 7) 'hour)
  16864. ((org-pos-in-match-range pos 8) 'minute)
  16865. ((or (org-pos-in-match-range pos 4)
  16866. (org-pos-in-match-range pos 5)) 'day)
  16867. ((and (> pos (or (match-end 8) (match-end 5)))
  16868. (< pos (match-end 0)))
  16869. (- pos (or (match-end 8) (match-end 5))))
  16870. (t 'day))))
  16871. ans))
  16872. (defun org-toggle-timestamp-type ()
  16873. ""
  16874. (interactive)
  16875. (when (org-at-timestamp-p t)
  16876. (save-excursion
  16877. (goto-char (match-beginning 0))
  16878. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16879. (goto-char (1- (match-end 0)))
  16880. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16881. (message "Timestamp is now %sactive"
  16882. (if (equal (char-before) ?>) "in" ""))))
  16883. (defun org-timestamp-change (n &optional what)
  16884. "Change the date in the time stamp at point.
  16885. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16886. `year', `minute', `second'. If WHAT is not given, the cursor position
  16887. in the timestamp determines what will be changed."
  16888. (let ((pos (point))
  16889. with-hm inactive
  16890. org-ts-what
  16891. extra
  16892. ts time time0)
  16893. (if (not (org-at-timestamp-p t))
  16894. (error "Not at a timestamp"))
  16895. (if (and (not what) (eq org-ts-what 'bracket))
  16896. (org-toggle-timestamp-type)
  16897. (if (and (not what) (not (eq org-ts-what 'day))
  16898. org-display-custom-times
  16899. (get-text-property (point) 'display)
  16900. (not (get-text-property (1- (point)) 'display)))
  16901. (setq org-ts-what 'day))
  16902. (setq org-ts-what (or what org-ts-what)
  16903. inactive (= (char-after (match-beginning 0)) ?\[)
  16904. ts (match-string 0))
  16905. (replace-match "")
  16906. (if (string-match
  16907. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16908. ts)
  16909. (setq extra (match-string 1 ts)))
  16910. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16911. (setq with-hm t))
  16912. (setq time0 (org-parse-time-string ts))
  16913. (setq time
  16914. (encode-time (or (car time0) 0)
  16915. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16916. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16917. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16918. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16919. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16920. (nthcdr 6 time0)))
  16921. (when (integerp org-ts-what)
  16922. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16923. (if (eq what 'calendar)
  16924. (let ((cal-date (org-get-date-from-calendar)))
  16925. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16926. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16927. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16928. (setcar time0 (or (car time0) 0))
  16929. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16930. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16931. (setq time (apply 'encode-time time0))))
  16932. (setq org-last-changed-timestamp
  16933. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16934. (org-clock-update-time-maybe)
  16935. (goto-char pos)
  16936. ;; Try to recenter the calendar window, if any
  16937. (if (and org-calendar-follow-timestamp-change
  16938. (get-buffer-window "*Calendar*" t)
  16939. (memq org-ts-what '(day month year)))
  16940. (org-recenter-calendar (time-to-days time))))))
  16941. ;; FIXME: does not yet work for lead times
  16942. (defun org-modify-ts-extra (s pos n)
  16943. "Change the different parts of the lead-time and repeat fields in timestamp."
  16944. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16945. ng h m new)
  16946. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16947. (cond
  16948. ((or (org-pos-in-match-range pos 2)
  16949. (org-pos-in-match-range pos 3))
  16950. (setq m (string-to-number (match-string 3 s))
  16951. h (string-to-number (match-string 2 s)))
  16952. (if (org-pos-in-match-range pos 2)
  16953. (setq h (+ h n))
  16954. (setq m (+ m n)))
  16955. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16956. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16957. (setq h (min 24 (max 0 h)))
  16958. (setq ng 1 new (format "-%02d:%02d" h m)))
  16959. ((org-pos-in-match-range pos 6)
  16960. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16961. ((org-pos-in-match-range pos 5)
  16962. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16963. (when ng
  16964. (setq s (concat
  16965. (substring s 0 (match-beginning ng))
  16966. new
  16967. (substring s (match-end ng))))))
  16968. s))
  16969. (defun org-recenter-calendar (date)
  16970. "If the calendar is visible, recenter it to DATE."
  16971. (let* ((win (selected-window))
  16972. (cwin (get-buffer-window "*Calendar*" t))
  16973. (calendar-move-hook nil))
  16974. (when cwin
  16975. (select-window cwin)
  16976. (calendar-goto-date (if (listp date) date
  16977. (calendar-gregorian-from-absolute date)))
  16978. (select-window win))))
  16979. (defun org-goto-calendar (&optional arg)
  16980. "Go to the Emacs calendar at the current date.
  16981. If there is a time stamp in the current line, go to that date.
  16982. A prefix ARG can be used to force the current date."
  16983. (interactive "P")
  16984. (let ((tsr org-ts-regexp) diff
  16985. (calendar-move-hook nil)
  16986. (view-calendar-holidays-initially nil)
  16987. (view-diary-entries-initially nil))
  16988. (if (or (org-at-timestamp-p)
  16989. (save-excursion
  16990. (beginning-of-line 1)
  16991. (looking-at (concat ".*" tsr))))
  16992. (let ((d1 (time-to-days (current-time)))
  16993. (d2 (time-to-days
  16994. (org-time-string-to-time (match-string 1)))))
  16995. (setq diff (- d2 d1))))
  16996. (calendar)
  16997. (calendar-goto-today)
  16998. (if (and diff (not arg)) (calendar-forward-day diff))))
  16999. (defun org-get-date-from-calendar ()
  17000. "Return a list (month day year) of date at point in calendar."
  17001. (with-current-buffer "*Calendar*"
  17002. (save-match-data
  17003. (calendar-cursor-to-date))))
  17004. (defun org-date-from-calendar ()
  17005. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17006. If there is already a time stamp at the cursor position, update it."
  17007. (interactive)
  17008. (if (org-at-timestamp-p t)
  17009. (org-timestamp-change 0 'calendar)
  17010. (let ((cal-date (org-get-date-from-calendar)))
  17011. (org-insert-time-stamp
  17012. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17013. ;; Make appt aware of appointments from the agenda
  17014. ;;;###autoload
  17015. (defun org-agenda-to-appt (&optional filter)
  17016. "Activate appointments found in `org-agenda-files'.
  17017. When prefixed, prompt for a regular expression and use it as a
  17018. filter: only add entries if they match this regular expression.
  17019. FILTER can be a string. In this case, use this string as a
  17020. regular expression to filter results.
  17021. FILTER can also be an alist, with the car of each cell being
  17022. either 'headline or 'category. For example:
  17023. '((headline \"IMPORTANT\")
  17024. (category \"Work\"))
  17025. will only add headlines containing IMPORTANT or headlines
  17026. belonging to the category \"Work\"."
  17027. (interactive "P")
  17028. (require 'calendar)
  17029. (if (equal filter '(4))
  17030. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17031. (let* ((cnt 0) ; count added events
  17032. (org-agenda-new-buffers nil)
  17033. (today (org-date-to-gregorian
  17034. (time-to-days (current-time))))
  17035. (files (org-agenda-files)) entries file)
  17036. ;; Get all entries which may contain an appt
  17037. (while (setq file (pop files))
  17038. (setq entries
  17039. (append entries
  17040. (org-agenda-get-day-entries
  17041. file today
  17042. :timestamp :scheduled :deadline))))
  17043. (setq entries (delq nil entries))
  17044. ;; Map thru entries and find if they pass thru the filter
  17045. (mapc
  17046. (lambda(x)
  17047. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17048. (cat (get-text-property 1 'org-category x))
  17049. (tod (get-text-property 1 'time-of-day x))
  17050. (ok (or (null filter)
  17051. (and (stringp filter) (string-match filter evt))
  17052. (and (listp filter)
  17053. (or (string-match
  17054. (cadr (assoc 'category filter)) cat)
  17055. (string-match
  17056. (cadr (assoc 'headline filter)) evt))))))
  17057. ;; FIXME: Shall we remove text-properties for the appt text?
  17058. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17059. (when (and ok tod)
  17060. (setq tod (number-to-string tod)
  17061. tod (when (string-match
  17062. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17063. (concat (match-string 1 tod) ":"
  17064. (match-string 2 tod))))
  17065. (appt-add tod evt)
  17066. (setq cnt (1+ cnt))))) entries)
  17067. (org-release-buffers org-agenda-new-buffers)
  17068. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  17069. ;;; The clock for measuring work time.
  17070. (defvar org-mode-line-string "")
  17071. (put 'org-mode-line-string 'risky-local-variable t)
  17072. (defvar org-mode-line-timer nil)
  17073. (defvar org-clock-heading "")
  17074. (defvar org-clock-start-time "")
  17075. (defun org-update-mode-line ()
  17076. (let* ((delta (- (time-to-seconds (current-time))
  17077. (time-to-seconds org-clock-start-time)))
  17078. (h (floor delta 3600))
  17079. (m (floor (- delta (* 3600 h)) 60)))
  17080. (setq org-mode-line-string
  17081. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17082. 'help-echo "Org-mode clock is running"))
  17083. (force-mode-line-update)))
  17084. (defvar org-clock-marker (make-marker)
  17085. "Marker recording the last clock-in.")
  17086. (defvar org-clock-mode-line-entry nil
  17087. "Information for the modeline about the running clock.")
  17088. (defun org-clock-in ()
  17089. "Start the clock on the current item.
  17090. If necessary, clock-out of the currently active clock."
  17091. (interactive)
  17092. (org-clock-out t)
  17093. (let (ts)
  17094. (save-excursion
  17095. (org-back-to-heading t)
  17096. (when (and org-clock-in-switch-to-state
  17097. (not (looking-at (concat outline-regexp "[ \t]*"
  17098. org-clock-in-switch-to-state
  17099. "\\>"))))
  17100. (org-todo org-clock-in-switch-to-state))
  17101. (if (and org-clock-heading-function
  17102. (functionp org-clock-heading-function))
  17103. (setq org-clock-heading (funcall org-clock-heading-function))
  17104. (if (looking-at org-complex-heading-regexp)
  17105. (setq org-clock-heading (match-string 4))
  17106. (setq org-clock-heading "???")))
  17107. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17108. (org-clock-find-position)
  17109. (insert "\n") (backward-char 1)
  17110. (indent-relative)
  17111. (insert org-clock-string " ")
  17112. (setq org-clock-start-time (current-time))
  17113. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17114. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17115. (or global-mode-string (setq global-mode-string '("")))
  17116. (or (memq 'org-mode-line-string global-mode-string)
  17117. (setq global-mode-string
  17118. (append global-mode-string '(org-mode-line-string))))
  17119. (org-update-mode-line)
  17120. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17121. (message "Clock started at %s" ts))))
  17122. (defun org-clock-find-position ()
  17123. "Find the location where the next clock line should be inserted."
  17124. (org-back-to-heading t)
  17125. (catch 'exit
  17126. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17127. (re (concat "^[ \t]*" org-clock-string))
  17128. (cnt 0)
  17129. first last)
  17130. (goto-char beg)
  17131. (when (eobp) (newline) (setq end (max (point) end)))
  17132. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17133. ;; we seem to have a CLOCK drawer, so go there.
  17134. (beginning-of-line 2)
  17135. (throw 'exit t))
  17136. ;; Lets count the CLOCK lines
  17137. (goto-char beg)
  17138. (while (re-search-forward re end t)
  17139. (setq first (or first (match-beginning 0))
  17140. last (match-beginning 0)
  17141. cnt (1+ cnt)))
  17142. (when (and (integerp org-clock-into-drawer)
  17143. (>= (1+ cnt) org-clock-into-drawer))
  17144. ;; Wrap current entries into a new drawer
  17145. (goto-char last)
  17146. (beginning-of-line 2)
  17147. (if (org-at-item-p) (org-end-of-item))
  17148. (insert ":END:\n")
  17149. (beginning-of-line 0)
  17150. (org-indent-line-function)
  17151. (goto-char first)
  17152. (insert ":CLOCK:\n")
  17153. (beginning-of-line 0)
  17154. (org-indent-line-function)
  17155. (org-flag-drawer t)
  17156. (beginning-of-line 2)
  17157. (throw 'exit nil))
  17158. (goto-char beg)
  17159. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17160. (not (equal (match-string 1) org-clock-string)))
  17161. ;; Planning info, skip to after it
  17162. (beginning-of-line 2)
  17163. (or (bolp) (newline)))
  17164. (when (eq t org-clock-into-drawer)
  17165. (insert ":CLOCK:\n:END:\n")
  17166. (beginning-of-line -1)
  17167. (org-indent-line-function)
  17168. (org-flag-drawer t)
  17169. (beginning-of-line 2)
  17170. (org-indent-line-function)))))
  17171. (defun org-clock-out (&optional fail-quietly)
  17172. "Stop the currently running clock.
  17173. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17174. (interactive)
  17175. (catch 'exit
  17176. (if (not (marker-buffer org-clock-marker))
  17177. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17178. (let (ts te s h m)
  17179. (save-excursion
  17180. (set-buffer (marker-buffer org-clock-marker))
  17181. (goto-char org-clock-marker)
  17182. (beginning-of-line 1)
  17183. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17184. (equal (match-string 1) org-clock-string))
  17185. (setq ts (match-string 2))
  17186. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17187. (goto-char (match-end 0))
  17188. (delete-region (point) (point-at-eol))
  17189. (insert "--")
  17190. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17191. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17192. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17193. h (floor (/ s 3600))
  17194. s (- s (* 3600 h))
  17195. m (floor (/ s 60))
  17196. s (- s (* 60 s)))
  17197. (insert " => " (format "%2d:%02d" h m))
  17198. (move-marker org-clock-marker nil)
  17199. (when org-log-note-clock-out
  17200. (org-add-log-maybe 'clock-out))
  17201. (when org-mode-line-timer
  17202. (cancel-timer org-mode-line-timer)
  17203. (setq org-mode-line-timer nil))
  17204. (setq global-mode-string
  17205. (delq 'org-mode-line-string global-mode-string))
  17206. (force-mode-line-update)
  17207. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17208. (defun org-clock-cancel ()
  17209. "Cancel the running clock be removing the start timestamp."
  17210. (interactive)
  17211. (if (not (marker-buffer org-clock-marker))
  17212. (error "No active clock"))
  17213. (save-excursion
  17214. (set-buffer (marker-buffer org-clock-marker))
  17215. (goto-char org-clock-marker)
  17216. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17217. (setq global-mode-string
  17218. (delq 'org-mode-line-string global-mode-string))
  17219. (force-mode-line-update)
  17220. (message "Clock canceled"))
  17221. (defun org-clock-goto (&optional delete-windows)
  17222. "Go to the currently clocked-in entry."
  17223. (interactive "P")
  17224. (if (not (marker-buffer org-clock-marker))
  17225. (error "No active clock"))
  17226. (switch-to-buffer-other-window
  17227. (marker-buffer org-clock-marker))
  17228. (if delete-windows (delete-other-windows))
  17229. (goto-char org-clock-marker)
  17230. (org-show-entry)
  17231. (org-back-to-heading)
  17232. (recenter))
  17233. (defvar org-clock-file-total-minutes nil
  17234. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17235. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17236. (defun org-clock-sum (&optional tstart tend)
  17237. "Sum the times for each subtree.
  17238. Puts the resulting times in minutes as a text property on each headline."
  17239. (interactive)
  17240. (let* ((bmp (buffer-modified-p))
  17241. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17242. org-clock-string
  17243. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17244. (lmax 30)
  17245. (ltimes (make-vector lmax 0))
  17246. (t1 0)
  17247. (level 0)
  17248. ts te dt
  17249. time)
  17250. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17251. (save-excursion
  17252. (goto-char (point-max))
  17253. (while (re-search-backward re nil t)
  17254. (cond
  17255. ((match-end 2)
  17256. ;; Two time stamps
  17257. (setq ts (match-string 2)
  17258. te (match-string 3)
  17259. ts (time-to-seconds
  17260. (apply 'encode-time (org-parse-time-string ts)))
  17261. te (time-to-seconds
  17262. (apply 'encode-time (org-parse-time-string te)))
  17263. ts (if tstart (max ts tstart) ts)
  17264. te (if tend (min te tend) te)
  17265. dt (- te ts)
  17266. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17267. ((match-end 4)
  17268. ;; A naket time
  17269. (setq t1 (+ t1 (string-to-number (match-string 5))
  17270. (* 60 (string-to-number (match-string 4))))))
  17271. (t ;; A headline
  17272. (setq level (- (match-end 1) (match-beginning 1)))
  17273. (when (or (> t1 0) (> (aref ltimes level) 0))
  17274. (loop for l from 0 to level do
  17275. (aset ltimes l (+ (aref ltimes l) t1)))
  17276. (setq t1 0 time (aref ltimes level))
  17277. (loop for l from level to (1- lmax) do
  17278. (aset ltimes l 0))
  17279. (goto-char (match-beginning 0))
  17280. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17281. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17282. (set-buffer-modified-p bmp)))
  17283. (defun org-clock-display (&optional total-only)
  17284. "Show subtree times in the entire buffer.
  17285. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17286. in the echo area."
  17287. (interactive)
  17288. (org-remove-clock-overlays)
  17289. (let (time h m p)
  17290. (org-clock-sum)
  17291. (unless total-only
  17292. (save-excursion
  17293. (goto-char (point-min))
  17294. (while (or (and (equal (setq p (point)) (point-min))
  17295. (get-text-property p :org-clock-minutes))
  17296. (setq p (next-single-property-change
  17297. (point) :org-clock-minutes)))
  17298. (goto-char p)
  17299. (when (setq time (get-text-property p :org-clock-minutes))
  17300. (org-put-clock-overlay time (funcall outline-level))))
  17301. (setq h (/ org-clock-file-total-minutes 60)
  17302. m (- org-clock-file-total-minutes (* 60 h)))
  17303. ;; Arrange to remove the overlays upon next change.
  17304. (when org-remove-highlights-with-change
  17305. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17306. nil 'local))))
  17307. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17308. (defvar org-clock-overlays nil)
  17309. (make-variable-buffer-local 'org-clock-overlays)
  17310. (defun org-put-clock-overlay (time &optional level)
  17311. "Put an overlays on the current line, displaying TIME.
  17312. If LEVEL is given, prefix time with a corresponding number of stars.
  17313. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17314. will be easy to remove."
  17315. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17316. (l (if level (org-get-legal-level level 0) 0))
  17317. (off 0)
  17318. ov tx)
  17319. (move-to-column c)
  17320. (unless (eolp) (skip-chars-backward "^ \t"))
  17321. (skip-chars-backward " \t")
  17322. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17323. tx (concat (buffer-substring (1- (point)) (point))
  17324. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17325. (org-add-props (format "%s %2d:%02d%s"
  17326. (make-string l ?*) h m
  17327. (make-string (- 10 l) ?\ ))
  17328. '(face secondary-selection))
  17329. ""))
  17330. (if (not (featurep 'xemacs))
  17331. (org-overlay-put ov 'display tx)
  17332. (org-overlay-put ov 'invisible t)
  17333. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17334. (push ov org-clock-overlays)))
  17335. (defun org-remove-clock-overlays (&optional beg end noremove)
  17336. "Remove the occur highlights from the buffer.
  17337. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17338. from the `before-change-functions' in the current buffer."
  17339. (interactive)
  17340. (unless org-inhibit-highlight-removal
  17341. (mapc 'org-delete-overlay org-clock-overlays)
  17342. (setq org-clock-overlays nil)
  17343. (unless noremove
  17344. (remove-hook 'before-change-functions
  17345. 'org-remove-clock-overlays 'local))))
  17346. (defun org-clock-out-if-current ()
  17347. "Clock out if the current entry contains the running clock.
  17348. This is used to stop the clock after a TODO entry is marked DONE,
  17349. and is only done if the variable `org-clock-out-when-done' is not nil."
  17350. (when (and org-clock-out-when-done
  17351. (member state org-done-keywords)
  17352. (equal (marker-buffer org-clock-marker) (current-buffer))
  17353. (< (point) org-clock-marker)
  17354. (> (save-excursion (outline-next-heading) (point))
  17355. org-clock-marker))
  17356. ;; Clock out, but don't accept a logging message for this.
  17357. (let ((org-log-note-clock-out nil))
  17358. (org-clock-out))))
  17359. (add-hook 'org-after-todo-state-change-hook
  17360. 'org-clock-out-if-current)
  17361. (defun org-check-running-clock ()
  17362. "Check if the current buffer contains the running clock.
  17363. If yes, offer to stop it and to save the buffer with the changes."
  17364. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17365. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17366. (buffer-name))))
  17367. (org-clock-out)
  17368. (when (y-or-n-p "Save changed buffer?")
  17369. (save-buffer))))
  17370. (defun org-clock-report (&optional arg)
  17371. "Create a table containing a report about clocked time.
  17372. If the cursor is inside an existing clocktable block, then the table
  17373. will be updated. If not, a new clocktable will be inserted.
  17374. When called with a prefix argument, move to the first clock table in the
  17375. buffer and update it."
  17376. (interactive "P")
  17377. (org-remove-clock-overlays)
  17378. (when arg
  17379. (org-find-dblock "clocktable")
  17380. (org-show-entry))
  17381. (if (org-in-clocktable-p)
  17382. (goto-char (org-in-clocktable-p))
  17383. (org-create-dblock (list :name "clocktable"
  17384. :maxlevel 2 :scope 'file)))
  17385. (org-update-dblock))
  17386. (defun org-in-clocktable-p ()
  17387. "Check if the cursor is in a clocktable."
  17388. (let ((pos (point)) start)
  17389. (save-excursion
  17390. (end-of-line 1)
  17391. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17392. (setq start (match-beginning 0))
  17393. (re-search-forward "^#\\+END:.*" nil t)
  17394. (>= (match-end 0) pos)
  17395. start))))
  17396. (defun org-clock-update-time-maybe ()
  17397. "If this is a CLOCK line, update it and return t.
  17398. Otherwise, return nil."
  17399. (interactive)
  17400. (save-excursion
  17401. (beginning-of-line 1)
  17402. (skip-chars-forward " \t")
  17403. (when (looking-at org-clock-string)
  17404. (let ((re (concat "[ \t]*" org-clock-string
  17405. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17406. "\\([ \t]*=>.*\\)?"))
  17407. ts te h m s)
  17408. (if (not (looking-at re))
  17409. nil
  17410. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17411. (end-of-line 1)
  17412. (setq ts (match-string 1)
  17413. te (match-string 2))
  17414. (setq s (- (time-to-seconds
  17415. (apply 'encode-time (org-parse-time-string te)))
  17416. (time-to-seconds
  17417. (apply 'encode-time (org-parse-time-string ts))))
  17418. h (floor (/ s 3600))
  17419. s (- s (* 3600 h))
  17420. m (floor (/ s 60))
  17421. s (- s (* 60 s)))
  17422. (insert " => " (format "%2d:%02d" h m))
  17423. t)))))
  17424. (defun org-clock-special-range (key &optional time as-strings)
  17425. "Return two times bordering a special time range.
  17426. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17427. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17428. A week starts Monday 0:00 and ends Sunday 24:00.
  17429. The range is determined relative to TIME. TIME defaults to the current time.
  17430. The return value is a cons cell with two internal times like the ones
  17431. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17432. the returned times will be formatted strings."
  17433. (let* ((tm (decode-time (or time (current-time))))
  17434. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17435. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17436. (dow (nth 6 tm))
  17437. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17438. (cond
  17439. ((eq key 'today)
  17440. (setq h 0 m 0 h1 24 m1 0))
  17441. ((eq key 'yesterday)
  17442. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17443. ((eq key 'thisweek)
  17444. (setq diff (if (= dow 0) 6 (1- dow))
  17445. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17446. ((eq key 'lastweek)
  17447. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17448. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17449. ((eq key 'thismonth)
  17450. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17451. ((eq key 'lastmonth)
  17452. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17453. ((eq key 'thisyear)
  17454. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17455. ((eq key 'lastyear)
  17456. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17457. (t (error "No such time block %s" key)))
  17458. (setq ts (encode-time s m h d month y)
  17459. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17460. (or d1 d) (or month1 month) (or y1 y)))
  17461. (setq fm (cdr org-time-stamp-formats))
  17462. (if as-strings
  17463. (cons (format-time-string fm ts) (format-time-string fm te))
  17464. (cons ts te))))
  17465. (defun org-dblock-write:clocktable (params)
  17466. "Write the standard clocktable."
  17467. (catch 'exit
  17468. (let* ((hlchars '((1 . "*") (2 . "/")))
  17469. (ins (make-marker))
  17470. (total-time nil)
  17471. (scope (plist-get params :scope))
  17472. (tostring (plist-get params :tostring))
  17473. (multifile (plist-get params :multifile))
  17474. (header (plist-get params :header))
  17475. (maxlevel (or (plist-get params :maxlevel) 3))
  17476. (step (plist-get params :step))
  17477. (emph (plist-get params :emphasize))
  17478. (ts (plist-get params :tstart))
  17479. (te (plist-get params :tend))
  17480. (block (plist-get params :block))
  17481. ipos time h m p level hlc hdl
  17482. cc beg end pos tbl)
  17483. (when step
  17484. (org-clocktable-steps params)
  17485. (throw 'exit nil))
  17486. (when block
  17487. (setq cc (org-clock-special-range block nil t)
  17488. ts (car cc) te (cdr cc)))
  17489. (if ts (setq ts (time-to-seconds
  17490. (apply 'encode-time (org-parse-time-string ts)))))
  17491. (if te (setq te (time-to-seconds
  17492. (apply 'encode-time (org-parse-time-string te)))))
  17493. (move-marker ins (point))
  17494. (setq ipos (point))
  17495. ;; Get the right scope
  17496. (setq pos (point))
  17497. (save-restriction
  17498. (cond
  17499. ((not scope))
  17500. ((eq scope 'file) (widen))
  17501. ((eq scope 'subtree) (org-narrow-to-subtree))
  17502. ((eq scope 'tree)
  17503. (while (org-up-heading-safe))
  17504. (org-narrow-to-subtree))
  17505. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17506. (symbol-name scope)))
  17507. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17508. (catch 'exit
  17509. (while (org-up-heading-safe)
  17510. (looking-at outline-regexp)
  17511. (if (<= (org-reduced-level (funcall outline-level)) level)
  17512. (throw 'exit nil))))
  17513. (org-narrow-to-subtree))
  17514. ((or (listp scope) (eq scope 'agenda))
  17515. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17516. (scope 'agenda)
  17517. (p1 (copy-sequence params))
  17518. file)
  17519. (plist-put p1 :tostring t)
  17520. (plist-put p1 :multifile t)
  17521. (plist-put p1 :scope 'file)
  17522. (org-prepare-agenda-buffers files)
  17523. (while (setq file (pop files))
  17524. (with-current-buffer (find-buffer-visiting file)
  17525. (push (org-clocktable-add-file
  17526. file (org-dblock-write:clocktable p1)) tbl)
  17527. (setq total-time (+ (or total-time 0)
  17528. org-clock-file-total-minutes)))))))
  17529. (goto-char pos)
  17530. (unless (eq scope 'agenda)
  17531. (org-clock-sum ts te)
  17532. (goto-char (point-min))
  17533. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17534. (goto-char p)
  17535. (when (setq time (get-text-property p :org-clock-minutes))
  17536. (save-excursion
  17537. (beginning-of-line 1)
  17538. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17539. (setq level (org-reduced-level
  17540. (- (match-end 1) (match-beginning 1))))
  17541. (<= level maxlevel))
  17542. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17543. hdl (match-string 2)
  17544. h (/ time 60)
  17545. m (- time (* 60 h)))
  17546. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17547. (push (concat
  17548. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17549. (make-string (1- level) ?|)
  17550. hlc (format "%d:%02d" h m) hlc
  17551. " |") tbl))))))
  17552. (setq tbl (nreverse tbl))
  17553. (if tostring
  17554. (if tbl (mapconcat 'identity tbl "\n") nil)
  17555. (goto-char ins)
  17556. (insert-before-markers
  17557. (or header
  17558. (concat
  17559. "Clock summary at ["
  17560. (substring
  17561. (format-time-string (cdr org-time-stamp-formats))
  17562. 1 -1)
  17563. "]."
  17564. (if block
  17565. (format " Considered range is /%s/." block)
  17566. "")
  17567. "\n\n"))
  17568. (if (eq scope 'agenda) "|File" "")
  17569. "|L|Headline|Time|\n")
  17570. (setq total-time (or total-time org-clock-file-total-minutes)
  17571. h (/ total-time 60)
  17572. m (- total-time (* 60 h)))
  17573. (insert-before-markers
  17574. "|-\n|"
  17575. (if (eq scope 'agenda) "|" "")
  17576. "|"
  17577. "*Total time*| "
  17578. (format "*%d:%02d*" h m)
  17579. "|\n|-\n")
  17580. (setq tbl (delq nil tbl))
  17581. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17582. (equal (substring (car tbl) 0 2) "|-"))
  17583. (pop tbl))
  17584. (insert-before-markers (mapconcat
  17585. 'identity (delq nil tbl)
  17586. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17587. (backward-delete-char 1)
  17588. (goto-char ipos)
  17589. (skip-chars-forward "^|")
  17590. (org-table-align))))))
  17591. (defun org-clocktable-steps (params)
  17592. (let* ((p1 (copy-sequence params))
  17593. (ts (plist-get p1 :tstart))
  17594. (te (plist-get p1 :tend))
  17595. (step0 (plist-get p1 :step))
  17596. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17597. (block (plist-get p1 :block))
  17598. cc)
  17599. (when block
  17600. (setq cc (org-clock-special-range block nil t)
  17601. ts (car cc) te (cdr cc)))
  17602. (if ts (setq ts (time-to-seconds
  17603. (apply 'encode-time (org-parse-time-string ts)))))
  17604. (if te (setq te (time-to-seconds
  17605. (apply 'encode-time (org-parse-time-string te)))))
  17606. (plist-put p1 :header "")
  17607. (plist-put p1 :step nil)
  17608. (plist-put p1 :block nil)
  17609. (while (< ts te)
  17610. (or (bolp) (insert "\n"))
  17611. (plist-put p1 :tstart (format-time-string
  17612. (car org-time-stamp-formats)
  17613. (seconds-to-time ts)))
  17614. (plist-put p1 :tend (format-time-string
  17615. (car org-time-stamp-formats)
  17616. (seconds-to-time (setq ts (+ ts step)))))
  17617. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17618. (plist-get p1 :tstart) "\n")
  17619. (org-dblock-write:clocktable p1)
  17620. (re-search-forward "#\\+END:")
  17621. (end-of-line 0))))
  17622. (defun org-clocktable-add-file (file table)
  17623. (if table
  17624. (let ((lines (org-split-string table "\n"))
  17625. (ff (file-name-nondirectory file)))
  17626. (mapconcat 'identity
  17627. (mapcar (lambda (x)
  17628. (if (string-match org-table-dataline-regexp x)
  17629. (concat "|" ff x)
  17630. x))
  17631. lines)
  17632. "\n"))))
  17633. ;; FIXME: I don't think anybody uses this, ask David
  17634. (defun org-collect-clock-time-entries ()
  17635. "Return an internal list with clocking information.
  17636. This list has one entry for each CLOCK interval.
  17637. FIXME: describe the elements."
  17638. (interactive)
  17639. (let ((re (concat "^[ \t]*" org-clock-string
  17640. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17641. rtn beg end next cont level title total closedp leafp
  17642. clockpos titlepos h m donep)
  17643. (save-excursion
  17644. (org-clock-sum)
  17645. (goto-char (point-min))
  17646. (while (re-search-forward re nil t)
  17647. (setq clockpos (match-beginning 0)
  17648. beg (match-string 1) end (match-string 2)
  17649. cont (match-end 0))
  17650. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17651. end (apply 'encode-time (org-parse-time-string end)))
  17652. (org-back-to-heading t)
  17653. (setq donep (org-entry-is-done-p))
  17654. (setq titlepos (point)
  17655. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17656. h (/ total 60) m (- total (* 60 h))
  17657. total (cons h m))
  17658. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17659. (setq level (- (match-end 1) (match-beginning 1))
  17660. title (org-match-string-no-properties 2))
  17661. (save-excursion (outline-next-heading) (setq next (point)))
  17662. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17663. (goto-char next)
  17664. (setq leafp (and (looking-at "^\\*+ ")
  17665. (<= (- (match-end 0) (point)) level)))
  17666. (push (list beg end clockpos closedp donep
  17667. total title titlepos level leafp)
  17668. rtn)
  17669. (goto-char cont)))
  17670. (nreverse rtn)))
  17671. ;;;; Agenda, and Diary Integration
  17672. ;;; Define the Org-agenda-mode
  17673. (defvar org-agenda-mode-map (make-sparse-keymap)
  17674. "Keymap for `org-agenda-mode'.")
  17675. (defvar org-agenda-menu) ; defined later in this file.
  17676. (defvar org-agenda-follow-mode nil)
  17677. (defvar org-agenda-show-log nil)
  17678. (defvar org-agenda-redo-command nil)
  17679. (defvar org-agenda-mode-hook nil)
  17680. (defvar org-agenda-type nil)
  17681. (defvar org-agenda-force-single-file nil)
  17682. (defun org-agenda-mode ()
  17683. "Mode for time-sorted view on action items in Org-mode files.
  17684. The following commands are available:
  17685. \\{org-agenda-mode-map}"
  17686. (interactive)
  17687. (kill-all-local-variables)
  17688. (setq org-agenda-undo-list nil
  17689. org-agenda-pending-undo-list nil)
  17690. (setq major-mode 'org-agenda-mode)
  17691. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17692. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17693. (setq mode-name "Org-Agenda")
  17694. (use-local-map org-agenda-mode-map)
  17695. (easy-menu-add org-agenda-menu)
  17696. (if org-startup-truncated (setq truncate-lines t))
  17697. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17698. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17699. ;; Make sure properties are removed when copying text
  17700. (when (boundp 'buffer-substring-filters)
  17701. (org-set-local 'buffer-substring-filters
  17702. (cons (lambda (x)
  17703. (set-text-properties 0 (length x) nil x) x)
  17704. buffer-substring-filters)))
  17705. (unless org-agenda-keep-modes
  17706. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17707. org-agenda-show-log nil))
  17708. (easy-menu-change
  17709. '("Agenda") "Agenda Files"
  17710. (append
  17711. (list
  17712. (vector
  17713. (if (get 'org-agenda-files 'org-restrict)
  17714. "Restricted to single file"
  17715. "Edit File List")
  17716. '(org-edit-agenda-file-list)
  17717. (not (get 'org-agenda-files 'org-restrict)))
  17718. "--")
  17719. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17720. (org-agenda-set-mode-name)
  17721. (apply
  17722. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17723. (list 'org-agenda-mode-hook)))
  17724. (substitute-key-definition 'undo 'org-agenda-undo
  17725. org-agenda-mode-map global-map)
  17726. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17727. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17728. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17729. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17730. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17731. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17732. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17733. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17734. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17735. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17736. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17737. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17738. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17739. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17740. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17741. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17742. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17743. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17744. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17745. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17746. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17747. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17748. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17749. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17750. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17751. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17752. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17753. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17754. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17755. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17756. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17757. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17758. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17759. (while l (org-defkey org-agenda-mode-map
  17760. (int-to-string (pop l)) 'digit-argument)))
  17761. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17762. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17763. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17764. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17765. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17766. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17767. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17768. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17769. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17770. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17771. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17772. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17773. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17774. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17775. (org-defkey org-agenda-mode-map "n" 'next-line)
  17776. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17777. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17778. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17779. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17780. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17781. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17782. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17783. (eval-after-load "calendar"
  17784. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17785. 'org-calendar-goto-agenda))
  17786. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17787. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17788. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17789. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17790. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17791. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17792. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17793. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17794. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17795. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17796. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17797. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17798. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17799. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17800. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17801. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17802. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17803. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17804. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17805. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17806. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17807. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17808. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17809. "Local keymap for agenda entries from Org-mode.")
  17810. (org-defkey org-agenda-keymap
  17811. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17812. (org-defkey org-agenda-keymap
  17813. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17814. (when org-agenda-mouse-1-follows-link
  17815. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17816. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17817. '("Agenda"
  17818. ("Agenda Files")
  17819. "--"
  17820. ["Show" org-agenda-show t]
  17821. ["Go To (other window)" org-agenda-goto t]
  17822. ["Go To (this window)" org-agenda-switch-to t]
  17823. ["Follow Mode" org-agenda-follow-mode
  17824. :style toggle :selected org-agenda-follow-mode :active t]
  17825. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17826. "--"
  17827. ["Cycle TODO" org-agenda-todo t]
  17828. ["Archive subtree" org-agenda-archive t]
  17829. ["Delete subtree" org-agenda-kill t]
  17830. "--"
  17831. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17832. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17833. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17834. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17835. "--"
  17836. ("Tags and Properties"
  17837. ["Show all Tags" org-agenda-show-tags t]
  17838. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17839. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17840. "--"
  17841. ["Column View" org-columns t])
  17842. ("Date/Schedule"
  17843. ["Schedule" org-agenda-schedule t]
  17844. ["Set Deadline" org-agenda-deadline t]
  17845. "--"
  17846. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17847. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17848. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17849. ("Clock"
  17850. ["Clock in" org-agenda-clock-in t]
  17851. ["Clock out" org-agenda-clock-out t]
  17852. ["Clock cancel" org-agenda-clock-cancel t]
  17853. ["Goto running clock" org-clock-goto t])
  17854. ("Priority"
  17855. ["Set Priority" org-agenda-priority t]
  17856. ["Increase Priority" org-agenda-priority-up t]
  17857. ["Decrease Priority" org-agenda-priority-down t]
  17858. ["Show Priority" org-agenda-show-priority t])
  17859. ("Calendar/Diary"
  17860. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17861. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17862. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17863. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17864. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17865. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17866. "--"
  17867. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17868. "--"
  17869. ("View"
  17870. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17871. :style radio :selected (equal org-agenda-ndays 1)]
  17872. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17873. :style radio :selected (equal org-agenda-ndays 7)]
  17874. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17875. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17876. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17877. :style radio :selected (member org-agenda-ndays '(365 366))]
  17878. "--"
  17879. ["Show Logbook entries" org-agenda-log-mode
  17880. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17881. ["Include Diary" org-agenda-toggle-diary
  17882. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17883. ["Use Time Grid" org-agenda-toggle-time-grid
  17884. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17885. ["Write view to file" org-write-agenda t]
  17886. ["Rebuild buffer" org-agenda-redo t]
  17887. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17888. "--"
  17889. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17890. "--"
  17891. ["Quit" org-agenda-quit t]
  17892. ["Exit and Release Buffers" org-agenda-exit t]
  17893. ))
  17894. ;;; Agenda undo
  17895. (defvar org-agenda-allow-remote-undo t
  17896. "Non-nil means, allow remote undo from the agenda buffer.")
  17897. (defvar org-agenda-undo-list nil
  17898. "List of undoable operations in the agenda since last refresh.")
  17899. (defvar org-agenda-undo-has-started-in nil
  17900. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17901. (defvar org-agenda-pending-undo-list nil
  17902. "In a series of undo commands, this is the list of remaning undo items.")
  17903. (defmacro org-if-unprotected (&rest body)
  17904. "Execute BODY if there is no `org-protected' text property at point."
  17905. (declare (debug t))
  17906. `(unless (get-text-property (point) 'org-protected)
  17907. ,@body))
  17908. (defmacro org-with-remote-undo (_buffer &rest _body)
  17909. "Execute BODY while recording undo information in two buffers."
  17910. (declare (indent 1) (debug t))
  17911. `(let ((_cline (org-current-line))
  17912. (_cmd this-command)
  17913. (_buf1 (current-buffer))
  17914. (_buf2 ,_buffer)
  17915. (_undo1 buffer-undo-list)
  17916. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17917. _c1 _c2)
  17918. ,@_body
  17919. (when org-agenda-allow-remote-undo
  17920. (setq _c1 (org-verify-change-for-undo
  17921. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17922. _c2 (org-verify-change-for-undo
  17923. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17924. (when (or _c1 _c2)
  17925. ;; make sure there are undo boundaries
  17926. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17927. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17928. ;; remember which buffer to undo
  17929. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17930. org-agenda-undo-list)))))
  17931. (defun org-agenda-undo ()
  17932. "Undo a remote editing step in the agenda.
  17933. This undoes changes both in the agenda buffer and in the remote buffer
  17934. that have been changed along."
  17935. (interactive)
  17936. (or org-agenda-allow-remote-undo
  17937. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17938. (if (not (eq this-command last-command))
  17939. (setq org-agenda-undo-has-started-in nil
  17940. org-agenda-pending-undo-list org-agenda-undo-list))
  17941. (if (not org-agenda-pending-undo-list)
  17942. (error "No further undo information"))
  17943. (let* ((entry (pop org-agenda-pending-undo-list))
  17944. buf line cmd rembuf)
  17945. (setq cmd (pop entry) line (pop entry))
  17946. (setq rembuf (nth 2 entry))
  17947. (org-with-remote-undo rembuf
  17948. (while (bufferp (setq buf (pop entry)))
  17949. (if (pop entry)
  17950. (with-current-buffer buf
  17951. (let ((last-undo-buffer buf)
  17952. (inhibit-read-only t))
  17953. (unless (memq buf org-agenda-undo-has-started-in)
  17954. (push buf org-agenda-undo-has-started-in)
  17955. (make-local-variable 'pending-undo-list)
  17956. (undo-start))
  17957. (while (and pending-undo-list
  17958. (listp pending-undo-list)
  17959. (not (car pending-undo-list)))
  17960. (pop pending-undo-list))
  17961. (undo-more 1))))))
  17962. (goto-line line)
  17963. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17964. (defun org-verify-change-for-undo (l1 l2)
  17965. "Verify that a real change occurred between the undo lists L1 and L2."
  17966. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17967. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17968. (not (eq l1 l2)))
  17969. ;;; Agenda dispatch
  17970. (defvar org-agenda-restrict nil)
  17971. (defvar org-agenda-restrict-begin (make-marker))
  17972. (defvar org-agenda-restrict-end (make-marker))
  17973. (defvar org-agenda-last-dispatch-buffer nil)
  17974. (defvar org-agenda-overriding-restriction nil)
  17975. ;;;###autoload
  17976. (defun org-agenda (arg &optional keys restriction)
  17977. "Dispatch agenda commands to collect entries to the agenda buffer.
  17978. Prompts for a command to execute. Any prefix arg will be passed
  17979. on to the selected command. The default selections are:
  17980. a Call `org-agenda-list' to display the agenda for current day or week.
  17981. t Call `org-todo-list' to display the global todo list.
  17982. T Call `org-todo-list' to display the global todo list, select only
  17983. entries with a specific TODO keyword (the user gets a prompt).
  17984. m Call `org-tags-view' to display headlines with tags matching
  17985. a condition (the user is prompted for the condition).
  17986. M Like `m', but select only TODO entries, no ordinary headlines.
  17987. L Create a timeline for the current buffer.
  17988. e Export views to associated files.
  17989. More commands can be added by configuring the variable
  17990. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17991. searches can be pre-defined in this way.
  17992. If the current buffer is in Org-mode and visiting a file, you can also
  17993. first press `<' once to indicate that the agenda should be temporarily
  17994. \(until the next use of \\[org-agenda]) restricted to the current file.
  17995. Pressing `<' twice means to restrict to the current subtree or region
  17996. \(if active)."
  17997. (interactive "P")
  17998. (catch 'exit
  17999. (let* ((prefix-descriptions nil)
  18000. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18001. (org-agenda-custom-commands
  18002. ;; normalize different versions
  18003. (delq nil
  18004. (mapcar
  18005. (lambda (x)
  18006. (cond ((stringp (cdr x))
  18007. (push x prefix-descriptions)
  18008. nil)
  18009. ((stringp (nth 1 x)) x)
  18010. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18011. (t (cons (car x) (cons "" (cdr x))))))
  18012. org-agenda-custom-commands)))
  18013. (buf (current-buffer))
  18014. (bfn (buffer-file-name (buffer-base-buffer)))
  18015. entry key type match lprops ans)
  18016. ;; Turn off restriction unless there is an overriding one
  18017. (unless org-agenda-overriding-restriction
  18018. (put 'org-agenda-files 'org-restrict nil)
  18019. (setq org-agenda-restrict nil)
  18020. (move-marker org-agenda-restrict-begin nil)
  18021. (move-marker org-agenda-restrict-end nil))
  18022. ;; Delete old local properties
  18023. (put 'org-agenda-redo-command 'org-lprops nil)
  18024. ;; Remember where this call originated
  18025. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18026. (unless keys
  18027. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18028. keys (car ans)
  18029. restriction (cdr ans)))
  18030. ;; Estabish the restriction, if any
  18031. (when (and (not org-agenda-overriding-restriction) restriction)
  18032. (put 'org-agenda-files 'org-restrict (list bfn))
  18033. (cond
  18034. ((eq restriction 'region)
  18035. (setq org-agenda-restrict t)
  18036. (move-marker org-agenda-restrict-begin (region-beginning))
  18037. (move-marker org-agenda-restrict-end (region-end)))
  18038. ((eq restriction 'subtree)
  18039. (save-excursion
  18040. (setq org-agenda-restrict t)
  18041. (org-back-to-heading t)
  18042. (move-marker org-agenda-restrict-begin (point))
  18043. (move-marker org-agenda-restrict-end
  18044. (progn (org-end-of-subtree t)))))))
  18045. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18046. ;; For example the todo list should not need it (but does...)
  18047. (cond
  18048. ((setq entry (assoc keys org-agenda-custom-commands))
  18049. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18050. (progn
  18051. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18052. (put 'org-agenda-redo-command 'org-lprops lprops)
  18053. (cond
  18054. ((eq type 'agenda)
  18055. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18056. ((eq type 'alltodo)
  18057. (org-let lprops '(org-todo-list current-prefix-arg)))
  18058. ((eq type 'stuck)
  18059. (org-let lprops '(org-agenda-list-stuck-projects
  18060. current-prefix-arg)))
  18061. ((eq type 'tags)
  18062. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18063. ((eq type 'tags-todo)
  18064. (org-let lprops '(org-tags-view '(4) match)))
  18065. ((eq type 'todo)
  18066. (org-let lprops '(org-todo-list match)))
  18067. ((eq type 'tags-tree)
  18068. (org-check-for-org-mode)
  18069. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18070. ((eq type 'todo-tree)
  18071. (org-check-for-org-mode)
  18072. (org-let lprops
  18073. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18074. (regexp-quote match) "\\>"))))
  18075. ((eq type 'occur-tree)
  18076. (org-check-for-org-mode)
  18077. (org-let lprops '(org-occur match)))
  18078. ((functionp type)
  18079. (org-let lprops '(funcall type match)))
  18080. ((fboundp type)
  18081. (org-let lprops '(funcall type match)))
  18082. (t (error "Invalid custom agenda command type %s" type))))
  18083. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18084. ((equal keys "C")
  18085. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18086. (customize-variable 'org-agenda-custom-commands))
  18087. ((equal keys "a") (call-interactively 'org-agenda-list))
  18088. ((equal keys "t") (call-interactively 'org-todo-list))
  18089. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18090. ((equal keys "m") (call-interactively 'org-tags-view))
  18091. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18092. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18093. ((equal keys "L")
  18094. (unless (org-mode-p)
  18095. (error "This is not an Org-mode file"))
  18096. (unless restriction
  18097. (put 'org-agenda-files 'org-restrict (list bfn))
  18098. (org-call-with-arg 'org-timeline arg)))
  18099. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18100. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18101. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18102. (t (error "Invalid agenda key"))))))
  18103. (defun org-agenda-normalize-custom-commands (cmds)
  18104. (delq nil
  18105. (mapcar
  18106. (lambda (x)
  18107. (cond ((stringp (cdr x)) nil)
  18108. ((stringp (nth 1 x)) x)
  18109. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18110. (t (cons (car x) (cons "" (cdr x))))))
  18111. cmds)))
  18112. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18113. "The user interface for selecting an agenda command."
  18114. (catch 'exit
  18115. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18116. (restrict-ok (and bfn (org-mode-p)))
  18117. (region-p (org-region-active-p))
  18118. (custom org-agenda-custom-commands)
  18119. (selstring "")
  18120. restriction second-time
  18121. c entry key type match prefixes rmheader header-end custom1 desc)
  18122. (save-window-excursion
  18123. (delete-other-windows)
  18124. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18125. (erase-buffer)
  18126. (insert (eval-when-compile
  18127. (let ((header
  18128. "
  18129. Press key for an agenda command: < Buffer,subtree/region restriction
  18130. -------------------------------- > Remove restriction
  18131. a Agenda for current week or day e Export agenda views
  18132. t List of all TODO entries T Entries with special TODO kwd
  18133. m Match a TAGS query M Like m, but only TODO entries
  18134. L Timeline for current buffer # List stuck projects (!=configure)
  18135. / Multi-occur C Configure custom agenda commands
  18136. ")
  18137. (start 0))
  18138. (while (string-match
  18139. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18140. header start)
  18141. (setq start (match-end 0))
  18142. (add-text-properties (match-beginning 2) (match-end 2)
  18143. '(face bold) header))
  18144. header)))
  18145. (setq header-end (move-marker (make-marker) (point)))
  18146. (while t
  18147. (setq custom1 custom)
  18148. (when (eq rmheader t)
  18149. (goto-line 1)
  18150. (re-search-forward ":" nil t)
  18151. (delete-region (match-end 0) (point-at-eol))
  18152. (forward-char 1)
  18153. (looking-at "-+")
  18154. (delete-region (match-end 0) (point-at-eol))
  18155. (move-marker header-end (match-end 0)))
  18156. (goto-char header-end)
  18157. (delete-region (point) (point-max))
  18158. (while (setq entry (pop custom1))
  18159. (setq key (car entry) desc (nth 1 entry)
  18160. type (nth 2 entry) match (nth 3 entry))
  18161. (if (> (length key) 1)
  18162. (add-to-list 'prefixes (string-to-char key))
  18163. (insert
  18164. (format
  18165. "\n%-4s%-14s: %s"
  18166. (org-add-props (copy-sequence key)
  18167. '(face bold))
  18168. (cond
  18169. ((string-match "\\S-" desc) desc)
  18170. ((eq type 'agenda) "Agenda for current week or day")
  18171. ((eq type 'alltodo) "List of all TODO entries")
  18172. ((eq type 'stuck) "List of stuck projects")
  18173. ((eq type 'todo) "TODO keyword")
  18174. ((eq type 'tags) "Tags query")
  18175. ((eq type 'tags-todo) "Tags (TODO)")
  18176. ((eq type 'tags-tree) "Tags tree")
  18177. ((eq type 'todo-tree) "TODO kwd tree")
  18178. ((eq type 'occur-tree) "Occur tree")
  18179. ((functionp type) (if (symbolp type)
  18180. (symbol-name type)
  18181. "Lambda expression"))
  18182. (t "???"))
  18183. (cond
  18184. ((stringp match)
  18185. (org-add-props match nil 'face 'org-warning))
  18186. (match
  18187. (format "set of %d commands" (length match)))
  18188. (t ""))))))
  18189. (when prefixes
  18190. (mapc (lambda (x)
  18191. (insert
  18192. (format "\n%s %s"
  18193. (org-add-props (char-to-string x)
  18194. nil 'face 'bold)
  18195. (or (cdr (assoc (concat selstring (char-to-string x))
  18196. prefix-descriptions))
  18197. "Prefix key"))))
  18198. prefixes))
  18199. (goto-char (point-min))
  18200. (when (fboundp 'fit-window-to-buffer)
  18201. (if second-time
  18202. (if (not (pos-visible-in-window-p (point-max)))
  18203. (fit-window-to-buffer))
  18204. (setq second-time t)
  18205. (fit-window-to-buffer)))
  18206. (message "Press key for agenda command%s:"
  18207. (if (or restrict-ok org-agenda-overriding-restriction)
  18208. (if org-agenda-overriding-restriction
  18209. " (restriction lock active)"
  18210. (if restriction
  18211. (format " (restricted to %s)" restriction)
  18212. " (unrestricted)"))
  18213. ""))
  18214. (setq c (read-char-exclusive))
  18215. (message "")
  18216. (cond
  18217. ((assoc (char-to-string c) custom)
  18218. (setq selstring (concat selstring (char-to-string c)))
  18219. (throw 'exit (cons selstring restriction)))
  18220. ((memq c prefixes)
  18221. (setq selstring (concat selstring (char-to-string c))
  18222. prefixes nil
  18223. rmheader (or rmheader t)
  18224. custom (delq nil (mapcar
  18225. (lambda (x)
  18226. (if (or (= (length (car x)) 1)
  18227. (/= (string-to-char (car x)) c))
  18228. nil
  18229. (cons (substring (car x) 1) (cdr x))))
  18230. custom))))
  18231. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18232. (message "Restriction is only possible in Org-mode buffers")
  18233. (ding) (sit-for 1))
  18234. ((eq c ?1)
  18235. (org-agenda-remove-restriction-lock 'noupdate)
  18236. (setq restriction 'buffer))
  18237. ((eq c ?0)
  18238. (org-agenda-remove-restriction-lock 'noupdate)
  18239. (setq restriction (if region-p 'region 'subtree)))
  18240. ((eq c ?<)
  18241. (org-agenda-remove-restriction-lock 'noupdate)
  18242. (setq restriction
  18243. (cond
  18244. ((eq restriction 'buffer)
  18245. (if region-p 'region 'subtree))
  18246. ((memq restriction '(subtree region))
  18247. nil)
  18248. (t 'buffer))))
  18249. ((eq c ?>)
  18250. (org-agenda-remove-restriction-lock 'noupdate)
  18251. (setq restriction nil))
  18252. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18253. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18254. ((and (> (length selstring) 0) (eq c ?\d))
  18255. (delete-window)
  18256. (org-agenda-get-restriction-and-command prefix-descriptions))
  18257. ((equal c ?q) (error "Abort"))
  18258. (t (error "Invalid key %c" c))))))))
  18259. (defun org-run-agenda-series (name series)
  18260. (org-prepare-agenda name)
  18261. (let* ((org-agenda-multi t)
  18262. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18263. (cmds (car series))
  18264. (gprops (nth 1 series))
  18265. match ;; The byte compiler incorrectly complains about this. Keep it!
  18266. cmd type lprops)
  18267. (while (setq cmd (pop cmds))
  18268. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18269. (cond
  18270. ((eq type 'agenda)
  18271. (org-let2 gprops lprops
  18272. '(call-interactively 'org-agenda-list)))
  18273. ((eq type 'alltodo)
  18274. (org-let2 gprops lprops
  18275. '(call-interactively 'org-todo-list)))
  18276. ((eq type 'stuck)
  18277. (org-let2 gprops lprops
  18278. '(call-interactively 'org-agenda-list-stuck-projects)))
  18279. ((eq type 'tags)
  18280. (org-let2 gprops lprops
  18281. '(org-tags-view current-prefix-arg match)))
  18282. ((eq type 'tags-todo)
  18283. (org-let2 gprops lprops
  18284. '(org-tags-view '(4) match)))
  18285. ((eq type 'todo)
  18286. (org-let2 gprops lprops
  18287. '(org-todo-list match)))
  18288. ((fboundp type)
  18289. (org-let2 gprops lprops
  18290. '(funcall type match)))
  18291. (t (error "Invalid type in command series"))))
  18292. (widen)
  18293. (setq org-agenda-redo-command redo)
  18294. (goto-char (point-min)))
  18295. (org-finalize-agenda))
  18296. ;;;###autoload
  18297. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18298. "Run an agenda command in batch mode and send the result to STDOUT.
  18299. If CMD-KEY is a string of length 1, it is used as a key in
  18300. `org-agenda-custom-commands' and triggers this command. If it is a
  18301. longer string it is used as a tags/todo match string.
  18302. Paramters are alternating variable names and values that will be bound
  18303. before running the agenda command."
  18304. (let (pars)
  18305. (while parameters
  18306. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18307. (if (> (length cmd-key) 2)
  18308. (eval (list 'let (nreverse pars)
  18309. (list 'org-tags-view nil cmd-key)))
  18310. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18311. (set-buffer org-agenda-buffer-name)
  18312. (princ (org-encode-for-stdout (buffer-string)))))
  18313. (defun org-encode-for-stdout (string)
  18314. (if (fboundp 'encode-coding-string)
  18315. (encode-coding-string string buffer-file-coding-system)
  18316. string))
  18317. (defvar org-agenda-info nil)
  18318. ;;;###autoload
  18319. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18320. "Run an agenda command in batch mode and send the result to STDOUT.
  18321. If CMD-KEY is a string of length 1, it is used as a key in
  18322. `org-agenda-custom-commands' and triggers this command. If it is a
  18323. longer string it is used as a tags/todo match string.
  18324. Paramters are alternating variable names and values that will be bound
  18325. before running the agenda command.
  18326. The output gives a line for each selected agenda item. Each
  18327. item is a list of comma-separated values, like this:
  18328. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18329. category The category of the item
  18330. head The headline, without TODO kwd, TAGS and PRIORITY
  18331. type The type of the agenda entry, can be
  18332. todo selected in TODO match
  18333. tagsmatch selected in tags match
  18334. diary imported from diary
  18335. deadline a deadline on given date
  18336. scheduled scheduled on given date
  18337. timestamp entry has timestamp on given date
  18338. closed entry was closed on given date
  18339. upcoming-deadline warning about deadline
  18340. past-scheduled forwarded scheduled item
  18341. block entry has date block including g. date
  18342. todo The todo keyword, if any
  18343. tags All tags including inherited ones, separated by colons
  18344. date The relevant date, like 2007-2-14
  18345. time The time, like 15:00-16:50
  18346. extra Sting with extra planning info
  18347. priority-l The priority letter if any was given
  18348. priority-n The computed numerical priority
  18349. agenda-day The day in the agenda where this is listed"
  18350. (let (pars)
  18351. (while parameters
  18352. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18353. (push (list 'org-agenda-remove-tags t) pars)
  18354. (if (> (length cmd-key) 2)
  18355. (eval (list 'let (nreverse pars)
  18356. (list 'org-tags-view nil cmd-key)))
  18357. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18358. (set-buffer org-agenda-buffer-name)
  18359. (let* ((lines (org-split-string (buffer-string) "\n"))
  18360. line)
  18361. (while (setq line (pop lines))
  18362. (catch 'next
  18363. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18364. (setq org-agenda-info
  18365. (org-fix-agenda-info (text-properties-at 0 line)))
  18366. (princ
  18367. (org-encode-for-stdout
  18368. (mapconcat 'org-agenda-export-csv-mapper
  18369. '(org-category txt type todo tags date time-of-day extra
  18370. priority-letter priority agenda-day)
  18371. ",")))
  18372. (princ "\n"))))))
  18373. (defun org-fix-agenda-info (props)
  18374. "Make sure all properties on an agenda item have a canonical form,
  18375. so the export commands can easily use it."
  18376. (let (tmp re)
  18377. (when (setq tmp (plist-get props 'tags))
  18378. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18379. (when (setq tmp (plist-get props 'date))
  18380. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18381. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18382. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18383. (setq tmp (calendar-date-string tmp)))
  18384. (setq props (plist-put props 'date tmp)))
  18385. (when (setq tmp (plist-get props 'day))
  18386. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18387. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18388. (setq tmp (calendar-date-string tmp)))
  18389. (setq props (plist-put props 'day tmp))
  18390. (setq props (plist-put props 'agenda-day tmp)))
  18391. (when (setq tmp (plist-get props 'txt))
  18392. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18393. (plist-put props 'priority-letter (match-string 1 tmp))
  18394. (setq tmp (replace-match "" t t tmp)))
  18395. (when (and (setq re (plist-get props 'org-todo-regexp))
  18396. (setq re (concat "\\`\\.*" re " ?"))
  18397. (string-match re tmp))
  18398. (plist-put props 'todo (match-string 1 tmp))
  18399. (setq tmp (replace-match "" t t tmp)))
  18400. (plist-put props 'txt tmp)))
  18401. props)
  18402. (defun org-agenda-export-csv-mapper (prop)
  18403. (let ((res (plist-get org-agenda-info prop)))
  18404. (setq res
  18405. (cond
  18406. ((not res) "")
  18407. ((stringp res) res)
  18408. (t (prin1-to-string res))))
  18409. (while (string-match "," res)
  18410. (setq res (replace-match ";" t t res)))
  18411. (org-trim res)))
  18412. ;;;###autoload
  18413. (defun org-store-agenda-views (&rest parameters)
  18414. (interactive)
  18415. (eval (list 'org-batch-store-agenda-views)))
  18416. ;; FIXME, why is this a macro?????
  18417. ;;;###autoload
  18418. (defmacro org-batch-store-agenda-views (&rest parameters)
  18419. "Run all custom agenda commands that have a file argument."
  18420. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18421. (pop-up-frames nil)
  18422. (dir default-directory)
  18423. pars cmd thiscmdkey files opts)
  18424. (while parameters
  18425. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18426. (setq pars (reverse pars))
  18427. (save-window-excursion
  18428. (while cmds
  18429. (setq cmd (pop cmds)
  18430. thiscmdkey (car cmd)
  18431. opts (nth 4 cmd)
  18432. files (nth 5 cmd))
  18433. (if (stringp files) (setq files (list files)))
  18434. (when files
  18435. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18436. (list 'org-agenda nil thiscmdkey)))
  18437. (set-buffer org-agenda-buffer-name)
  18438. (while files
  18439. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18440. (list 'org-write-agenda
  18441. (expand-file-name (pop files) dir) t))))
  18442. (and (get-buffer org-agenda-buffer-name)
  18443. (kill-buffer org-agenda-buffer-name)))))))
  18444. (defun org-write-agenda (file &optional nosettings)
  18445. "Write the current buffer (an agenda view) as a file.
  18446. Depending on the extension of the file name, plain text (.txt),
  18447. HTML (.html or .htm) or Postscript (.ps) is produced.
  18448. If NOSETTINGS is given, do not scope the settings of
  18449. `org-agenda-exporter-settings' into the export commands. This is used when
  18450. the settings have already been scoped and we do not wish to overrule other,
  18451. higher priority settings."
  18452. (interactive "FWrite agenda to file: ")
  18453. (if (not (file-writable-p file))
  18454. (error "Cannot write agenda to file %s" file))
  18455. (cond
  18456. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18457. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18458. (org-let (if nosettings nil org-agenda-exporter-settings)
  18459. '(save-excursion
  18460. (save-window-excursion
  18461. (cond
  18462. ((string-match "\\.html?\\'" file)
  18463. (set-buffer (htmlize-buffer (current-buffer)))
  18464. (when (and org-agenda-export-html-style
  18465. (string-match "<style>" org-agenda-export-html-style))
  18466. ;; replace <style> section with org-agenda-export-html-style
  18467. (goto-char (point-min))
  18468. (kill-region (- (search-forward "<style") 6)
  18469. (search-forward "</style>"))
  18470. (insert org-agenda-export-html-style))
  18471. (write-file file)
  18472. (kill-buffer (current-buffer))
  18473. (message "HTML written to %s" file))
  18474. ((string-match "\\.ps\\'" file)
  18475. (ps-print-buffer-with-faces file)
  18476. (message "Postscript written to %s" file))
  18477. (t
  18478. (let ((bs (buffer-string)))
  18479. (find-file file)
  18480. (insert bs)
  18481. (save-buffer 0)
  18482. (kill-buffer (current-buffer))
  18483. (message "Plain text written to %s" file))))))
  18484. (set-buffer org-agenda-buffer-name)))
  18485. (defmacro org-no-read-only (&rest body)
  18486. "Inhibit read-only for BODY."
  18487. `(let ((inhibit-read-only t)) ,@body))
  18488. (defun org-check-for-org-mode ()
  18489. "Make sure current buffer is in org-mode. Error if not."
  18490. (or (org-mode-p)
  18491. (error "Cannot execute org-mode agenda command on buffer in %s."
  18492. major-mode)))
  18493. (defun org-fit-agenda-window ()
  18494. "Fit the window to the buffer size."
  18495. (and (memq org-agenda-window-setup '(reorganize-frame))
  18496. (fboundp 'fit-window-to-buffer)
  18497. (fit-window-to-buffer
  18498. nil
  18499. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18500. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18501. ;;; Agenda file list
  18502. (defun org-agenda-files (&optional unrestricted)
  18503. "Get the list of agenda files.
  18504. Optional UNRESTRICTED means return the full list even if a restriction
  18505. is currently in place."
  18506. (let ((files
  18507. (cond
  18508. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18509. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18510. ((listp org-agenda-files) org-agenda-files)
  18511. (t (error "Invalid value of `org-agenda-files'")))))
  18512. (setq files (apply 'append
  18513. (mapcar (lambda (f)
  18514. (if (file-directory-p f)
  18515. (directory-files f t
  18516. org-agenda-file-regexp)
  18517. (list f)))
  18518. files)))
  18519. (if org-agenda-skip-unavailable-files
  18520. (delq nil
  18521. (mapcar (function
  18522. (lambda (file)
  18523. (and (file-readable-p file) file)))
  18524. files))
  18525. files))) ; `org-check-agenda-file' will remove them from the list
  18526. (defun org-edit-agenda-file-list ()
  18527. "Edit the list of agenda files.
  18528. Depending on setup, this either uses customize to edit the variable
  18529. `org-agenda-files', or it visits the file that is holding the list. In the
  18530. latter case, the buffer is set up in a way that saving it automatically kills
  18531. the buffer and restores the previous window configuration."
  18532. (interactive)
  18533. (if (stringp org-agenda-files)
  18534. (let ((cw (current-window-configuration)))
  18535. (find-file org-agenda-files)
  18536. (org-set-local 'org-window-configuration cw)
  18537. (org-add-hook 'after-save-hook
  18538. (lambda ()
  18539. (set-window-configuration
  18540. (prog1 org-window-configuration
  18541. (kill-buffer (current-buffer))))
  18542. (org-install-agenda-files-menu)
  18543. (message "New agenda file list installed"))
  18544. nil 'local)
  18545. (message "%s" (substitute-command-keys
  18546. "Edit list and finish with \\[save-buffer]")))
  18547. (customize-variable 'org-agenda-files)))
  18548. (defun org-store-new-agenda-file-list (list)
  18549. "Set new value for the agenda file list and save it correcly."
  18550. (if (stringp org-agenda-files)
  18551. (let ((f org-agenda-files) b)
  18552. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18553. (with-temp-file f
  18554. (insert (mapconcat 'identity list "\n") "\n")))
  18555. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18556. (setq org-agenda-files list)
  18557. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18558. (defun org-read-agenda-file-list ()
  18559. "Read the list of agenda files from a file."
  18560. (when (stringp org-agenda-files)
  18561. (with-temp-buffer
  18562. (insert-file-contents org-agenda-files)
  18563. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18564. ;;;###autoload
  18565. (defun org-cycle-agenda-files ()
  18566. "Cycle through the files in `org-agenda-files'.
  18567. If the current buffer visits an agenda file, find the next one in the list.
  18568. If the current buffer does not, find the first agenda file."
  18569. (interactive)
  18570. (let* ((fs (org-agenda-files t))
  18571. (files (append fs (list (car fs))))
  18572. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18573. file)
  18574. (unless files (error "No agenda files"))
  18575. (catch 'exit
  18576. (while (setq file (pop files))
  18577. (if (equal (file-truename file) tcf)
  18578. (when (car files)
  18579. (find-file (car files))
  18580. (throw 'exit t))))
  18581. (find-file (car fs)))
  18582. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18583. (defun org-agenda-file-to-front (&optional to-end)
  18584. "Move/add the current file to the top of the agenda file list.
  18585. If the file is not present in the list, it is added to the front. If it is
  18586. present, it is moved there. With optional argument TO-END, add/move to the
  18587. end of the list."
  18588. (interactive "P")
  18589. (let ((org-agenda-skip-unavailable-files nil)
  18590. (file-alist (mapcar (lambda (x)
  18591. (cons (file-truename x) x))
  18592. (org-agenda-files t)))
  18593. (ctf (file-truename buffer-file-name))
  18594. x had)
  18595. (setq x (assoc ctf file-alist) had x)
  18596. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18597. (if to-end
  18598. (setq file-alist (append (delq x file-alist) (list x)))
  18599. (setq file-alist (cons x (delq x file-alist))))
  18600. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18601. (org-install-agenda-files-menu)
  18602. (message "File %s to %s of agenda file list"
  18603. (if had "moved" "added") (if to-end "end" "front"))))
  18604. (defun org-remove-file (&optional file)
  18605. "Remove current file from the list of files in variable `org-agenda-files'.
  18606. These are the files which are being checked for agenda entries.
  18607. Optional argument FILE means, use this file instead of the current."
  18608. (interactive)
  18609. (let* ((org-agenda-skip-unavailable-files nil)
  18610. (file (or file buffer-file-name))
  18611. (true-file (file-truename file))
  18612. (afile (abbreviate-file-name file))
  18613. (files (delq nil (mapcar
  18614. (lambda (x)
  18615. (if (equal true-file
  18616. (file-truename x))
  18617. nil x))
  18618. (org-agenda-files t)))))
  18619. (if (not (= (length files) (length (org-agenda-files t))))
  18620. (progn
  18621. (org-store-new-agenda-file-list files)
  18622. (org-install-agenda-files-menu)
  18623. (message "Removed file: %s" afile))
  18624. (message "File was not in list: %s (not removed)" afile))))
  18625. (defun org-file-menu-entry (file)
  18626. (vector file (list 'find-file file) t))
  18627. (defun org-check-agenda-file (file)
  18628. "Make sure FILE exists. If not, ask user what to do."
  18629. (when (not (file-exists-p file))
  18630. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18631. (abbreviate-file-name file))
  18632. (let ((r (downcase (read-char-exclusive))))
  18633. (cond
  18634. ((equal r ?r)
  18635. (org-remove-file file)
  18636. (throw 'nextfile t))
  18637. (t (error "Abort"))))))
  18638. ;;; Agenda prepare and finalize
  18639. (defvar org-agenda-multi nil) ; dynammically scoped
  18640. (defvar org-agenda-buffer-name "*Org Agenda*")
  18641. (defvar org-pre-agenda-window-conf nil)
  18642. (defvar org-agenda-name nil)
  18643. (defun org-prepare-agenda (&optional name)
  18644. (setq org-todo-keywords-for-agenda nil)
  18645. (setq org-done-keywords-for-agenda nil)
  18646. (if org-agenda-multi
  18647. (progn
  18648. (setq buffer-read-only nil)
  18649. (goto-char (point-max))
  18650. (unless (or (bobp) org-agenda-compact-blocks)
  18651. (insert "\n" (make-string (window-width) ?=) "\n"))
  18652. (narrow-to-region (point) (point-max)))
  18653. (org-agenda-reset-markers)
  18654. (org-prepare-agenda-buffers (org-agenda-files))
  18655. (setq org-todo-keywords-for-agenda
  18656. (org-uniquify org-todo-keywords-for-agenda))
  18657. (setq org-done-keywords-for-agenda
  18658. (org-uniquify org-done-keywords-for-agenda))
  18659. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18660. (awin (get-buffer-window abuf)))
  18661. (cond
  18662. ((equal (current-buffer) abuf) nil)
  18663. (awin (select-window awin))
  18664. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18665. ((equal org-agenda-window-setup 'current-window)
  18666. (switch-to-buffer abuf))
  18667. ((equal org-agenda-window-setup 'other-window)
  18668. (org-switch-to-buffer-other-window abuf))
  18669. ((equal org-agenda-window-setup 'other-frame)
  18670. (switch-to-buffer-other-frame abuf))
  18671. ((equal org-agenda-window-setup 'reorganize-frame)
  18672. (delete-other-windows)
  18673. (org-switch-to-buffer-other-window abuf))))
  18674. (setq buffer-read-only nil)
  18675. (erase-buffer)
  18676. (org-agenda-mode)
  18677. (and name (not org-agenda-name)
  18678. (org-set-local 'org-agenda-name name)))
  18679. (setq buffer-read-only nil))
  18680. (defun org-finalize-agenda ()
  18681. "Finishing touch for the agenda buffer, called just before displaying it."
  18682. (unless org-agenda-multi
  18683. (save-excursion
  18684. (let ((inhibit-read-only t))
  18685. (goto-char (point-min))
  18686. (while (org-activate-bracket-links (point-max))
  18687. (add-text-properties (match-beginning 0) (match-end 0)
  18688. '(face org-link)))
  18689. (org-agenda-align-tags)
  18690. (unless org-agenda-with-colors
  18691. (remove-text-properties (point-min) (point-max) '(face nil))))
  18692. (if (and (boundp 'org-overriding-columns-format)
  18693. org-overriding-columns-format)
  18694. (org-set-local 'org-overriding-columns-format
  18695. org-overriding-columns-format))
  18696. (if (and (boundp 'org-agenda-view-columns-initially)
  18697. org-agenda-view-columns-initially)
  18698. (org-agenda-columns))
  18699. (when org-agenda-fontify-priorities
  18700. (org-fontify-priorities))
  18701. (run-hooks 'org-finalize-agenda-hook)
  18702. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18703. )))
  18704. (defun org-fontify-priorities ()
  18705. "Make highest priority lines bold, and lowest italic."
  18706. (interactive)
  18707. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18708. (org-delete-overlay o)))
  18709. (org-overlays-in (point-min) (point-max)))
  18710. (save-excursion
  18711. (let ((inhibit-read-only t)
  18712. b e p ov h l)
  18713. (goto-char (point-min))
  18714. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18715. (setq h (or (get-char-property (point) 'org-highest-priority)
  18716. org-highest-priority)
  18717. l (or (get-char-property (point) 'org-lowest-priority)
  18718. org-lowest-priority)
  18719. p (string-to-char (match-string 1))
  18720. b (match-beginning 0) e (point-at-eol)
  18721. ov (org-make-overlay b e))
  18722. (org-overlay-put
  18723. ov 'face
  18724. (cond ((listp org-agenda-fontify-priorities)
  18725. (cdr (assoc p org-agenda-fontify-priorities)))
  18726. ((equal p l) 'italic)
  18727. ((equal p h) 'bold)))
  18728. (org-overlay-put ov 'org-type 'org-priority)))))
  18729. (defun org-prepare-agenda-buffers (files)
  18730. "Create buffers for all agenda files, protect archived trees and comments."
  18731. (interactive)
  18732. (let ((pa '(:org-archived t))
  18733. (pc '(:org-comment t))
  18734. (pall '(:org-archived t :org-comment t))
  18735. (inhibit-read-only t)
  18736. (rea (concat ":" org-archive-tag ":"))
  18737. bmp file re)
  18738. (save-excursion
  18739. (save-restriction
  18740. (while (setq file (pop files))
  18741. (if (bufferp file)
  18742. (set-buffer file)
  18743. (org-check-agenda-file file)
  18744. (set-buffer (org-get-agenda-file-buffer file)))
  18745. (widen)
  18746. (setq bmp (buffer-modified-p))
  18747. (org-refresh-category-properties)
  18748. (setq org-todo-keywords-for-agenda
  18749. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18750. (setq org-done-keywords-for-agenda
  18751. (append org-done-keywords-for-agenda org-done-keywords))
  18752. (save-excursion
  18753. (remove-text-properties (point-min) (point-max) pall)
  18754. (when org-agenda-skip-archived-trees
  18755. (goto-char (point-min))
  18756. (while (re-search-forward rea nil t)
  18757. (if (org-on-heading-p t)
  18758. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18759. (goto-char (point-min))
  18760. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18761. (while (re-search-forward re nil t)
  18762. (add-text-properties
  18763. (match-beginning 0) (org-end-of-subtree t) pc)))
  18764. (set-buffer-modified-p bmp))))))
  18765. (defvar org-agenda-skip-function nil
  18766. "Function to be called at each match during agenda construction.
  18767. If this function returns nil, the current match should not be skipped.
  18768. Otherwise, the function must return a position from where the search
  18769. should be continued.
  18770. This may also be a Lisp form, it will be evaluated.
  18771. Never set this variable using `setq' or so, because then it will apply
  18772. to all future agenda commands. Instead, bind it with `let' to scope
  18773. it dynamically into the agenda-constructing command. A good way to set
  18774. it is through options in org-agenda-custom-commands.")
  18775. (defun org-agenda-skip ()
  18776. "Throw to `:skip' in places that should be skipped.
  18777. Also moves point to the end of the skipped region, so that search can
  18778. continue from there."
  18779. (let ((p (point-at-bol)) to fp)
  18780. (and org-agenda-skip-archived-trees
  18781. (get-text-property p :org-archived)
  18782. (org-end-of-subtree t)
  18783. (throw :skip t))
  18784. (and (get-text-property p :org-comment)
  18785. (org-end-of-subtree t)
  18786. (throw :skip t))
  18787. (if (equal (char-after p) ?#) (throw :skip t))
  18788. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18789. (consp org-agenda-skip-function))
  18790. (setq to (save-excursion
  18791. (save-match-data
  18792. (if fp
  18793. (funcall org-agenda-skip-function)
  18794. (eval org-agenda-skip-function))))))
  18795. (goto-char to)
  18796. (throw :skip t))))
  18797. (defvar org-agenda-markers nil
  18798. "List of all currently active markers created by `org-agenda'.")
  18799. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18800. "Creation time of the last agenda marker.")
  18801. (defun org-agenda-new-marker (&optional pos)
  18802. "Return a new agenda marker.
  18803. Org-mode keeps a list of these markers and resets them when they are
  18804. no longer in use."
  18805. (let ((m (copy-marker (or pos (point)))))
  18806. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18807. (push m org-agenda-markers)
  18808. m))
  18809. (defun org-agenda-reset-markers ()
  18810. "Reset markers created by `org-agenda'."
  18811. (while org-agenda-markers
  18812. (move-marker (pop org-agenda-markers) nil)))
  18813. (defun org-get-agenda-file-buffer (file)
  18814. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18815. it to the list of buffers which might be released later."
  18816. (let ((buf (org-find-base-buffer-visiting file)))
  18817. (if buf
  18818. buf ; just return it
  18819. ;; Make a new buffer and remember it
  18820. (setq buf (find-file-noselect file))
  18821. (if buf (push buf org-agenda-new-buffers))
  18822. buf)))
  18823. (defun org-release-buffers (blist)
  18824. "Release all buffers in list, asking the user for confirmation when needed.
  18825. When a buffer is unmodified, it is just killed. When modified, it is saved
  18826. \(if the user agrees) and then killed."
  18827. (let (buf file)
  18828. (while (setq buf (pop blist))
  18829. (setq file (buffer-file-name buf))
  18830. (when (and (buffer-modified-p buf)
  18831. file
  18832. (y-or-n-p (format "Save file %s? " file)))
  18833. (with-current-buffer buf (save-buffer)))
  18834. (kill-buffer buf))))
  18835. (defun org-get-category (&optional pos)
  18836. "Get the category applying to position POS."
  18837. (get-text-property (or pos (point)) 'org-category))
  18838. ;;; Agenda timeline
  18839. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18840. (defun org-timeline (&optional include-all)
  18841. "Show a time-sorted view of the entries in the current org file.
  18842. Only entries with a time stamp of today or later will be listed. With
  18843. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18844. under the current date.
  18845. If the buffer contains an active region, only check the region for
  18846. dates."
  18847. (interactive "P")
  18848. (require 'calendar)
  18849. (org-compile-prefix-format 'timeline)
  18850. (org-set-sorting-strategy 'timeline)
  18851. (let* ((dopast t)
  18852. (dotodo include-all)
  18853. (doclosed org-agenda-show-log)
  18854. (entry buffer-file-name)
  18855. (date (calendar-current-date))
  18856. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18857. (end (if (org-region-active-p) (region-end) (point-max)))
  18858. (day-numbers (org-get-all-dates beg end 'no-ranges
  18859. t doclosed ; always include today
  18860. org-timeline-show-empty-dates))
  18861. (org-deadline-warning-days 0)
  18862. (org-agenda-only-exact-dates t)
  18863. (today (time-to-days (current-time)))
  18864. (past t)
  18865. args
  18866. s e rtn d emptyp)
  18867. (setq org-agenda-redo-command
  18868. (list 'progn
  18869. (list 'org-switch-to-buffer-other-window (current-buffer))
  18870. (list 'org-timeline (list 'quote include-all))))
  18871. (if (not dopast)
  18872. ;; Remove past dates from the list of dates.
  18873. (setq day-numbers (delq nil (mapcar (lambda(x)
  18874. (if (>= x today) x nil))
  18875. day-numbers))))
  18876. (org-prepare-agenda (concat "Timeline "
  18877. (file-name-nondirectory buffer-file-name)))
  18878. (if doclosed (push :closed args))
  18879. (push :timestamp args)
  18880. (push :deadline args)
  18881. (push :scheduled args)
  18882. (push :sexp args)
  18883. (if dotodo (push :todo args))
  18884. (while (setq d (pop day-numbers))
  18885. (if (and (listp d) (eq (car d) :omitted))
  18886. (progn
  18887. (setq s (point))
  18888. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18889. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18890. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18891. (if (and (>= d today)
  18892. dopast
  18893. past)
  18894. (progn
  18895. (setq past nil)
  18896. (insert (make-string 79 ?-) "\n")))
  18897. (setq date (calendar-gregorian-from-absolute d))
  18898. (setq s (point))
  18899. (setq rtn (and (not emptyp)
  18900. (apply 'org-agenda-get-day-entries entry
  18901. date args)))
  18902. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18903. (progn
  18904. (insert
  18905. (if (stringp org-agenda-format-date)
  18906. (format-time-string org-agenda-format-date
  18907. (org-time-from-absolute date))
  18908. (funcall org-agenda-format-date date))
  18909. "\n")
  18910. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18911. (put-text-property s (1- (point)) 'org-date-line t)
  18912. (if (equal d today)
  18913. (put-text-property s (1- (point)) 'org-today t))
  18914. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18915. (put-text-property s (1- (point)) 'day d)))))
  18916. (goto-char (point-min))
  18917. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18918. (point-min)))
  18919. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18920. (org-finalize-agenda)
  18921. (setq buffer-read-only t)))
  18922. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  18923. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18924. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18925. not every single day in the range. If FORCE-TODAY is non-nil, make
  18926. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18927. inactive time stamps (those in square brackets) are included.
  18928. When EMPTY is non-nil, also include days without any entries."
  18929. (let ((re (concat
  18930. (if pre-re pre-re "")
  18931. (if inactive org-ts-regexp-both org-ts-regexp)))
  18932. dates dates1 date day day1 day2 ts1 ts2)
  18933. (if force-today
  18934. (setq dates (list (time-to-days (current-time)))))
  18935. (save-excursion
  18936. (goto-char beg)
  18937. (while (re-search-forward re end t)
  18938. (setq day (time-to-days (org-time-string-to-time
  18939. (substring (match-string 1) 0 10))))
  18940. (or (memq day dates) (push day dates)))
  18941. (unless no-ranges
  18942. (goto-char beg)
  18943. (while (re-search-forward org-tr-regexp end t)
  18944. (setq ts1 (substring (match-string 1) 0 10)
  18945. ts2 (substring (match-string 2) 0 10)
  18946. day1 (time-to-days (org-time-string-to-time ts1))
  18947. day2 (time-to-days (org-time-string-to-time ts2)))
  18948. (while (< (setq day1 (1+ day1)) day2)
  18949. (or (memq day1 dates) (push day1 dates)))))
  18950. (setq dates (sort dates '<))
  18951. (when empty
  18952. (while (setq day (pop dates))
  18953. (setq day2 (car dates))
  18954. (push day dates1)
  18955. (when (and day2 empty)
  18956. (if (or (eq empty t)
  18957. (and (numberp empty) (<= (- day2 day) empty)))
  18958. (while (< (setq day (1+ day)) day2)
  18959. (push (list day) dates1))
  18960. (push (cons :omitted (- day2 day)) dates1))))
  18961. (setq dates (nreverse dates1)))
  18962. dates)))
  18963. ;;; Agenda Daily/Weekly
  18964. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18965. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18966. (defvar org-agenda-last-arguments nil
  18967. "The arguments of the previous call to org-agenda")
  18968. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18969. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18970. (defvar org-include-all-loc nil) ; local variable
  18971. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18972. ;;;###autoload
  18973. (defun org-agenda-list (&optional include-all start-day ndays)
  18974. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18975. The view will be for the current day or week, but from the overview buffer
  18976. you will be able to go to other days/weeks.
  18977. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18978. all unfinished TODO items will also be shown, before the agenda.
  18979. This feature is considered obsolete, please use the TODO list or a block
  18980. agenda instead.
  18981. With a numeric prefix argument in an interactive call, the agenda will
  18982. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18983. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18984. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18985. given in `org-agenda-start-on-weekday'."
  18986. (interactive "P")
  18987. (if (and (integerp include-all) (> include-all 0))
  18988. (setq ndays include-all include-all nil))
  18989. (setq ndays (or ndays org-agenda-ndays)
  18990. start-day (or start-day org-agenda-start-day))
  18991. (if org-agenda-overriding-arguments
  18992. (setq include-all (car org-agenda-overriding-arguments)
  18993. start-day (nth 1 org-agenda-overriding-arguments)
  18994. ndays (nth 2 org-agenda-overriding-arguments)))
  18995. (if (stringp start-day)
  18996. ;; Convert to an absolute day number
  18997. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18998. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18999. (org-compile-prefix-format 'agenda)
  19000. (org-set-sorting-strategy 'agenda)
  19001. (require 'calendar)
  19002. (let* ((org-agenda-start-on-weekday
  19003. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19004. org-agenda-start-on-weekday nil))
  19005. (thefiles (org-agenda-files))
  19006. (files thefiles)
  19007. (today (time-to-days
  19008. (time-subtract (current-time)
  19009. (list 0 (* 3600 org-extend-today-until) 0))))
  19010. (sd (or start-day today))
  19011. (start (if (or (null org-agenda-start-on-weekday)
  19012. (< org-agenda-ndays 7))
  19013. sd
  19014. (let* ((nt (calendar-day-of-week
  19015. (calendar-gregorian-from-absolute sd)))
  19016. (n1 org-agenda-start-on-weekday)
  19017. (d (- nt n1)))
  19018. (- sd (+ (if (< d 0) 7 0) d)))))
  19019. (day-numbers (list start))
  19020. (day-cnt 0)
  19021. (inhibit-redisplay (not debug-on-error))
  19022. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19023. (setq org-agenda-redo-command
  19024. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19025. ;; Make the list of days
  19026. (setq ndays (or ndays org-agenda-ndays)
  19027. nd ndays)
  19028. (while (> ndays 1)
  19029. (push (1+ (car day-numbers)) day-numbers)
  19030. (setq ndays (1- ndays)))
  19031. (setq day-numbers (nreverse day-numbers))
  19032. (org-prepare-agenda "Day/Week")
  19033. (org-set-local 'org-starting-day (car day-numbers))
  19034. (org-set-local 'org-include-all-loc include-all)
  19035. (org-set-local 'org-agenda-span
  19036. (org-agenda-ndays-to-span nd))
  19037. (when (and (or include-all org-agenda-include-all-todo)
  19038. (member today day-numbers))
  19039. (setq files thefiles
  19040. rtnall nil)
  19041. (while (setq file (pop files))
  19042. (catch 'nextfile
  19043. (org-check-agenda-file file)
  19044. (setq date (calendar-gregorian-from-absolute today)
  19045. rtn (org-agenda-get-day-entries
  19046. file date :todo))
  19047. (setq rtnall (append rtnall rtn))))
  19048. (when rtnall
  19049. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19050. (add-text-properties (point-min) (1- (point))
  19051. (list 'face 'org-agenda-structure))
  19052. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19053. (unless org-agenda-compact-blocks
  19054. (setq s (point))
  19055. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19056. "-agenda:\n")
  19057. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19058. 'org-date-line t)))
  19059. (while (setq d (pop day-numbers))
  19060. (setq date (calendar-gregorian-from-absolute d)
  19061. s (point))
  19062. (if (or (setq todayp (= d today))
  19063. (and (not start-pos) (= d sd)))
  19064. (setq start-pos (point))
  19065. (if (and start-pos (not end-pos))
  19066. (setq end-pos (point))))
  19067. (setq files thefiles
  19068. rtnall nil)
  19069. (while (setq file (pop files))
  19070. (catch 'nextfile
  19071. (org-check-agenda-file file)
  19072. (if org-agenda-show-log
  19073. (setq rtn (org-agenda-get-day-entries
  19074. file date
  19075. :deadline :scheduled :timestamp :sexp :closed))
  19076. (setq rtn (org-agenda-get-day-entries
  19077. file date
  19078. :deadline :scheduled :sexp :timestamp)))
  19079. (setq rtnall (append rtnall rtn))))
  19080. (if org-agenda-include-diary
  19081. (progn
  19082. (require 'diary-lib)
  19083. (setq rtn (org-get-entries-from-diary date))
  19084. (setq rtnall (append rtnall rtn))))
  19085. (if (or rtnall org-agenda-show-all-dates)
  19086. (progn
  19087. (setq day-cnt (1+ day-cnt))
  19088. (insert
  19089. (if (stringp org-agenda-format-date)
  19090. (format-time-string org-agenda-format-date
  19091. (org-time-from-absolute date))
  19092. (funcall org-agenda-format-date date))
  19093. "\n")
  19094. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19095. (put-text-property s (1- (point)) 'org-date-line t)
  19096. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19097. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19098. (if rtnall (insert
  19099. (org-finalize-agenda-entries
  19100. (org-agenda-add-time-grid-maybe
  19101. rtnall nd todayp))
  19102. "\n"))
  19103. (put-text-property s (1- (point)) 'day d)
  19104. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19105. (goto-char (point-min))
  19106. (org-fit-agenda-window)
  19107. (unless (and (pos-visible-in-window-p (point-min))
  19108. (pos-visible-in-window-p (point-max)))
  19109. (goto-char (1- (point-max)))
  19110. (recenter -1)
  19111. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19112. (progn
  19113. (goto-char (or start-pos 1))
  19114. (recenter 1))))
  19115. (goto-char (or start-pos 1))
  19116. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19117. (org-finalize-agenda)
  19118. (setq buffer-read-only t)
  19119. (message "")))
  19120. (defun org-agenda-ndays-to-span (n)
  19121. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19122. ;;; Agenda TODO list
  19123. (defvar org-select-this-todo-keyword nil)
  19124. (defvar org-last-arg nil)
  19125. ;;;###autoload
  19126. (defun org-todo-list (arg)
  19127. "Show all TODO entries from all agenda file in a single list.
  19128. The prefix arg can be used to select a specific TODO keyword and limit
  19129. the list to these. When using \\[universal-argument], you will be prompted
  19130. for a keyword. A numeric prefix directly selects the Nth keyword in
  19131. `org-todo-keywords-1'."
  19132. (interactive "P")
  19133. (require 'calendar)
  19134. (org-compile-prefix-format 'todo)
  19135. (org-set-sorting-strategy 'todo)
  19136. (org-prepare-agenda "TODO")
  19137. (let* ((today (time-to-days (current-time)))
  19138. (date (calendar-gregorian-from-absolute today))
  19139. (kwds org-todo-keywords-for-agenda)
  19140. (completion-ignore-case t)
  19141. (org-select-this-todo-keyword
  19142. (if (stringp arg) arg
  19143. (and arg (integerp arg) (> arg 0)
  19144. (nth (1- arg) kwds))))
  19145. rtn rtnall files file pos)
  19146. (when (equal arg '(4))
  19147. (setq org-select-this-todo-keyword
  19148. (completing-read "Keyword (or KWD1|K2D2|...): "
  19149. (mapcar 'list kwds) nil nil)))
  19150. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19151. (org-set-local 'org-last-arg arg)
  19152. (setq org-agenda-redo-command
  19153. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19154. (setq files (org-agenda-files)
  19155. rtnall nil)
  19156. (while (setq file (pop files))
  19157. (catch 'nextfile
  19158. (org-check-agenda-file file)
  19159. (setq rtn (org-agenda-get-day-entries file date :todo))
  19160. (setq rtnall (append rtnall rtn))))
  19161. (if org-agenda-overriding-header
  19162. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19163. nil 'face 'org-agenda-structure) "\n")
  19164. (insert "Global list of TODO items of type: ")
  19165. (add-text-properties (point-min) (1- (point))
  19166. (list 'face 'org-agenda-structure))
  19167. (setq pos (point))
  19168. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19169. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19170. (setq pos (point))
  19171. (unless org-agenda-multi
  19172. (insert "Available with `N r': (0)ALL")
  19173. (let ((n 0) s)
  19174. (mapc (lambda (x)
  19175. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19176. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19177. (insert "\n "))
  19178. (insert " " s))
  19179. kwds))
  19180. (insert "\n"))
  19181. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19182. (when rtnall
  19183. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19184. (goto-char (point-min))
  19185. (org-fit-agenda-window)
  19186. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19187. (org-finalize-agenda)
  19188. (setq buffer-read-only t)))
  19189. ;;; Agenda tags match
  19190. ;;;###autoload
  19191. (defun org-tags-view (&optional todo-only match)
  19192. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19193. The prefix arg TODO-ONLY limits the search to TODO entries."
  19194. (interactive "P")
  19195. (org-compile-prefix-format 'tags)
  19196. (org-set-sorting-strategy 'tags)
  19197. (let* ((org-tags-match-list-sublevels
  19198. (if todo-only t org-tags-match-list-sublevels))
  19199. (completion-ignore-case t)
  19200. rtn rtnall files file pos matcher
  19201. buffer)
  19202. (setq matcher (org-make-tags-matcher match)
  19203. match (car matcher) matcher (cdr matcher))
  19204. (org-prepare-agenda (concat "TAGS " match))
  19205. (setq org-agenda-redo-command
  19206. (list 'org-tags-view (list 'quote todo-only)
  19207. (list 'if 'current-prefix-arg nil match)))
  19208. (setq files (org-agenda-files)
  19209. rtnall nil)
  19210. (while (setq file (pop files))
  19211. (catch 'nextfile
  19212. (org-check-agenda-file file)
  19213. (setq buffer (if (file-exists-p file)
  19214. (org-get-agenda-file-buffer file)
  19215. (error "No such file %s" file)))
  19216. (if (not buffer)
  19217. ;; If file does not exist, merror message to agenda
  19218. (setq rtn (list
  19219. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19220. rtnall (append rtnall rtn))
  19221. (with-current-buffer buffer
  19222. (unless (org-mode-p)
  19223. (error "Agenda file %s is not in `org-mode'" file))
  19224. (save-excursion
  19225. (save-restriction
  19226. (if org-agenda-restrict
  19227. (narrow-to-region org-agenda-restrict-begin
  19228. org-agenda-restrict-end)
  19229. (widen))
  19230. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19231. (setq rtnall (append rtnall rtn))))))))
  19232. (if org-agenda-overriding-header
  19233. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19234. nil 'face 'org-agenda-structure) "\n")
  19235. (insert "Headlines with TAGS match: ")
  19236. (add-text-properties (point-min) (1- (point))
  19237. (list 'face 'org-agenda-structure))
  19238. (setq pos (point))
  19239. (insert match "\n")
  19240. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19241. (setq pos (point))
  19242. (unless org-agenda-multi
  19243. (insert "Press `C-u r' to search again with new search string\n"))
  19244. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19245. (when rtnall
  19246. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19247. (goto-char (point-min))
  19248. (org-fit-agenda-window)
  19249. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19250. (org-finalize-agenda)
  19251. (setq buffer-read-only t)))
  19252. ;;; Agenda Finding stuck projects
  19253. (defvar org-agenda-skip-regexp nil
  19254. "Regular expression used in skipping subtrees for the agenda.
  19255. This is basically a temporary global variable that can be set and then
  19256. used by user-defined selections using `org-agenda-skip-function'.")
  19257. (defvar org-agenda-overriding-header nil
  19258. "When this is set during todo and tags searches, will replace header.")
  19259. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19260. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19261. If yes, it returns the end position of this tree, causing agenda commands
  19262. to skip this subtree. This is a function that can be put into
  19263. `org-agenda-skip-function' for the duration of a command."
  19264. (let ((end (save-excursion (org-end-of-subtree t)))
  19265. skip)
  19266. (save-excursion
  19267. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19268. (and skip end)))
  19269. (defun org-agenda-skip-entry-if (&rest conditions)
  19270. "Skip entry if any of CONDITIONS is true.
  19271. See `org-agenda-skip-if' for details."
  19272. (org-agenda-skip-if nil conditions))
  19273. (defun org-agenda-skip-subtree-if (&rest conditions)
  19274. "Skip entry if any of CONDITIONS is true.
  19275. See `org-agenda-skip-if' for details."
  19276. (org-agenda-skip-if t conditions))
  19277. (defun org-agenda-skip-if (subtree conditions)
  19278. "Checks current entity for CONDITIONS.
  19279. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19280. the entry, i.e. the text before the next heading is checked.
  19281. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19282. from different tests. Valid conditions are:
  19283. scheduled Check if there is a scheduled cookie
  19284. notscheduled Check if there is no scheduled cookie
  19285. deadline Check if there is a deadline
  19286. notdeadline Check if there is no deadline
  19287. regexp Check if regexp matches
  19288. notregexp Check if regexp does not match.
  19289. The regexp is taken from the conditions list, it must come right after
  19290. the `regexp' or `notregexp' element.
  19291. If any of these conditions is met, this function returns the end point of
  19292. the entity, causing the search to continue from there. This is a function
  19293. that can be put into `org-agenda-skip-function' for the duration of a command."
  19294. (let (beg end m)
  19295. (org-back-to-heading t)
  19296. (setq beg (point)
  19297. end (if subtree
  19298. (progn (org-end-of-subtree t) (point))
  19299. (progn (outline-next-heading) (1- (point)))))
  19300. (goto-char beg)
  19301. (and
  19302. (or
  19303. (and (memq 'scheduled conditions)
  19304. (re-search-forward org-scheduled-time-regexp end t))
  19305. (and (memq 'notscheduled conditions)
  19306. (not (re-search-forward org-scheduled-time-regexp end t)))
  19307. (and (memq 'deadline conditions)
  19308. (re-search-forward org-deadline-time-regexp end t))
  19309. (and (memq 'notdeadline conditions)
  19310. (not (re-search-forward org-deadline-time-regexp end t)))
  19311. (and (setq m (memq 'regexp conditions))
  19312. (stringp (nth 1 m))
  19313. (re-search-forward (nth 1 m) end t))
  19314. (and (setq m (memq 'notregexp conditions))
  19315. (stringp (nth 1 m))
  19316. (not (re-search-forward (nth 1 m) end t))))
  19317. end)))
  19318. ;;;###autoload
  19319. (defun org-agenda-list-stuck-projects (&rest ignore)
  19320. "Create agenda view for projects that are stuck.
  19321. Stuck projects are project that have no next actions. For the definitions
  19322. of what a project is and how to check if it stuck, customize the variable
  19323. `org-stuck-projects'.
  19324. MATCH is being ignored."
  19325. (interactive)
  19326. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19327. ;; FIXME: we could have used org-agenda-skip-if here.
  19328. (org-agenda-overriding-header "List of stuck projects: ")
  19329. (matcher (nth 0 org-stuck-projects))
  19330. (todo (nth 1 org-stuck-projects))
  19331. (todo-wds (if (member "*" todo)
  19332. (progn
  19333. (org-prepare-agenda-buffers (org-agenda-files))
  19334. (org-delete-all
  19335. org-done-keywords-for-agenda
  19336. (copy-sequence org-todo-keywords-for-agenda)))
  19337. todo))
  19338. (todo-re (concat "^\\*+[ \t]+\\("
  19339. (mapconcat 'identity todo-wds "\\|")
  19340. "\\)\\>"))
  19341. (tags (nth 2 org-stuck-projects))
  19342. (tags-re (if (member "*" tags)
  19343. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19344. (concat "^\\*+ .*:\\("
  19345. (mapconcat 'identity tags "\\|")
  19346. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19347. (gen-re (nth 3 org-stuck-projects))
  19348. (re-list
  19349. (delq nil
  19350. (list
  19351. (if todo todo-re)
  19352. (if tags tags-re)
  19353. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19354. gen-re)))))
  19355. (setq org-agenda-skip-regexp
  19356. (if re-list
  19357. (mapconcat 'identity re-list "\\|")
  19358. (error "No information how to identify unstuck projects")))
  19359. (org-tags-view nil matcher)
  19360. (with-current-buffer org-agenda-buffer-name
  19361. (setq org-agenda-redo-command
  19362. '(org-agenda-list-stuck-projects
  19363. (or current-prefix-arg org-last-arg))))))
  19364. ;;; Diary integration
  19365. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19366. (defun org-get-entries-from-diary (date)
  19367. "Get the (Emacs Calendar) diary entries for DATE."
  19368. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19369. (diary-display-hook '(fancy-diary-display))
  19370. (pop-up-frames nil)
  19371. (list-diary-entries-hook
  19372. (cons 'org-diary-default-entry list-diary-entries-hook))
  19373. (diary-file-name-prefix-function nil) ; turn this feature off
  19374. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19375. entries
  19376. (org-disable-agenda-to-diary t))
  19377. (save-excursion
  19378. (save-window-excursion
  19379. (funcall (if (fboundp 'diary-list-entries)
  19380. 'diary-list-entries 'list-diary-entries)
  19381. date 1)))
  19382. (if (not (get-buffer fancy-diary-buffer))
  19383. (setq entries nil)
  19384. (with-current-buffer fancy-diary-buffer
  19385. (setq buffer-read-only nil)
  19386. (if (zerop (buffer-size))
  19387. ;; No entries
  19388. (setq entries nil)
  19389. ;; Omit the date and other unnecessary stuff
  19390. (org-agenda-cleanup-fancy-diary)
  19391. ;; Add prefix to each line and extend the text properties
  19392. (if (zerop (buffer-size))
  19393. (setq entries nil)
  19394. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19395. (set-buffer-modified-p nil)
  19396. (kill-buffer fancy-diary-buffer)))
  19397. (when entries
  19398. (setq entries (org-split-string entries "\n"))
  19399. (setq entries
  19400. (mapcar
  19401. (lambda (x)
  19402. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19403. ;; Extend the text properties to the beginning of the line
  19404. (org-add-props x (text-properties-at (1- (length x)) x)
  19405. 'type "diary" 'date date))
  19406. entries)))))
  19407. (defun org-agenda-cleanup-fancy-diary ()
  19408. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19409. This gets rid of the date, the underline under the date, and
  19410. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19411. date. It also removes lines that contain only whitespace."
  19412. (goto-char (point-min))
  19413. (if (looking-at ".*?:[ \t]*")
  19414. (progn
  19415. (replace-match "")
  19416. (re-search-forward "\n=+$" nil t)
  19417. (replace-match "")
  19418. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19419. (re-search-forward "\n=+$" nil t)
  19420. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19421. (goto-char (point-min))
  19422. (while (re-search-forward "^ +\n" nil t)
  19423. (replace-match ""))
  19424. (goto-char (point-min))
  19425. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19426. (replace-match "")))
  19427. ;; Make sure entries from the diary have the right text properties.
  19428. (eval-after-load "diary-lib"
  19429. '(if (boundp 'diary-modify-entry-list-string-function)
  19430. ;; We can rely on the hook, nothing to do
  19431. nil
  19432. ;; Hook not avaiable, must use advice to make this work
  19433. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19434. "Make the position visible."
  19435. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19436. (stringp string)
  19437. buffer-file-name)
  19438. (setq string (org-modify-diary-entry-string string))))))
  19439. (defun org-modify-diary-entry-string (string)
  19440. "Add text properties to string, allowing org-mode to act on it."
  19441. (org-add-props string nil
  19442. 'mouse-face 'highlight
  19443. 'keymap org-agenda-keymap
  19444. 'help-echo (if buffer-file-name
  19445. (format "mouse-2 or RET jump to diary file %s"
  19446. (abbreviate-file-name buffer-file-name))
  19447. "")
  19448. 'org-agenda-diary-link t
  19449. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19450. (defun org-diary-default-entry ()
  19451. "Add a dummy entry to the diary.
  19452. Needed to avoid empty dates which mess up holiday display."
  19453. ;; Catch the error if dealing with the new add-to-diary-alist
  19454. (when org-disable-agenda-to-diary
  19455. (condition-case nil
  19456. (add-to-diary-list original-date "Org-mode dummy" "")
  19457. (error
  19458. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19459. ;;;###autoload
  19460. (defun org-diary (&rest args)
  19461. "Return diary information from org-files.
  19462. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19463. It accesses org files and extracts information from those files to be
  19464. listed in the diary. The function accepts arguments specifying what
  19465. items should be listed. The following arguments are allowed:
  19466. :timestamp List the headlines of items containing a date stamp or
  19467. date range matching the selected date. Deadlines will
  19468. also be listed, on the expiration day.
  19469. :sexp List entries resulting from diary-like sexps.
  19470. :deadline List any deadlines past due, or due within
  19471. `org-deadline-warning-days'. The listing occurs only
  19472. in the diary for *today*, not at any other date. If
  19473. an entry is marked DONE, it is no longer listed.
  19474. :scheduled List all items which are scheduled for the given date.
  19475. The diary for *today* also contains items which were
  19476. scheduled earlier and are not yet marked DONE.
  19477. :todo List all TODO items from the org-file. This may be a
  19478. long list - so this is not turned on by default.
  19479. Like deadlines, these entries only show up in the
  19480. diary for *today*, not at any other date.
  19481. The call in the diary file should look like this:
  19482. &%%(org-diary) ~/path/to/some/orgfile.org
  19483. Use a separate line for each org file to check. Or, if you omit the file name,
  19484. all files listed in `org-agenda-files' will be checked automatically:
  19485. &%%(org-diary)
  19486. If you don't give any arguments (as in the example above), the default
  19487. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19488. So the example above may also be written as
  19489. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19490. The function expects the lisp variables `entry' and `date' to be provided
  19491. by the caller, because this is how the calendar works. Don't use this
  19492. function from a program - use `org-agenda-get-day-entries' instead."
  19493. (when (> (- (time-to-seconds (current-time))
  19494. org-agenda-last-marker-time)
  19495. 5)
  19496. (org-agenda-reset-markers))
  19497. (org-compile-prefix-format 'agenda)
  19498. (org-set-sorting-strategy 'agenda)
  19499. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19500. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19501. (list entry)
  19502. (org-agenda-files t)))
  19503. file rtn results)
  19504. (org-prepare-agenda-buffers files)
  19505. ;; If this is called during org-agenda, don't return any entries to
  19506. ;; the calendar. Org Agenda will list these entries itself.
  19507. (if org-disable-agenda-to-diary (setq files nil))
  19508. (while (setq file (pop files))
  19509. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19510. (setq results (append results rtn)))
  19511. (if results
  19512. (concat (org-finalize-agenda-entries results) "\n"))))
  19513. ;;; Agenda entry finders
  19514. (defun org-agenda-get-day-entries (file date &rest args)
  19515. "Does the work for `org-diary' and `org-agenda'.
  19516. FILE is the path to a file to be checked for entries. DATE is date like
  19517. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19518. which kind of entries should be extracted. For details about these, see
  19519. the documentation of `org-diary'."
  19520. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19521. (let* ((org-startup-folded nil)
  19522. (org-startup-align-all-tables nil)
  19523. (buffer (if (file-exists-p file)
  19524. (org-get-agenda-file-buffer file)
  19525. (error "No such file %s" file)))
  19526. arg results rtn)
  19527. (if (not buffer)
  19528. ;; If file does not exist, make sure an error message ends up in diary
  19529. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19530. (with-current-buffer buffer
  19531. (unless (org-mode-p)
  19532. (error "Agenda file %s is not in `org-mode'" file))
  19533. (let ((case-fold-search nil))
  19534. (save-excursion
  19535. (save-restriction
  19536. (if org-agenda-restrict
  19537. (narrow-to-region org-agenda-restrict-begin
  19538. org-agenda-restrict-end)
  19539. (widen))
  19540. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19541. (while (setq arg (pop args))
  19542. (cond
  19543. ((and (eq arg :todo)
  19544. (equal date (calendar-current-date)))
  19545. (setq rtn (org-agenda-get-todos))
  19546. (setq results (append results rtn)))
  19547. ((eq arg :timestamp)
  19548. (setq rtn (org-agenda-get-blocks))
  19549. (setq results (append results rtn))
  19550. (setq rtn (org-agenda-get-timestamps))
  19551. (setq results (append results rtn)))
  19552. ((eq arg :sexp)
  19553. (setq rtn (org-agenda-get-sexps))
  19554. (setq results (append results rtn)))
  19555. ((eq arg :scheduled)
  19556. (setq rtn (org-agenda-get-scheduled))
  19557. (setq results (append results rtn)))
  19558. ((eq arg :closed)
  19559. (setq rtn (org-agenda-get-closed))
  19560. (setq results (append results rtn)))
  19561. ((eq arg :deadline)
  19562. (setq rtn (org-agenda-get-deadlines))
  19563. (setq results (append results rtn))))))))
  19564. results))))
  19565. (defun org-entry-is-todo-p ()
  19566. (member (org-get-todo-state) org-not-done-keywords))
  19567. (defun org-entry-is-done-p ()
  19568. (member (org-get-todo-state) org-done-keywords))
  19569. (defun org-get-todo-state ()
  19570. (save-excursion
  19571. (org-back-to-heading t)
  19572. (and (looking-at org-todo-line-regexp)
  19573. (match-end 2)
  19574. (match-string 2))))
  19575. (defun org-at-date-range-p (&optional inactive-ok)
  19576. "Is the cursor inside a date range?"
  19577. (interactive)
  19578. (save-excursion
  19579. (catch 'exit
  19580. (let ((pos (point)))
  19581. (skip-chars-backward "^[<\r\n")
  19582. (skip-chars-backward "<[")
  19583. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19584. (>= (match-end 0) pos)
  19585. (throw 'exit t))
  19586. (skip-chars-backward "^<[\r\n")
  19587. (skip-chars-backward "<[")
  19588. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19589. (>= (match-end 0) pos)
  19590. (throw 'exit t)))
  19591. nil)))
  19592. (defun org-agenda-get-todos ()
  19593. "Return the TODO information for agenda display."
  19594. (let* ((props (list 'face nil
  19595. 'done-face 'org-done
  19596. 'org-not-done-regexp org-not-done-regexp
  19597. 'org-todo-regexp org-todo-regexp
  19598. 'mouse-face 'highlight
  19599. 'keymap org-agenda-keymap
  19600. 'help-echo
  19601. (format "mouse-2 or RET jump to org file %s"
  19602. (abbreviate-file-name buffer-file-name))))
  19603. ;; FIXME: get rid of the \n at some point but watch out
  19604. (regexp (concat "^\\*+[ \t]+\\("
  19605. (if org-select-this-todo-keyword
  19606. (if (equal org-select-this-todo-keyword "*")
  19607. org-todo-regexp
  19608. (concat "\\<\\("
  19609. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19610. "\\)\\>"))
  19611. org-not-done-regexp)
  19612. "[^\n\r]*\\)"))
  19613. marker priority category tags
  19614. ee txt beg end)
  19615. (goto-char (point-min))
  19616. (while (re-search-forward regexp nil t)
  19617. (catch :skip
  19618. (save-match-data
  19619. (beginning-of-line)
  19620. (setq beg (point) end (progn (outline-next-heading) (point)))
  19621. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19622. (re-search-forward org-ts-regexp end t))
  19623. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19624. (re-search-forward org-scheduled-time-regexp end t))
  19625. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19626. (re-search-forward org-deadline-time-regexp end t)
  19627. (org-deadline-close (match-string 1))))
  19628. (goto-char (1+ beg))
  19629. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19630. (throw :skip nil)))
  19631. (goto-char beg)
  19632. (org-agenda-skip)
  19633. (goto-char (match-beginning 1))
  19634. (setq marker (org-agenda-new-marker (match-beginning 0))
  19635. category (org-get-category)
  19636. tags (org-get-tags-at (point))
  19637. txt (org-format-agenda-item "" (match-string 1) category tags)
  19638. priority (1+ (org-get-priority txt)))
  19639. (org-add-props txt props
  19640. 'org-marker marker 'org-hd-marker marker
  19641. 'priority priority 'org-category category
  19642. 'type "todo")
  19643. (push txt ee)
  19644. (if org-agenda-todo-list-sublevels
  19645. (goto-char (match-end 1))
  19646. (org-end-of-subtree 'invisible))))
  19647. (nreverse ee)))
  19648. (defconst org-agenda-no-heading-message
  19649. "No heading for this item in buffer or region.")
  19650. (defun org-agenda-get-timestamps ()
  19651. "Return the date stamp information for agenda display."
  19652. (let* ((props (list 'face nil
  19653. 'org-not-done-regexp org-not-done-regexp
  19654. 'org-todo-regexp org-todo-regexp
  19655. 'mouse-face 'highlight
  19656. 'keymap org-agenda-keymap
  19657. 'help-echo
  19658. (format "mouse-2 or RET jump to org file %s"
  19659. (abbreviate-file-name buffer-file-name))))
  19660. (d1 (calendar-absolute-from-gregorian date))
  19661. (remove-re
  19662. (concat
  19663. (regexp-quote
  19664. (format-time-string
  19665. "<%Y-%m-%d"
  19666. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19667. ".*?>"))
  19668. (regexp
  19669. (concat
  19670. (regexp-quote
  19671. (substring
  19672. (format-time-string
  19673. (car org-time-stamp-formats)
  19674. (apply 'encode-time ; DATE bound by calendar
  19675. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19676. 0 11))
  19677. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19678. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19679. marker hdmarker deadlinep scheduledp donep tmp priority category
  19680. ee txt timestr tags b0 b3 e3 head)
  19681. (goto-char (point-min))
  19682. (while (re-search-forward regexp nil t)
  19683. (setq b0 (match-beginning 0)
  19684. b3 (match-beginning 3) e3 (match-end 3))
  19685. (catch :skip
  19686. (and (org-at-date-range-p) (throw :skip nil))
  19687. (org-agenda-skip)
  19688. (if (and (match-end 1)
  19689. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19690. (throw :skip nil))
  19691. (if (and e3
  19692. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19693. (throw :skip nil))
  19694. (setq marker (org-agenda-new-marker b0)
  19695. category (org-get-category b0)
  19696. tmp (buffer-substring (max (point-min)
  19697. (- b0 org-ds-keyword-length))
  19698. b0)
  19699. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19700. deadlinep (string-match org-deadline-regexp tmp)
  19701. scheduledp (string-match org-scheduled-regexp tmp)
  19702. donep (org-entry-is-done-p))
  19703. (if (or scheduledp deadlinep) (throw :skip t))
  19704. (if (string-match ">" timestr)
  19705. ;; substring should only run to end of time stamp
  19706. (setq timestr (substring timestr 0 (match-end 0))))
  19707. (save-excursion
  19708. (if (re-search-backward "^\\*+ " nil t)
  19709. (progn
  19710. (goto-char (match-beginning 0))
  19711. (setq hdmarker (org-agenda-new-marker)
  19712. tags (org-get-tags-at))
  19713. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19714. (setq head (match-string 1))
  19715. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19716. (setq txt (org-format-agenda-item
  19717. nil head category tags timestr nil
  19718. remove-re)))
  19719. (setq txt org-agenda-no-heading-message))
  19720. (setq priority (org-get-priority txt))
  19721. (org-add-props txt props
  19722. 'org-marker marker 'org-hd-marker hdmarker)
  19723. (org-add-props txt nil 'priority priority
  19724. 'org-category category 'date date
  19725. 'type "timestamp")
  19726. (push txt ee))
  19727. (outline-next-heading)))
  19728. (nreverse ee)))
  19729. (defun org-agenda-get-sexps ()
  19730. "Return the sexp information for agenda display."
  19731. (require 'diary-lib)
  19732. (let* ((props (list 'face nil
  19733. 'mouse-face 'highlight
  19734. 'keymap org-agenda-keymap
  19735. 'help-echo
  19736. (format "mouse-2 or RET jump to org file %s"
  19737. (abbreviate-file-name buffer-file-name))))
  19738. (regexp "^&?%%(")
  19739. marker category ee txt tags entry result beg b sexp sexp-entry)
  19740. (goto-char (point-min))
  19741. (while (re-search-forward regexp nil t)
  19742. (catch :skip
  19743. (org-agenda-skip)
  19744. (setq beg (match-beginning 0))
  19745. (goto-char (1- (match-end 0)))
  19746. (setq b (point))
  19747. (forward-sexp 1)
  19748. (setq sexp (buffer-substring b (point)))
  19749. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19750. (org-trim (match-string 1))
  19751. ""))
  19752. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19753. (when result
  19754. (setq marker (org-agenda-new-marker beg)
  19755. category (org-get-category beg))
  19756. (if (string-match "\\S-" result)
  19757. (setq txt result)
  19758. (setq txt "SEXP entry returned empty string"))
  19759. (setq txt (org-format-agenda-item
  19760. "" txt category tags 'time))
  19761. (org-add-props txt props 'org-marker marker)
  19762. (org-add-props txt nil
  19763. 'org-category category 'date date
  19764. 'type "sexp")
  19765. (push txt ee))))
  19766. (nreverse ee)))
  19767. (defun org-agenda-get-closed ()
  19768. "Return the logged TODO entries for agenda display."
  19769. (let* ((props (list 'mouse-face 'highlight
  19770. 'org-not-done-regexp org-not-done-regexp
  19771. 'org-todo-regexp org-todo-regexp
  19772. 'keymap org-agenda-keymap
  19773. 'help-echo
  19774. (format "mouse-2 or RET jump to org file %s"
  19775. (abbreviate-file-name buffer-file-name))))
  19776. (regexp (concat
  19777. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19778. (regexp-quote
  19779. (substring
  19780. (format-time-string
  19781. (car org-time-stamp-formats)
  19782. (apply 'encode-time ; DATE bound by calendar
  19783. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19784. 1 11))))
  19785. marker hdmarker priority category tags closedp
  19786. ee txt timestr)
  19787. (goto-char (point-min))
  19788. (while (re-search-forward regexp nil t)
  19789. (catch :skip
  19790. (org-agenda-skip)
  19791. (setq marker (org-agenda-new-marker (match-beginning 0))
  19792. closedp (equal (match-string 1) org-closed-string)
  19793. category (org-get-category (match-beginning 0))
  19794. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19795. ;; donep (org-entry-is-done-p)
  19796. )
  19797. (if (string-match "\\]" timestr)
  19798. ;; substring should only run to end of time stamp
  19799. (setq timestr (substring timestr 0 (match-end 0))))
  19800. (save-excursion
  19801. (if (re-search-backward "^\\*+ " nil t)
  19802. (progn
  19803. (goto-char (match-beginning 0))
  19804. (setq hdmarker (org-agenda-new-marker)
  19805. tags (org-get-tags-at))
  19806. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19807. (setq txt (org-format-agenda-item
  19808. (if closedp "Closed: " "Clocked: ")
  19809. (match-string 1) category tags timestr)))
  19810. (setq txt org-agenda-no-heading-message))
  19811. (setq priority 100000)
  19812. (org-add-props txt props
  19813. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19814. 'priority priority 'org-category category
  19815. 'type "closed" 'date date
  19816. 'undone-face 'org-warning 'done-face 'org-done)
  19817. (push txt ee))
  19818. (goto-char (point-at-eol))))
  19819. (nreverse ee)))
  19820. (defun org-agenda-get-deadlines ()
  19821. "Return the deadline information for agenda display."
  19822. (let* ((props (list 'mouse-face 'highlight
  19823. 'org-not-done-regexp org-not-done-regexp
  19824. 'org-todo-regexp org-todo-regexp
  19825. 'keymap org-agenda-keymap
  19826. 'help-echo
  19827. (format "mouse-2 or RET jump to org file %s"
  19828. (abbreviate-file-name buffer-file-name))))
  19829. (regexp org-deadline-time-regexp)
  19830. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19831. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19832. d2 diff dfrac wdays pos pos1 category tags
  19833. ee txt head face s upcomingp donep timestr)
  19834. (goto-char (point-min))
  19835. (while (re-search-forward regexp nil t)
  19836. (catch :skip
  19837. (org-agenda-skip)
  19838. (setq s (match-string 1)
  19839. pos (1- (match-beginning 1))
  19840. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19841. diff (- d2 d1)
  19842. wdays (org-get-wdays s)
  19843. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  19844. upcomingp (and todayp (> diff 0)))
  19845. ;; When to show a deadline in the calendar:
  19846. ;; If the expiration is within wdays warning time.
  19847. ;; Past-due deadlines are only shown on the current date
  19848. (if (or (and (<= diff wdays)
  19849. (and todayp (not org-agenda-only-exact-dates)))
  19850. (= diff 0))
  19851. (save-excursion
  19852. (setq category (org-get-category))
  19853. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19854. (progn
  19855. (goto-char (match-end 0))
  19856. (setq pos1 (match-beginning 0))
  19857. (setq tags (org-get-tags-at pos1))
  19858. (setq head (buffer-substring-no-properties
  19859. (point)
  19860. (progn (skip-chars-forward "^\r\n")
  19861. (point))))
  19862. (setq donep (string-match org-looking-at-done-regexp head))
  19863. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19864. (setq timestr
  19865. (concat (substring s (match-beginning 1)) " "))
  19866. (setq timestr 'time))
  19867. (if (and donep
  19868. (or org-agenda-skip-deadline-if-done
  19869. (not (= diff 0))))
  19870. (setq txt nil)
  19871. (setq txt (org-format-agenda-item
  19872. (if (= diff 0)
  19873. (car org-agenda-deadline-leaders)
  19874. (format (nth 1 org-agenda-deadline-leaders)
  19875. diff))
  19876. head category tags timestr))))
  19877. (setq txt org-agenda-no-heading-message))
  19878. (when txt
  19879. (setq face (org-agenda-deadline-face dfrac))
  19880. (org-add-props txt props
  19881. 'org-marker (org-agenda-new-marker pos)
  19882. 'org-hd-marker (org-agenda-new-marker pos1)
  19883. 'priority (+ (- diff)
  19884. (org-get-priority txt))
  19885. 'org-category category
  19886. 'type (if upcomingp "upcoming-deadline" "deadline")
  19887. 'date (if upcomingp date d2)
  19888. 'face (if donep 'org-done face)
  19889. 'undone-face face 'done-face 'org-done)
  19890. (push txt ee))))))
  19891. (nreverse ee)))
  19892. (defun org-agenda-deadline-face (fraction)
  19893. "Return the face to displaying a deadline item.
  19894. FRACTION is what fraction of the head-warning time has passed."
  19895. (let ((faces org-agenda-deadline-faces) f)
  19896. (catch 'exit
  19897. (while (setq f (pop faces))
  19898. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19899. (defun org-agenda-get-scheduled ()
  19900. "Return the scheduled information for agenda display."
  19901. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19902. 'org-todo-regexp org-todo-regexp
  19903. 'done-face 'org-done
  19904. 'mouse-face 'highlight
  19905. 'keymap org-agenda-keymap
  19906. 'help-echo
  19907. (format "mouse-2 or RET jump to org file %s"
  19908. (abbreviate-file-name buffer-file-name))))
  19909. (regexp org-scheduled-time-regexp)
  19910. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19911. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19912. d2 diff pos pos1 category tags
  19913. ee txt head pastschedp donep face timestr s)
  19914. (goto-char (point-min))
  19915. (while (re-search-forward regexp nil t)
  19916. (catch :skip
  19917. (org-agenda-skip)
  19918. (setq s (match-string 1)
  19919. pos (1- (match-beginning 1))
  19920. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19921. ;;; is this right?
  19922. ;;; do we need to do this for deadleine too????
  19923. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  19924. diff (- d2 d1))
  19925. (setq pastschedp (and todayp (< diff 0)))
  19926. ;; When to show a scheduled item in the calendar:
  19927. ;; If it is on or past the date.
  19928. (if (or (and (< diff 0)
  19929. (and todayp (not org-agenda-only-exact-dates)))
  19930. (= diff 0))
  19931. (save-excursion
  19932. (setq category (org-get-category))
  19933. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19934. (progn
  19935. (goto-char (match-end 0))
  19936. (setq pos1 (match-beginning 0))
  19937. (setq tags (org-get-tags-at))
  19938. (setq head (buffer-substring-no-properties
  19939. (point)
  19940. (progn (skip-chars-forward "^\r\n") (point))))
  19941. (setq donep (string-match org-looking-at-done-regexp head))
  19942. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19943. (setq timestr
  19944. (concat (substring s (match-beginning 1)) " "))
  19945. (setq timestr 'time))
  19946. (if (and donep
  19947. (or org-agenda-skip-scheduled-if-done
  19948. (not (= diff 0))))
  19949. (setq txt nil)
  19950. (setq txt (org-format-agenda-item
  19951. (if (= diff 0)
  19952. (car org-agenda-scheduled-leaders)
  19953. (format (nth 1 org-agenda-scheduled-leaders)
  19954. (- 1 diff)))
  19955. head category tags timestr))))
  19956. (setq txt org-agenda-no-heading-message))
  19957. (when txt
  19958. (setq face (if pastschedp
  19959. 'org-scheduled-previously
  19960. 'org-scheduled-today))
  19961. (org-add-props txt props
  19962. 'undone-face face
  19963. 'face (if donep 'org-done face)
  19964. 'org-marker (org-agenda-new-marker pos)
  19965. 'org-hd-marker (org-agenda-new-marker pos1)
  19966. 'type (if pastschedp "past-scheduled" "scheduled")
  19967. 'date (if pastschedp d2 date)
  19968. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19969. 'org-category category)
  19970. (push txt ee))))))
  19971. (nreverse ee)))
  19972. (defun org-agenda-get-blocks ()
  19973. "Return the date-range information for agenda display."
  19974. (let* ((props (list 'face nil
  19975. 'org-not-done-regexp org-not-done-regexp
  19976. 'org-todo-regexp org-todo-regexp
  19977. 'mouse-face 'highlight
  19978. 'keymap org-agenda-keymap
  19979. 'help-echo
  19980. (format "mouse-2 or RET jump to org file %s"
  19981. (abbreviate-file-name buffer-file-name))))
  19982. (regexp org-tr-regexp)
  19983. (d0 (calendar-absolute-from-gregorian date))
  19984. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19985. donep head)
  19986. (goto-char (point-min))
  19987. (while (re-search-forward regexp nil t)
  19988. (catch :skip
  19989. (org-agenda-skip)
  19990. (setq pos (point))
  19991. (setq timestr (match-string 0)
  19992. s1 (match-string 1)
  19993. s2 (match-string 2)
  19994. d1 (time-to-days (org-time-string-to-time s1))
  19995. d2 (time-to-days (org-time-string-to-time s2)))
  19996. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19997. ;; Only allow days between the limits, because the normal
  19998. ;; date stamps will catch the limits.
  19999. (save-excursion
  20000. (setq marker (org-agenda-new-marker (point)))
  20001. (setq category (org-get-category))
  20002. (if (re-search-backward "^\\*+ " nil t)
  20003. (progn
  20004. (goto-char (match-beginning 0))
  20005. (setq hdmarker (org-agenda-new-marker (point)))
  20006. (setq tags (org-get-tags-at))
  20007. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20008. (setq head (match-string 1))
  20009. (and org-agenda-skip-timestamp-if-done
  20010. (org-entry-is-done-p)
  20011. (throw :skip t))
  20012. (setq txt (org-format-agenda-item
  20013. (format (if (= d1 d2) "" "(%d/%d): ")
  20014. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20015. head category tags
  20016. (if (= d0 d1) timestr))))
  20017. (setq txt org-agenda-no-heading-message))
  20018. (org-add-props txt props
  20019. 'org-marker marker 'org-hd-marker hdmarker
  20020. 'type "block" 'date date
  20021. 'priority (org-get-priority txt) 'org-category category)
  20022. (push txt ee)))
  20023. (goto-char pos)))
  20024. ;; Sort the entries by expiration date.
  20025. (nreverse ee)))
  20026. ;;; Agenda presentation and sorting
  20027. (defconst org-plain-time-of-day-regexp
  20028. (concat
  20029. "\\(\\<[012]?[0-9]"
  20030. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20031. "\\(--?"
  20032. "\\(\\<[012]?[0-9]"
  20033. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20034. "\\)?")
  20035. "Regular expression to match a plain time or time range.
  20036. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20037. groups carry important information:
  20038. 0 the full match
  20039. 1 the first time, range or not
  20040. 8 the second time, if it is a range.")
  20041. (defconst org-plain-time-extension-regexp
  20042. (concat
  20043. "\\(\\<[012]?[0-9]"
  20044. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20045. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20046. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20047. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20048. groups carry important information:
  20049. 0 the full match
  20050. 7 hours of duration
  20051. 9 minutes of duration")
  20052. (defconst org-stamp-time-of-day-regexp
  20053. (concat
  20054. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20055. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20056. "\\(--?"
  20057. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20058. "Regular expression to match a timestamp time or time range.
  20059. After a match, the following groups carry important information:
  20060. 0 the full match
  20061. 1 date plus weekday, for backreferencing to make sure both times on same day
  20062. 2 the first time, range or not
  20063. 4 the second time, if it is a range.")
  20064. (defvar org-prefix-has-time nil
  20065. "A flag, set by `org-compile-prefix-format'.
  20066. The flag is set if the currently compiled format contains a `%t'.")
  20067. (defvar org-prefix-has-tag nil
  20068. "A flag, set by `org-compile-prefix-format'.
  20069. The flag is set if the currently compiled format contains a `%T'.")
  20070. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20071. noprefix remove-re)
  20072. "Format TXT to be inserted into the agenda buffer.
  20073. In particular, it adds the prefix and corresponding text properties. EXTRA
  20074. must be a string and replaces the `%s' specifier in the prefix format.
  20075. CATEGORY (string, symbol or nil) may be used to overrule the default
  20076. category taken from local variable or file name. It will replace the `%c'
  20077. specifier in the format. DOTIME, when non-nil, indicates that a
  20078. time-of-day should be extracted from TXT for sorting of this entry, and for
  20079. the `%t' specifier in the format. When DOTIME is a string, this string is
  20080. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20081. only the correctly processes TXT should be returned - this is used by
  20082. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20083. Any match of REMOVE-RE will be removed from TXT."
  20084. (save-match-data
  20085. ;; Diary entries sometimes have extra whitespace at the beginning
  20086. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20087. (let* ((category (or category
  20088. org-category
  20089. (if buffer-file-name
  20090. (file-name-sans-extension
  20091. (file-name-nondirectory buffer-file-name))
  20092. "")))
  20093. (tag (if tags (nth (1- (length tags)) tags) ""))
  20094. time ; time and tag are needed for the eval of the prefix format
  20095. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20096. (time-of-day (and dotime (org-get-time-of-day ts)))
  20097. stamp plain s0 s1 s2 rtn srp)
  20098. (when (and dotime time-of-day org-prefix-has-time)
  20099. ;; Extract starting and ending time and move them to prefix
  20100. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20101. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20102. (setq s0 (match-string 0 ts)
  20103. srp (and stamp (match-end 3))
  20104. s1 (match-string (if plain 1 2) ts)
  20105. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20106. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20107. ;; them, we might want to remove them there to avoid duplication.
  20108. ;; The user can turn this off with a variable.
  20109. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20110. (string-match (concat (regexp-quote s0) " *") txt)
  20111. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20112. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20113. (= (match-beginning 0) 0)
  20114. t))
  20115. (setq txt (replace-match "" nil nil txt))))
  20116. ;; Normalize the time(s) to 24 hour
  20117. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20118. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20119. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20120. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20121. (let ((m (+ (string-to-number (match-string 2 s1))
  20122. (* 60 (string-to-number (match-string 1 s1)))
  20123. org-agenda-default-appointment-duration))
  20124. h)
  20125. (setq h (/ m 60) m (- m (* h 60)))
  20126. (setq s2 (format "%02d:%02d" h m))))
  20127. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20128. txt)
  20129. ;; Tags are in the string
  20130. (if (or (eq org-agenda-remove-tags t)
  20131. (and org-agenda-remove-tags
  20132. org-prefix-has-tag))
  20133. (setq txt (replace-match "" t t txt))
  20134. (setq txt (replace-match
  20135. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20136. (match-string 2 txt))
  20137. t t txt))))
  20138. (when remove-re
  20139. (while (string-match remove-re txt)
  20140. (setq txt (replace-match "" t t txt))))
  20141. ;; Create the final string
  20142. (if noprefix
  20143. (setq rtn txt)
  20144. ;; Prepare the variables needed in the eval of the compiled format
  20145. (setq time (cond (s2 (concat s1 "-" s2))
  20146. (s1 (concat s1 "......"))
  20147. (t ""))
  20148. extra (or extra "")
  20149. category (if (symbolp category) (symbol-name category) category))
  20150. ;; Evaluate the compiled format
  20151. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20152. ;; And finally add the text properties
  20153. (org-add-props rtn nil
  20154. 'org-category (downcase category) 'tags tags
  20155. 'org-highest-priority org-highest-priority
  20156. 'org-lowest-priority org-lowest-priority
  20157. 'prefix-length (- (length rtn) (length txt))
  20158. 'time-of-day time-of-day
  20159. 'txt txt
  20160. 'time time
  20161. 'extra extra
  20162. 'dotime dotime))))
  20163. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20164. (defvar org-agenda-sorting-strategy-selected nil)
  20165. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20166. (catch 'exit
  20167. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20168. ((and todayp (member 'today (car org-agenda-time-grid))))
  20169. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20170. ((member 'weekly (car org-agenda-time-grid)))
  20171. (t (throw 'exit list)))
  20172. (let* ((have (delq nil (mapcar
  20173. (lambda (x) (get-text-property 1 'time-of-day x))
  20174. list)))
  20175. (string (nth 1 org-agenda-time-grid))
  20176. (gridtimes (nth 2 org-agenda-time-grid))
  20177. (req (car org-agenda-time-grid))
  20178. (remove (member 'remove-match req))
  20179. new time)
  20180. (if (and (member 'require-timed req) (not have))
  20181. ;; don't show empty grid
  20182. (throw 'exit list))
  20183. (while (setq time (pop gridtimes))
  20184. (unless (and remove (member time have))
  20185. (setq time (int-to-string time))
  20186. (push (org-format-agenda-item
  20187. nil string "" nil
  20188. (concat (substring time 0 -2) ":" (substring time -2)))
  20189. new)
  20190. (put-text-property
  20191. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20192. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20193. (append new list)
  20194. (append list new)))))
  20195. (defun org-compile-prefix-format (key)
  20196. "Compile the prefix format into a Lisp form that can be evaluated.
  20197. The resulting form is returned and stored in the variable
  20198. `org-prefix-format-compiled'."
  20199. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20200. (let ((s (cond
  20201. ((stringp org-agenda-prefix-format)
  20202. org-agenda-prefix-format)
  20203. ((assq key org-agenda-prefix-format)
  20204. (cdr (assq key org-agenda-prefix-format)))
  20205. (t " %-12:c%?-12t% s")))
  20206. (start 0)
  20207. varform vars var e c f opt)
  20208. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20209. s start)
  20210. (setq var (cdr (assoc (match-string 4 s)
  20211. '(("c" . category) ("t" . time) ("s" . extra)
  20212. ("T" . tag))))
  20213. c (or (match-string 3 s) "")
  20214. opt (match-beginning 1)
  20215. start (1+ (match-beginning 0)))
  20216. (if (equal var 'time) (setq org-prefix-has-time t))
  20217. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20218. (setq f (concat "%" (match-string 2 s) "s"))
  20219. (if opt
  20220. (setq varform
  20221. `(if (equal "" ,var)
  20222. ""
  20223. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20224. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20225. (setq s (replace-match "%s" t nil s))
  20226. (push varform vars))
  20227. (setq vars (nreverse vars))
  20228. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20229. (defun org-set-sorting-strategy (key)
  20230. (if (symbolp (car org-agenda-sorting-strategy))
  20231. ;; the old format
  20232. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20233. (setq org-agenda-sorting-strategy-selected
  20234. (or (cdr (assq key org-agenda-sorting-strategy))
  20235. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20236. '(time-up category-keep priority-down)))))
  20237. (defun org-get-time-of-day (s &optional string mod24)
  20238. "Check string S for a time of day.
  20239. If found, return it as a military time number between 0 and 2400.
  20240. If not found, return nil.
  20241. The optional STRING argument forces conversion into a 5 character wide string
  20242. HH:MM."
  20243. (save-match-data
  20244. (when
  20245. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20246. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20247. (let* ((h (string-to-number (match-string 1 s)))
  20248. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20249. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20250. (am-p (equal ampm "am"))
  20251. (h1 (cond ((not ampm) h)
  20252. ((= h 12) (if am-p 0 12))
  20253. (t (+ h (if am-p 0 12)))))
  20254. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20255. (mod h1 24) h1))
  20256. (t0 (+ (* 100 h2) m))
  20257. (t1 (concat (if (>= h1 24) "+" " ")
  20258. (if (< t0 100) "0" "")
  20259. (if (< t0 10) "0" "")
  20260. (int-to-string t0))))
  20261. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20262. (defun org-finalize-agenda-entries (list &optional nosort)
  20263. "Sort and concatenate the agenda items."
  20264. (setq list (mapcar 'org-agenda-highlight-todo list))
  20265. (if nosort
  20266. list
  20267. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20268. (defun org-agenda-highlight-todo (x)
  20269. (let (re pl)
  20270. (if (eq x 'line)
  20271. (save-excursion
  20272. (beginning-of-line 1)
  20273. (setq re (get-text-property (point) 'org-todo-regexp))
  20274. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20275. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20276. (add-text-properties (match-beginning 0) (match-end 0)
  20277. (list 'face (org-get-todo-face 0)))
  20278. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20279. (delete-region (match-beginning 1) (1- (match-end 0)))
  20280. (goto-char (match-beginning 1))
  20281. (insert (format org-agenda-todo-keyword-format s)))))
  20282. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20283. pl (get-text-property 0 'prefix-length x))
  20284. (when (and re
  20285. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20286. x (or pl 0)) pl))
  20287. (add-text-properties
  20288. (or (match-end 1) (match-end 0)) (match-end 0)
  20289. (list 'face (org-get-todo-face (match-string 2 x)))
  20290. x)
  20291. (setq x (concat (substring x 0 (match-end 1))
  20292. (format org-agenda-todo-keyword-format
  20293. (match-string 2 x))
  20294. " "
  20295. (substring x (match-end 3)))))
  20296. x)))
  20297. (defsubst org-cmp-priority (a b)
  20298. "Compare the priorities of string A and B."
  20299. (let ((pa (or (get-text-property 1 'priority a) 0))
  20300. (pb (or (get-text-property 1 'priority b) 0)))
  20301. (cond ((> pa pb) +1)
  20302. ((< pa pb) -1)
  20303. (t nil))))
  20304. (defsubst org-cmp-category (a b)
  20305. "Compare the string values of categories of strings A and B."
  20306. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20307. (cb (or (get-text-property 1 'org-category b) "")))
  20308. (cond ((string-lessp ca cb) -1)
  20309. ((string-lessp cb ca) +1)
  20310. (t nil))))
  20311. (defsubst org-cmp-tag (a b)
  20312. "Compare the string values of categories of strings A and B."
  20313. (let ((ta (car (last (get-text-property 1 'tags a))))
  20314. (tb (car (last (get-text-property 1 'tags b)))))
  20315. (cond ((not ta) +1)
  20316. ((not tb) -1)
  20317. ((string-lessp ta tb) -1)
  20318. ((string-lessp tb ta) +1)
  20319. (t nil))))
  20320. (defsubst org-cmp-time (a b)
  20321. "Compare the time-of-day values of strings A and B."
  20322. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20323. (ta (or (get-text-property 1 'time-of-day a) def))
  20324. (tb (or (get-text-property 1 'time-of-day b) def)))
  20325. (cond ((< ta tb) -1)
  20326. ((< tb ta) +1)
  20327. (t nil))))
  20328. (defun org-entries-lessp (a b)
  20329. "Predicate for sorting agenda entries."
  20330. ;; The following variables will be used when the form is evaluated.
  20331. ;; So even though the compiler complains, keep them.
  20332. (let* ((time-up (org-cmp-time a b))
  20333. (time-down (if time-up (- time-up) nil))
  20334. (priority-up (org-cmp-priority a b))
  20335. (priority-down (if priority-up (- priority-up) nil))
  20336. (category-up (org-cmp-category a b))
  20337. (category-down (if category-up (- category-up) nil))
  20338. (category-keep (if category-up +1 nil))
  20339. (tag-up (org-cmp-tag a b))
  20340. (tag-down (if tag-up (- tag-up) nil)))
  20341. (cdr (assoc
  20342. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20343. '((-1 . t) (1 . nil) (nil . nil))))))
  20344. ;;; Agenda restriction lock
  20345. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20346. "Overlay to mark the headline to which arenda commands are restricted.")
  20347. (org-overlay-put org-agenda-restriction-lock-overlay
  20348. 'face 'org-agenda-restriction-lock)
  20349. (org-overlay-put org-agenda-restriction-lock-overlay
  20350. 'help-echo "Agendas are currently limited to this subtree.")
  20351. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20352. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20353. "Overlay marking the agenda restriction line in speedbar.")
  20354. (org-overlay-put org-speedbar-restriction-lock-overlay
  20355. 'face 'org-agenda-restriction-lock)
  20356. (org-overlay-put org-speedbar-restriction-lock-overlay
  20357. 'help-echo "Agendas are currently limited to this item.")
  20358. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20359. (defun org-agenda-set-restriction-lock (&optional type)
  20360. "Set restriction lock for agenda, to current subtree or file.
  20361. Restriction will be the file if TYPE is `file', or if type is the
  20362. universal prefix '(4), or if the cursor is before the first headline
  20363. in the file. Otherwise, restriction will be to the current subtree."
  20364. (interactive "P")
  20365. (and (equal type '(4)) (setq type 'file))
  20366. (setq type (cond
  20367. (type type)
  20368. ((org-at-heading-p) 'subtree)
  20369. ((condition-case nil (org-back-to-heading t) (error nil))
  20370. 'subtree)
  20371. (t 'file)))
  20372. (if (eq type 'subtree)
  20373. (progn
  20374. (setq org-agenda-restrict t)
  20375. (setq org-agenda-overriding-restriction 'subtree)
  20376. (put 'org-agenda-files 'org-restrict
  20377. (list (buffer-file-name (buffer-base-buffer))))
  20378. (org-back-to-heading t)
  20379. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20380. (move-marker org-agenda-restrict-begin (point))
  20381. (move-marker org-agenda-restrict-end
  20382. (save-excursion (org-end-of-subtree t)))
  20383. (message "Locking agenda restriction to subtree"))
  20384. (put 'org-agenda-files 'org-restrict
  20385. (list (buffer-file-name (buffer-base-buffer))))
  20386. (setq org-agenda-restrict nil)
  20387. (setq org-agenda-overriding-restriction 'file)
  20388. (move-marker org-agenda-restrict-begin nil)
  20389. (move-marker org-agenda-restrict-end nil)
  20390. (message "Locking agenda restriction to file"))
  20391. (setq current-prefix-arg nil)
  20392. (org-agenda-maybe-redo))
  20393. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20394. "Remove the agenda restriction lock."
  20395. (interactive "P")
  20396. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20397. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20398. (setq org-agenda-overriding-restriction nil)
  20399. (setq org-agenda-restrict nil)
  20400. (put 'org-agenda-files 'org-restrict nil)
  20401. (move-marker org-agenda-restrict-begin nil)
  20402. (move-marker org-agenda-restrict-end nil)
  20403. (setq current-prefix-arg nil)
  20404. (message "Agenda restriction lock removed")
  20405. (or noupdate (org-agenda-maybe-redo)))
  20406. (defun org-agenda-maybe-redo ()
  20407. "If there is any window showing the agenda view, update it."
  20408. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20409. (w0 (selected-window)))
  20410. (when w
  20411. (select-window w)
  20412. (org-agenda-redo)
  20413. (select-window w0)
  20414. (if org-agenda-overriding-restriction
  20415. (message "Agenda view shifted to new %s restriction"
  20416. org-agenda-overriding-restriction)
  20417. (message "Agenda restriction lock removed")))))
  20418. ;;; Agenda commands
  20419. (defun org-agenda-check-type (error &rest types)
  20420. "Check if agenda buffer is of allowed type.
  20421. If ERROR is non-nil, throw an error, otherwise just return nil."
  20422. (if (memq org-agenda-type types)
  20423. t
  20424. (if error
  20425. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20426. nil)))
  20427. (defun org-agenda-quit ()
  20428. "Exit agenda by removing the window or the buffer."
  20429. (interactive)
  20430. (let ((buf (current-buffer)))
  20431. (if (not (one-window-p)) (delete-window))
  20432. (kill-buffer buf)
  20433. (org-agenda-reset-markers)
  20434. (org-columns-remove-overlays))
  20435. ;; Maybe restore the pre-agenda window configuration.
  20436. (and org-agenda-restore-windows-after-quit
  20437. (not (eq org-agenda-window-setup 'other-frame))
  20438. org-pre-agenda-window-conf
  20439. (set-window-configuration org-pre-agenda-window-conf)))
  20440. (defun org-agenda-exit ()
  20441. "Exit agenda by removing the window or the buffer.
  20442. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20443. Org-mode buffers visited directly by the user will not be touched."
  20444. (interactive)
  20445. (org-release-buffers org-agenda-new-buffers)
  20446. (setq org-agenda-new-buffers nil)
  20447. (org-agenda-quit))
  20448. (defun org-agenda-execute (arg)
  20449. "Execute another agenda command, keeping same window.\\<global-map>
  20450. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20451. (interactive "P")
  20452. (let ((org-agenda-window-setup 'current-window))
  20453. (org-agenda arg)))
  20454. (defun org-save-all-org-buffers ()
  20455. "Save all Org-mode buffers without user confirmation."
  20456. (interactive)
  20457. (message "Saving all Org-mode buffers...")
  20458. (save-some-buffers t 'org-mode-p)
  20459. (message "Saving all Org-mode buffers... done"))
  20460. (defun org-agenda-redo ()
  20461. "Rebuild Agenda.
  20462. When this is the global TODO list, a prefix argument will be interpreted."
  20463. (interactive)
  20464. (let* ((org-agenda-keep-modes t)
  20465. (line (org-current-line))
  20466. (window-line (- line (org-current-line (window-start))))
  20467. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20468. (message "Rebuilding agenda buffer...")
  20469. (org-let lprops '(eval org-agenda-redo-command))
  20470. (setq org-agenda-undo-list nil
  20471. org-agenda-pending-undo-list nil)
  20472. (message "Rebuilding agenda buffer...done")
  20473. (goto-line line)
  20474. (recenter window-line)))
  20475. (defun org-agenda-goto-date (date)
  20476. "Jump to DATE in agenda."
  20477. (interactive (list (org-read-date)))
  20478. (org-agenda-list nil date))
  20479. (defun org-agenda-goto-today ()
  20480. "Go to today."
  20481. (interactive)
  20482. (org-agenda-check-type t 'timeline 'agenda)
  20483. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20484. (cond
  20485. (tdpos (goto-char tdpos))
  20486. ((eq org-agenda-type 'agenda)
  20487. (let* ((sd (time-to-days
  20488. (time-subtract (current-time)
  20489. (list 0 (* 3600 org-extend-today-until) 0))))
  20490. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20491. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20492. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20493. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20494. (org-agenda-redo)
  20495. (org-agenda-find-same-or-today-or-agenda)))
  20496. (t (error "Cannot find today")))))
  20497. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20498. (goto-char
  20499. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20500. (text-property-any (point-min) (point-max) 'org-today t)
  20501. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20502. (point-min))))
  20503. (defun org-agenda-later (arg)
  20504. "Go forward in time by thee current span.
  20505. With prefix ARG, go forward that many times the current span."
  20506. (interactive "p")
  20507. (org-agenda-check-type t 'agenda)
  20508. (let* ((span org-agenda-span)
  20509. (sd org-starting-day)
  20510. (greg (calendar-gregorian-from-absolute sd))
  20511. (cnt (get-text-property (point) 'org-day-cnt))
  20512. greg2 nd)
  20513. (cond
  20514. ((eq span 'day)
  20515. (setq sd (+ arg sd) nd 1))
  20516. ((eq span 'week)
  20517. (setq sd (+ (* 7 arg) sd) nd 7))
  20518. ((eq span 'month)
  20519. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20520. sd (calendar-absolute-from-gregorian greg2))
  20521. (setcar greg2 (1+ (car greg2)))
  20522. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20523. ((eq span 'year)
  20524. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20525. sd (calendar-absolute-from-gregorian greg2))
  20526. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20527. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20528. (let ((org-agenda-overriding-arguments
  20529. (list (car org-agenda-last-arguments) sd nd t)))
  20530. (org-agenda-redo)
  20531. (org-agenda-find-same-or-today-or-agenda cnt))))
  20532. (defun org-agenda-earlier (arg)
  20533. "Go backward in time by the current span.
  20534. With prefix ARG, go backward that many times the current span."
  20535. (interactive "p")
  20536. (org-agenda-later (- arg)))
  20537. (defun org-agenda-day-view ()
  20538. "Switch to daily view for agenda."
  20539. (interactive)
  20540. (setq org-agenda-ndays 1)
  20541. (org-agenda-change-time-span 'day))
  20542. (defun org-agenda-week-view ()
  20543. "Switch to daily view for agenda."
  20544. (interactive)
  20545. (setq org-agenda-ndays 7)
  20546. (org-agenda-change-time-span 'week))
  20547. (defun org-agenda-month-view ()
  20548. "Switch to daily view for agenda."
  20549. (interactive)
  20550. (org-agenda-change-time-span 'month))
  20551. (defun org-agenda-year-view ()
  20552. "Switch to daily view for agenda."
  20553. (interactive)
  20554. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20555. (org-agenda-change-time-span 'year)
  20556. (error "Abort")))
  20557. (defun org-agenda-change-time-span (span)
  20558. "Change the agenda view to SPAN.
  20559. SPAN may be `day', `week', `month', `year'."
  20560. (org-agenda-check-type t 'agenda)
  20561. (if (equal org-agenda-span span)
  20562. (error "Viewing span is already \"%s\"" span))
  20563. (let* ((sd (or (get-text-property (point) 'day)
  20564. org-starting-day))
  20565. (computed (org-agenda-compute-time-span sd span))
  20566. (org-agenda-overriding-arguments
  20567. (list (car org-agenda-last-arguments)
  20568. (car computed) (cdr computed) t)))
  20569. (org-agenda-redo)
  20570. (org-agenda-find-same-or-today-or-agenda))
  20571. (org-agenda-set-mode-name)
  20572. (message "Switched to %s view" span))
  20573. (defun org-agenda-compute-time-span (sd span)
  20574. "Compute starting date and number of days for agenda.
  20575. SPAN may be `day', `week', `month', `year'. The return value
  20576. is a cons cell with the starting date and the number of days,
  20577. so that the date SD will be in that range."
  20578. (let* ((greg (calendar-gregorian-from-absolute sd))
  20579. nd)
  20580. (cond
  20581. ((eq span 'day)
  20582. (setq nd 1))
  20583. ((eq span 'week)
  20584. (let* ((nt (calendar-day-of-week
  20585. (calendar-gregorian-from-absolute sd)))
  20586. (d (if org-agenda-start-on-weekday
  20587. (- nt org-agenda-start-on-weekday)
  20588. 0)))
  20589. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20590. (setq nd 7)))
  20591. ((eq span 'month)
  20592. (setq sd (calendar-absolute-from-gregorian
  20593. (list (car greg) 1 (nth 2 greg)))
  20594. nd (- (calendar-absolute-from-gregorian
  20595. (list (1+ (car greg)) 1 (nth 2 greg)))
  20596. sd)))
  20597. ((eq span 'year)
  20598. (setq sd (calendar-absolute-from-gregorian
  20599. (list 1 1 (nth 2 greg)))
  20600. nd (- (calendar-absolute-from-gregorian
  20601. (list 1 1 (1+ (nth 2 greg))))
  20602. sd))))
  20603. (cons sd nd)))
  20604. ;; FIXME: does not work if user makes date format that starts with a blank
  20605. (defun org-agenda-next-date-line (&optional arg)
  20606. "Jump to the next line indicating a date in agenda buffer."
  20607. (interactive "p")
  20608. (org-agenda-check-type t 'agenda 'timeline)
  20609. (beginning-of-line 1)
  20610. (if (looking-at "^\\S-") (forward-char 1))
  20611. (if (not (re-search-forward "^\\S-" nil t arg))
  20612. (progn
  20613. (backward-char 1)
  20614. (error "No next date after this line in this buffer")))
  20615. (goto-char (match-beginning 0)))
  20616. (defun org-agenda-previous-date-line (&optional arg)
  20617. "Jump to the previous line indicating a date in agenda buffer."
  20618. (interactive "p")
  20619. (org-agenda-check-type t 'agenda 'timeline)
  20620. (beginning-of-line 1)
  20621. (if (not (re-search-backward "^\\S-" nil t arg))
  20622. (error "No previous date before this line in this buffer")))
  20623. ;; Initialize the highlight
  20624. (defvar org-hl (org-make-overlay 1 1))
  20625. (org-overlay-put org-hl 'face 'highlight)
  20626. (defun org-highlight (begin end &optional buffer)
  20627. "Highlight a region with overlay."
  20628. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20629. org-hl begin end (or buffer (current-buffer))))
  20630. (defun org-unhighlight ()
  20631. "Detach overlay INDEX."
  20632. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20633. ;; FIXME this is currently not used.
  20634. (defun org-highlight-until-next-command (beg end &optional buffer)
  20635. (org-highlight beg end buffer)
  20636. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20637. (defun org-unhighlight-once ()
  20638. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20639. (org-unhighlight))
  20640. (defun org-agenda-follow-mode ()
  20641. "Toggle follow mode in an agenda buffer."
  20642. (interactive)
  20643. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20644. (org-agenda-set-mode-name)
  20645. (message "Follow mode is %s"
  20646. (if org-agenda-follow-mode "on" "off")))
  20647. (defun org-agenda-log-mode ()
  20648. "Toggle log mode in an agenda buffer."
  20649. (interactive)
  20650. (org-agenda-check-type t 'agenda 'timeline)
  20651. (setq org-agenda-show-log (not org-agenda-show-log))
  20652. (org-agenda-set-mode-name)
  20653. (org-agenda-redo)
  20654. (message "Log mode is %s"
  20655. (if org-agenda-show-log "on" "off")))
  20656. (defun org-agenda-toggle-diary ()
  20657. "Toggle diary inclusion in an agenda buffer."
  20658. (interactive)
  20659. (org-agenda-check-type t 'agenda)
  20660. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20661. (org-agenda-redo)
  20662. (org-agenda-set-mode-name)
  20663. (message "Diary inclusion turned %s"
  20664. (if org-agenda-include-diary "on" "off")))
  20665. (defun org-agenda-toggle-time-grid ()
  20666. "Toggle time grid in an agenda buffer."
  20667. (interactive)
  20668. (org-agenda-check-type t 'agenda)
  20669. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20670. (org-agenda-redo)
  20671. (org-agenda-set-mode-name)
  20672. (message "Time-grid turned %s"
  20673. (if org-agenda-use-time-grid "on" "off")))
  20674. (defun org-agenda-set-mode-name ()
  20675. "Set the mode name to indicate all the small mode settings."
  20676. (setq mode-name
  20677. (concat "Org-Agenda"
  20678. (if (equal org-agenda-ndays 1) " Day" "")
  20679. (if (equal org-agenda-ndays 7) " Week" "")
  20680. (if org-agenda-follow-mode " Follow" "")
  20681. (if org-agenda-include-diary " Diary" "")
  20682. (if org-agenda-use-time-grid " Grid" "")
  20683. (if org-agenda-show-log " Log" "")))
  20684. (force-mode-line-update))
  20685. (defun org-agenda-post-command-hook ()
  20686. (and (eolp) (not (bolp)) (backward-char 1))
  20687. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20688. (if (and org-agenda-follow-mode
  20689. (get-text-property (point) 'org-marker))
  20690. (org-agenda-show)))
  20691. (defun org-agenda-show-priority ()
  20692. "Show the priority of the current item.
  20693. This priority is composed of the main priority given with the [#A] cookies,
  20694. and by additional input from the age of a schedules or deadline entry."
  20695. (interactive)
  20696. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20697. (message "Priority is %d" (if pri pri -1000))))
  20698. (defun org-agenda-show-tags ()
  20699. "Show the tags applicable to the current item."
  20700. (interactive)
  20701. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20702. (if tags
  20703. (message "Tags are :%s:"
  20704. (org-no-properties (mapconcat 'identity tags ":")))
  20705. (message "No tags associated with this line"))))
  20706. (defun org-agenda-goto (&optional highlight)
  20707. "Go to the Org-mode file which contains the item at point."
  20708. (interactive)
  20709. (let* ((marker (or (get-text-property (point) 'org-marker)
  20710. (org-agenda-error)))
  20711. (buffer (marker-buffer marker))
  20712. (pos (marker-position marker)))
  20713. (switch-to-buffer-other-window buffer)
  20714. (widen)
  20715. (goto-char pos)
  20716. (when (org-mode-p)
  20717. (org-show-context 'agenda)
  20718. (save-excursion
  20719. (and (outline-next-heading)
  20720. (org-flag-heading nil)))) ; show the next heading
  20721. (recenter (/ (window-height) 2))
  20722. (run-hooks 'org-agenda-after-show-hook)
  20723. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20724. (defvar org-agenda-after-show-hook nil
  20725. "Normal hook run after an item has been shown from the agenda.
  20726. Point is in the buffer where the item originated.")
  20727. (defun org-agenda-kill ()
  20728. "Kill the entry or subtree belonging to the current agenda entry."
  20729. (interactive)
  20730. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20731. (let* ((marker (or (get-text-property (point) 'org-marker)
  20732. (org-agenda-error)))
  20733. (buffer (marker-buffer marker))
  20734. (pos (marker-position marker))
  20735. (type (get-text-property (point) 'type))
  20736. dbeg dend (n 0) conf)
  20737. (org-with-remote-undo buffer
  20738. (with-current-buffer buffer
  20739. (save-excursion
  20740. (goto-char pos)
  20741. (if (and (org-mode-p) (not (member type '("sexp"))))
  20742. (setq dbeg (progn (org-back-to-heading t) (point))
  20743. dend (org-end-of-subtree t t))
  20744. (setq dbeg (point-at-bol)
  20745. dend (min (point-max) (1+ (point-at-eol)))))
  20746. (goto-char dbeg)
  20747. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20748. (setq conf (or (eq t org-agenda-confirm-kill)
  20749. (and (numberp org-agenda-confirm-kill)
  20750. (> n org-agenda-confirm-kill))))
  20751. (and conf
  20752. (not (y-or-n-p
  20753. (format "Delete entry with %d lines in buffer \"%s\"? "
  20754. n (buffer-name buffer))))
  20755. (error "Abort"))
  20756. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20757. (with-current-buffer buffer (delete-region dbeg dend))
  20758. (message "Agenda item and source killed"))))
  20759. (defun org-agenda-archive ()
  20760. "Kill the entry or subtree belonging to the current agenda entry."
  20761. (interactive)
  20762. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20763. (let* ((marker (or (get-text-property (point) 'org-marker)
  20764. (org-agenda-error)))
  20765. (buffer (marker-buffer marker))
  20766. (pos (marker-position marker)))
  20767. (org-with-remote-undo buffer
  20768. (with-current-buffer buffer
  20769. (if (org-mode-p)
  20770. (save-excursion
  20771. (goto-char pos)
  20772. (org-remove-subtree-entries-from-agenda)
  20773. (org-back-to-heading t)
  20774. (org-archive-subtree))
  20775. (error "Archiving works only in Org-mode files"))))))
  20776. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20777. "Remove all lines in the agenda that correspond to a given subtree.
  20778. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20779. If this information is not given, the function uses the tree at point."
  20780. (let ((buf (or buf (current-buffer))) m p)
  20781. (save-excursion
  20782. (unless (and beg end)
  20783. (org-back-to-heading t)
  20784. (setq beg (point))
  20785. (org-end-of-subtree t)
  20786. (setq end (point)))
  20787. (set-buffer (get-buffer org-agenda-buffer-name))
  20788. (save-excursion
  20789. (goto-char (point-max))
  20790. (beginning-of-line 1)
  20791. (while (not (bobp))
  20792. (when (and (setq m (get-text-property (point) 'org-marker))
  20793. (equal buf (marker-buffer m))
  20794. (setq p (marker-position m))
  20795. (>= p beg)
  20796. (<= p end))
  20797. (let ((inhibit-read-only t))
  20798. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20799. (beginning-of-line 0))))))
  20800. (defun org-agenda-open-link ()
  20801. "Follow the link in the current line, if any."
  20802. (interactive)
  20803. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20804. (save-excursion
  20805. (save-restriction
  20806. (narrow-to-region (point-at-bol) (point-at-eol))
  20807. (org-open-at-point))))
  20808. (defun org-agenda-copy-local-variable (var)
  20809. "Get a variable from a referenced buffer and install it here."
  20810. (let ((m (get-text-property (point) 'org-marker)))
  20811. (when (and m (buffer-live-p (marker-buffer m)))
  20812. (org-set-local var (with-current-buffer (marker-buffer m)
  20813. (symbol-value var))))))
  20814. (defun org-agenda-switch-to (&optional delete-other-windows)
  20815. "Go to the Org-mode file which contains the item at point."
  20816. (interactive)
  20817. (let* ((marker (or (get-text-property (point) 'org-marker)
  20818. (org-agenda-error)))
  20819. (buffer (marker-buffer marker))
  20820. (pos (marker-position marker)))
  20821. (switch-to-buffer buffer)
  20822. (and delete-other-windows (delete-other-windows))
  20823. (widen)
  20824. (goto-char pos)
  20825. (when (org-mode-p)
  20826. (org-show-context 'agenda)
  20827. (save-excursion
  20828. (and (outline-next-heading)
  20829. (org-flag-heading nil)))))) ; show the next heading
  20830. (defun org-agenda-goto-mouse (ev)
  20831. "Go to the Org-mode file which contains the item at the mouse click."
  20832. (interactive "e")
  20833. (mouse-set-point ev)
  20834. (org-agenda-goto))
  20835. (defun org-agenda-show ()
  20836. "Display the Org-mode file which contains the item at point."
  20837. (interactive)
  20838. (let ((win (selected-window)))
  20839. (org-agenda-goto t)
  20840. (select-window win)))
  20841. (defun org-agenda-recenter (arg)
  20842. "Display the Org-mode file which contains the item at point and recenter."
  20843. (interactive "P")
  20844. (let ((win (selected-window)))
  20845. (org-agenda-goto t)
  20846. (recenter arg)
  20847. (select-window win)))
  20848. (defun org-agenda-show-mouse (ev)
  20849. "Display the Org-mode file which contains the item at the mouse click."
  20850. (interactive "e")
  20851. (mouse-set-point ev)
  20852. (org-agenda-show))
  20853. (defun org-agenda-check-no-diary ()
  20854. "Check if the entry is a diary link and abort if yes."
  20855. (if (get-text-property (point) 'org-agenda-diary-link)
  20856. (org-agenda-error)))
  20857. (defun org-agenda-error ()
  20858. (error "Command not allowed in this line"))
  20859. (defun org-agenda-tree-to-indirect-buffer ()
  20860. "Show the subtree corresponding to the current entry in an indirect buffer.
  20861. This calls the command `org-tree-to-indirect-buffer' from the original
  20862. Org-mode buffer.
  20863. With numerical prefix arg ARG, go up to this level and then take that tree.
  20864. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20865. dedicated frame)."
  20866. (interactive)
  20867. (org-agenda-check-no-diary)
  20868. (let* ((marker (or (get-text-property (point) 'org-marker)
  20869. (org-agenda-error)))
  20870. (buffer (marker-buffer marker))
  20871. (pos (marker-position marker)))
  20872. (with-current-buffer buffer
  20873. (save-excursion
  20874. (goto-char pos)
  20875. (call-interactively 'org-tree-to-indirect-buffer)))))
  20876. (defvar org-last-heading-marker (make-marker)
  20877. "Marker pointing to the headline that last changed its TODO state
  20878. by a remote command from the agenda.")
  20879. (defun org-agenda-todo-nextset ()
  20880. "Switch TODO entry to next sequence."
  20881. (interactive)
  20882. (org-agenda-todo 'nextset))
  20883. (defun org-agenda-todo-previousset ()
  20884. "Switch TODO entry to previous sequence."
  20885. (interactive)
  20886. (org-agenda-todo 'previousset))
  20887. (defun org-agenda-todo (&optional arg)
  20888. "Cycle TODO state of line at point, also in Org-mode file.
  20889. This changes the line at point, all other lines in the agenda referring to
  20890. the same tree node, and the headline of the tree node in the Org-mode file."
  20891. (interactive "P")
  20892. (org-agenda-check-no-diary)
  20893. (let* ((col (current-column))
  20894. (marker (or (get-text-property (point) 'org-marker)
  20895. (org-agenda-error)))
  20896. (buffer (marker-buffer marker))
  20897. (pos (marker-position marker))
  20898. (hdmarker (get-text-property (point) 'org-hd-marker))
  20899. (inhibit-read-only t)
  20900. newhead)
  20901. (org-with-remote-undo buffer
  20902. (with-current-buffer buffer
  20903. (widen)
  20904. (goto-char pos)
  20905. (org-show-context 'agenda)
  20906. (save-excursion
  20907. (and (outline-next-heading)
  20908. (org-flag-heading nil))) ; show the next heading
  20909. (org-todo arg)
  20910. (and (bolp) (forward-char 1))
  20911. (setq newhead (org-get-heading))
  20912. (save-excursion
  20913. (org-back-to-heading)
  20914. (move-marker org-last-heading-marker (point))))
  20915. (beginning-of-line 1)
  20916. (save-excursion
  20917. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20918. (move-to-column col))))
  20919. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20920. "Change all lines in the agenda buffer which match HDMARKER.
  20921. The new content of the line will be NEWHEAD (as modified by
  20922. `org-format-agenda-item'). HDMARKER is checked with
  20923. `equal' against all `org-hd-marker' text properties in the file.
  20924. If FIXFACE is non-nil, the face of each item is modified acording to
  20925. the new TODO state."
  20926. (let* ((inhibit-read-only t)
  20927. props m pl undone-face done-face finish new dotime cat tags)
  20928. (save-excursion
  20929. (goto-char (point-max))
  20930. (beginning-of-line 1)
  20931. (while (not finish)
  20932. (setq finish (bobp))
  20933. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20934. (equal m hdmarker))
  20935. (setq props (text-properties-at (point))
  20936. dotime (get-text-property (point) 'dotime)
  20937. cat (get-text-property (point) 'org-category)
  20938. tags (get-text-property (point) 'tags)
  20939. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20940. pl (get-text-property (point) 'prefix-length)
  20941. undone-face (get-text-property (point) 'undone-face)
  20942. done-face (get-text-property (point) 'done-face))
  20943. (move-to-column pl)
  20944. (cond
  20945. ((equal new "")
  20946. (beginning-of-line 1)
  20947. (and (looking-at ".*\n?") (replace-match "")))
  20948. ((looking-at ".*")
  20949. (replace-match new t t)
  20950. (beginning-of-line 1)
  20951. (add-text-properties (point-at-bol) (point-at-eol) props)
  20952. (when fixface
  20953. (add-text-properties
  20954. (point-at-bol) (point-at-eol)
  20955. (list 'face
  20956. (if org-last-todo-state-is-todo
  20957. undone-face done-face))))
  20958. (org-agenda-highlight-todo 'line)
  20959. (beginning-of-line 1))
  20960. (t (error "Line update did not work"))))
  20961. (beginning-of-line 0)))
  20962. (org-finalize-agenda)))
  20963. (defun org-agenda-align-tags (&optional line)
  20964. "Align all tags in agenda items to `org-agenda-tags-column'."
  20965. (let ((inhibit-read-only t) l c)
  20966. (save-excursion
  20967. (goto-char (if line (point-at-bol) (point-min)))
  20968. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20969. (if line (point-at-eol) nil) t)
  20970. (add-text-properties
  20971. (match-beginning 2) (match-end 2)
  20972. (list 'face (list 'org-tag (get-text-property
  20973. (match-beginning 2) 'face))))
  20974. (setq l (- (match-end 2) (match-beginning 2))
  20975. c (if (< org-agenda-tags-column 0)
  20976. (- (abs org-agenda-tags-column) l)
  20977. org-agenda-tags-column))
  20978. (delete-region (match-beginning 1) (match-end 1))
  20979. (goto-char (match-beginning 1))
  20980. (insert (org-add-props
  20981. (make-string (max 1 (- c (current-column))) ?\ )
  20982. (text-properties-at (point))))))))
  20983. (defun org-agenda-priority-up ()
  20984. "Increase the priority of line at point, also in Org-mode file."
  20985. (interactive)
  20986. (org-agenda-priority 'up))
  20987. (defun org-agenda-priority-down ()
  20988. "Decrease the priority of line at point, also in Org-mode file."
  20989. (interactive)
  20990. (org-agenda-priority 'down))
  20991. (defun org-agenda-priority (&optional force-direction)
  20992. "Set the priority of line at point, also in Org-mode file.
  20993. This changes the line at point, all other lines in the agenda referring to
  20994. the same tree node, and the headline of the tree node in the Org-mode file."
  20995. (interactive)
  20996. (org-agenda-check-no-diary)
  20997. (let* ((marker (or (get-text-property (point) 'org-marker)
  20998. (org-agenda-error)))
  20999. (hdmarker (get-text-property (point) 'org-hd-marker))
  21000. (buffer (marker-buffer hdmarker))
  21001. (pos (marker-position hdmarker))
  21002. (inhibit-read-only t)
  21003. newhead)
  21004. (org-with-remote-undo buffer
  21005. (with-current-buffer buffer
  21006. (widen)
  21007. (goto-char pos)
  21008. (org-show-context 'agenda)
  21009. (save-excursion
  21010. (and (outline-next-heading)
  21011. (org-flag-heading nil))) ; show the next heading
  21012. (funcall 'org-priority force-direction)
  21013. (end-of-line 1)
  21014. (setq newhead (org-get-heading)))
  21015. (org-agenda-change-all-lines newhead hdmarker)
  21016. (beginning-of-line 1))))
  21017. (defun org-get-tags-at (&optional pos)
  21018. "Get a list of all headline tags applicable at POS.
  21019. POS defaults to point. If tags are inherited, the list contains
  21020. the targets in the same sequence as the headlines appear, i.e.
  21021. the tags of the current headline come last."
  21022. (interactive)
  21023. (let (tags lastpos)
  21024. (save-excursion
  21025. (save-restriction
  21026. (widen)
  21027. (goto-char (or pos (point)))
  21028. (save-match-data
  21029. (org-back-to-heading t)
  21030. (condition-case nil
  21031. (while (not (equal lastpos (point)))
  21032. (setq lastpos (point))
  21033. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21034. (setq tags (append (org-split-string
  21035. (org-match-string-no-properties 1) ":")
  21036. tags)))
  21037. (or org-use-tag-inheritance (error ""))
  21038. (org-up-heading-all 1))
  21039. (error nil))))
  21040. tags)))
  21041. ;; FIXME: should fix the tags property of the agenda line.
  21042. (defun org-agenda-set-tags ()
  21043. "Set tags for the current headline."
  21044. (interactive)
  21045. (org-agenda-check-no-diary)
  21046. (if (and (org-region-active-p) (interactive-p))
  21047. (call-interactively 'org-change-tag-in-region)
  21048. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21049. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21050. (org-agenda-error)))
  21051. (buffer (marker-buffer hdmarker))
  21052. (pos (marker-position hdmarker))
  21053. (inhibit-read-only t)
  21054. newhead)
  21055. (org-with-remote-undo buffer
  21056. (with-current-buffer buffer
  21057. (widen)
  21058. (goto-char pos)
  21059. (save-excursion
  21060. (org-show-context 'agenda))
  21061. (save-excursion
  21062. (and (outline-next-heading)
  21063. (org-flag-heading nil))) ; show the next heading
  21064. (goto-char pos)
  21065. (call-interactively 'org-set-tags)
  21066. (end-of-line 1)
  21067. (setq newhead (org-get-heading)))
  21068. (org-agenda-change-all-lines newhead hdmarker)
  21069. (beginning-of-line 1)))))
  21070. (defun org-agenda-toggle-archive-tag ()
  21071. "Toggle the archive tag for the current entry."
  21072. (interactive)
  21073. (org-agenda-check-no-diary)
  21074. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21075. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21076. (org-agenda-error)))
  21077. (buffer (marker-buffer hdmarker))
  21078. (pos (marker-position hdmarker))
  21079. (inhibit-read-only t)
  21080. newhead)
  21081. (org-with-remote-undo buffer
  21082. (with-current-buffer buffer
  21083. (widen)
  21084. (goto-char pos)
  21085. (org-show-context 'agenda)
  21086. (save-excursion
  21087. (and (outline-next-heading)
  21088. (org-flag-heading nil))) ; show the next heading
  21089. (call-interactively 'org-toggle-archive-tag)
  21090. (end-of-line 1)
  21091. (setq newhead (org-get-heading)))
  21092. (org-agenda-change-all-lines newhead hdmarker)
  21093. (beginning-of-line 1))))
  21094. (defun org-agenda-date-later (arg &optional what)
  21095. "Change the date of this item to one day later."
  21096. (interactive "p")
  21097. (org-agenda-check-type t 'agenda 'timeline)
  21098. (org-agenda-check-no-diary)
  21099. (let* ((marker (or (get-text-property (point) 'org-marker)
  21100. (org-agenda-error)))
  21101. (buffer (marker-buffer marker))
  21102. (pos (marker-position marker)))
  21103. (org-with-remote-undo buffer
  21104. (with-current-buffer buffer
  21105. (widen)
  21106. (goto-char pos)
  21107. (if (not (org-at-timestamp-p))
  21108. (error "Cannot find time stamp"))
  21109. (org-timestamp-change arg (or what 'day)))
  21110. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21111. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21112. (defun org-agenda-date-earlier (arg &optional what)
  21113. "Change the date of this item to one day earlier."
  21114. (interactive "p")
  21115. (org-agenda-date-later (- arg) what))
  21116. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21117. "Show new date stamp via text properties."
  21118. ;; We use text properties to make this undoable
  21119. (let ((inhibit-read-only t))
  21120. (setq stamp (concat " " prefix " => " stamp))
  21121. (save-excursion
  21122. (goto-char (point-max))
  21123. (while (not (bobp))
  21124. (when (equal marker (get-text-property (point) 'org-marker))
  21125. (move-to-column (- (window-width) (length stamp)) t)
  21126. (if (featurep 'xemacs)
  21127. ;; Use `duplicable' property to trigger undo recording
  21128. (let ((ex (make-extent nil nil))
  21129. (gl (make-glyph stamp)))
  21130. (set-glyph-face gl 'secondary-selection)
  21131. (set-extent-properties
  21132. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21133. (insert-extent ex (1- (point)) (point-at-eol)))
  21134. (add-text-properties
  21135. (1- (point)) (point-at-eol)
  21136. (list 'display (org-add-props stamp nil
  21137. 'face 'secondary-selection))))
  21138. (beginning-of-line 1))
  21139. (beginning-of-line 0)))))
  21140. (defun org-agenda-date-prompt (arg)
  21141. "Change the date of this item. Date is prompted for, with default today.
  21142. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21143. be used to request time specification in the time stamp."
  21144. (interactive "P")
  21145. (org-agenda-check-type t 'agenda 'timeline)
  21146. (org-agenda-check-no-diary)
  21147. (let* ((marker (or (get-text-property (point) 'org-marker)
  21148. (org-agenda-error)))
  21149. (buffer (marker-buffer marker))
  21150. (pos (marker-position marker)))
  21151. (org-with-remote-undo buffer
  21152. (with-current-buffer buffer
  21153. (widen)
  21154. (goto-char pos)
  21155. (if (not (org-at-timestamp-p))
  21156. (error "Cannot find time stamp"))
  21157. (org-time-stamp arg)
  21158. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21159. (defun org-agenda-schedule (arg)
  21160. "Schedule the item at point."
  21161. (interactive "P")
  21162. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21163. (org-agenda-check-no-diary)
  21164. (let* ((marker (or (get-text-property (point) 'org-marker)
  21165. (org-agenda-error)))
  21166. (buffer (marker-buffer marker))
  21167. (pos (marker-position marker))
  21168. (org-insert-labeled-timestamps-at-point nil)
  21169. ts)
  21170. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21171. (set-marker-insertion-type marker t)
  21172. (org-with-remote-undo buffer
  21173. (with-current-buffer buffer
  21174. (widen)
  21175. (goto-char pos)
  21176. (setq ts (org-schedule arg)))
  21177. (org-agenda-show-new-time marker ts "S"))
  21178. (message "Item scheduled for %s" ts)))
  21179. (defun org-agenda-deadline (arg)
  21180. "Schedule the item at point."
  21181. (interactive "P")
  21182. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21183. (org-agenda-check-no-diary)
  21184. (let* ((marker (or (get-text-property (point) 'org-marker)
  21185. (org-agenda-error)))
  21186. (buffer (marker-buffer marker))
  21187. (pos (marker-position marker))
  21188. (org-insert-labeled-timestamps-at-point nil)
  21189. ts)
  21190. (org-with-remote-undo buffer
  21191. (with-current-buffer buffer
  21192. (widen)
  21193. (goto-char pos)
  21194. (setq ts (org-deadline arg)))
  21195. (org-agenda-show-new-time marker ts "S"))
  21196. (message "Deadline for this item set to %s" ts)))
  21197. (defun org-get-heading (&optional no-tags)
  21198. "Return the heading of the current entry, without the stars."
  21199. (save-excursion
  21200. (org-back-to-heading t)
  21201. (if (looking-at
  21202. (if no-tags
  21203. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21204. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21205. (match-string 1) "")))
  21206. (defun org-agenda-clock-in (&optional arg)
  21207. "Start the clock on the currently selected item."
  21208. (interactive "P")
  21209. (org-agenda-check-no-diary)
  21210. (let* ((marker (or (get-text-property (point) 'org-marker)
  21211. (org-agenda-error)))
  21212. (pos (marker-position marker)))
  21213. (org-with-remote-undo (marker-buffer marker)
  21214. (with-current-buffer (marker-buffer marker)
  21215. (widen)
  21216. (goto-char pos)
  21217. (org-clock-in)))))
  21218. (defun org-agenda-clock-out (&optional arg)
  21219. "Stop the currently running clock."
  21220. (interactive "P")
  21221. (unless (marker-buffer org-clock-marker)
  21222. (error "No running clock"))
  21223. (org-with-remote-undo (marker-buffer org-clock-marker)
  21224. (org-clock-out)))
  21225. (defun org-agenda-clock-cancel (&optional arg)
  21226. "Cancel the currently running clock."
  21227. (interactive "P")
  21228. (unless (marker-buffer org-clock-marker)
  21229. (error "No running clock"))
  21230. (org-with-remote-undo (marker-buffer org-clock-marker)
  21231. (org-clock-cancel)))
  21232. (defun org-agenda-diary-entry ()
  21233. "Make a diary entry, like the `i' command from the calendar.
  21234. All the standard commands work: block, weekly etc."
  21235. (interactive)
  21236. (org-agenda-check-type t 'agenda 'timeline)
  21237. (require 'diary-lib)
  21238. (let* ((char (progn
  21239. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21240. (read-char-exclusive)))
  21241. (cmd (cdr (assoc char
  21242. '((?d . insert-diary-entry)
  21243. (?w . insert-weekly-diary-entry)
  21244. (?m . insert-monthly-diary-entry)
  21245. (?y . insert-yearly-diary-entry)
  21246. (?a . insert-anniversary-diary-entry)
  21247. (?b . insert-block-diary-entry)
  21248. (?c . insert-cyclic-diary-entry)))))
  21249. (oldf (symbol-function 'calendar-cursor-to-date))
  21250. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21251. (point (point))
  21252. (mark (or (mark t) (point))))
  21253. (unless cmd
  21254. (error "No command associated with <%c>" char))
  21255. (unless (and (get-text-property point 'day)
  21256. (or (not (equal ?b char))
  21257. (get-text-property mark 'day)))
  21258. (error "Don't know which date to use for diary entry"))
  21259. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21260. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21261. (let ((calendar-mark-ring
  21262. (list (calendar-gregorian-from-absolute
  21263. (or (get-text-property mark 'day)
  21264. (get-text-property point 'day))))))
  21265. (unwind-protect
  21266. (progn
  21267. (fset 'calendar-cursor-to-date
  21268. (lambda (&optional error)
  21269. (calendar-gregorian-from-absolute
  21270. (get-text-property point 'day))))
  21271. (call-interactively cmd))
  21272. (fset 'calendar-cursor-to-date oldf)))))
  21273. (defun org-agenda-execute-calendar-command (cmd)
  21274. "Execute a calendar command from the agenda, with the date associated to
  21275. the cursor position."
  21276. (org-agenda-check-type t 'agenda 'timeline)
  21277. (require 'diary-lib)
  21278. (unless (get-text-property (point) 'day)
  21279. (error "Don't know which date to use for calendar command"))
  21280. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21281. (point (point))
  21282. (date (calendar-gregorian-from-absolute
  21283. (get-text-property point 'day)))
  21284. ;; the following 3 vars are needed in the calendar
  21285. (displayed-day (extract-calendar-day date))
  21286. (displayed-month (extract-calendar-month date))
  21287. (displayed-year (extract-calendar-year date)))
  21288. (unwind-protect
  21289. (progn
  21290. (fset 'calendar-cursor-to-date
  21291. (lambda (&optional error)
  21292. (calendar-gregorian-from-absolute
  21293. (get-text-property point 'day))))
  21294. (call-interactively cmd))
  21295. (fset 'calendar-cursor-to-date oldf))))
  21296. (defun org-agenda-phases-of-moon ()
  21297. "Display the phases of the moon for the 3 months around the cursor date."
  21298. (interactive)
  21299. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21300. (defun org-agenda-holidays ()
  21301. "Display the holidays for the 3 months around the cursor date."
  21302. (interactive)
  21303. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21304. (defun org-agenda-sunrise-sunset (arg)
  21305. "Display sunrise and sunset for the cursor date.
  21306. Latitude and longitude can be specified with the variables
  21307. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21308. argument, latitude and longitude will be prompted for."
  21309. (interactive "P")
  21310. (let ((calendar-longitude (if arg nil calendar-longitude))
  21311. (calendar-latitude (if arg nil calendar-latitude))
  21312. (calendar-location-name
  21313. (if arg "the given coordinates" calendar-location-name)))
  21314. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21315. (defun org-agenda-goto-calendar ()
  21316. "Open the Emacs calendar with the date at the cursor."
  21317. (interactive)
  21318. (org-agenda-check-type t 'agenda 'timeline)
  21319. (let* ((day (or (get-text-property (point) 'day)
  21320. (error "Don't know which date to open in calendar")))
  21321. (date (calendar-gregorian-from-absolute day))
  21322. (calendar-move-hook nil)
  21323. (view-calendar-holidays-initially nil)
  21324. (view-diary-entries-initially nil))
  21325. (calendar)
  21326. (calendar-goto-date date)))
  21327. (defun org-calendar-goto-agenda ()
  21328. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21329. This is a command that has to be installed in `calendar-mode-map'."
  21330. (interactive)
  21331. (org-agenda-list nil (calendar-absolute-from-gregorian
  21332. (calendar-cursor-to-date))
  21333. nil))
  21334. (defun org-agenda-convert-date ()
  21335. (interactive)
  21336. (org-agenda-check-type t 'agenda 'timeline)
  21337. (let ((day (get-text-property (point) 'day))
  21338. date s)
  21339. (unless day
  21340. (error "Don't know which date to convert"))
  21341. (setq date (calendar-gregorian-from-absolute day))
  21342. (setq s (concat
  21343. "Gregorian: " (calendar-date-string date) "\n"
  21344. "ISO: " (calendar-iso-date-string date) "\n"
  21345. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21346. "Julian: " (calendar-julian-date-string date) "\n"
  21347. "Astron. JD: " (calendar-astro-date-string date)
  21348. " (Julian date number at noon UTC)\n"
  21349. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21350. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21351. "French: " (calendar-french-date-string date) "\n"
  21352. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21353. "Mayan: " (calendar-mayan-date-string date) "\n"
  21354. "Coptic: " (calendar-coptic-date-string date) "\n"
  21355. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21356. "Persian: " (calendar-persian-date-string date) "\n"
  21357. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21358. (with-output-to-temp-buffer "*Dates*"
  21359. (princ s))
  21360. (if (fboundp 'fit-window-to-buffer)
  21361. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21362. ;;;; Embedded LaTeX
  21363. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21364. "Keymap for the minor `org-cdlatex-mode'.")
  21365. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21366. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21367. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21368. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21369. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21370. (defvar org-cdlatex-texmathp-advice-is-done nil
  21371. "Flag remembering if we have applied the advice to texmathp already.")
  21372. (define-minor-mode org-cdlatex-mode
  21373. "Toggle the minor `org-cdlatex-mode'.
  21374. This mode supports entering LaTeX environment and math in LaTeX fragments
  21375. in Org-mode.
  21376. \\{org-cdlatex-mode-map}"
  21377. nil " OCDL" nil
  21378. (when org-cdlatex-mode (require 'cdlatex))
  21379. (unless org-cdlatex-texmathp-advice-is-done
  21380. (setq org-cdlatex-texmathp-advice-is-done t)
  21381. (defadvice texmathp (around org-math-always-on activate)
  21382. "Always return t in org-mode buffers.
  21383. This is because we want to insert math symbols without dollars even outside
  21384. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21385. en embedded LaTeX fragement, let texmathp do its job.
  21386. \\[org-cdlatex-mode-map]"
  21387. (interactive)
  21388. (let (p)
  21389. (cond
  21390. ((not (org-mode-p)) ad-do-it)
  21391. ((eq this-command 'cdlatex-math-symbol)
  21392. (setq ad-return-value t
  21393. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21394. (t
  21395. (let ((p (org-inside-LaTeX-fragment-p)))
  21396. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21397. (setq ad-return-value t
  21398. texmathp-why '("Org-mode embedded math" . 0))
  21399. (if p ad-do-it)))))))))
  21400. (defun turn-on-org-cdlatex ()
  21401. "Unconditionally turn on `org-cdlatex-mode'."
  21402. (org-cdlatex-mode 1))
  21403. (defun org-inside-LaTeX-fragment-p ()
  21404. "Test if point is inside a LaTeX fragment.
  21405. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21406. sequence appearing also before point.
  21407. Even though the matchers for math are configurable, this function assumes
  21408. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21409. delimiters are skipped when they have been removed by customization.
  21410. The return value is nil, or a cons cell with the delimiter and
  21411. and the position of this delimiter.
  21412. This function does a reasonably good job, but can locally be fooled by
  21413. for example currency specifications. For example it will assume being in
  21414. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21415. fragments that are properly closed, but during editing, we have to live
  21416. with the uncertainty caused by missing closing delimiters. This function
  21417. looks only before point, not after."
  21418. (catch 'exit
  21419. (let ((pos (point))
  21420. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21421. (lim (progn
  21422. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21423. (point)))
  21424. dd-on str (start 0) m re)
  21425. (goto-char pos)
  21426. (when dodollar
  21427. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21428. re (nth 1 (assoc "$" org-latex-regexps)))
  21429. (while (string-match re str start)
  21430. (cond
  21431. ((= (match-end 0) (length str))
  21432. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21433. ((= (match-end 0) (- (length str) 5))
  21434. (throw 'exit nil))
  21435. (t (setq start (match-end 0))))))
  21436. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21437. (goto-char pos)
  21438. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21439. (and (match-beginning 2) (throw 'exit nil))
  21440. ;; count $$
  21441. (while (re-search-backward "\\$\\$" lim t)
  21442. (setq dd-on (not dd-on)))
  21443. (goto-char pos)
  21444. (if dd-on (cons "$$" m))))))
  21445. (defun org-try-cdlatex-tab ()
  21446. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21447. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21448. - inside a LaTeX fragment, or
  21449. - after the first word in a line, where an abbreviation expansion could
  21450. insert a LaTeX environment."
  21451. (when org-cdlatex-mode
  21452. (cond
  21453. ((save-excursion
  21454. (skip-chars-backward "a-zA-Z0-9*")
  21455. (skip-chars-backward " \t")
  21456. (bolp))
  21457. (cdlatex-tab) t)
  21458. ((org-inside-LaTeX-fragment-p)
  21459. (cdlatex-tab) t)
  21460. (t nil))))
  21461. (defun org-cdlatex-underscore-caret (&optional arg)
  21462. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21463. Revert to the normal definition outside of these fragments."
  21464. (interactive "P")
  21465. (if (org-inside-LaTeX-fragment-p)
  21466. (call-interactively 'cdlatex-sub-superscript)
  21467. (let (org-cdlatex-mode)
  21468. (call-interactively (key-binding (vector last-input-event))))))
  21469. (defun org-cdlatex-math-modify (&optional arg)
  21470. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21471. Revert to the normal definition outside of these fragments."
  21472. (interactive "P")
  21473. (if (org-inside-LaTeX-fragment-p)
  21474. (call-interactively 'cdlatex-math-modify)
  21475. (let (org-cdlatex-mode)
  21476. (call-interactively (key-binding (vector last-input-event))))))
  21477. (defvar org-latex-fragment-image-overlays nil
  21478. "List of overlays carrying the images of latex fragments.")
  21479. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21480. (defun org-remove-latex-fragment-image-overlays ()
  21481. "Remove all overlays with LaTeX fragment images in current buffer."
  21482. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21483. (setq org-latex-fragment-image-overlays nil))
  21484. (defun org-preview-latex-fragment (&optional subtree)
  21485. "Preview the LaTeX fragment at point, or all locally or globally.
  21486. If the cursor is in a LaTeX fragment, create the image and overlay
  21487. it over the source code. If there is no fragment at point, display
  21488. all fragments in the current text, from one headline to the next. With
  21489. prefix SUBTREE, display all fragments in the current subtree. With a
  21490. double prefix `C-u C-u', or when the cursor is before the first headline,
  21491. display all fragments in the buffer.
  21492. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21493. (interactive "P")
  21494. (org-remove-latex-fragment-image-overlays)
  21495. (save-excursion
  21496. (save-restriction
  21497. (let (beg end at msg)
  21498. (cond
  21499. ((or (equal subtree '(16))
  21500. (not (save-excursion
  21501. (re-search-backward (concat "^" outline-regexp) nil t))))
  21502. (setq beg (point-min) end (point-max)
  21503. msg "Creating images for buffer...%s"))
  21504. ((equal subtree '(4))
  21505. (org-back-to-heading)
  21506. (setq beg (point) end (org-end-of-subtree t)
  21507. msg "Creating images for subtree...%s"))
  21508. (t
  21509. (if (setq at (org-inside-LaTeX-fragment-p))
  21510. (goto-char (max (point-min) (- (cdr at) 2)))
  21511. (org-back-to-heading))
  21512. (setq beg (point) end (progn (outline-next-heading) (point))
  21513. msg (if at "Creating image...%s"
  21514. "Creating images for entry...%s"))))
  21515. (message msg "")
  21516. (narrow-to-region beg end)
  21517. (goto-char beg)
  21518. (org-format-latex
  21519. (concat "ltxpng/" (file-name-sans-extension
  21520. (file-name-nondirectory
  21521. buffer-file-name)))
  21522. default-directory 'overlays msg at 'forbuffer)
  21523. (message msg "done. Use `C-c C-c' to remove images.")))))
  21524. (defvar org-latex-regexps
  21525. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21526. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21527. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21528. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21529. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21530. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21531. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21532. "Regular expressions for matching embedded LaTeX.")
  21533. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21534. "Replace LaTeX fragments with links to an image, and produce images."
  21535. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21536. (let* ((prefixnodir (file-name-nondirectory prefix))
  21537. (absprefix (expand-file-name prefix dir))
  21538. (todir (file-name-directory absprefix))
  21539. (opt org-format-latex-options)
  21540. (matchers (plist-get opt :matchers))
  21541. (re-list org-latex-regexps)
  21542. (cnt 0) txt link beg end re e checkdir
  21543. m n block linkfile movefile ov)
  21544. ;; Check if there are old images files with this prefix, and remove them
  21545. (when (file-directory-p todir)
  21546. (mapc 'delete-file
  21547. (directory-files
  21548. todir 'full
  21549. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21550. ;; Check the different regular expressions
  21551. (while (setq e (pop re-list))
  21552. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21553. block (if (nth 3 e) "\n\n" ""))
  21554. (when (member m matchers)
  21555. (goto-char (point-min))
  21556. (while (re-search-forward re nil t)
  21557. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21558. (setq txt (match-string n)
  21559. beg (match-beginning n) end (match-end n)
  21560. cnt (1+ cnt)
  21561. linkfile (format "%s_%04d.png" prefix cnt)
  21562. movefile (format "%s_%04d.png" absprefix cnt)
  21563. link (concat block "[[file:" linkfile "]]" block))
  21564. (if msg (message msg cnt))
  21565. (goto-char beg)
  21566. (unless checkdir ; make sure the directory exists
  21567. (setq checkdir t)
  21568. (or (file-directory-p todir) (make-directory todir)))
  21569. (org-create-formula-image
  21570. txt movefile opt forbuffer)
  21571. (if overlays
  21572. (progn
  21573. (setq ov (org-make-overlay beg end))
  21574. (if (featurep 'xemacs)
  21575. (progn
  21576. (org-overlay-put ov 'invisible t)
  21577. (org-overlay-put
  21578. ov 'end-glyph
  21579. (make-glyph (vector 'png :file movefile))))
  21580. (org-overlay-put
  21581. ov 'display
  21582. (list 'image :type 'png :file movefile :ascent 'center)))
  21583. (push ov org-latex-fragment-image-overlays)
  21584. (goto-char end))
  21585. (delete-region beg end)
  21586. (insert link))))))))
  21587. ;; This function borrows from Ganesh Swami's latex2png.el
  21588. (defun org-create-formula-image (string tofile options buffer)
  21589. (let* ((tmpdir (if (featurep 'xemacs)
  21590. (temp-directory)
  21591. temporary-file-directory))
  21592. (texfilebase (make-temp-name
  21593. (expand-file-name "orgtex" tmpdir)))
  21594. (texfile (concat texfilebase ".tex"))
  21595. (dvifile (concat texfilebase ".dvi"))
  21596. (pngfile (concat texfilebase ".png"))
  21597. (fnh (face-attribute 'default :height nil))
  21598. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21599. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21600. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21601. "Black"))
  21602. (bg (or (plist-get options (if buffer :background :html-background))
  21603. "Transparent")))
  21604. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21605. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21606. (with-temp-file texfile
  21607. (insert org-format-latex-header
  21608. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21609. (let ((dir default-directory))
  21610. (condition-case nil
  21611. (progn
  21612. (cd tmpdir)
  21613. (call-process "latex" nil nil nil texfile))
  21614. (error nil))
  21615. (cd dir))
  21616. (if (not (file-exists-p dvifile))
  21617. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21618. (call-process "dvipng" nil nil nil
  21619. "-E" "-fg" fg "-bg" bg
  21620. "-D" dpi
  21621. ;;"-x" scale "-y" scale
  21622. "-T" "tight"
  21623. "-o" pngfile
  21624. dvifile)
  21625. (if (not (file-exists-p pngfile))
  21626. (progn (message "Failed to create png file from %s" texfile) nil)
  21627. ;; Use the requested file name and clean up
  21628. (copy-file pngfile tofile 'replace)
  21629. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21630. (delete-file (concat texfilebase e)))
  21631. pngfile))))
  21632. (defun org-dvipng-color (attr)
  21633. "Return an rgb color specification for dvipng."
  21634. (apply 'format "rgb %s %s %s"
  21635. (mapcar 'org-normalize-color
  21636. (color-values (face-attribute 'default attr nil)))))
  21637. (defun org-normalize-color (value)
  21638. "Return string to be used as color value for an RGB component."
  21639. (format "%g" (/ value 65535.0)))
  21640. ;;;; Exporting
  21641. ;;; Variables, constants, and parameter plists
  21642. (defconst org-level-max 20)
  21643. (defvar org-export-html-preamble nil
  21644. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21645. (defvar org-export-html-postamble nil
  21646. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21647. (defvar org-export-html-auto-preamble t
  21648. "Should default preamble be inserted? Set by publishing functions.")
  21649. (defvar org-export-html-auto-postamble t
  21650. "Should default postamble be inserted? Set by publishing functions.")
  21651. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21652. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21653. (defconst org-export-plist-vars
  21654. '((:language . org-export-default-language)
  21655. (:customtime . org-display-custom-times)
  21656. (:headline-levels . org-export-headline-levels)
  21657. (:section-numbers . org-export-with-section-numbers)
  21658. (:table-of-contents . org-export-with-toc)
  21659. (:preserve-breaks . org-export-preserve-breaks)
  21660. (:archived-trees . org-export-with-archived-trees)
  21661. (:emphasize . org-export-with-emphasize)
  21662. (:sub-superscript . org-export-with-sub-superscripts)
  21663. (:special-strings . org-export-with-special-strings)
  21664. (:footnotes . org-export-with-footnotes)
  21665. (:drawers . org-export-with-drawers)
  21666. (:tags . org-export-with-tags)
  21667. (:TeX-macros . org-export-with-TeX-macros)
  21668. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21669. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21670. (:fixed-width . org-export-with-fixed-width)
  21671. (:timestamps . org-export-with-timestamps)
  21672. (:author-info . org-export-author-info)
  21673. (:time-stamp-file . org-export-time-stamp-file)
  21674. (:tables . org-export-with-tables)
  21675. (:table-auto-headline . org-export-highlight-first-table-line)
  21676. (:style . org-export-html-style)
  21677. (:agenda-style . org-agenda-export-html-style)
  21678. (:convert-org-links . org-export-html-link-org-files-as-html)
  21679. (:inline-images . org-export-html-inline-images)
  21680. (:html-extension . org-export-html-extension)
  21681. (:html-table-tag . org-export-html-table-tag)
  21682. (:expand-quoted-html . org-export-html-expand)
  21683. (:timestamp . org-export-html-with-timestamp)
  21684. (:publishing-directory . org-export-publishing-directory)
  21685. (:preamble . org-export-html-preamble)
  21686. (:postamble . org-export-html-postamble)
  21687. (:auto-preamble . org-export-html-auto-preamble)
  21688. (:auto-postamble . org-export-html-auto-postamble)
  21689. (:author . user-full-name)
  21690. (:email . user-mail-address)))
  21691. (defun org-default-export-plist ()
  21692. "Return the property list with default settings for the export variables."
  21693. (let ((l org-export-plist-vars) rtn e)
  21694. (while (setq e (pop l))
  21695. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21696. rtn))
  21697. (defun org-infile-export-plist ()
  21698. "Return the property list with file-local settings for export."
  21699. (save-excursion
  21700. (save-restriction
  21701. (widen)
  21702. (goto-char 0)
  21703. (let ((re (org-make-options-regexp
  21704. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21705. p key val text options)
  21706. (while (re-search-forward re nil t)
  21707. (setq key (org-match-string-no-properties 1)
  21708. val (org-match-string-no-properties 2))
  21709. (cond
  21710. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21711. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21712. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21713. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21714. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21715. ((string-equal key "TEXT")
  21716. (setq text (if text (concat text "\n" val) val)))
  21717. ((string-equal key "OPTIONS") (setq options val))))
  21718. (setq p (plist-put p :text text))
  21719. (when options
  21720. (let ((op '(("H" . :headline-levels)
  21721. ("num" . :section-numbers)
  21722. ("toc" . :table-of-contents)
  21723. ("\\n" . :preserve-breaks)
  21724. ("@" . :expand-quoted-html)
  21725. (":" . :fixed-width)
  21726. ("|" . :tables)
  21727. ("^" . :sub-superscript)
  21728. ("-" . :special-strings)
  21729. ("f" . :footnotes)
  21730. ("d" . :drawers)
  21731. ("tags" . :tags)
  21732. ("*" . :emphasize)
  21733. ("TeX" . :TeX-macros)
  21734. ("LaTeX" . :LaTeX-fragments)
  21735. ("skip" . :skip-before-1st-heading)
  21736. ("author" . :author-info)
  21737. ("timestamp" . :time-stamp-file)))
  21738. o)
  21739. (while (setq o (pop op))
  21740. (if (string-match (concat (regexp-quote (car o))
  21741. ":\\([^ \t\n\r;,.]*\\)")
  21742. options)
  21743. (setq p (plist-put p (cdr o)
  21744. (car (read-from-string
  21745. (match-string 1 options)))))))))
  21746. p))))
  21747. (defun org-export-directory (type plist)
  21748. (let* ((val (plist-get plist :publishing-directory))
  21749. (dir (if (listp val)
  21750. (or (cdr (assoc type val)) ".")
  21751. val)))
  21752. dir))
  21753. (defun org-skip-comments (lines)
  21754. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21755. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21756. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21757. (case-fold-search nil)
  21758. rtn line level)
  21759. (while (setq line (pop lines))
  21760. (cond
  21761. ((and (string-match re1 line)
  21762. (setq level (- (match-end 1) (match-beginning 1))))
  21763. ;; Beginning of a COMMENT subtree. Skip it.
  21764. (while (and (setq line (pop lines))
  21765. (or (not (string-match re2 line))
  21766. (> (- (match-end 1) (match-beginning 1)) level))))
  21767. (setq lines (cons line lines)))
  21768. ((string-match "^#" line)
  21769. ;; an ordinary comment line
  21770. )
  21771. ((and org-export-table-remove-special-lines
  21772. (string-match "^[ \t]*|" line)
  21773. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21774. (and (string-match "| *<[0-9]+> *|" line)
  21775. (not (string-match "| *[^ <|]" line)))))
  21776. ;; a special table line that should be removed
  21777. )
  21778. (t (setq rtn (cons line rtn)))))
  21779. (nreverse rtn)))
  21780. (defun org-export (&optional arg)
  21781. (interactive)
  21782. (let ((help "[t] insert the export option template
  21783. \[v] limit export to visible part of outline tree
  21784. \[a] export as ASCII
  21785. \[h] export as HTML
  21786. \[H] export as HTML to temporary buffer
  21787. \[R] export region as HTML
  21788. \[b] export as HTML and browse immediately
  21789. \[x] export as XOXO
  21790. \[l] export as LaTeX
  21791. \[L] export as LaTeX to temporary buffer
  21792. \[i] export current file as iCalendar file
  21793. \[I] export all agenda files as iCalendar files
  21794. \[c] export agenda files into combined iCalendar file
  21795. \[F] publish current file
  21796. \[P] publish current project
  21797. \[X] publish... (project will be prompted for)
  21798. \[A] publish all projects")
  21799. (cmds
  21800. '((?t . org-insert-export-options-template)
  21801. (?v . org-export-visible)
  21802. (?a . org-export-as-ascii)
  21803. (?h . org-export-as-html)
  21804. (?b . org-export-as-html-and-open)
  21805. (?H . org-export-as-html-to-buffer)
  21806. (?R . org-export-region-as-html)
  21807. (?x . org-export-as-xoxo)
  21808. (?l . org-export-as-latex)
  21809. (?L . org-export-as-latex-to-buffer)
  21810. (?i . org-export-icalendar-this-file)
  21811. (?I . org-export-icalendar-all-agenda-files)
  21812. (?c . org-export-icalendar-combine-agenda-files)
  21813. (?F . org-publish-current-file)
  21814. (?P . org-publish-current-project)
  21815. (?X . org-publish)
  21816. (?A . org-publish-all)))
  21817. r1 r2 ass)
  21818. (save-window-excursion
  21819. (delete-other-windows)
  21820. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21821. (princ help))
  21822. (message "Select command: ")
  21823. (setq r1 (read-char-exclusive)))
  21824. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21825. (if (setq ass (assq r2 cmds))
  21826. (call-interactively (cdr ass))
  21827. (error "No command associated with key %c" r1))))
  21828. (defconst org-html-entities
  21829. '(("nbsp")
  21830. ("iexcl")
  21831. ("cent")
  21832. ("pound")
  21833. ("curren")
  21834. ("yen")
  21835. ("brvbar")
  21836. ("vert" . "&#124;")
  21837. ("sect")
  21838. ("uml")
  21839. ("copy")
  21840. ("ordf")
  21841. ("laquo")
  21842. ("not")
  21843. ("shy")
  21844. ("reg")
  21845. ("macr")
  21846. ("deg")
  21847. ("plusmn")
  21848. ("sup2")
  21849. ("sup3")
  21850. ("acute")
  21851. ("micro")
  21852. ("para")
  21853. ("middot")
  21854. ("odot"."o")
  21855. ("star"."*")
  21856. ("cedil")
  21857. ("sup1")
  21858. ("ordm")
  21859. ("raquo")
  21860. ("frac14")
  21861. ("frac12")
  21862. ("frac34")
  21863. ("iquest")
  21864. ("Agrave")
  21865. ("Aacute")
  21866. ("Acirc")
  21867. ("Atilde")
  21868. ("Auml")
  21869. ("Aring") ("AA"."&Aring;")
  21870. ("AElig")
  21871. ("Ccedil")
  21872. ("Egrave")
  21873. ("Eacute")
  21874. ("Ecirc")
  21875. ("Euml")
  21876. ("Igrave")
  21877. ("Iacute")
  21878. ("Icirc")
  21879. ("Iuml")
  21880. ("ETH")
  21881. ("Ntilde")
  21882. ("Ograve")
  21883. ("Oacute")
  21884. ("Ocirc")
  21885. ("Otilde")
  21886. ("Ouml")
  21887. ("times")
  21888. ("Oslash")
  21889. ("Ugrave")
  21890. ("Uacute")
  21891. ("Ucirc")
  21892. ("Uuml")
  21893. ("Yacute")
  21894. ("THORN")
  21895. ("szlig")
  21896. ("agrave")
  21897. ("aacute")
  21898. ("acirc")
  21899. ("atilde")
  21900. ("auml")
  21901. ("aring")
  21902. ("aelig")
  21903. ("ccedil")
  21904. ("egrave")
  21905. ("eacute")
  21906. ("ecirc")
  21907. ("euml")
  21908. ("igrave")
  21909. ("iacute")
  21910. ("icirc")
  21911. ("iuml")
  21912. ("eth")
  21913. ("ntilde")
  21914. ("ograve")
  21915. ("oacute")
  21916. ("ocirc")
  21917. ("otilde")
  21918. ("ouml")
  21919. ("divide")
  21920. ("oslash")
  21921. ("ugrave")
  21922. ("uacute")
  21923. ("ucirc")
  21924. ("uuml")
  21925. ("yacute")
  21926. ("thorn")
  21927. ("yuml")
  21928. ("fnof")
  21929. ("Alpha")
  21930. ("Beta")
  21931. ("Gamma")
  21932. ("Delta")
  21933. ("Epsilon")
  21934. ("Zeta")
  21935. ("Eta")
  21936. ("Theta")
  21937. ("Iota")
  21938. ("Kappa")
  21939. ("Lambda")
  21940. ("Mu")
  21941. ("Nu")
  21942. ("Xi")
  21943. ("Omicron")
  21944. ("Pi")
  21945. ("Rho")
  21946. ("Sigma")
  21947. ("Tau")
  21948. ("Upsilon")
  21949. ("Phi")
  21950. ("Chi")
  21951. ("Psi")
  21952. ("Omega")
  21953. ("alpha")
  21954. ("beta")
  21955. ("gamma")
  21956. ("delta")
  21957. ("epsilon")
  21958. ("varepsilon"."&epsilon;")
  21959. ("zeta")
  21960. ("eta")
  21961. ("theta")
  21962. ("iota")
  21963. ("kappa")
  21964. ("lambda")
  21965. ("mu")
  21966. ("nu")
  21967. ("xi")
  21968. ("omicron")
  21969. ("pi")
  21970. ("rho")
  21971. ("sigmaf") ("varsigma"."&sigmaf;")
  21972. ("sigma")
  21973. ("tau")
  21974. ("upsilon")
  21975. ("phi")
  21976. ("chi")
  21977. ("psi")
  21978. ("omega")
  21979. ("thetasym") ("vartheta"."&thetasym;")
  21980. ("upsih")
  21981. ("piv")
  21982. ("bull") ("bullet"."&bull;")
  21983. ("hellip") ("dots"."&hellip;")
  21984. ("prime")
  21985. ("Prime")
  21986. ("oline")
  21987. ("frasl")
  21988. ("weierp")
  21989. ("image")
  21990. ("real")
  21991. ("trade")
  21992. ("alefsym")
  21993. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  21994. ("uarr") ("uparrow"."&uarr;")
  21995. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  21996. ("darr")("downarrow"."&darr;")
  21997. ("harr") ("leftrightarrow"."&harr;")
  21998. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  21999. ("lArr") ("Leftarrow"."&lArr;")
  22000. ("uArr") ("Uparrow"."&uArr;")
  22001. ("rArr") ("Rightarrow"."&rArr;")
  22002. ("dArr") ("Downarrow"."&dArr;")
  22003. ("hArr") ("Leftrightarrow"."&hArr;")
  22004. ("forall")
  22005. ("part") ("partial"."&part;")
  22006. ("exist") ("exists"."&exist;")
  22007. ("empty") ("emptyset"."&empty;")
  22008. ("nabla")
  22009. ("isin") ("in"."&isin;")
  22010. ("notin")
  22011. ("ni")
  22012. ("prod")
  22013. ("sum")
  22014. ("minus")
  22015. ("lowast") ("ast"."&lowast;")
  22016. ("radic")
  22017. ("prop") ("proptp"."&prop;")
  22018. ("infin") ("infty"."&infin;")
  22019. ("ang") ("angle"."&ang;")
  22020. ("and") ("wedge"."&and;")
  22021. ("or") ("vee"."&or;")
  22022. ("cap")
  22023. ("cup")
  22024. ("int")
  22025. ("there4")
  22026. ("sim")
  22027. ("cong") ("simeq"."&cong;")
  22028. ("asymp")("approx"."&asymp;")
  22029. ("ne") ("neq"."&ne;")
  22030. ("equiv")
  22031. ("le")
  22032. ("ge")
  22033. ("sub") ("subset"."&sub;")
  22034. ("sup") ("supset"."&sup;")
  22035. ("nsub")
  22036. ("sube")
  22037. ("supe")
  22038. ("oplus")
  22039. ("otimes")
  22040. ("perp")
  22041. ("sdot") ("cdot"."&sdot;")
  22042. ("lceil")
  22043. ("rceil")
  22044. ("lfloor")
  22045. ("rfloor")
  22046. ("lang")
  22047. ("rang")
  22048. ("loz") ("Diamond"."&loz;")
  22049. ("spades") ("spadesuit"."&spades;")
  22050. ("clubs") ("clubsuit"."&clubs;")
  22051. ("hearts") ("diamondsuit"."&hearts;")
  22052. ("diams") ("diamondsuit"."&diams;")
  22053. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22054. ("quot")
  22055. ("amp")
  22056. ("lt")
  22057. ("gt")
  22058. ("OElig")
  22059. ("oelig")
  22060. ("Scaron")
  22061. ("scaron")
  22062. ("Yuml")
  22063. ("circ")
  22064. ("tilde")
  22065. ("ensp")
  22066. ("emsp")
  22067. ("thinsp")
  22068. ("zwnj")
  22069. ("zwj")
  22070. ("lrm")
  22071. ("rlm")
  22072. ("ndash")
  22073. ("mdash")
  22074. ("lsquo")
  22075. ("rsquo")
  22076. ("sbquo")
  22077. ("ldquo")
  22078. ("rdquo")
  22079. ("bdquo")
  22080. ("dagger")
  22081. ("Dagger")
  22082. ("permil")
  22083. ("lsaquo")
  22084. ("rsaquo")
  22085. ("euro")
  22086. ("arccos"."arccos")
  22087. ("arcsin"."arcsin")
  22088. ("arctan"."arctan")
  22089. ("arg"."arg")
  22090. ("cos"."cos")
  22091. ("cosh"."cosh")
  22092. ("cot"."cot")
  22093. ("coth"."coth")
  22094. ("csc"."csc")
  22095. ("deg"."deg")
  22096. ("det"."det")
  22097. ("dim"."dim")
  22098. ("exp"."exp")
  22099. ("gcd"."gcd")
  22100. ("hom"."hom")
  22101. ("inf"."inf")
  22102. ("ker"."ker")
  22103. ("lg"."lg")
  22104. ("lim"."lim")
  22105. ("liminf"."liminf")
  22106. ("limsup"."limsup")
  22107. ("ln"."ln")
  22108. ("log"."log")
  22109. ("max"."max")
  22110. ("min"."min")
  22111. ("Pr"."Pr")
  22112. ("sec"."sec")
  22113. ("sin"."sin")
  22114. ("sinh"."sinh")
  22115. ("sup"."sup")
  22116. ("tan"."tan")
  22117. ("tanh"."tanh")
  22118. )
  22119. "Entities for TeX->HTML translation.
  22120. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22121. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22122. In that case, \"\\ent\" will be translated to \"&other;\".
  22123. The list contains HTML entities for Latin-1, Greek and other symbols.
  22124. It is supplemented by a number of commonly used TeX macros with appropriate
  22125. translations. There is currently no way for users to extend this.")
  22126. ;;; General functions for all backends
  22127. (defun org-cleaned-string-for-export (string &rest parameters)
  22128. "Cleanup a buffer STRING so that links can be created safely."
  22129. (interactive)
  22130. (let* ((re-radio (and org-target-link-regexp
  22131. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22132. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22133. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22134. (re-archive (concat ":" org-archive-tag ":"))
  22135. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22136. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22137. (htmlp (plist-get parameters :for-html))
  22138. (asciip (plist-get parameters :for-ascii))
  22139. (latexp (plist-get parameters :for-LaTeX))
  22140. (commentsp (plist-get parameters :comments))
  22141. (archived-trees (plist-get parameters :archived-trees))
  22142. (inhibit-read-only t)
  22143. (drawers org-drawers)
  22144. (exp-drawers (plist-get parameters :drawers))
  22145. (outline-regexp "\\*+ ")
  22146. a b xx
  22147. rtn p)
  22148. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22149. (erase-buffer)
  22150. (insert string)
  22151. ;; Remove license-to-kill stuff
  22152. (while (setq p (text-property-any (point-min) (point-max)
  22153. :org-license-to-kill t))
  22154. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22155. (let ((org-inhibit-startup t)) (org-mode))
  22156. (untabify (point-min) (point-max))
  22157. ;; Get rid of drawers
  22158. (unless (eq t exp-drawers)
  22159. (goto-char (point-min))
  22160. (let ((re (concat "^[ \t]*:\\("
  22161. (mapconcat
  22162. 'identity
  22163. (org-delete-all exp-drawers
  22164. (copy-sequence drawers))
  22165. "\\|")
  22166. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22167. (while (re-search-forward re nil t)
  22168. (replace-match ""))))
  22169. ;; Get the correct stuff before the first headline
  22170. (when (plist-get parameters :skip-before-1st-heading)
  22171. (goto-char (point-min))
  22172. (when (re-search-forward "^\\*+[ \t]" nil t)
  22173. (delete-region (point-min) (match-beginning 0))
  22174. (goto-char (point-min))
  22175. (insert "\n")))
  22176. (when (plist-get parameters :add-text)
  22177. (goto-char (point-min))
  22178. (insert (plist-get parameters :add-text) "\n"))
  22179. ;; Get rid of archived trees
  22180. (when (not (eq archived-trees t))
  22181. (goto-char (point-min))
  22182. (while (re-search-forward re-archive nil t)
  22183. (if (not (org-on-heading-p t))
  22184. (org-end-of-subtree t)
  22185. (beginning-of-line 1)
  22186. (setq a (if archived-trees
  22187. (1+ (point-at-eol)) (point))
  22188. b (org-end-of-subtree t))
  22189. (if (> b a) (delete-region a b)))))
  22190. ;; Find targets in comments and move them out of comments,
  22191. ;; but mark them as targets that should be invisible
  22192. (goto-char (point-min))
  22193. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22194. (replace-match "\\1(INVISIBLE)"))
  22195. ;; Protect backend specific stuff, throw away the others.
  22196. (let ((formatters
  22197. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22198. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22199. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22200. fmt)
  22201. (goto-char (point-min))
  22202. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22203. (goto-char (match-end 0))
  22204. (while (not (looking-at "#\\+END_EXAMPLE"))
  22205. (insert ": ")
  22206. (beginning-of-line 2)))
  22207. (goto-char (point-min))
  22208. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22209. (add-text-properties (match-beginning 0) (match-end 0)
  22210. '(org-protected t)))
  22211. (while formatters
  22212. (setq fmt (pop formatters))
  22213. (when (car fmt)
  22214. (goto-char (point-min))
  22215. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22216. ":[ \t]*\\(.*\\)") nil t)
  22217. (replace-match "\\1" t)
  22218. (add-text-properties
  22219. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22220. '(org-protected t))))
  22221. (goto-char (point-min))
  22222. (while (re-search-forward
  22223. (concat "^#\\+"
  22224. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22225. (cadddr fmt) "\\>.*\n?") nil t)
  22226. (if (car fmt)
  22227. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22228. '(org-protected t))
  22229. (delete-region (match-beginning 0) (match-end 0))))))
  22230. ;; Protect quoted subtrees
  22231. (goto-char (point-min))
  22232. (while (re-search-forward re-quote nil t)
  22233. (goto-char (match-beginning 0))
  22234. (end-of-line 1)
  22235. (add-text-properties (point) (org-end-of-subtree t)
  22236. '(org-protected t)))
  22237. ;; Protect verbatim elements
  22238. (goto-char (point-min))
  22239. (while (re-search-forward org-verbatim-re nil t)
  22240. (add-text-properties (match-beginning 4) (match-end 4)
  22241. '(org-protected t))
  22242. (goto-char (1+ (match-end 4))))
  22243. ;; Remove subtrees that are commented
  22244. (goto-char (point-min))
  22245. (while (re-search-forward re-commented nil t)
  22246. (goto-char (match-beginning 0))
  22247. (delete-region (point) (org-end-of-subtree t)))
  22248. ;; Remove special table lines
  22249. (when org-export-table-remove-special-lines
  22250. (goto-char (point-min))
  22251. (while (re-search-forward "^[ \t]*|" nil t)
  22252. (beginning-of-line 1)
  22253. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22254. (and (looking-at ".*?| *<[0-9]+> *|")
  22255. (not (looking-at ".*?| *[^ <|]"))))
  22256. (delete-region (max (point-min) (1- (point-at-bol)))
  22257. (point-at-eol))
  22258. (end-of-line 1))))
  22259. ;; Specific LaTeX stuff
  22260. (when latexp
  22261. (require 'org-export-latex nil)
  22262. (org-export-latex-cleaned-string))
  22263. (when asciip
  22264. (org-export-ascii-clean-string))
  22265. ;; Specific HTML stuff
  22266. (when htmlp
  22267. ;; Convert LaTeX fragments to images
  22268. (when (plist-get parameters :LaTeX-fragments)
  22269. (org-format-latex
  22270. (concat "ltxpng/" (file-name-sans-extension
  22271. (file-name-nondirectory
  22272. org-current-export-file)))
  22273. org-current-export-dir nil "Creating LaTeX image %s"))
  22274. (message "Exporting..."))
  22275. ;; Remove or replace comments
  22276. (goto-char (point-min))
  22277. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22278. (if commentsp
  22279. (progn (add-text-properties
  22280. (match-beginning 0) (match-end 0) '(org-protected t))
  22281. (replace-match (format commentsp (match-string 1)) t t))
  22282. (replace-match "")))
  22283. ;; Find matches for radio targets and turn them into internal links
  22284. (goto-char (point-min))
  22285. (when re-radio
  22286. (while (re-search-forward re-radio nil t)
  22287. (org-if-unprotected
  22288. (replace-match "\\1[[\\2]]"))))
  22289. ;; Find all links that contain a newline and put them into a single line
  22290. (goto-char (point-min))
  22291. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22292. (org-if-unprotected
  22293. (replace-match "\\1 \\3")
  22294. (goto-char (match-beginning 0))))
  22295. ;; Normalize links: Convert angle and plain links into bracket links
  22296. ;; Expand link abbreviations
  22297. (goto-char (point-min))
  22298. (while (re-search-forward re-plain-link nil t)
  22299. (goto-char (1- (match-end 0)))
  22300. (org-if-unprotected
  22301. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22302. ":" (match-string 3) "]]")))
  22303. ;; added 'org-link face to links
  22304. (put-text-property 0 (length s) 'face 'org-link s)
  22305. (replace-match s t t))))
  22306. (goto-char (point-min))
  22307. (while (re-search-forward re-angle-link nil t)
  22308. (goto-char (1- (match-end 0)))
  22309. (org-if-unprotected
  22310. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22311. ":" (match-string 3) "]]")))
  22312. (put-text-property 0 (length s) 'face 'org-link s)
  22313. (replace-match s t t))))
  22314. (goto-char (point-min))
  22315. (while (re-search-forward org-bracket-link-regexp nil t)
  22316. (org-if-unprotected
  22317. (let* ((s (concat "[[" (setq xx (save-match-data
  22318. (org-link-expand-abbrev (match-string 1))))
  22319. "]"
  22320. (if (match-end 3)
  22321. (match-string 2)
  22322. (concat "[" xx "]"))
  22323. "]")))
  22324. (put-text-property 0 (length s) 'face 'org-link s)
  22325. (replace-match s t t))))
  22326. ;; Find multiline emphasis and put them into single line
  22327. (when (plist-get parameters :emph-multiline)
  22328. (goto-char (point-min))
  22329. (while (re-search-forward org-emph-re nil t)
  22330. (if (not (= (char-after (match-beginning 3))
  22331. (char-after (match-beginning 4))))
  22332. (org-if-unprotected
  22333. (subst-char-in-region (match-beginning 0) (match-end 0)
  22334. ?\n ?\ t)
  22335. (goto-char (1- (match-end 0))))
  22336. (goto-char (1+ (match-beginning 0))))))
  22337. (setq rtn (buffer-string)))
  22338. (kill-buffer " org-mode-tmp")
  22339. rtn))
  22340. (defun org-export-grab-title-from-buffer ()
  22341. "Get a title for the current document, from looking at the buffer."
  22342. (let ((inhibit-read-only t))
  22343. (save-excursion
  22344. (goto-char (point-min))
  22345. (let ((end (save-excursion (outline-next-heading) (point))))
  22346. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22347. ;; Mark the line so that it will not be exported as normal text.
  22348. (org-unmodified
  22349. (add-text-properties (match-beginning 0) (match-end 0)
  22350. (list :org-license-to-kill t)))
  22351. ;; Return the title string
  22352. (org-trim (match-string 0)))))))
  22353. (defun org-export-get-title-from-subtree ()
  22354. "Return subtree title and exclude it from export."
  22355. (let (title (m (mark)))
  22356. (save-excursion
  22357. (goto-char (region-beginning))
  22358. (when (and (org-at-heading-p)
  22359. (>= (org-end-of-subtree t t) (region-end)))
  22360. ;; This is a subtree, we take the title from the first heading
  22361. (goto-char (region-beginning))
  22362. (looking-at org-todo-line-regexp)
  22363. (setq title (match-string 3))
  22364. (org-unmodified
  22365. (add-text-properties (point) (1+ (point-at-eol))
  22366. (list :org-license-to-kill t)))))
  22367. title))
  22368. (defun org-solidify-link-text (s &optional alist)
  22369. "Take link text and make a safe target out of it."
  22370. (save-match-data
  22371. (let* ((rtn
  22372. (mapconcat
  22373. 'identity
  22374. (org-split-string s "[ \t\r\n]+") "--"))
  22375. (a (assoc rtn alist)))
  22376. (or (cdr a) rtn))))
  22377. (defun org-get-min-level (lines)
  22378. "Get the minimum level in LINES."
  22379. (let ((re "^\\(\\*+\\) ") l min)
  22380. (catch 'exit
  22381. (while (setq l (pop lines))
  22382. (if (string-match re l)
  22383. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22384. 1)))
  22385. ;; Variable holding the vector with section numbers
  22386. (defvar org-section-numbers (make-vector org-level-max 0))
  22387. (defun org-init-section-numbers ()
  22388. "Initialize the vector for the section numbers."
  22389. (let* ((level -1)
  22390. (numbers (nreverse (org-split-string "" "\\.")))
  22391. (depth (1- (length org-section-numbers)))
  22392. (i depth) number-string)
  22393. (while (>= i 0)
  22394. (if (> i level)
  22395. (aset org-section-numbers i 0)
  22396. (setq number-string (or (car numbers) "0"))
  22397. (if (string-match "\\`[A-Z]\\'" number-string)
  22398. (aset org-section-numbers i
  22399. (- (string-to-char number-string) ?A -1))
  22400. (aset org-section-numbers i (string-to-number number-string)))
  22401. (pop numbers))
  22402. (setq i (1- i)))))
  22403. (defun org-section-number (&optional level)
  22404. "Return a string with the current section number.
  22405. When LEVEL is non-nil, increase section numbers on that level."
  22406. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22407. (when level
  22408. (when (> level -1)
  22409. (aset org-section-numbers
  22410. level (1+ (aref org-section-numbers level))))
  22411. (setq idx (1+ level))
  22412. (while (<= idx depth)
  22413. (if (not (= idx 1))
  22414. (aset org-section-numbers idx 0))
  22415. (setq idx (1+ idx))))
  22416. (setq idx 0)
  22417. (while (<= idx depth)
  22418. (setq n (aref org-section-numbers idx))
  22419. (setq string (concat string (if (not (string= string "")) "." "")
  22420. (int-to-string n)))
  22421. (setq idx (1+ idx)))
  22422. (save-match-data
  22423. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22424. (setq string (replace-match "" t nil string)))
  22425. (if (string-match "\\(\\.0\\)+\\'" string)
  22426. (setq string (replace-match "" t nil string))))
  22427. string))
  22428. ;;; ASCII export
  22429. (defvar org-last-level nil) ; dynamically scoped variable
  22430. (defvar org-min-level nil) ; dynamically scoped variable
  22431. (defvar org-levels-open nil) ; dynamically scoped parameter
  22432. (defvar org-ascii-current-indentation nil) ; For communication
  22433. (defun org-export-as-ascii (arg)
  22434. "Export the outline as a pretty ASCII file.
  22435. If there is an active region, export only the region.
  22436. The prefix ARG specifies how many levels of the outline should become
  22437. underlined headlines. The default is 3."
  22438. (interactive "P")
  22439. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22440. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22441. (org-infile-export-plist)))
  22442. (region-p (org-region-active-p))
  22443. (subtree-p
  22444. (when region-p
  22445. (save-excursion
  22446. (goto-char (region-beginning))
  22447. (and (org-at-heading-p)
  22448. (>= (org-end-of-subtree t t) (region-end))))))
  22449. (custom-times org-display-custom-times)
  22450. (org-ascii-current-indentation '(0 . 0))
  22451. (level 0) line txt
  22452. (umax nil)
  22453. (umax-toc nil)
  22454. (case-fold-search nil)
  22455. (filename (concat (file-name-as-directory
  22456. (org-export-directory :ascii opt-plist))
  22457. (file-name-sans-extension
  22458. (or (and subtree-p
  22459. (org-entry-get (region-beginning)
  22460. "EXPORT_FILE_NAME" t))
  22461. (file-name-nondirectory buffer-file-name)))
  22462. ".txt"))
  22463. (filename (if (equal (file-truename filename)
  22464. (file-truename buffer-file-name))
  22465. (concat filename ".txt")
  22466. filename))
  22467. (buffer (find-file-noselect filename))
  22468. (org-levels-open (make-vector org-level-max nil))
  22469. (odd org-odd-levels-only)
  22470. (date (plist-get opt-plist :date))
  22471. (author (plist-get opt-plist :author))
  22472. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22473. (plist-get opt-plist :title)
  22474. (and (not
  22475. (plist-get opt-plist :skip-before-1st-heading))
  22476. (org-export-grab-title-from-buffer))
  22477. (file-name-sans-extension
  22478. (file-name-nondirectory buffer-file-name))))
  22479. (email (plist-get opt-plist :email))
  22480. (language (plist-get opt-plist :language))
  22481. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22482. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22483. (todo nil)
  22484. (lang-words nil)
  22485. (region
  22486. (buffer-substring
  22487. (if (org-region-active-p) (region-beginning) (point-min))
  22488. (if (org-region-active-p) (region-end) (point-max))))
  22489. (lines (org-split-string
  22490. (org-cleaned-string-for-export
  22491. region
  22492. :for-ascii t
  22493. :skip-before-1st-heading
  22494. (plist-get opt-plist :skip-before-1st-heading)
  22495. :drawers (plist-get opt-plist :drawers)
  22496. :verbatim-multiline t
  22497. :archived-trees
  22498. (plist-get opt-plist :archived-trees)
  22499. :add-text (plist-get opt-plist :text))
  22500. "\n"))
  22501. thetoc have-headings first-heading-pos
  22502. table-open table-buffer)
  22503. (let ((inhibit-read-only t))
  22504. (org-unmodified
  22505. (remove-text-properties (point-min) (point-max)
  22506. '(:org-license-to-kill t))))
  22507. (setq org-min-level (org-get-min-level lines))
  22508. (setq org-last-level org-min-level)
  22509. (org-init-section-numbers)
  22510. (find-file-noselect filename)
  22511. (setq lang-words (or (assoc language org-export-language-setup)
  22512. (assoc "en" org-export-language-setup)))
  22513. (switch-to-buffer-other-window buffer)
  22514. (erase-buffer)
  22515. (fundamental-mode)
  22516. ;; create local variables for all options, to make sure all called
  22517. ;; functions get the correct information
  22518. (mapc (lambda (x)
  22519. (set (make-local-variable (cdr x))
  22520. (plist-get opt-plist (car x))))
  22521. org-export-plist-vars)
  22522. (org-set-local 'org-odd-levels-only odd)
  22523. (setq umax (if arg (prefix-numeric-value arg)
  22524. org-export-headline-levels))
  22525. (setq umax-toc (if (integerp org-export-with-toc)
  22526. (min org-export-with-toc umax)
  22527. umax))
  22528. ;; File header
  22529. (if title (org-insert-centered title ?=))
  22530. (insert "\n")
  22531. (if (and (or author email)
  22532. org-export-author-info)
  22533. (insert (concat (nth 1 lang-words) ": " (or author "")
  22534. (if email (concat " <" email ">") "")
  22535. "\n")))
  22536. (cond
  22537. ((and date (string-match "%" date))
  22538. (setq date (format-time-string date (current-time))))
  22539. (date)
  22540. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22541. (if (and date org-export-time-stamp-file)
  22542. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22543. (insert "\n\n")
  22544. (if org-export-with-toc
  22545. (progn
  22546. (push (concat (nth 3 lang-words) "\n") thetoc)
  22547. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22548. (mapc '(lambda (line)
  22549. (if (string-match org-todo-line-regexp
  22550. line)
  22551. ;; This is a headline
  22552. (progn
  22553. (setq have-headings t)
  22554. (setq level (- (match-end 1) (match-beginning 1))
  22555. level (org-tr-level level)
  22556. txt (match-string 3 line)
  22557. todo
  22558. (or (and org-export-mark-todo-in-toc
  22559. (match-beginning 2)
  22560. (not (member (match-string 2 line)
  22561. org-done-keywords)))
  22562. ; TODO, not DONE
  22563. (and org-export-mark-todo-in-toc
  22564. (= level umax-toc)
  22565. (org-search-todo-below
  22566. line lines level))))
  22567. (setq txt (org-html-expand-for-ascii txt))
  22568. (while (string-match org-bracket-link-regexp txt)
  22569. (setq txt
  22570. (replace-match
  22571. (match-string (if (match-end 2) 3 1) txt)
  22572. t t txt)))
  22573. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22574. (string-match
  22575. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22576. txt))
  22577. (setq txt (replace-match "" t t txt)))
  22578. (if (string-match quote-re0 txt)
  22579. (setq txt (replace-match "" t t txt)))
  22580. (if org-export-with-section-numbers
  22581. (setq txt (concat (org-section-number level)
  22582. " " txt)))
  22583. (if (<= level umax-toc)
  22584. (progn
  22585. (push
  22586. (concat
  22587. (make-string
  22588. (* (max 0 (- level org-min-level)) 4) ?\ )
  22589. (format (if todo "%s (*)\n" "%s\n") txt))
  22590. thetoc)
  22591. (setq org-last-level level))
  22592. ))))
  22593. lines)
  22594. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22595. (org-init-section-numbers)
  22596. (while (setq line (pop lines))
  22597. ;; Remove the quoted HTML tags.
  22598. (setq line (org-html-expand-for-ascii line))
  22599. ;; Remove targets
  22600. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22601. (setq line (replace-match "" t t line)))
  22602. ;; Replace internal links
  22603. (while (string-match org-bracket-link-regexp line)
  22604. (setq line (replace-match
  22605. (if (match-end 3) "[\\3]" "[\\1]")
  22606. t nil line)))
  22607. (when custom-times
  22608. (setq line (org-translate-time line)))
  22609. (cond
  22610. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22611. ;; a Headline
  22612. (setq first-heading-pos (or first-heading-pos (point)))
  22613. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22614. txt (match-string 2 line))
  22615. (org-ascii-level-start level txt umax lines))
  22616. ((and org-export-with-tables
  22617. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22618. (if (not table-open)
  22619. ;; New table starts
  22620. (setq table-open t table-buffer nil))
  22621. ;; Accumulate lines
  22622. (setq table-buffer (cons line table-buffer))
  22623. (when (or (not lines)
  22624. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22625. (car lines))))
  22626. (setq table-open nil
  22627. table-buffer (nreverse table-buffer))
  22628. (insert (mapconcat
  22629. (lambda (x)
  22630. (org-fix-indentation x org-ascii-current-indentation))
  22631. (org-format-table-ascii table-buffer)
  22632. "\n") "\n")))
  22633. (t
  22634. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22635. (if (and org-export-with-fixed-width
  22636. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22637. (setq line (replace-match "\\1" nil nil line)))
  22638. (insert line "\n"))))
  22639. (normal-mode)
  22640. ;; insert the table of contents
  22641. (when thetoc
  22642. (goto-char (point-min))
  22643. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22644. (progn
  22645. (goto-char (match-beginning 0))
  22646. (replace-match ""))
  22647. (goto-char first-heading-pos))
  22648. (mapc 'insert thetoc)
  22649. (or (looking-at "[ \t]*\n[ \t]*\n")
  22650. (insert "\n\n")))
  22651. ;; Convert whitespace place holders
  22652. (goto-char (point-min))
  22653. (let (beg end)
  22654. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22655. (setq end (next-single-property-change beg 'org-whitespace))
  22656. (goto-char beg)
  22657. (delete-region beg end)
  22658. (insert (make-string (- end beg) ?\ ))))
  22659. (save-buffer)
  22660. ;; remove display and invisible chars
  22661. (let (beg end)
  22662. (goto-char (point-min))
  22663. (while (setq beg (next-single-property-change (point) 'display))
  22664. (setq end (next-single-property-change beg 'display))
  22665. (delete-region beg end)
  22666. (goto-char beg)
  22667. (insert "=>"))
  22668. (goto-char (point-min))
  22669. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22670. (setq end (next-single-property-change beg 'org-cwidth))
  22671. (delete-region beg end)
  22672. (goto-char beg)))
  22673. (goto-char (point-min))))
  22674. (defun org-export-ascii-clean-string ()
  22675. "Do extra work for ASCII export"
  22676. (goto-char (point-min))
  22677. (while (re-search-forward org-verbatim-re nil t)
  22678. (goto-char (match-end 2))
  22679. (backward-delete-char 1) (insert "'")
  22680. (goto-char (match-beginning 2))
  22681. (delete-char 1) (insert "`")
  22682. (goto-char (match-end 2))))
  22683. (defun org-search-todo-below (line lines level)
  22684. "Search the subtree below LINE for any TODO entries."
  22685. (let ((rest (cdr (memq line lines)))
  22686. (re org-todo-line-regexp)
  22687. line lv todo)
  22688. (catch 'exit
  22689. (while (setq line (pop rest))
  22690. (if (string-match re line)
  22691. (progn
  22692. (setq lv (- (match-end 1) (match-beginning 1))
  22693. todo (and (match-beginning 2)
  22694. (not (member (match-string 2 line)
  22695. org-done-keywords))))
  22696. ; TODO, not DONE
  22697. (if (<= lv level) (throw 'exit nil))
  22698. (if todo (throw 'exit t))))))))
  22699. (defun org-html-expand-for-ascii (line)
  22700. "Handle quoted HTML for ASCII export."
  22701. (if org-export-html-expand
  22702. (while (string-match "@<[^<>\n]*>" line)
  22703. ;; We just remove the tags for now.
  22704. (setq line (replace-match "" nil nil line))))
  22705. line)
  22706. (defun org-insert-centered (s &optional underline)
  22707. "Insert the string S centered and underline it with character UNDERLINE."
  22708. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22709. (insert (make-string ind ?\ ) s "\n")
  22710. (if underline
  22711. (insert (make-string ind ?\ )
  22712. (make-string (string-width s) underline)
  22713. "\n"))))
  22714. (defun org-ascii-level-start (level title umax &optional lines)
  22715. "Insert a new level in ASCII export."
  22716. (let (char (n (- level umax 1)) (ind 0))
  22717. (if (> level umax)
  22718. (progn
  22719. (insert (make-string (* 2 n) ?\ )
  22720. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22721. org-export-ascii-bullets))
  22722. " " title "\n")
  22723. ;; find the indentation of the next non-empty line
  22724. (catch 'stop
  22725. (while lines
  22726. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22727. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22728. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22729. (pop lines)))
  22730. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22731. (if (or (not (equal (char-before) ?\n))
  22732. (not (equal (char-before (1- (point))) ?\n)))
  22733. (insert "\n"))
  22734. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22735. (unless org-export-with-tags
  22736. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22737. (setq title (replace-match "" t t title))))
  22738. (if org-export-with-section-numbers
  22739. (setq title (concat (org-section-number level) " " title)))
  22740. (insert title "\n" (make-string (string-width title) char) "\n")
  22741. (setq org-ascii-current-indentation '(0 . 0)))))
  22742. (defun org-export-visible (type arg)
  22743. "Create a copy of the visible part of the current buffer, and export it.
  22744. The copy is created in a temporary buffer and removed after use.
  22745. TYPE is the final key (as a string) that also select the export command in
  22746. the `C-c C-e' export dispatcher.
  22747. As a special case, if the you type SPC at the prompt, the temporary
  22748. org-mode file will not be removed but presented to you so that you can
  22749. continue to use it. The prefix arg ARG is passed through to the exporting
  22750. command."
  22751. (interactive
  22752. (list (progn
  22753. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22754. (read-char-exclusive))
  22755. current-prefix-arg))
  22756. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22757. (error "Invalid export key"))
  22758. (let* ((binding (cdr (assoc type
  22759. '((?a . org-export-as-ascii)
  22760. (?\C-a . org-export-as-ascii)
  22761. (?b . org-export-as-html-and-open)
  22762. (?\C-b . org-export-as-html-and-open)
  22763. (?h . org-export-as-html)
  22764. (?H . org-export-as-html-to-buffer)
  22765. (?R . org-export-region-as-html)
  22766. (?x . org-export-as-xoxo)))))
  22767. (keepp (equal type ?\ ))
  22768. (file buffer-file-name)
  22769. (buffer (get-buffer-create "*Org Export Visible*"))
  22770. s e)
  22771. ;; Need to hack the drawers here.
  22772. (save-excursion
  22773. (goto-char (point-min))
  22774. (while (re-search-forward org-drawer-regexp nil t)
  22775. (goto-char (match-beginning 1))
  22776. (or (org-invisible-p) (org-flag-drawer nil))))
  22777. (with-current-buffer buffer (erase-buffer))
  22778. (save-excursion
  22779. (setq s (goto-char (point-min)))
  22780. (while (not (= (point) (point-max)))
  22781. (goto-char (org-find-invisible))
  22782. (append-to-buffer buffer s (point))
  22783. (setq s (goto-char (org-find-visible))))
  22784. (org-cycle-hide-drawers 'all)
  22785. (goto-char (point-min))
  22786. (unless keepp
  22787. ;; Copy all comment lines to the end, to make sure #+ settings are
  22788. ;; still available for the second export step. Kind of a hack, but
  22789. ;; does do the trick.
  22790. (if (looking-at "#[^\r\n]*")
  22791. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22792. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22793. (append-to-buffer buffer (1+ (match-beginning 0))
  22794. (min (point-max) (1+ (match-end 0))))))
  22795. (set-buffer buffer)
  22796. (let ((buffer-file-name file)
  22797. (org-inhibit-startup t))
  22798. (org-mode)
  22799. (show-all)
  22800. (unless keepp (funcall binding arg))))
  22801. (if (not keepp)
  22802. (kill-buffer buffer)
  22803. (switch-to-buffer-other-window buffer)
  22804. (goto-char (point-min)))))
  22805. (defun org-find-visible ()
  22806. (let ((s (point)))
  22807. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22808. (get-char-property s 'invisible)))
  22809. s))
  22810. (defun org-find-invisible ()
  22811. (let ((s (point)))
  22812. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22813. (not (get-char-property s 'invisible))))
  22814. s))
  22815. ;;; HTML export
  22816. (defun org-get-current-options ()
  22817. "Return a string with current options as keyword options.
  22818. Does include HTML export options as well as TODO and CATEGORY stuff."
  22819. (format
  22820. "#+TITLE: %s
  22821. #+AUTHOR: %s
  22822. #+EMAIL: %s
  22823. #+LANGUAGE: %s
  22824. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22825. #+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
  22826. #+CATEGORY: %s
  22827. #+SEQ_TODO: %s
  22828. #+TYP_TODO: %s
  22829. #+PRIORITIES: %c %c %c
  22830. #+DRAWERS: %s
  22831. #+STARTUP: %s %s %s %s %s
  22832. #+TAGS: %s
  22833. #+ARCHIVE: %s
  22834. #+LINK: %s
  22835. "
  22836. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22837. org-export-headline-levels
  22838. org-export-with-section-numbers
  22839. org-export-with-toc
  22840. org-export-preserve-breaks
  22841. org-export-html-expand
  22842. org-export-with-fixed-width
  22843. org-export-with-tables
  22844. org-export-with-sub-superscripts
  22845. org-export-with-special-strings
  22846. org-export-with-footnotes
  22847. org-export-with-emphasize
  22848. org-export-with-TeX-macros
  22849. org-export-with-LaTeX-fragments
  22850. org-export-skip-text-before-1st-heading
  22851. org-export-with-drawers
  22852. org-export-with-tags
  22853. (file-name-nondirectory buffer-file-name)
  22854. "TODO FEEDBACK VERIFY DONE"
  22855. "Me Jason Marie DONE"
  22856. org-highest-priority org-lowest-priority org-default-priority
  22857. (mapconcat 'identity org-drawers " ")
  22858. (cdr (assoc org-startup-folded
  22859. '((nil . "showall") (t . "overview") (content . "content"))))
  22860. (if org-odd-levels-only "odd" "oddeven")
  22861. (if org-hide-leading-stars "hidestars" "showstars")
  22862. (if org-startup-align-all-tables "align" "noalign")
  22863. (cond ((eq org-log-done t) "logdone")
  22864. ((equal org-log-done 'note) "lognotedone")
  22865. ((not org-log-done) "nologdone"))
  22866. (or (mapconcat (lambda (x)
  22867. (cond
  22868. ((equal '(:startgroup) x) "{")
  22869. ((equal '(:endgroup) x) "}")
  22870. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22871. (t (car x))))
  22872. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22873. org-archive-location
  22874. "org file:~/org/%s.org"
  22875. ))
  22876. (defun org-insert-export-options-template ()
  22877. "Insert into the buffer a template with information for exporting."
  22878. (interactive)
  22879. (if (not (bolp)) (newline))
  22880. (let ((s (org-get-current-options)))
  22881. (and (string-match "#\\+CATEGORY" s)
  22882. (setq s (substring s 0 (match-beginning 0))))
  22883. (insert s)))
  22884. (defun org-toggle-fixed-width-section (arg)
  22885. "Toggle the fixed-width export.
  22886. If there is no active region, the QUOTE keyword at the current headline is
  22887. inserted or removed. When present, it causes the text between this headline
  22888. and the next to be exported as fixed-width text, and unmodified.
  22889. If there is an active region, this command adds or removes a colon as the
  22890. first character of this line. If the first character of a line is a colon,
  22891. this line is also exported in fixed-width font."
  22892. (interactive "P")
  22893. (let* ((cc 0)
  22894. (regionp (org-region-active-p))
  22895. (beg (if regionp (region-beginning) (point)))
  22896. (end (if regionp (region-end)))
  22897. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22898. (case-fold-search nil)
  22899. (re "[ \t]*\\(:\\)")
  22900. off)
  22901. (if regionp
  22902. (save-excursion
  22903. (goto-char beg)
  22904. (setq cc (current-column))
  22905. (beginning-of-line 1)
  22906. (setq off (looking-at re))
  22907. (while (> nlines 0)
  22908. (setq nlines (1- nlines))
  22909. (beginning-of-line 1)
  22910. (cond
  22911. (arg
  22912. (move-to-column cc t)
  22913. (insert ":\n")
  22914. (forward-line -1))
  22915. ((and off (looking-at re))
  22916. (replace-match "" t t nil 1))
  22917. ((not off) (move-to-column cc t) (insert ":")))
  22918. (forward-line 1)))
  22919. (save-excursion
  22920. (org-back-to-heading)
  22921. (if (looking-at (concat outline-regexp
  22922. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22923. (replace-match "" t t nil 1)
  22924. (if (looking-at outline-regexp)
  22925. (progn
  22926. (goto-char (match-end 0))
  22927. (insert org-quote-string " "))))))))
  22928. (defun org-export-as-html-and-open (arg)
  22929. "Export the outline as HTML and immediately open it with a browser.
  22930. If there is an active region, export only the region.
  22931. The prefix ARG specifies how many levels of the outline should become
  22932. headlines. The default is 3. Lower levels will become bulleted lists."
  22933. (interactive "P")
  22934. (org-export-as-html arg 'hidden)
  22935. (org-open-file buffer-file-name))
  22936. (defun org-export-as-html-batch ()
  22937. "Call `org-export-as-html', may be used in batch processing as
  22938. emacs --batch
  22939. --load=$HOME/lib/emacs/org.el
  22940. --eval \"(setq org-export-headline-levels 2)\"
  22941. --visit=MyFile --funcall org-export-as-html-batch"
  22942. (org-export-as-html org-export-headline-levels 'hidden))
  22943. (defun org-export-as-html-to-buffer (arg)
  22944. "Call `org-exort-as-html` with output to a temporary buffer.
  22945. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22946. (interactive "P")
  22947. (org-export-as-html arg nil nil "*Org HTML Export*")
  22948. (switch-to-buffer-other-window "*Org HTML Export*"))
  22949. (defun org-replace-region-by-html (beg end)
  22950. "Assume the current region has org-mode syntax, and convert it to HTML.
  22951. This can be used in any buffer. For example, you could write an
  22952. itemized list in org-mode syntax in an HTML buffer and then use this
  22953. command to convert it."
  22954. (interactive "r")
  22955. (let (reg html buf pop-up-frames)
  22956. (save-window-excursion
  22957. (if (org-mode-p)
  22958. (setq html (org-export-region-as-html
  22959. beg end t 'string))
  22960. (setq reg (buffer-substring beg end)
  22961. buf (get-buffer-create "*Org tmp*"))
  22962. (with-current-buffer buf
  22963. (erase-buffer)
  22964. (insert reg)
  22965. (org-mode)
  22966. (setq html (org-export-region-as-html
  22967. (point-min) (point-max) t 'string)))
  22968. (kill-buffer buf)))
  22969. (delete-region beg end)
  22970. (insert html)))
  22971. (defun org-export-region-as-html (beg end &optional body-only buffer)
  22972. "Convert region from BEG to END in org-mode buffer to HTML.
  22973. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  22974. contents, and only produce the region of converted text, useful for
  22975. cut-and-paste operations.
  22976. If BUFFER is a buffer or a string, use/create that buffer as a target
  22977. of the converted HTML. If BUFFER is the symbol `string', return the
  22978. produced HTML as a string and leave not buffer behind. For example,
  22979. a Lisp program could call this function in the following way:
  22980. (setq html (org-export-region-as-html beg end t 'string))
  22981. When called interactively, the output buffer is selected, and shown
  22982. in a window. A non-interactive call will only retunr the buffer."
  22983. (interactive "r\nP")
  22984. (when (interactive-p)
  22985. (setq buffer "*Org HTML Export*"))
  22986. (let ((transient-mark-mode t) (zmacs-regions t)
  22987. rtn)
  22988. (goto-char end)
  22989. (set-mark (point)) ;; to activate the region
  22990. (goto-char beg)
  22991. (setq rtn (org-export-as-html
  22992. nil nil nil
  22993. buffer body-only))
  22994. (if (fboundp 'deactivate-mark) (deactivate-mark))
  22995. (if (and (interactive-p) (bufferp rtn))
  22996. (switch-to-buffer-other-window rtn)
  22997. rtn)))
  22998. (defvar html-table-tag nil) ; dynamically scoped into this.
  22999. (defun org-export-as-html (arg &optional hidden ext-plist
  23000. to-buffer body-only)
  23001. "Export the outline as a pretty HTML file.
  23002. If there is an active region, export only the region. The prefix
  23003. ARG specifies how many levels of the outline should become
  23004. headlines. The default is 3. Lower levels will become bulleted
  23005. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23006. EXT-PLIST is a property list with external parameters overriding
  23007. org-mode's default settings, but still inferior to file-local
  23008. settings. When TO-BUFFER is non-nil, create a buffer with that
  23009. name and export to that buffer. If TO-BUFFER is the symbol `string',
  23010. don't leave any buffer behind but just return the resulting HTML as
  23011. a string. When BODY-ONLY is set, don't produce the file header and footer,
  23012. simply return the content of <body>...</body>, without even
  23013. the body tags themselves."
  23014. (interactive "P")
  23015. ;; Make sure we have a file name when we need it.
  23016. (when (and (not (or to-buffer body-only))
  23017. (not buffer-file-name))
  23018. (if (buffer-base-buffer)
  23019. (org-set-local 'buffer-file-name
  23020. (with-current-buffer (buffer-base-buffer)
  23021. buffer-file-name))
  23022. (error "Need a file name to be able to export.")))
  23023. (message "Exporting...")
  23024. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23025. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23026. (setq-default org-done-keywords org-done-keywords)
  23027. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23028. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23029. ext-plist
  23030. (org-infile-export-plist)))
  23031. (style (plist-get opt-plist :style))
  23032. (html-extension (plist-get opt-plist :html-extension))
  23033. (link-validate (plist-get opt-plist :link-validation-function))
  23034. valid thetoc have-headings first-heading-pos
  23035. (odd org-odd-levels-only)
  23036. (region-p (org-region-active-p))
  23037. (subtree-p
  23038. (when region-p
  23039. (save-excursion
  23040. (goto-char (region-beginning))
  23041. (and (org-at-heading-p)
  23042. (>= (org-end-of-subtree t t) (region-end))))))
  23043. ;; The following two are dynamically scoped into other
  23044. ;; routines below.
  23045. (org-current-export-dir (org-export-directory :html opt-plist))
  23046. (org-current-export-file buffer-file-name)
  23047. (level 0) (line "") (origline "") txt todo
  23048. (umax nil)
  23049. (umax-toc nil)
  23050. (filename (if to-buffer nil
  23051. (expand-file-name
  23052. (concat
  23053. (file-name-sans-extension
  23054. (or (and subtree-p
  23055. (org-entry-get (region-beginning)
  23056. "EXPORT_FILE_NAME" t))
  23057. (file-name-nondirectory buffer-file-name)))
  23058. "." html-extension)
  23059. (file-name-as-directory
  23060. (org-export-directory :html opt-plist)))))
  23061. (current-dir (if buffer-file-name
  23062. (file-name-directory buffer-file-name)
  23063. default-directory))
  23064. (buffer (if to-buffer
  23065. (cond
  23066. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23067. (t (get-buffer-create to-buffer)))
  23068. (find-file-noselect filename)))
  23069. (org-levels-open (make-vector org-level-max nil))
  23070. (date (plist-get opt-plist :date))
  23071. (author (plist-get opt-plist :author))
  23072. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23073. (plist-get opt-plist :title)
  23074. (and (not
  23075. (plist-get opt-plist :skip-before-1st-heading))
  23076. (org-export-grab-title-from-buffer))
  23077. (and buffer-file-name
  23078. (file-name-sans-extension
  23079. (file-name-nondirectory buffer-file-name)))
  23080. "UNTITLED"))
  23081. (html-table-tag (plist-get opt-plist :html-table-tag))
  23082. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23083. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23084. (inquote nil)
  23085. (infixed nil)
  23086. (in-local-list nil)
  23087. (local-list-num nil)
  23088. (local-list-indent nil)
  23089. (llt org-plain-list-ordered-item-terminator)
  23090. (email (plist-get opt-plist :email))
  23091. (language (plist-get opt-plist :language))
  23092. (lang-words nil)
  23093. (target-alist nil) tg
  23094. (head-count 0) cnt
  23095. (start 0)
  23096. (coding-system (and (boundp 'buffer-file-coding-system)
  23097. buffer-file-coding-system))
  23098. (coding-system-for-write (or org-export-html-coding-system
  23099. coding-system))
  23100. (save-buffer-coding-system (or org-export-html-coding-system
  23101. coding-system))
  23102. (charset (and coding-system-for-write
  23103. (fboundp 'coding-system-get)
  23104. (coding-system-get coding-system-for-write
  23105. 'mime-charset)))
  23106. (region
  23107. (buffer-substring
  23108. (if region-p (region-beginning) (point-min))
  23109. (if region-p (region-end) (point-max))))
  23110. (lines
  23111. (org-split-string
  23112. (org-cleaned-string-for-export
  23113. region
  23114. :emph-multiline t
  23115. :for-html t
  23116. :skip-before-1st-heading
  23117. (plist-get opt-plist :skip-before-1st-heading)
  23118. :drawers (plist-get opt-plist :drawers)
  23119. :archived-trees
  23120. (plist-get opt-plist :archived-trees)
  23121. :add-text
  23122. (plist-get opt-plist :text)
  23123. :LaTeX-fragments
  23124. (plist-get opt-plist :LaTeX-fragments))
  23125. "[\r\n]"))
  23126. table-open type
  23127. table-buffer table-orig-buffer
  23128. ind start-is-num starter didclose
  23129. rpl path desc descp desc1 desc2 link
  23130. )
  23131. (let ((inhibit-read-only t))
  23132. (org-unmodified
  23133. (remove-text-properties (point-min) (point-max)
  23134. '(:org-license-to-kill t))))
  23135. (message "Exporting...")
  23136. (setq org-min-level (org-get-min-level lines))
  23137. (setq org-last-level org-min-level)
  23138. (org-init-section-numbers)
  23139. (cond
  23140. ((and date (string-match "%" date))
  23141. (setq date (format-time-string date (current-time))))
  23142. (date)
  23143. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23144. ;; Get the language-dependent settings
  23145. (setq lang-words (or (assoc language org-export-language-setup)
  23146. (assoc "en" org-export-language-setup)))
  23147. ;; Switch to the output buffer
  23148. (set-buffer buffer)
  23149. (let ((inhibit-read-only t)) (erase-buffer))
  23150. (fundamental-mode)
  23151. (and (fboundp 'set-buffer-file-coding-system)
  23152. (set-buffer-file-coding-system coding-system-for-write))
  23153. (let ((case-fold-search nil)
  23154. (org-odd-levels-only odd))
  23155. ;; create local variables for all options, to make sure all called
  23156. ;; functions get the correct information
  23157. (mapc (lambda (x)
  23158. (set (make-local-variable (cdr x))
  23159. (plist-get opt-plist (car x))))
  23160. org-export-plist-vars)
  23161. (setq umax (if arg (prefix-numeric-value arg)
  23162. org-export-headline-levels))
  23163. (setq umax-toc (if (integerp org-export-with-toc)
  23164. (min org-export-with-toc umax)
  23165. umax))
  23166. (unless body-only
  23167. ;; File header
  23168. (insert (format
  23169. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23170. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23171. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23172. lang=\"%s\" xml:lang=\"%s\">
  23173. <head>
  23174. <title>%s</title>
  23175. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23176. <meta name=\"generator\" content=\"Org-mode\"/>
  23177. <meta name=\"generated\" content=\"%s\"/>
  23178. <meta name=\"author\" content=\"%s\"/>
  23179. %s
  23180. </head><body>
  23181. "
  23182. language language (org-html-expand title)
  23183. (or charset "iso-8859-1") date author style))
  23184. (insert (or (plist-get opt-plist :preamble) ""))
  23185. (when (plist-get opt-plist :auto-preamble)
  23186. (if title (insert (format org-export-html-title-format
  23187. (org-html-expand title))))))
  23188. (if (and org-export-with-toc (not body-only))
  23189. (progn
  23190. (push (format "<h%d>%s</h%d>\n"
  23191. org-export-html-toplevel-hlevel
  23192. (nth 3 lang-words)
  23193. org-export-html-toplevel-hlevel)
  23194. thetoc)
  23195. (push "<ul>\n<li>" thetoc)
  23196. (setq lines
  23197. (mapcar '(lambda (line)
  23198. (if (string-match org-todo-line-regexp line)
  23199. ;; This is a headline
  23200. (progn
  23201. (setq have-headings t)
  23202. (setq level (- (match-end 1) (match-beginning 1))
  23203. level (org-tr-level level)
  23204. txt (save-match-data
  23205. (org-html-expand
  23206. (org-export-cleanup-toc-line
  23207. (match-string 3 line))))
  23208. todo
  23209. (or (and org-export-mark-todo-in-toc
  23210. (match-beginning 2)
  23211. (not (member (match-string 2 line)
  23212. org-done-keywords)))
  23213. ; TODO, not DONE
  23214. (and org-export-mark-todo-in-toc
  23215. (= level umax-toc)
  23216. (org-search-todo-below
  23217. line lines level))))
  23218. (if (string-match
  23219. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23220. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23221. (if (string-match quote-re0 txt)
  23222. (setq txt (replace-match "" t t txt)))
  23223. (if org-export-with-section-numbers
  23224. (setq txt (concat (org-section-number level)
  23225. " " txt)))
  23226. (if (<= level (max umax umax-toc))
  23227. (setq head-count (+ head-count 1)))
  23228. (if (<= level umax-toc)
  23229. (progn
  23230. (if (> level org-last-level)
  23231. (progn
  23232. (setq cnt (- level org-last-level))
  23233. (while (>= (setq cnt (1- cnt)) 0)
  23234. (push "\n<ul>\n<li>" thetoc))
  23235. (push "\n" thetoc)))
  23236. (if (< level org-last-level)
  23237. (progn
  23238. (setq cnt (- org-last-level level))
  23239. (while (>= (setq cnt (1- cnt)) 0)
  23240. (push "</li>\n</ul>" thetoc))
  23241. (push "\n" thetoc)))
  23242. ;; Check for targets
  23243. (while (string-match org-target-regexp line)
  23244. (setq tg (match-string 1 line)
  23245. line (replace-match
  23246. (concat "@<span class=\"target\">" tg "@</span> ")
  23247. t t line))
  23248. (push (cons (org-solidify-link-text tg)
  23249. (format "sec-%d" head-count))
  23250. target-alist))
  23251. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23252. (setq txt (replace-match "" t t txt)))
  23253. (push
  23254. (format
  23255. (if todo
  23256. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23257. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23258. head-count txt) thetoc)
  23259. (setq org-last-level level))
  23260. )))
  23261. line)
  23262. lines))
  23263. (while (> org-last-level (1- org-min-level))
  23264. (setq org-last-level (1- org-last-level))
  23265. (push "</li>\n</ul>\n" thetoc))
  23266. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23267. (setq head-count 0)
  23268. (org-init-section-numbers)
  23269. (while (setq line (pop lines) origline line)
  23270. (catch 'nextline
  23271. ;; end of quote section?
  23272. (when (and inquote (string-match "^\\*+ " line))
  23273. (insert "</pre>\n")
  23274. (setq inquote nil))
  23275. ;; inside a quote section?
  23276. (when inquote
  23277. (insert (org-html-protect line) "\n")
  23278. (throw 'nextline nil))
  23279. ;; verbatim lines
  23280. (when (and org-export-with-fixed-width
  23281. (string-match "^[ \t]*:\\(.*\\)" line))
  23282. (when (not infixed)
  23283. (setq infixed t)
  23284. (insert "<pre>\n"))
  23285. (insert (org-html-protect (match-string 1 line)) "\n")
  23286. (when (and lines
  23287. (not (string-match "^[ \t]*\\(:.*\\)"
  23288. (car lines))))
  23289. (setq infixed nil)
  23290. (insert "</pre>\n"))
  23291. (throw 'nextline nil))
  23292. ;; Protected HTML
  23293. (when (get-text-property 0 'org-protected line)
  23294. (let (par)
  23295. (when (re-search-backward
  23296. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23297. (setq par (match-string 1))
  23298. (replace-match "\\2\n"))
  23299. (insert line "\n")
  23300. (while (and lines
  23301. (or (= (length (car lines)) 0)
  23302. (get-text-property 0 'org-protected (car lines))))
  23303. (insert (pop lines) "\n"))
  23304. (and par (insert "<p>\n")))
  23305. (throw 'nextline nil))
  23306. ;; Horizontal line
  23307. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23308. (insert "\n<hr/>\n")
  23309. (throw 'nextline nil))
  23310. ;; make targets to anchors
  23311. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23312. (cond
  23313. ((match-end 2)
  23314. (setq line (replace-match
  23315. (concat "@<a name=\""
  23316. (org-solidify-link-text (match-string 1 line))
  23317. "\">\\nbsp@</a>")
  23318. t t line)))
  23319. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23320. (setq line (replace-match
  23321. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23322. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23323. t t line)))
  23324. (t
  23325. (setq line (replace-match
  23326. (concat "@<a name=\""
  23327. (org-solidify-link-text (match-string 1 line))
  23328. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23329. t t line)))))
  23330. (setq line (org-html-handle-time-stamps line))
  23331. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23332. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23333. ;; Also handle sub_superscripts and checkboxes
  23334. (or (string-match org-table-hline-regexp line)
  23335. (setq line (org-html-expand line)))
  23336. ;; Format the links
  23337. (setq start 0)
  23338. (while (string-match org-bracket-link-analytic-regexp line start)
  23339. (setq start (match-beginning 0))
  23340. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23341. (setq path (match-string 3 line))
  23342. (setq desc1 (if (match-end 5) (match-string 5 line))
  23343. desc2 (if (match-end 2) (concat type ":" path) path)
  23344. descp (and desc1 (not (equal desc1 desc2)))
  23345. desc (or desc1 desc2))
  23346. ;; Make an image out of the description if that is so wanted
  23347. (when (and descp (org-file-image-p desc))
  23348. (save-match-data
  23349. (if (string-match "^file:" desc)
  23350. (setq desc (substring desc (match-end 0)))))
  23351. (setq desc (concat "<img src=\"" desc "\"/>")))
  23352. ;; FIXME: do we need to unescape here somewhere?
  23353. (cond
  23354. ((equal type "internal")
  23355. (setq rpl
  23356. (concat
  23357. "<a href=\"#"
  23358. (org-solidify-link-text
  23359. (save-match-data (org-link-unescape path)) target-alist)
  23360. "\">" desc "</a>")))
  23361. ((member type '("http" "https"))
  23362. ;; standard URL, just check if we need to inline an image
  23363. (if (and (or (eq t org-export-html-inline-images)
  23364. (and org-export-html-inline-images (not descp)))
  23365. (org-file-image-p path))
  23366. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23367. (setq link (concat type ":" path))
  23368. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23369. ((member type '("ftp" "mailto" "news"))
  23370. ;; standard URL
  23371. (setq link (concat type ":" path))
  23372. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23373. ((string= type "file")
  23374. ;; FILE link
  23375. (let* ((filename path)
  23376. (abs-p (file-name-absolute-p filename))
  23377. thefile file-is-image-p search)
  23378. (save-match-data
  23379. (if (string-match "::\\(.*\\)" filename)
  23380. (setq search (match-string 1 filename)
  23381. filename (replace-match "" t nil filename)))
  23382. (setq valid
  23383. (if (functionp link-validate)
  23384. (funcall link-validate filename current-dir)
  23385. t))
  23386. (setq file-is-image-p (org-file-image-p filename))
  23387. (setq thefile (if abs-p (expand-file-name filename) filename))
  23388. (when (and org-export-html-link-org-files-as-html
  23389. (string-match "\\.org$" thefile))
  23390. (setq thefile (concat (substring thefile 0
  23391. (match-beginning 0))
  23392. "." html-extension))
  23393. (if (and search
  23394. ;; make sure this is can be used as target search
  23395. (not (string-match "^[0-9]*$" search))
  23396. (not (string-match "^\\*" search))
  23397. (not (string-match "^/.*/$" search)))
  23398. (setq thefile (concat thefile "#"
  23399. (org-solidify-link-text
  23400. (org-link-unescape search)))))
  23401. (when (string-match "^file:" desc)
  23402. (setq desc (replace-match "" t t desc))
  23403. (if (string-match "\\.org$" desc)
  23404. (setq desc (replace-match "" t t desc))))))
  23405. (setq rpl (if (and file-is-image-p
  23406. (or (eq t org-export-html-inline-images)
  23407. (and org-export-html-inline-images
  23408. (not descp))))
  23409. (concat "<img src=\"" thefile "\"/>")
  23410. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23411. (if (not valid) (setq rpl desc))))
  23412. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23413. (setq rpl (concat "<i>&lt;" type ":"
  23414. (save-match-data (org-link-unescape path))
  23415. "&gt;</i>"))))
  23416. (setq line (replace-match rpl t t line)
  23417. start (+ start (length rpl))))
  23418. ;; TODO items
  23419. (if (and (string-match org-todo-line-regexp line)
  23420. (match-beginning 2))
  23421. (setq line
  23422. (concat (substring line 0 (match-beginning 2))
  23423. "<span class=\""
  23424. (if (member (match-string 2 line)
  23425. org-done-keywords)
  23426. "done" "todo")
  23427. "\">" (match-string 2 line)
  23428. "</span>" (substring line (match-end 2)))))
  23429. ;; Does this contain a reference to a footnote?
  23430. (when org-export-with-footnotes
  23431. (setq start 0)
  23432. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23433. (if (get-text-property (match-beginning 2) 'org-protected line)
  23434. (setq start (match-end 2))
  23435. (let ((n (match-string 2 line)))
  23436. (setq line
  23437. (replace-match
  23438. (format
  23439. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23440. (match-string 1 line) n n n)
  23441. t t line))))))
  23442. (cond
  23443. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23444. ;; This is a headline
  23445. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23446. txt (match-string 2 line))
  23447. (if (string-match quote-re0 txt)
  23448. (setq txt (replace-match "" t t txt)))
  23449. (if (<= level (max umax umax-toc))
  23450. (setq head-count (+ head-count 1)))
  23451. (when in-local-list
  23452. ;; Close any local lists before inserting a new header line
  23453. (while local-list-num
  23454. (org-close-li)
  23455. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23456. (pop local-list-num))
  23457. (setq local-list-indent nil
  23458. in-local-list nil))
  23459. (setq first-heading-pos (or first-heading-pos (point)))
  23460. (org-html-level-start level txt umax
  23461. (and org-export-with-toc (<= level umax))
  23462. head-count)
  23463. ;; QUOTES
  23464. (when (string-match quote-re line)
  23465. (insert "<pre>")
  23466. (setq inquote t)))
  23467. ((and org-export-with-tables
  23468. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23469. (if (not table-open)
  23470. ;; New table starts
  23471. (setq table-open t table-buffer nil table-orig-buffer nil))
  23472. ;; Accumulate lines
  23473. (setq table-buffer (cons line table-buffer)
  23474. table-orig-buffer (cons origline table-orig-buffer))
  23475. (when (or (not lines)
  23476. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23477. (car lines))))
  23478. (setq table-open nil
  23479. table-buffer (nreverse table-buffer)
  23480. table-orig-buffer (nreverse table-orig-buffer))
  23481. (org-close-par-maybe)
  23482. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23483. (t
  23484. ;; Normal lines
  23485. (when (string-match
  23486. (cond
  23487. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23488. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23489. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23490. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23491. line)
  23492. (setq ind (org-get-string-indentation line)
  23493. start-is-num (match-beginning 4)
  23494. starter (if (match-beginning 2)
  23495. (substring (match-string 2 line) 0 -1))
  23496. line (substring line (match-beginning 5)))
  23497. (unless (string-match "[^ \t]" line)
  23498. ;; empty line. Pretend indentation is large.
  23499. (setq ind (if org-empty-line-terminates-plain-lists
  23500. 0
  23501. (1+ (or (car local-list-indent) 1)))))
  23502. (setq didclose nil)
  23503. (while (and in-local-list
  23504. (or (and (= ind (car local-list-indent))
  23505. (not starter))
  23506. (< ind (car local-list-indent))))
  23507. (setq didclose t)
  23508. (org-close-li)
  23509. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23510. (pop local-list-num) (pop local-list-indent)
  23511. (setq in-local-list local-list-indent))
  23512. (cond
  23513. ((and starter
  23514. (or (not in-local-list)
  23515. (> ind (car local-list-indent))))
  23516. ;; Start new (level of) list
  23517. (org-close-par-maybe)
  23518. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23519. (push start-is-num local-list-num)
  23520. (push ind local-list-indent)
  23521. (setq in-local-list t))
  23522. (starter
  23523. ;; continue current list
  23524. (org-close-li)
  23525. (insert "<li>\n"))
  23526. (didclose
  23527. ;; we did close a list, normal text follows: need <p>
  23528. (org-open-par)))
  23529. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23530. (setq line
  23531. (replace-match
  23532. (if (equal (match-string 1 line) "X")
  23533. "<b>[X]</b>"
  23534. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23535. t t line))))
  23536. ;; Empty lines start a new paragraph. If hand-formatted lists
  23537. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23538. ;; also start a new paragraph.
  23539. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23540. ;; Is this the start of a footnote?
  23541. (when org-export-with-footnotes
  23542. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23543. (org-close-par-maybe)
  23544. (let ((n (match-string 1 line)))
  23545. (setq line (replace-match
  23546. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23547. ;; Check if the line break needs to be conserved
  23548. (cond
  23549. ((string-match "\\\\\\\\[ \t]*$" line)
  23550. (setq line (replace-match "<br/>" t t line)))
  23551. (org-export-preserve-breaks
  23552. (setq line (concat line "<br/>"))))
  23553. (insert line "\n")))))
  23554. ;; Properly close all local lists and other lists
  23555. (when inquote (insert "</pre>\n"))
  23556. (when in-local-list
  23557. ;; Close any local lists before inserting a new header line
  23558. (while local-list-num
  23559. (org-close-li)
  23560. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23561. (pop local-list-num))
  23562. (setq local-list-indent nil
  23563. in-local-list nil))
  23564. (org-html-level-start 1 nil umax
  23565. (and org-export-with-toc (<= level umax))
  23566. head-count)
  23567. (unless body-only
  23568. (when (plist-get opt-plist :auto-postamble)
  23569. (insert "<div id=\"postamble\">")
  23570. (when (and org-export-author-info author)
  23571. (insert "<p class=\"author\"> "
  23572. (nth 1 lang-words) ": " author "\n")
  23573. (when email
  23574. (if (listp (split-string email ",+ *"))
  23575. (mapc (lambda(e)
  23576. (insert "<a href=\"mailto:" e "\">&lt;"
  23577. e "&gt;</a>\n"))
  23578. (split-string email ",+ *"))
  23579. (insert "<a href=\"mailto:" email "\">&lt;"
  23580. email "&gt;</a>\n")))
  23581. (insert "</p>\n"))
  23582. (when (and date org-export-time-stamp-file)
  23583. (insert "<p class=\"date\"> "
  23584. (nth 2 lang-words) ": "
  23585. date "</p>\n"))
  23586. (insert "</div>"))
  23587. (if org-export-html-with-timestamp
  23588. (insert org-export-html-html-helper-timestamp))
  23589. (insert (or (plist-get opt-plist :postamble) ""))
  23590. (insert "</body>\n</html>\n"))
  23591. (normal-mode)
  23592. (if (eq major-mode default-major-mode) (html-mode))
  23593. ;; insert the table of contents
  23594. (goto-char (point-min))
  23595. (when thetoc
  23596. (if (or (re-search-forward
  23597. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23598. (re-search-forward
  23599. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23600. (progn
  23601. (goto-char (match-beginning 0))
  23602. (replace-match ""))
  23603. (goto-char first-heading-pos)
  23604. (when (looking-at "\\s-*</p>")
  23605. (goto-char (match-end 0))
  23606. (insert "\n")))
  23607. (insert "<div id=\"table-of-contents\">\n")
  23608. (mapc 'insert thetoc)
  23609. (insert "</div>\n"))
  23610. ;; remove empty paragraphs and lists
  23611. (goto-char (point-min))
  23612. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23613. (replace-match ""))
  23614. (goto-char (point-min))
  23615. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23616. (replace-match ""))
  23617. (goto-char (point-min))
  23618. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23619. (replace-match ""))
  23620. ;; Convert whitespace place holders
  23621. (goto-char (point-min))
  23622. (let (beg end n)
  23623. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23624. (setq n (get-text-property beg 'org-whitespace)
  23625. end (next-single-property-change beg 'org-whitespace))
  23626. (goto-char beg)
  23627. (delete-region beg end)
  23628. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23629. (make-string n ?x)))))
  23630. (or to-buffer (save-buffer))
  23631. (goto-char (point-min))
  23632. (message "Exporting... done")
  23633. (if (eq to-buffer 'string)
  23634. (prog1 (buffer-substring (point-min) (point-max))
  23635. (kill-buffer (current-buffer)))
  23636. (current-buffer)))))
  23637. (defvar org-table-colgroup-info nil)
  23638. (defun org-format-table-ascii (lines)
  23639. "Format a table for ascii export."
  23640. (if (stringp lines)
  23641. (setq lines (org-split-string lines "\n")))
  23642. (if (not (string-match "^[ \t]*|" (car lines)))
  23643. ;; Table made by table.el - test for spanning
  23644. lines
  23645. ;; A normal org table
  23646. ;; Get rid of hlines at beginning and end
  23647. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23648. (setq lines (nreverse lines))
  23649. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23650. (setq lines (nreverse lines))
  23651. (when org-export-table-remove-special-lines
  23652. ;; Check if the table has a marking column. If yes remove the
  23653. ;; column and the special lines
  23654. (setq lines (org-table-clean-before-export lines)))
  23655. ;; Get rid of the vertical lines except for grouping
  23656. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23657. rtn line vl1 start)
  23658. (while (setq line (pop lines))
  23659. (if (string-match org-table-hline-regexp line)
  23660. (and (string-match "|\\(.*\\)|" line)
  23661. (setq line (replace-match " \\1" t nil line)))
  23662. (setq start 0 vl1 vl)
  23663. (while (string-match "|" line start)
  23664. (setq start (match-end 0))
  23665. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23666. (push line rtn))
  23667. (nreverse rtn))))
  23668. (defun org-colgroup-info-to-vline-list (info)
  23669. (let (vl new last)
  23670. (while info
  23671. (setq last new new (pop info))
  23672. (if (or (memq last '(:end :startend))
  23673. (memq new '(:start :startend)))
  23674. (push t vl)
  23675. (push nil vl)))
  23676. (setq vl (nreverse vl))
  23677. (and vl (setcar vl nil))
  23678. vl))
  23679. (defun org-format-table-html (lines olines)
  23680. "Find out which HTML converter to use and return the HTML code."
  23681. (if (stringp lines)
  23682. (setq lines (org-split-string lines "\n")))
  23683. (if (string-match "^[ \t]*|" (car lines))
  23684. ;; A normal org table
  23685. (org-format-org-table-html lines)
  23686. ;; Table made by table.el - test for spanning
  23687. (let* ((hlines (delq nil (mapcar
  23688. (lambda (x)
  23689. (if (string-match "^[ \t]*\\+-" x) x
  23690. nil))
  23691. lines)))
  23692. (first (car hlines))
  23693. (ll (and (string-match "\\S-+" first)
  23694. (match-string 0 first)))
  23695. (re (concat "^[ \t]*" (regexp-quote ll)))
  23696. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23697. hlines))))
  23698. (if (and (not spanning)
  23699. (not org-export-prefer-native-exporter-for-tables))
  23700. ;; We can use my own converter with HTML conversions
  23701. (org-format-table-table-html lines)
  23702. ;; Need to use the code generator in table.el, with the original text.
  23703. (org-format-table-table-html-using-table-generate-source olines)))))
  23704. (defun org-format-org-table-html (lines &optional splice)
  23705. "Format a table into HTML."
  23706. ;; Get rid of hlines at beginning and end
  23707. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23708. (setq lines (nreverse lines))
  23709. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23710. (setq lines (nreverse lines))
  23711. (when org-export-table-remove-special-lines
  23712. ;; Check if the table has a marking column. If yes remove the
  23713. ;; column and the special lines
  23714. (setq lines (org-table-clean-before-export lines)))
  23715. (let ((head (and org-export-highlight-first-table-line
  23716. (delq nil (mapcar
  23717. (lambda (x) (string-match "^[ \t]*|-" x))
  23718. (cdr lines)))))
  23719. (nlines 0) fnum i
  23720. tbopen line fields html gr colgropen)
  23721. (if splice (setq head nil))
  23722. (unless splice (push (if head "<thead>" "<tbody>") html))
  23723. (setq tbopen t)
  23724. (while (setq line (pop lines))
  23725. (catch 'next-line
  23726. (if (string-match "^[ \t]*|-" line)
  23727. (progn
  23728. (unless splice
  23729. (push (if head "</thead>" "</tbody>") html)
  23730. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23731. (setq head nil) ;; head ends here, first time around
  23732. ;; ignore this line
  23733. (throw 'next-line t)))
  23734. ;; Break the line into fields
  23735. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23736. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23737. (setq nlines (1+ nlines) i -1)
  23738. (push (concat "<tr>"
  23739. (mapconcat
  23740. (lambda (x)
  23741. (setq i (1+ i))
  23742. (if (and (< i nlines)
  23743. (string-match org-table-number-regexp x))
  23744. (incf (aref fnum i)))
  23745. (if head
  23746. (concat (car org-export-table-header-tags) x
  23747. (cdr org-export-table-header-tags))
  23748. (concat (car org-export-table-data-tags) x
  23749. (cdr org-export-table-data-tags))))
  23750. fields "")
  23751. "</tr>")
  23752. html)))
  23753. (unless splice (if tbopen (push "</tbody>" html)))
  23754. (unless splice (push "</table>\n" html))
  23755. (setq html (nreverse html))
  23756. (unless splice
  23757. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23758. (push (mapconcat
  23759. (lambda (x)
  23760. (setq gr (pop org-table-colgroup-info))
  23761. (format "%s<col align=\"%s\"></col>%s"
  23762. (if (memq gr '(:start :startend))
  23763. (prog1
  23764. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23765. (setq colgropen t))
  23766. "")
  23767. (if (> (/ (float x) nlines) org-table-number-fraction)
  23768. "right" "left")
  23769. (if (memq gr '(:end :startend))
  23770. (progn (setq colgropen nil) "</colgroup>")
  23771. "")))
  23772. fnum "")
  23773. html)
  23774. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23775. (push html-table-tag html))
  23776. (concat (mapconcat 'identity html "\n") "\n")))
  23777. (defun org-table-clean-before-export (lines)
  23778. "Check if the table has a marking column.
  23779. If yes remove the column and the special lines."
  23780. (setq org-table-colgroup-info nil)
  23781. (if (memq nil
  23782. (mapcar
  23783. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23784. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23785. lines))
  23786. (progn
  23787. (setq org-table-clean-did-remove-column nil)
  23788. (delq nil
  23789. (mapcar
  23790. (lambda (x)
  23791. (cond
  23792. ((string-match "^[ \t]*| */ *|" x)
  23793. (setq org-table-colgroup-info
  23794. (mapcar (lambda (x)
  23795. (cond ((member x '("<" "&lt;")) :start)
  23796. ((member x '(">" "&gt;")) :end)
  23797. ((member x '("<>" "&lt;&gt;")) :startend)
  23798. (t nil)))
  23799. (org-split-string x "[ \t]*|[ \t]*")))
  23800. nil)
  23801. (t x)))
  23802. lines)))
  23803. (setq org-table-clean-did-remove-column t)
  23804. (delq nil
  23805. (mapcar
  23806. (lambda (x)
  23807. (cond
  23808. ((string-match "^[ \t]*| */ *|" x)
  23809. (setq org-table-colgroup-info
  23810. (mapcar (lambda (x)
  23811. (cond ((member x '("<" "&lt;")) :start)
  23812. ((member x '(">" "&gt;")) :end)
  23813. ((member x '("<>" "&lt;&gt;")) :startend)
  23814. (t nil)))
  23815. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23816. nil)
  23817. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23818. nil) ; ignore this line
  23819. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23820. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23821. ;; remove the first column
  23822. (replace-match "\\1|" t nil x))))
  23823. lines))))
  23824. (defun org-format-table-table-html (lines)
  23825. "Format a table generated by table.el into HTML.
  23826. This conversion does *not* use `table-generate-source' from table.el.
  23827. This has the advantage that Org-mode's HTML conversions can be used.
  23828. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23829. (let (line field-buffer
  23830. (head org-export-highlight-first-table-line)
  23831. fields html empty)
  23832. (setq html (concat html-table-tag "\n"))
  23833. (while (setq line (pop lines))
  23834. (setq empty "&nbsp;")
  23835. (catch 'next-line
  23836. (if (string-match "^[ \t]*\\+-" line)
  23837. (progn
  23838. (if field-buffer
  23839. (progn
  23840. (setq
  23841. html
  23842. (concat
  23843. html
  23844. "<tr>"
  23845. (mapconcat
  23846. (lambda (x)
  23847. (if (equal x "") (setq x empty))
  23848. (if head
  23849. (concat (car org-export-table-header-tags) x
  23850. (cdr org-export-table-header-tags))
  23851. (concat (car org-export-table-data-tags) x
  23852. (cdr org-export-table-data-tags))))
  23853. field-buffer "\n")
  23854. "</tr>\n"))
  23855. (setq head nil)
  23856. (setq field-buffer nil)))
  23857. ;; Ignore this line
  23858. (throw 'next-line t)))
  23859. ;; Break the line into fields and store the fields
  23860. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23861. (if field-buffer
  23862. (setq field-buffer (mapcar
  23863. (lambda (x)
  23864. (concat x "<br/>" (pop fields)))
  23865. field-buffer))
  23866. (setq field-buffer fields))))
  23867. (setq html (concat html "</table>\n"))
  23868. html))
  23869. (defun org-format-table-table-html-using-table-generate-source (lines)
  23870. "Format a table into html, using `table-generate-source' from table.el.
  23871. This has the advantage that cell- or row-spanning is allowed.
  23872. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23873. (require 'table)
  23874. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23875. (erase-buffer)
  23876. (insert (mapconcat 'identity lines "\n"))
  23877. (goto-char (point-min))
  23878. (if (not (re-search-forward "|[^+]" nil t))
  23879. (error "Error processing table"))
  23880. (table-recognize-table)
  23881. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23882. (table-generate-source 'html " org-tmp2 ")
  23883. (set-buffer " org-tmp2 ")
  23884. (buffer-substring (point-min) (point-max))))
  23885. (defun org-html-handle-time-stamps (s)
  23886. "Format time stamps in string S, or remove them."
  23887. (catch 'exit
  23888. (let (r b)
  23889. (while (string-match org-maybe-keyword-time-regexp s)
  23890. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23891. ;; never export CLOCK
  23892. (throw 'exit ""))
  23893. (or b (setq b (substring s 0 (match-beginning 0))))
  23894. (if (not org-export-with-timestamps)
  23895. (setq r (concat r (substring s 0 (match-beginning 0)))
  23896. s (substring s (match-end 0)))
  23897. (setq r (concat
  23898. r (substring s 0 (match-beginning 0))
  23899. (if (match-end 1)
  23900. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23901. (match-string 1 s)))
  23902. (format " @<span class=\"timestamp\">%s@</span>"
  23903. (substring
  23904. (org-translate-time (match-string 3 s)) 1 -1)))
  23905. s (substring s (match-end 0)))))
  23906. ;; Line break if line started and ended with time stamp stuff
  23907. (if (not r)
  23908. s
  23909. (setq r (concat r s))
  23910. (unless (string-match "\\S-" (concat b s))
  23911. (setq r (concat r "@<br/>")))
  23912. r))))
  23913. (defun org-html-protect (s)
  23914. ;; convert & to &amp;, < to &lt; and > to &gt;
  23915. (let ((start 0))
  23916. (while (string-match "&" s start)
  23917. (setq s (replace-match "&amp;" t t s)
  23918. start (1+ (match-beginning 0))))
  23919. (while (string-match "<" s)
  23920. (setq s (replace-match "&lt;" t t s)))
  23921. (while (string-match ">" s)
  23922. (setq s (replace-match "&gt;" t t s))))
  23923. s)
  23924. (defun org-export-cleanup-toc-line (s)
  23925. "Remove tags and time staps from lines going into the toc."
  23926. (when (memq org-export-with-tags '(not-in-toc nil))
  23927. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23928. (setq s (replace-match "" t t s))))
  23929. (when org-export-remove-timestamps-from-toc
  23930. (while (string-match org-maybe-keyword-time-regexp s)
  23931. (setq s (replace-match "" t t s))))
  23932. (while (string-match org-bracket-link-regexp s)
  23933. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23934. t t s)))
  23935. s)
  23936. (defun org-html-expand (string)
  23937. "Prepare STRING for HTML export. Applies all active conversions.
  23938. If there are links in the string, don't modify these."
  23939. (let* ((re (concat org-bracket-link-regexp "\\|"
  23940. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23941. m s l res)
  23942. (while (setq m (string-match re string))
  23943. (setq s (substring string 0 m)
  23944. l (match-string 0 string)
  23945. string (substring string (match-end 0)))
  23946. (push (org-html-do-expand s) res)
  23947. (push l res))
  23948. (push (org-html-do-expand string) res)
  23949. (apply 'concat (nreverse res))))
  23950. (defun org-html-do-expand (s)
  23951. "Apply all active conversions to translate special ASCII to HTML."
  23952. (setq s (org-html-protect s))
  23953. (if org-export-html-expand
  23954. (let ((start 0))
  23955. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23956. (setq s (replace-match "<\\1>" t nil s)))))
  23957. (if org-export-with-emphasize
  23958. (setq s (org-export-html-convert-emphasize s)))
  23959. (if org-export-with-special-strings
  23960. (setq s (org-export-html-convert-special-strings s)))
  23961. (if org-export-with-sub-superscripts
  23962. (setq s (org-export-html-convert-sub-super s)))
  23963. (if org-export-with-TeX-macros
  23964. (let ((start 0) wd ass)
  23965. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  23966. (if (get-text-property (match-beginning 0) 'org-protected s)
  23967. (setq start (match-end 0))
  23968. (setq wd (match-string 1 s))
  23969. (if (setq ass (assoc wd org-html-entities))
  23970. (setq s (replace-match (or (cdr ass)
  23971. (concat "&" (car ass) ";"))
  23972. t t s))
  23973. (setq start (+ start (length wd))))))))
  23974. s)
  23975. (defun org-create-multibrace-regexp (left right n)
  23976. "Create a regular expression which will match a balanced sexp.
  23977. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  23978. as single character strings.
  23979. The regexp returned will match the entire expression including the
  23980. delimiters. It will also define a single group which contains the
  23981. match except for the outermost delimiters. The maximum depth of
  23982. stacked delimiters is N. Escaping delimiters is not possible."
  23983. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  23984. (or "\\|")
  23985. (re nothing)
  23986. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  23987. (while (> n 1)
  23988. (setq n (1- n)
  23989. re (concat re or next)
  23990. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  23991. (concat left "\\(" re "\\)" right)))
  23992. (defvar org-match-substring-regexp
  23993. (concat
  23994. "\\([^\\]\\)\\([_^]\\)\\("
  23995. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23996. "\\|"
  23997. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  23998. "\\|"
  23999. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24000. "The regular expression matching a sub- or superscript.")
  24001. (defvar org-match-substring-with-braces-regexp
  24002. (concat
  24003. "\\([^\\]\\)\\([_^]\\)\\("
  24004. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24005. "\\)")
  24006. "The regular expression matching a sub- or superscript, forcing braces.")
  24007. (defconst org-export-html-special-string-regexps
  24008. '(("\\\\-" . "&shy;")
  24009. ("---\\([^-]\\)" . "&mdash;\\1")
  24010. ("--\\([^-]\\)" . "&ndash;\\1")
  24011. ("\\.\\.\\." . "&hellip;"))
  24012. "Regular expressions for special string conversion.")
  24013. (defun org-export-html-convert-special-strings (string)
  24014. "Convert special characters in STRING to HTML."
  24015. (let ((all org-export-html-special-string-regexps)
  24016. e a re rpl start)
  24017. (while (setq a (pop all))
  24018. (setq re (car a) rpl (cdr a) start 0)
  24019. (while (string-match re string start)
  24020. (if (get-text-property (match-beginning 0) 'org-protected string)
  24021. (setq start (match-end 0))
  24022. (setq string (replace-match rpl t nil string)))))
  24023. string))
  24024. (defun org-export-html-convert-sub-super (string)
  24025. "Convert sub- and superscripts in STRING to HTML."
  24026. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24027. (while (string-match org-match-substring-regexp string s)
  24028. (cond
  24029. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24030. ((get-text-property (match-beginning 2) 'org-protected string)
  24031. (setq s (match-end 2)))
  24032. (t
  24033. (setq s (match-end 1)
  24034. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24035. c (or (match-string 8 string)
  24036. (match-string 6 string)
  24037. (match-string 5 string))
  24038. string (replace-match
  24039. (concat (match-string 1 string)
  24040. "<" key ">" c "</" key ">")
  24041. t t string)))))
  24042. (while (string-match "\\\\\\([_^]\\)" string)
  24043. (setq string (replace-match (match-string 1 string) t t string)))
  24044. string))
  24045. (defun org-export-html-convert-emphasize (string)
  24046. "Apply emphasis."
  24047. (let ((s 0) rpl)
  24048. (while (string-match org-emph-re string s)
  24049. (if (not (equal
  24050. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24051. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24052. (setq s (match-beginning 0)
  24053. rpl
  24054. (concat
  24055. (match-string 1 string)
  24056. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24057. (match-string 4 string)
  24058. (nth 3 (assoc (match-string 3 string)
  24059. org-emphasis-alist))
  24060. (match-string 5 string))
  24061. string (replace-match rpl t t string)
  24062. s (+ s (- (length rpl) 2)))
  24063. (setq s (1+ s))))
  24064. string))
  24065. (defvar org-par-open nil)
  24066. (defun org-open-par ()
  24067. "Insert <p>, but first close previous paragraph if any."
  24068. (org-close-par-maybe)
  24069. (insert "\n<p>")
  24070. (setq org-par-open t))
  24071. (defun org-close-par-maybe ()
  24072. "Close paragraph if there is one open."
  24073. (when org-par-open
  24074. (insert "</p>")
  24075. (setq org-par-open nil)))
  24076. (defun org-close-li ()
  24077. "Close <li> if necessary."
  24078. (org-close-par-maybe)
  24079. (insert "</li>\n"))
  24080. (defvar body-only) ; dynamically scoped into this.
  24081. (defun org-html-level-start (level title umax with-toc head-count)
  24082. "Insert a new level in HTML export.
  24083. When TITLE is nil, just close all open levels."
  24084. (org-close-par-maybe)
  24085. (let ((l org-level-max))
  24086. (while (>= l level)
  24087. (if (aref org-levels-open (1- l))
  24088. (progn
  24089. (org-html-level-close l umax)
  24090. (aset org-levels-open (1- l) nil)))
  24091. (setq l (1- l)))
  24092. (when title
  24093. ;; If title is nil, this means this function is called to close
  24094. ;; all levels, so the rest is done only if title is given
  24095. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24096. (setq title (replace-match
  24097. (if org-export-with-tags
  24098. (save-match-data
  24099. (concat
  24100. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24101. (mapconcat 'identity (org-split-string
  24102. (match-string 1 title) ":")
  24103. "&nbsp;")
  24104. "</span>"))
  24105. "")
  24106. t t title)))
  24107. (if (> level umax)
  24108. (progn
  24109. (if (aref org-levels-open (1- level))
  24110. (progn
  24111. (org-close-li)
  24112. (insert "<li>" title "<br/>\n"))
  24113. (aset org-levels-open (1- level) t)
  24114. (org-close-par-maybe)
  24115. (insert "<ul>\n<li>" title "<br/>\n")))
  24116. (aset org-levels-open (1- level) t)
  24117. (if (and org-export-with-section-numbers (not body-only))
  24118. (setq title (concat (org-section-number level) " " title)))
  24119. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24120. (if with-toc
  24121. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24122. level level head-count title level))
  24123. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24124. (org-open-par)))))
  24125. (defun org-html-level-close (level max-outline-level)
  24126. "Terminate one level in HTML export."
  24127. (if (<= level max-outline-level)
  24128. (insert "</div>\n")
  24129. (org-close-li)
  24130. (insert "</ul>\n")))
  24131. ;;; iCalendar export
  24132. ;;;###autoload
  24133. (defun org-export-icalendar-this-file ()
  24134. "Export current file as an iCalendar file.
  24135. The iCalendar file will be located in the same directory as the Org-mode
  24136. file, but with extension `.ics'."
  24137. (interactive)
  24138. (org-export-icalendar nil buffer-file-name))
  24139. ;;;###autoload
  24140. (defun org-export-icalendar-all-agenda-files ()
  24141. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24142. Each iCalendar file will be located in the same directory as the Org-mode
  24143. file, but with extension `.ics'."
  24144. (interactive)
  24145. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24146. ;;;###autoload
  24147. (defun org-export-icalendar-combine-agenda-files ()
  24148. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24149. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24150. (interactive)
  24151. (apply 'org-export-icalendar t (org-agenda-files t)))
  24152. (defun org-export-icalendar (combine &rest files)
  24153. "Create iCalendar files for all elements of FILES.
  24154. If COMBINE is non-nil, combine all calendar entries into a single large
  24155. file and store it under the name `org-combined-agenda-icalendar-file'."
  24156. (save-excursion
  24157. (org-prepare-agenda-buffers files)
  24158. (let* ((dir (org-export-directory
  24159. :ical (list :publishing-directory
  24160. org-export-publishing-directory)))
  24161. file ical-file ical-buffer category started org-agenda-new-buffers)
  24162. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24163. (when combine
  24164. (setq ical-file
  24165. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24166. org-combined-agenda-icalendar-file
  24167. (expand-file-name org-combined-agenda-icalendar-file dir))
  24168. ical-buffer (org-get-agenda-file-buffer ical-file))
  24169. (set-buffer ical-buffer) (erase-buffer))
  24170. (while (setq file (pop files))
  24171. (catch 'nextfile
  24172. (org-check-agenda-file file)
  24173. (set-buffer (org-get-agenda-file-buffer file))
  24174. (unless combine
  24175. (setq ical-file (concat (file-name-as-directory dir)
  24176. (file-name-sans-extension
  24177. (file-name-nondirectory buffer-file-name))
  24178. ".ics"))
  24179. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24180. (with-current-buffer ical-buffer (erase-buffer)))
  24181. (setq category (or org-category
  24182. (file-name-sans-extension
  24183. (file-name-nondirectory buffer-file-name))))
  24184. (if (symbolp category) (setq category (symbol-name category)))
  24185. (let ((standard-output ical-buffer))
  24186. (if combine
  24187. (and (not started) (setq started t)
  24188. (org-start-icalendar-file org-icalendar-combined-name))
  24189. (org-start-icalendar-file category))
  24190. (org-print-icalendar-entries combine)
  24191. (when (or (and combine (not files)) (not combine))
  24192. (org-finish-icalendar-file)
  24193. (set-buffer ical-buffer)
  24194. (save-buffer)
  24195. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24196. (org-release-buffers org-agenda-new-buffers))))
  24197. (defvar org-after-save-iCalendar-file-hook nil
  24198. "Hook run after an iCalendar file has been saved.
  24199. The iCalendar buffer is still current when this hook is run.
  24200. A good way to use this is to tell a desktop calenndar application to re-read
  24201. the iCalendar file.")
  24202. (defun org-print-icalendar-entries (&optional combine)
  24203. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24204. When COMBINE is non nil, add the category to each line."
  24205. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24206. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24207. (dts (org-ical-ts-to-string
  24208. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24209. "DTSTART"))
  24210. hd ts ts2 state status (inc t) pos b sexp rrule
  24211. scheduledp deadlinep tmp pri category entry location summary desc
  24212. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24213. (org-refresh-category-properties)
  24214. (save-excursion
  24215. (goto-char (point-min))
  24216. (while (re-search-forward re1 nil t)
  24217. (catch :skip
  24218. (org-agenda-skip)
  24219. (setq pos (match-beginning 0)
  24220. ts (match-string 0)
  24221. inc t
  24222. hd (org-get-heading)
  24223. summary (org-icalendar-cleanup-string
  24224. (org-entry-get nil "SUMMARY"))
  24225. desc (org-icalendar-cleanup-string
  24226. (or (org-entry-get nil "DESCRIPTION")
  24227. (and org-icalendar-include-body (org-get-entry)))
  24228. t org-icalendar-include-body)
  24229. location (org-icalendar-cleanup-string
  24230. (org-entry-get nil "LOCATION"))
  24231. category (org-get-category))
  24232. (if (looking-at re2)
  24233. (progn
  24234. (goto-char (match-end 0))
  24235. (setq ts2 (match-string 1) inc nil))
  24236. (setq tmp (buffer-substring (max (point-min)
  24237. (- pos org-ds-keyword-length))
  24238. pos)
  24239. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24240. (progn
  24241. (setq inc nil)
  24242. (replace-match "\\1" t nil ts))
  24243. ts)
  24244. deadlinep (string-match org-deadline-regexp tmp)
  24245. scheduledp (string-match org-scheduled-regexp tmp)
  24246. ;; donep (org-entry-is-done-p)
  24247. ))
  24248. (if (or (string-match org-tr-regexp hd)
  24249. (string-match org-ts-regexp hd))
  24250. (setq hd (replace-match "" t t hd)))
  24251. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24252. (setq rrule
  24253. (concat "\nRRULE:FREQ="
  24254. (cdr (assoc
  24255. (match-string 2 ts)
  24256. '(("d" . "DAILY")("w" . "WEEKLY")
  24257. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24258. ";INTERVAL=" (match-string 1 ts)))
  24259. (setq rrule ""))
  24260. (setq summary (or summary hd))
  24261. (if (string-match org-bracket-link-regexp summary)
  24262. (setq summary
  24263. (replace-match (if (match-end 3)
  24264. (match-string 3 summary)
  24265. (match-string 1 summary))
  24266. t t summary)))
  24267. (if deadlinep (setq summary (concat "DL: " summary)))
  24268. (if scheduledp (setq summary (concat "S: " summary)))
  24269. (if (string-match "\\`<%%" ts)
  24270. (with-current-buffer sexp-buffer
  24271. (insert (substring ts 1 -1) " " summary "\n"))
  24272. (princ (format "BEGIN:VEVENT
  24273. %s
  24274. %s%s
  24275. SUMMARY:%s%s%s
  24276. CATEGORIES:%s
  24277. END:VEVENT\n"
  24278. (org-ical-ts-to-string ts "DTSTART")
  24279. (org-ical-ts-to-string ts2 "DTEND" inc)
  24280. rrule summary
  24281. (if (and desc (string-match "\\S-" desc))
  24282. (concat "\nDESCRIPTION: " desc) "")
  24283. (if (and location (string-match "\\S-" location))
  24284. (concat "\nLOCATION: " location) "")
  24285. category)))))
  24286. (when (and org-icalendar-include-sexps
  24287. (condition-case nil (require 'icalendar) (error nil))
  24288. (fboundp 'icalendar-export-region))
  24289. ;; Get all the literal sexps
  24290. (goto-char (point-min))
  24291. (while (re-search-forward "^&?%%(" nil t)
  24292. (catch :skip
  24293. (org-agenda-skip)
  24294. (setq b (match-beginning 0))
  24295. (goto-char (1- (match-end 0)))
  24296. (forward-sexp 1)
  24297. (end-of-line 1)
  24298. (setq sexp (buffer-substring b (point)))
  24299. (with-current-buffer sexp-buffer
  24300. (insert sexp "\n"))
  24301. (princ (org-diary-to-ical-string sexp-buffer)))))
  24302. (when org-icalendar-include-todo
  24303. (goto-char (point-min))
  24304. (while (re-search-forward org-todo-line-regexp nil t)
  24305. (catch :skip
  24306. (org-agenda-skip)
  24307. (setq state (match-string 2))
  24308. (setq status (if (member state org-done-keywords)
  24309. "COMPLETED" "NEEDS-ACTION"))
  24310. (when (and state
  24311. (or (not (member state org-done-keywords))
  24312. (eq org-icalendar-include-todo 'all))
  24313. (not (member org-archive-tag (org-get-tags-at)))
  24314. )
  24315. (setq hd (match-string 3)
  24316. summary (org-icalendar-cleanup-string
  24317. (org-entry-get nil "SUMMARY"))
  24318. desc (org-icalendar-cleanup-string
  24319. (or (org-entry-get nil "DESCRIPTION")
  24320. (and org-icalendar-include-body (org-get-entry)))
  24321. t org-icalendar-include-body)
  24322. location (org-icalendar-cleanup-string
  24323. (org-entry-get nil "LOCATION")))
  24324. (if (string-match org-bracket-link-regexp hd)
  24325. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24326. (match-string 1 hd))
  24327. t t hd)))
  24328. (if (string-match org-priority-regexp hd)
  24329. (setq pri (string-to-char (match-string 2 hd))
  24330. hd (concat (substring hd 0 (match-beginning 1))
  24331. (substring hd (match-end 1))))
  24332. (setq pri org-default-priority))
  24333. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24334. (- org-lowest-priority org-highest-priority))))))
  24335. (princ (format "BEGIN:VTODO
  24336. %s
  24337. SUMMARY:%s%s%s
  24338. CATEGORIES:%s
  24339. SEQUENCE:1
  24340. PRIORITY:%d
  24341. STATUS:%s
  24342. END:VTODO\n"
  24343. dts
  24344. (or summary hd)
  24345. (if (and location (string-match "\\S-" location))
  24346. (concat "\nLOCATION: " location) "")
  24347. (if (and desc (string-match "\\S-" desc))
  24348. (concat "\nDESCRIPTION: " desc) "")
  24349. category pri status)))))))))
  24350. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24351. "Take out stuff and quote what needs to be quoted.
  24352. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24353. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24354. characters."
  24355. (if (not s)
  24356. nil
  24357. (when is-body
  24358. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24359. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24360. (while (string-match re s) (setq s (replace-match "" t t s)))
  24361. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24362. (let ((start 0))
  24363. (while (string-match "\\([,;\\]\\)" s start)
  24364. (setq start (+ (match-beginning 0) 2)
  24365. s (replace-match "\\\\\\1" nil nil s))))
  24366. (when is-body
  24367. (while (string-match "[ \t]*\n[ \t]*" s)
  24368. (setq s (replace-match "\\n" t t s))))
  24369. (setq s (org-trim s))
  24370. (if is-body
  24371. (if maxlength
  24372. (if (and (numberp maxlength)
  24373. (> (length s) maxlength))
  24374. (setq s (substring s 0 maxlength)))))
  24375. s))
  24376. (defun org-get-entry ()
  24377. "Clean-up description string."
  24378. (save-excursion
  24379. (org-back-to-heading t)
  24380. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24381. (defun org-start-icalendar-file (name)
  24382. "Start an iCalendar file by inserting the header."
  24383. (let ((user user-full-name)
  24384. (name (or name "unknown"))
  24385. (timezone (cadr (current-time-zone))))
  24386. (princ
  24387. (format "BEGIN:VCALENDAR
  24388. VERSION:2.0
  24389. X-WR-CALNAME:%s
  24390. PRODID:-//%s//Emacs with Org-mode//EN
  24391. X-WR-TIMEZONE:%s
  24392. CALSCALE:GREGORIAN\n" name user timezone))))
  24393. (defun org-finish-icalendar-file ()
  24394. "Finish an iCalendar file by inserting the END statement."
  24395. (princ "END:VCALENDAR\n"))
  24396. (defun org-ical-ts-to-string (s keyword &optional inc)
  24397. "Take a time string S and convert it to iCalendar format.
  24398. KEYWORD is added in front, to make a complete line like DTSTART....
  24399. When INC is non-nil, increase the hour by two (if time string contains
  24400. a time), or the day by one (if it does not contain a time)."
  24401. (let ((t1 (org-parse-time-string s 'nodefault))
  24402. t2 fmt have-time time)
  24403. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24404. (setq t2 t1 have-time t)
  24405. (setq t2 (org-parse-time-string s)))
  24406. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24407. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24408. (when inc
  24409. (if have-time
  24410. (if org-agenda-default-appointment-duration
  24411. (setq mi (+ org-agenda-default-appointment-duration mi))
  24412. (setq h (+ 2 h)))
  24413. (setq d (1+ d))))
  24414. (setq time (encode-time s mi h d m y)))
  24415. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24416. (concat keyword (format-time-string fmt time))))
  24417. ;;; XOXO export
  24418. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24419. (with-current-buffer buffer
  24420. (apply 'insert output)))
  24421. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24422. (defun org-export-as-xoxo (&optional buffer)
  24423. "Export the org buffer as XOXO.
  24424. The XOXO buffer is named *xoxo-<source buffer name>*"
  24425. (interactive (list (current-buffer)))
  24426. ;; A quickie abstraction
  24427. ;; Output everything as XOXO
  24428. (with-current-buffer (get-buffer buffer)
  24429. (let* ((pos (point))
  24430. (opt-plist (org-combine-plists (org-default-export-plist)
  24431. (org-infile-export-plist)))
  24432. (filename (concat (file-name-as-directory
  24433. (org-export-directory :xoxo opt-plist))
  24434. (file-name-sans-extension
  24435. (file-name-nondirectory buffer-file-name))
  24436. ".html"))
  24437. (out (find-file-noselect filename))
  24438. (last-level 1)
  24439. (hanging-li nil))
  24440. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24441. ;; Check the output buffer is empty.
  24442. (with-current-buffer out (erase-buffer))
  24443. ;; Kick off the output
  24444. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24445. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24446. (let* ((hd (match-string-no-properties 1))
  24447. (level (length hd))
  24448. (text (concat
  24449. (match-string-no-properties 2)
  24450. (save-excursion
  24451. (goto-char (match-end 0))
  24452. (let ((str ""))
  24453. (catch 'loop
  24454. (while 't
  24455. (forward-line)
  24456. (if (looking-at "^[ \t]\\(.*\\)")
  24457. (setq str (concat str (match-string-no-properties 1)))
  24458. (throw 'loop str)))))))))
  24459. ;; Handle level rendering
  24460. (cond
  24461. ((> level last-level)
  24462. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24463. ((< level last-level)
  24464. (dotimes (- (- last-level level) 1)
  24465. (if hanging-li
  24466. (org-export-as-xoxo-insert-into out "</li>\n"))
  24467. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24468. (when hanging-li
  24469. (org-export-as-xoxo-insert-into out "</li>\n")
  24470. (setq hanging-li nil)))
  24471. ((equal level last-level)
  24472. (if hanging-li
  24473. (org-export-as-xoxo-insert-into out "</li>\n")))
  24474. )
  24475. (setq last-level level)
  24476. ;; And output the new li
  24477. (setq hanging-li 't)
  24478. (if (equal ?+ (elt text 0))
  24479. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24480. (org-export-as-xoxo-insert-into out "<li>" text))))
  24481. ;; Finally finish off the ol
  24482. (dotimes (- last-level 1)
  24483. (if hanging-li
  24484. (org-export-as-xoxo-insert-into out "</li>\n"))
  24485. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24486. (goto-char pos)
  24487. ;; Finish the buffer off and clean it up.
  24488. (switch-to-buffer-other-window out)
  24489. (indent-region (point-min) (point-max) nil)
  24490. (save-buffer)
  24491. (goto-char (point-min))
  24492. )))
  24493. ;;;; Key bindings
  24494. ;; Make `C-c C-x' a prefix key
  24495. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24496. ;; TAB key with modifiers
  24497. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24498. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24499. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24500. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24501. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24502. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24503. ;; The following line is necessary under Suse GNU/Linux
  24504. (unless (featurep 'xemacs)
  24505. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24506. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24507. (define-key org-mode-map [backtab] 'org-shifttab)
  24508. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24509. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24510. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24511. ;; Cursor keys with modifiers
  24512. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24513. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24514. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24515. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24516. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24517. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24518. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24519. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24520. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24521. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24522. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24523. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24524. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24525. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24526. ;;; Extra keys for tty access.
  24527. ;; We only set them when really needed because otherwise the
  24528. ;; menus don't show the simple keys
  24529. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24530. (not window-system))
  24531. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24532. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24533. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24534. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24535. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24536. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24537. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24538. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24539. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24540. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24541. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24542. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24543. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24544. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24545. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24546. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24547. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24548. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24549. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24550. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24551. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24552. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24553. ;; All the other keys
  24554. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24555. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24556. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24557. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24558. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24559. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24560. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24561. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24562. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24563. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24564. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24565. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24566. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24567. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24568. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24569. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24570. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24571. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24572. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24573. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24574. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24575. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24576. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24577. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24578. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24579. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24580. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24581. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24582. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24583. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24584. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24585. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24586. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24587. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24588. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24589. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24590. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24591. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24592. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24593. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24594. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24595. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24596. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24597. (org-defkey org-mode-map "\C-m" 'org-return)
  24598. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24599. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24600. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24601. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24602. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24603. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24604. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24605. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24606. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24607. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24608. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24609. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24610. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24611. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24612. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24613. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24614. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24615. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24616. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24617. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24618. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24619. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24620. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24621. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24622. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24623. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24624. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24625. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24626. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24627. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24628. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24629. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24630. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24631. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24632. (when (featurep 'xemacs)
  24633. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24634. (defsubst org-table-p () (org-at-table-p))
  24635. (defun org-self-insert-command (N)
  24636. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24637. If the cursor is in a table looking at whitespace, the whitespace is
  24638. overwritten, and the table is not marked as requiring realignment."
  24639. (interactive "p")
  24640. (if (and (org-table-p)
  24641. (progn
  24642. ;; check if we blank the field, and if that triggers align
  24643. (and org-table-auto-blank-field
  24644. (member last-command
  24645. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24646. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24647. ;; got extra space, this field does not determine column width
  24648. (let (org-table-may-need-update) (org-table-blank-field))
  24649. ;; no extra space, this field may determine column width
  24650. (org-table-blank-field)))
  24651. t)
  24652. (eq N 1)
  24653. (looking-at "[^|\n]* |"))
  24654. (let (org-table-may-need-update)
  24655. (goto-char (1- (match-end 0)))
  24656. (delete-backward-char 1)
  24657. (goto-char (match-beginning 0))
  24658. (self-insert-command N))
  24659. (setq org-table-may-need-update t)
  24660. (self-insert-command N)
  24661. (org-fix-tags-on-the-fly)))
  24662. (defun org-fix-tags-on-the-fly ()
  24663. (when (and (equal (char-after (point-at-bol)) ?*)
  24664. (org-on-heading-p))
  24665. (org-align-tags-here org-tags-column)))
  24666. (defun org-delete-backward-char (N)
  24667. "Like `delete-backward-char', insert whitespace at field end in tables.
  24668. When deleting backwards, in tables this function will insert whitespace in
  24669. front of the next \"|\" separator, to keep the table aligned. The table will
  24670. still be marked for re-alignment if the field did fill the entire column,
  24671. because, in this case the deletion might narrow the column."
  24672. (interactive "p")
  24673. (if (and (org-table-p)
  24674. (eq N 1)
  24675. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24676. (looking-at ".*?|"))
  24677. (let ((pos (point))
  24678. (noalign (looking-at "[^|\n\r]* |"))
  24679. (c org-table-may-need-update))
  24680. (backward-delete-char N)
  24681. (skip-chars-forward "^|")
  24682. (insert " ")
  24683. (goto-char (1- pos))
  24684. ;; noalign: if there were two spaces at the end, this field
  24685. ;; does not determine the width of the column.
  24686. (if noalign (setq org-table-may-need-update c)))
  24687. (backward-delete-char N)
  24688. (org-fix-tags-on-the-fly)))
  24689. (defun org-delete-char (N)
  24690. "Like `delete-char', but insert whitespace at field end in tables.
  24691. When deleting characters, in tables this function will insert whitespace in
  24692. front of the next \"|\" separator, to keep the table aligned. The table will
  24693. still be marked for re-alignment if the field did fill the entire column,
  24694. because, in this case the deletion might narrow the column."
  24695. (interactive "p")
  24696. (if (and (org-table-p)
  24697. (not (bolp))
  24698. (not (= (char-after) ?|))
  24699. (eq N 1))
  24700. (if (looking-at ".*?|")
  24701. (let ((pos (point))
  24702. (noalign (looking-at "[^|\n\r]* |"))
  24703. (c org-table-may-need-update))
  24704. (replace-match (concat
  24705. (substring (match-string 0) 1 -1)
  24706. " |"))
  24707. (goto-char pos)
  24708. ;; noalign: if there were two spaces at the end, this field
  24709. ;; does not determine the width of the column.
  24710. (if noalign (setq org-table-may-need-update c)))
  24711. (delete-char N))
  24712. (delete-char N)
  24713. (org-fix-tags-on-the-fly)))
  24714. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24715. (put 'org-self-insert-command 'delete-selection t)
  24716. (put 'orgtbl-self-insert-command 'delete-selection t)
  24717. (put 'org-delete-char 'delete-selection 'supersede)
  24718. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24719. ;; Make `flyspell-mode' delay after some commands
  24720. (put 'org-self-insert-command 'flyspell-delayed t)
  24721. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24722. (put 'org-delete-char 'flyspell-delayed t)
  24723. (put 'org-delete-backward-char 'flyspell-delayed t)
  24724. ;; Make pabbrev-mode expand after org-mode commands
  24725. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24726. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24727. ;; How to do this: Measure non-white length of current string
  24728. ;; If equal to column width, we should realign.
  24729. (defun org-remap (map &rest commands)
  24730. "In MAP, remap the functions given in COMMANDS.
  24731. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24732. (let (new old)
  24733. (while commands
  24734. (setq old (pop commands) new (pop commands))
  24735. (if (fboundp 'command-remapping)
  24736. (org-defkey map (vector 'remap old) new)
  24737. (substitute-key-definition old new map global-map)))))
  24738. (when (eq org-enable-table-editor 'optimized)
  24739. ;; If the user wants maximum table support, we need to hijack
  24740. ;; some standard editing functions
  24741. (org-remap org-mode-map
  24742. 'self-insert-command 'org-self-insert-command
  24743. 'delete-char 'org-delete-char
  24744. 'delete-backward-char 'org-delete-backward-char)
  24745. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24746. (defun org-shiftcursor-error ()
  24747. "Throw an error because Shift-Cursor command was applied in wrong context."
  24748. (error "This command is active in special context like tables, headlines or timestamps"))
  24749. (defun org-shifttab (&optional arg)
  24750. "Global visibility cycling or move to previous table field.
  24751. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24752. on context.
  24753. See the individual commands for more information."
  24754. (interactive "P")
  24755. (cond
  24756. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24757. (arg (message "Content view to level: ")
  24758. (org-content (prefix-numeric-value arg))
  24759. (setq org-cycle-global-status 'overview))
  24760. (t (call-interactively 'org-global-cycle))))
  24761. (defun org-shiftmetaleft ()
  24762. "Promote subtree or delete table column.
  24763. Calls `org-promote-subtree', `org-outdent-item',
  24764. or `org-table-delete-column', depending on context.
  24765. See the individual commands for more information."
  24766. (interactive)
  24767. (cond
  24768. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24769. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24770. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24771. (t (org-shiftcursor-error))))
  24772. (defun org-shiftmetaright ()
  24773. "Demote subtree or insert table column.
  24774. Calls `org-demote-subtree', `org-indent-item',
  24775. or `org-table-insert-column', depending on context.
  24776. See the individual commands for more information."
  24777. (interactive)
  24778. (cond
  24779. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24780. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24781. ((org-at-item-p) (call-interactively 'org-indent-item))
  24782. (t (org-shiftcursor-error))))
  24783. (defun org-shiftmetaup (&optional arg)
  24784. "Move subtree up or kill table row.
  24785. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24786. `org-move-item-up' depending on context. See the individual commands
  24787. for more information."
  24788. (interactive "P")
  24789. (cond
  24790. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24791. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24792. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24793. (t (org-shiftcursor-error))))
  24794. (defun org-shiftmetadown (&optional arg)
  24795. "Move subtree down or insert table row.
  24796. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24797. `org-move-item-down', depending on context. See the individual
  24798. commands for more information."
  24799. (interactive "P")
  24800. (cond
  24801. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24802. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24803. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24804. (t (org-shiftcursor-error))))
  24805. (defun org-metaleft (&optional arg)
  24806. "Promote heading or move table column to left.
  24807. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24808. With no specific context, calls the Emacs default `backward-word'.
  24809. See the individual commands for more information."
  24810. (interactive "P")
  24811. (cond
  24812. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24813. ((or (org-on-heading-p) (org-region-active-p))
  24814. (call-interactively 'org-do-promote))
  24815. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24816. (t (call-interactively 'backward-word))))
  24817. (defun org-metaright (&optional arg)
  24818. "Demote subtree or move table column to right.
  24819. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24820. With no specific context, calls the Emacs default `forward-word'.
  24821. See the individual commands for more information."
  24822. (interactive "P")
  24823. (cond
  24824. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24825. ((or (org-on-heading-p) (org-region-active-p))
  24826. (call-interactively 'org-do-demote))
  24827. ((org-at-item-p) (call-interactively 'org-indent-item))
  24828. (t (call-interactively 'forward-word))))
  24829. (defun org-metaup (&optional arg)
  24830. "Move subtree up or move table row up.
  24831. Calls `org-move-subtree-up' or `org-table-move-row' or
  24832. `org-move-item-up', depending on context. See the individual commands
  24833. for more information."
  24834. (interactive "P")
  24835. (cond
  24836. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24837. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24838. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24839. (t (transpose-lines 1) (beginning-of-line -1))))
  24840. (defun org-metadown (&optional arg)
  24841. "Move subtree down or move table row down.
  24842. Calls `org-move-subtree-down' or `org-table-move-row' or
  24843. `org-move-item-down', depending on context. See the individual
  24844. commands for more information."
  24845. (interactive "P")
  24846. (cond
  24847. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24848. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24849. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24850. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24851. (defun org-shiftup (&optional arg)
  24852. "Increase item in timestamp or increase priority of current headline.
  24853. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24854. depending on context. See the individual commands for more information."
  24855. (interactive "P")
  24856. (cond
  24857. ((org-at-timestamp-p t)
  24858. (call-interactively (if org-edit-timestamp-down-means-later
  24859. 'org-timestamp-down 'org-timestamp-up)))
  24860. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24861. ((org-at-item-p) (call-interactively 'org-previous-item))
  24862. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24863. (defun org-shiftdown (&optional arg)
  24864. "Decrease item in timestamp or decrease priority of current headline.
  24865. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24866. depending on context. See the individual commands for more information."
  24867. (interactive "P")
  24868. (cond
  24869. ((org-at-timestamp-p t)
  24870. (call-interactively (if org-edit-timestamp-down-means-later
  24871. 'org-timestamp-up 'org-timestamp-down)))
  24872. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24873. (t (call-interactively 'org-next-item))))
  24874. (defun org-shiftright ()
  24875. "Next TODO keyword or timestamp one day later, depending on context."
  24876. (interactive)
  24877. (cond
  24878. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24879. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24880. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24881. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24882. (t (org-shiftcursor-error))))
  24883. (defun org-shiftleft ()
  24884. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24885. (interactive)
  24886. (cond
  24887. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24888. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24889. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24890. ((org-at-property-p)
  24891. (call-interactively 'org-property-previous-allowed-value))
  24892. (t (org-shiftcursor-error))))
  24893. (defun org-shiftcontrolright ()
  24894. "Switch to next TODO set."
  24895. (interactive)
  24896. (cond
  24897. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24898. (t (org-shiftcursor-error))))
  24899. (defun org-shiftcontrolleft ()
  24900. "Switch to previous TODO set."
  24901. (interactive)
  24902. (cond
  24903. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24904. (t (org-shiftcursor-error))))
  24905. (defun org-ctrl-c-ret ()
  24906. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24907. (interactive)
  24908. (cond
  24909. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24910. (t (call-interactively 'org-insert-heading))))
  24911. (defun org-copy-special ()
  24912. "Copy region in table or copy current subtree.
  24913. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24914. See the individual commands for more information."
  24915. (interactive)
  24916. (call-interactively
  24917. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24918. (defun org-cut-special ()
  24919. "Cut region in table or cut current subtree.
  24920. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24921. See the individual commands for more information."
  24922. (interactive)
  24923. (call-interactively
  24924. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24925. (defun org-paste-special (arg)
  24926. "Paste rectangular region into table, or past subtree relative to level.
  24927. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24928. See the individual commands for more information."
  24929. (interactive "P")
  24930. (if (org-at-table-p)
  24931. (org-table-paste-rectangle)
  24932. (org-paste-subtree arg)))
  24933. (defun org-ctrl-c-ctrl-c (&optional arg)
  24934. "Set tags in headline, or update according to changed information at point.
  24935. This command does many different things, depending on context:
  24936. - If the cursor is in a headline, prompt for tags and insert them
  24937. into the current line, aligned to `org-tags-column'. When called
  24938. with prefix arg, realign all tags in the current buffer.
  24939. - If the cursor is in one of the special #+KEYWORD lines, this
  24940. triggers scanning the buffer for these lines and updating the
  24941. information.
  24942. - If the cursor is inside a table, realign the table. This command
  24943. works even if the automatic table editor has been turned off.
  24944. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24945. the entire table.
  24946. - If the cursor is a the beginning of a dynamic block, update it.
  24947. - If the cursor is inside a table created by the table.el package,
  24948. activate that table.
  24949. - If the current buffer is a remember buffer, close note and file it.
  24950. with a prefix argument, file it without further interaction to the default
  24951. location.
  24952. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24953. links in this buffer.
  24954. - If the cursor is on a numbered item in a plain list, renumber the
  24955. ordered list.
  24956. - If the cursor is on a checkbox, toggle it."
  24957. (interactive "P")
  24958. (let ((org-enable-table-editor t))
  24959. (cond
  24960. ((or org-clock-overlays
  24961. org-occur-highlights
  24962. org-latex-fragment-image-overlays)
  24963. (org-remove-clock-overlays)
  24964. (org-remove-occur-highlights)
  24965. (org-remove-latex-fragment-image-overlays)
  24966. (message "Temporary highlights/overlays removed from current buffer"))
  24967. ((and (local-variable-p 'org-finish-function (current-buffer))
  24968. (fboundp org-finish-function))
  24969. (funcall org-finish-function))
  24970. ((org-at-property-p)
  24971. (call-interactively 'org-property-action))
  24972. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  24973. ((org-on-heading-p) (call-interactively 'org-set-tags))
  24974. ((org-at-table.el-p)
  24975. (require 'table)
  24976. (beginning-of-line 1)
  24977. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  24978. (call-interactively 'table-recognize-table))
  24979. ((org-at-table-p)
  24980. (org-table-maybe-eval-formula)
  24981. (if arg
  24982. (call-interactively 'org-table-recalculate)
  24983. (org-table-maybe-recalculate-line))
  24984. (call-interactively 'org-table-align))
  24985. ((org-at-item-checkbox-p)
  24986. (call-interactively 'org-toggle-checkbox))
  24987. ((org-at-item-p)
  24988. (call-interactively 'org-maybe-renumber-ordered-list))
  24989. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  24990. ;; Dynamic block
  24991. (beginning-of-line 1)
  24992. (org-update-dblock))
  24993. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  24994. (cond
  24995. ((equal (match-string 1) "TBLFM")
  24996. ;; Recalculate the table before this line
  24997. (save-excursion
  24998. (beginning-of-line 1)
  24999. (skip-chars-backward " \r\n\t")
  25000. (if (org-at-table-p)
  25001. (org-call-with-arg 'org-table-recalculate t))))
  25002. (t
  25003. (call-interactively 'org-mode-restart))))
  25004. (t (error "C-c C-c can do nothing useful at this location.")))))
  25005. (defun org-mode-restart ()
  25006. "Restart Org-mode, to scan again for special lines.
  25007. Also updates the keyword regular expressions."
  25008. (interactive)
  25009. (let ((org-inhibit-startup t)) (org-mode))
  25010. (message "Org-mode restarted to refresh keyword and special line setup"))
  25011. (defun org-kill-note-or-show-branches ()
  25012. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25013. (interactive)
  25014. (if (not org-finish-function)
  25015. (call-interactively 'show-branches)
  25016. (let ((org-note-abort t))
  25017. (funcall org-finish-function))))
  25018. (defun org-return (&optional indent)
  25019. "Goto next table row or insert a newline.
  25020. Calls `org-table-next-row' or `newline', depending on context.
  25021. See the individual commands for more information."
  25022. (interactive)
  25023. (cond
  25024. ((bobp) (if indent (newline-and-indent) (newline)))
  25025. ((and (org-at-heading-p)
  25026. (looking-at
  25027. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25028. (org-show-entry)
  25029. (end-of-line 1)
  25030. (newline))
  25031. ((org-at-table-p)
  25032. (org-table-justify-field-maybe)
  25033. (call-interactively 'org-table-next-row))
  25034. (t (if indent (newline-and-indent) (newline)))))
  25035. (defun org-return-indent ()
  25036. (interactive)
  25037. "Goto next table row or insert a newline and indent.
  25038. Calls `org-table-next-row' or `newline-and-indent', depending on
  25039. context. See the individual commands for more information."
  25040. (org-return t))
  25041. (defun org-ctrl-c-minus ()
  25042. "Insert separator line in table or modify bullet type in list.
  25043. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  25044. depending on context."
  25045. (interactive)
  25046. (cond
  25047. ((org-at-table-p)
  25048. (call-interactively 'org-table-insert-hline))
  25049. ((org-on-heading-p)
  25050. ;; Convert to item
  25051. (save-excursion
  25052. (beginning-of-line 1)
  25053. (if (looking-at "\\*+ ")
  25054. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25055. ((org-in-item-p)
  25056. (call-interactively 'org-cycle-list-bullet))
  25057. (t (error "`C-c -' does have no function here."))))
  25058. (defun org-meta-return (&optional arg)
  25059. "Insert a new heading or wrap a region in a table.
  25060. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25061. See the individual commands for more information."
  25062. (interactive "P")
  25063. (cond
  25064. ((org-at-table-p)
  25065. (call-interactively 'org-table-wrap-region))
  25066. (t (call-interactively 'org-insert-heading))))
  25067. ;;; Menu entries
  25068. ;; Define the Org-mode menus
  25069. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25070. '("Tbl"
  25071. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25072. ["Next Field" org-cycle (org-at-table-p)]
  25073. ["Previous Field" org-shifttab (org-at-table-p)]
  25074. ["Next Row" org-return (org-at-table-p)]
  25075. "--"
  25076. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25077. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25078. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25079. "--"
  25080. ("Column"
  25081. ["Move Column Left" org-metaleft (org-at-table-p)]
  25082. ["Move Column Right" org-metaright (org-at-table-p)]
  25083. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25084. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25085. ("Row"
  25086. ["Move Row Up" org-metaup (org-at-table-p)]
  25087. ["Move Row Down" org-metadown (org-at-table-p)]
  25088. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25089. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25090. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25091. "--"
  25092. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25093. ("Rectangle"
  25094. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25095. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25096. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25097. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25098. "--"
  25099. ("Calculate"
  25100. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25101. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25102. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25103. "--"
  25104. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25105. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25106. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25107. "--"
  25108. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25109. "--"
  25110. ["Sum Column/Rectangle" org-table-sum
  25111. (or (org-at-table-p) (org-region-active-p))]
  25112. ["Which Column?" org-table-current-column (org-at-table-p)])
  25113. ["Debug Formulas"
  25114. org-table-toggle-formula-debugger
  25115. :style toggle :selected org-table-formula-debug]
  25116. ["Show Col/Row Numbers"
  25117. org-table-toggle-coordinate-overlays
  25118. :style toggle :selected org-table-overlay-coordinates]
  25119. "--"
  25120. ["Create" org-table-create (and (not (org-at-table-p))
  25121. org-enable-table-editor)]
  25122. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25123. ["Import from File" org-table-import (not (org-at-table-p))]
  25124. ["Export to File" org-table-export (org-at-table-p)]
  25125. "--"
  25126. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25127. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25128. '("Org"
  25129. ("Show/Hide"
  25130. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25131. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25132. ["Sparse Tree" org-occur t]
  25133. ["Reveal Context" org-reveal t]
  25134. ["Show All" show-all t]
  25135. "--"
  25136. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25137. "--"
  25138. ["New Heading" org-insert-heading t]
  25139. ("Navigate Headings"
  25140. ["Up" outline-up-heading t]
  25141. ["Next" outline-next-visible-heading t]
  25142. ["Previous" outline-previous-visible-heading t]
  25143. ["Next Same Level" outline-forward-same-level t]
  25144. ["Previous Same Level" outline-backward-same-level t]
  25145. "--"
  25146. ["Jump" org-goto t])
  25147. ("Edit Structure"
  25148. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25149. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25150. "--"
  25151. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25152. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25153. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25154. "--"
  25155. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25156. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25157. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25158. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25159. "--"
  25160. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25161. "--"
  25162. ["Convert to odd levels" org-convert-to-odd-levels t]
  25163. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25164. ("Editing"
  25165. ["Emphasis..." org-emphasize t])
  25166. ("Archive"
  25167. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25168. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25169. ; :active t :keys "C-u C-c C-x C-a"]
  25170. ["Sparse trees open ARCHIVE trees"
  25171. (setq org-sparse-tree-open-archived-trees
  25172. (not org-sparse-tree-open-archived-trees))
  25173. :style toggle :selected org-sparse-tree-open-archived-trees]
  25174. ["Cycling opens ARCHIVE trees"
  25175. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25176. :style toggle :selected org-cycle-open-archived-trees]
  25177. ["Agenda includes ARCHIVE trees"
  25178. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25179. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25180. "--"
  25181. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25182. ; ["Check and Move Children" (org-archive-subtree '(4))
  25183. ; :active t :keys "C-u C-c C-x C-s"]
  25184. )
  25185. "--"
  25186. ("TODO Lists"
  25187. ["TODO/DONE/-" org-todo t]
  25188. ("Select keyword"
  25189. ["Next keyword" org-shiftright (org-on-heading-p)]
  25190. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25191. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25192. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25193. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25194. ["Show TODO Tree" org-show-todo-tree t]
  25195. ["Global TODO list" org-todo-list t]
  25196. "--"
  25197. ["Set Priority" org-priority t]
  25198. ["Priority Up" org-shiftup t]
  25199. ["Priority Down" org-shiftdown t])
  25200. ("TAGS and Properties"
  25201. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25202. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25203. "--"
  25204. ["Set property" 'org-set-property t]
  25205. ["Column view of properties" org-columns t]
  25206. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25207. ("Dates and Scheduling"
  25208. ["Timestamp" org-time-stamp t]
  25209. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25210. ("Change Date"
  25211. ["1 Day Later" org-shiftright t]
  25212. ["1 Day Earlier" org-shiftleft t]
  25213. ["1 ... Later" org-shiftup t]
  25214. ["1 ... Earlier" org-shiftdown t])
  25215. ["Compute Time Range" org-evaluate-time-range t]
  25216. ["Schedule Item" org-schedule t]
  25217. ["Deadline" org-deadline t]
  25218. "--"
  25219. ["Custom time format" org-toggle-time-stamp-overlays
  25220. :style radio :selected org-display-custom-times]
  25221. "--"
  25222. ["Goto Calendar" org-goto-calendar t]
  25223. ["Date from Calendar" org-date-from-calendar t])
  25224. ("Logging work"
  25225. ["Clock in" org-clock-in t]
  25226. ["Clock out" org-clock-out t]
  25227. ["Clock cancel" org-clock-cancel t]
  25228. ["Goto running clock" org-clock-goto t]
  25229. ["Display times" org-clock-display t]
  25230. ["Create clock table" org-clock-report t]
  25231. "--"
  25232. ["Record DONE time"
  25233. (progn (setq org-log-done (not org-log-done))
  25234. (message "Switching to %s will %s record a timestamp"
  25235. (car org-done-keywords)
  25236. (if org-log-done "automatically" "not")))
  25237. :style toggle :selected org-log-done])
  25238. "--"
  25239. ["Agenda Command..." org-agenda t]
  25240. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25241. ("File List for Agenda")
  25242. ("Special views current file"
  25243. ["TODO Tree" org-show-todo-tree t]
  25244. ["Check Deadlines" org-check-deadlines t]
  25245. ["Timeline" org-timeline t]
  25246. ["Tags Tree" org-tags-sparse-tree t])
  25247. "--"
  25248. ("Hyperlinks"
  25249. ["Store Link (Global)" org-store-link t]
  25250. ["Insert Link" org-insert-link t]
  25251. ["Follow Link" org-open-at-point t]
  25252. "--"
  25253. ["Next link" org-next-link t]
  25254. ["Previous link" org-previous-link t]
  25255. "--"
  25256. ["Descriptive Links"
  25257. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25258. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25259. ["Literal Links"
  25260. (progn
  25261. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25262. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25263. "--"
  25264. ["Export/Publish..." org-export t]
  25265. ("LaTeX"
  25266. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25267. :selected org-cdlatex-mode]
  25268. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25269. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25270. ["Modify math symbol" org-cdlatex-math-modify
  25271. (org-inside-LaTeX-fragment-p)]
  25272. ["Export LaTeX fragments as images"
  25273. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25274. :style toggle :selected org-export-with-LaTeX-fragments])
  25275. "--"
  25276. ("Documentation"
  25277. ["Show Version" org-version t]
  25278. ["Info Documentation" org-info t])
  25279. ("Customize"
  25280. ["Browse Org Group" org-customize t]
  25281. "--"
  25282. ["Expand This Menu" org-create-customize-menu
  25283. (fboundp 'customize-menu-create)])
  25284. "--"
  25285. ["Refresh setup" org-mode-restart t]
  25286. ))
  25287. (defun org-info (&optional node)
  25288. "Read documentation for Org-mode in the info system.
  25289. With optional NODE, go directly to that node."
  25290. (interactive)
  25291. (require 'info)
  25292. (Info-goto-node (format "(org)%s" (or node ""))))
  25293. (defun org-install-agenda-files-menu ()
  25294. (let ((bl (buffer-list)))
  25295. (save-excursion
  25296. (while bl
  25297. (set-buffer (pop bl))
  25298. (if (org-mode-p) (setq bl nil)))
  25299. (when (org-mode-p)
  25300. (easy-menu-change
  25301. '("Org") "File List for Agenda"
  25302. (append
  25303. (list
  25304. ["Edit File List" (org-edit-agenda-file-list) t]
  25305. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25306. ["Remove Current File from List" org-remove-file t]
  25307. ["Cycle through agenda files" org-cycle-agenda-files t]
  25308. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25309. "--")
  25310. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25311. ;;;; Documentation
  25312. (defun org-customize ()
  25313. "Call the customize function with org as argument."
  25314. (interactive)
  25315. (customize-browse 'org))
  25316. (defun org-create-customize-menu ()
  25317. "Create a full customization menu for Org-mode, insert it into the menu."
  25318. (interactive)
  25319. (if (fboundp 'customize-menu-create)
  25320. (progn
  25321. (easy-menu-change
  25322. '("Org") "Customize"
  25323. `(["Browse Org group" org-customize t]
  25324. "--"
  25325. ,(customize-menu-create 'org)
  25326. ["Set" Custom-set t]
  25327. ["Save" Custom-save t]
  25328. ["Reset to Current" Custom-reset-current t]
  25329. ["Reset to Saved" Custom-reset-saved t]
  25330. ["Reset to Standard Settings" Custom-reset-standard t]))
  25331. (message "\"Org\"-menu now contains full customization menu"))
  25332. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25333. ;;;; Miscellaneous stuff
  25334. ;;; Generally useful functions
  25335. (defun org-context ()
  25336. "Return a list of contexts of the current cursor position.
  25337. If several contexts apply, all are returned.
  25338. Each context entry is a list with a symbol naming the context, and
  25339. two positions indicating start and end of the context. Possible
  25340. contexts are:
  25341. :headline anywhere in a headline
  25342. :headline-stars on the leading stars in a headline
  25343. :todo-keyword on a TODO keyword (including DONE) in a headline
  25344. :tags on the TAGS in a headline
  25345. :priority on the priority cookie in a headline
  25346. :item on the first line of a plain list item
  25347. :item-bullet on the bullet/number of a plain list item
  25348. :checkbox on the checkbox in a plain list item
  25349. :table in an org-mode table
  25350. :table-special on a special filed in a table
  25351. :table-table in a table.el table
  25352. :link on a hyperlink
  25353. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25354. :target on a <<target>>
  25355. :radio-target on a <<<radio-target>>>
  25356. :latex-fragment on a LaTeX fragment
  25357. :latex-preview on a LaTeX fragment with overlayed preview image
  25358. This function expects the position to be visible because it uses font-lock
  25359. faces as a help to recognize the following contexts: :table-special, :link,
  25360. and :keyword."
  25361. (let* ((f (get-text-property (point) 'face))
  25362. (faces (if (listp f) f (list f)))
  25363. (p (point)) clist o)
  25364. ;; First the large context
  25365. (cond
  25366. ((org-on-heading-p t)
  25367. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25368. (when (progn
  25369. (beginning-of-line 1)
  25370. (looking-at org-todo-line-tags-regexp))
  25371. (push (org-point-in-group p 1 :headline-stars) clist)
  25372. (push (org-point-in-group p 2 :todo-keyword) clist)
  25373. (push (org-point-in-group p 4 :tags) clist))
  25374. (goto-char p)
  25375. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25376. (if (looking-at "\\[#[A-Z0-9]\\]")
  25377. (push (org-point-in-group p 0 :priority) clist)))
  25378. ((org-at-item-p)
  25379. (push (org-point-in-group p 2 :item-bullet) clist)
  25380. (push (list :item (point-at-bol)
  25381. (save-excursion (org-end-of-item) (point)))
  25382. clist)
  25383. (and (org-at-item-checkbox-p)
  25384. (push (org-point-in-group p 0 :checkbox) clist)))
  25385. ((org-at-table-p)
  25386. (push (list :table (org-table-begin) (org-table-end)) clist)
  25387. (if (memq 'org-formula faces)
  25388. (push (list :table-special
  25389. (previous-single-property-change p 'face)
  25390. (next-single-property-change p 'face)) clist)))
  25391. ((org-at-table-p 'any)
  25392. (push (list :table-table) clist)))
  25393. (goto-char p)
  25394. ;; Now the small context
  25395. (cond
  25396. ((org-at-timestamp-p)
  25397. (push (org-point-in-group p 0 :timestamp) clist))
  25398. ((memq 'org-link faces)
  25399. (push (list :link
  25400. (previous-single-property-change p 'face)
  25401. (next-single-property-change p 'face)) clist))
  25402. ((memq 'org-special-keyword faces)
  25403. (push (list :keyword
  25404. (previous-single-property-change p 'face)
  25405. (next-single-property-change p 'face)) clist))
  25406. ((org-on-target-p)
  25407. (push (org-point-in-group p 0 :target) clist)
  25408. (goto-char (1- (match-beginning 0)))
  25409. (if (looking-at org-radio-target-regexp)
  25410. (push (org-point-in-group p 0 :radio-target) clist))
  25411. (goto-char p))
  25412. ((setq o (car (delq nil
  25413. (mapcar
  25414. (lambda (x)
  25415. (if (memq x org-latex-fragment-image-overlays) x))
  25416. (org-overlays-at (point))))))
  25417. (push (list :latex-fragment
  25418. (org-overlay-start o) (org-overlay-end o)) clist)
  25419. (push (list :latex-preview
  25420. (org-overlay-start o) (org-overlay-end o)) clist))
  25421. ((org-inside-LaTeX-fragment-p)
  25422. ;; FIXME: positions wrong.
  25423. (push (list :latex-fragment (point) (point)) clist)))
  25424. (setq clist (nreverse (delq nil clist)))
  25425. clist))
  25426. ;; FIXME: Compare with at-regexp-p Do we need both?
  25427. (defun org-in-regexp (re &optional nlines visually)
  25428. "Check if point is inside a match of regexp.
  25429. Normally only the current line is checked, but you can include NLINES extra
  25430. lines both before and after point into the search.
  25431. If VISUALLY is set, require that the cursor is not after the match but
  25432. really on, so that the block visually is on the match."
  25433. (catch 'exit
  25434. (let ((pos (point))
  25435. (eol (point-at-eol (+ 1 (or nlines 0))))
  25436. (inc (if visually 1 0)))
  25437. (save-excursion
  25438. (beginning-of-line (- 1 (or nlines 0)))
  25439. (while (re-search-forward re eol t)
  25440. (if (and (<= (match-beginning 0) pos)
  25441. (>= (+ inc (match-end 0)) pos))
  25442. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25443. (defun org-at-regexp-p (regexp)
  25444. "Is point inside a match of REGEXP in the current line?"
  25445. (catch 'exit
  25446. (save-excursion
  25447. (let ((pos (point)) (end (point-at-eol)))
  25448. (beginning-of-line 1)
  25449. (while (re-search-forward regexp end t)
  25450. (if (and (<= (match-beginning 0) pos)
  25451. (>= (match-end 0) pos))
  25452. (throw 'exit t)))
  25453. nil))))
  25454. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25455. "Call `multi-occur' with buffers for all agenda files."
  25456. (interactive "sOrg-files matching: \np")
  25457. (let* ((files (org-agenda-files))
  25458. (tnames (mapcar 'file-truename files))
  25459. (extra org-agenda-multi-occur-extra-files)
  25460. f)
  25461. (while (setq f (pop extra))
  25462. (unless (member (file-truename f) tnames)
  25463. (add-to-list 'files f 'append)
  25464. (add-to-list 'tnames (file-truename f) 'append)))
  25465. (multi-occur
  25466. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25467. regexp)))
  25468. (if (boundp 'occur-mode-find-occurrence-hook)
  25469. ;; Emacs 23
  25470. (add-hook 'occur-mode-find-occurrence-hook
  25471. (lambda ()
  25472. (when (org-mode-p)
  25473. (org-reveal))))
  25474. ;; Emacs 22
  25475. (defadvice occur-mode-goto-occurrence
  25476. (after org-occur-reveal activate)
  25477. (and (org-mode-p) (org-reveal)))
  25478. (defadvice occur-mode-goto-occurrence-other-window
  25479. (after org-occur-reveal activate)
  25480. (and (org-mode-p) (org-reveal)))
  25481. (defadvice occur-mode-display-occurrence
  25482. (after org-occur-reveal activate)
  25483. (when (org-mode-p)
  25484. (let ((pos (occur-mode-find-occurrence)))
  25485. (with-current-buffer (marker-buffer pos)
  25486. (save-excursion
  25487. (goto-char pos)
  25488. (org-reveal)))))))
  25489. (defun org-uniquify (list)
  25490. "Remove duplicate elements from LIST."
  25491. (let (res)
  25492. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25493. res))
  25494. (defun org-delete-all (elts list)
  25495. "Remove all elements in ELTS from LIST."
  25496. (while elts
  25497. (setq list (delete (pop elts) list)))
  25498. list)
  25499. (defun org-back-over-empty-lines ()
  25500. "Move backwards over witespace, to the beginning of the first empty line.
  25501. Returns the number o empty lines passed."
  25502. (let ((pos (point)))
  25503. (skip-chars-backward " \t\n\r")
  25504. (beginning-of-line 2)
  25505. (goto-char (min (point) pos))
  25506. (count-lines (point) pos)))
  25507. (defun org-skip-whitespace ()
  25508. (skip-chars-forward " \t\n\r"))
  25509. (defun org-point-in-group (point group &optional context)
  25510. "Check if POINT is in match-group GROUP.
  25511. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25512. match. If the match group does ot exist or point is not inside it,
  25513. return nil."
  25514. (and (match-beginning group)
  25515. (>= point (match-beginning group))
  25516. (<= point (match-end group))
  25517. (if context
  25518. (list context (match-beginning group) (match-end group))
  25519. t)))
  25520. (defun org-switch-to-buffer-other-window (&rest args)
  25521. "Switch to buffer in a second window on the current frame.
  25522. In particular, do not allow pop-up frames."
  25523. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25524. special-display-function)
  25525. (apply 'switch-to-buffer-other-window args)))
  25526. (defun org-combine-plists (&rest plists)
  25527. "Create a single property list from all plists in PLISTS.
  25528. The process starts by copying the first list, and then setting properties
  25529. from the other lists. Settings in the last list are the most significant
  25530. ones and overrule settings in the other lists."
  25531. (let ((rtn (copy-sequence (pop plists)))
  25532. p v ls)
  25533. (while plists
  25534. (setq ls (pop plists))
  25535. (while ls
  25536. (setq p (pop ls) v (pop ls))
  25537. (setq rtn (plist-put rtn p v))))
  25538. rtn))
  25539. (defun org-move-line-down (arg)
  25540. "Move the current line down. With prefix argument, move it past ARG lines."
  25541. (interactive "p")
  25542. (let ((col (current-column))
  25543. beg end pos)
  25544. (beginning-of-line 1) (setq beg (point))
  25545. (beginning-of-line 2) (setq end (point))
  25546. (beginning-of-line (+ 1 arg))
  25547. (setq pos (move-marker (make-marker) (point)))
  25548. (insert (delete-and-extract-region beg end))
  25549. (goto-char pos)
  25550. (move-to-column col)))
  25551. (defun org-move-line-up (arg)
  25552. "Move the current line up. With prefix argument, move it past ARG lines."
  25553. (interactive "p")
  25554. (let ((col (current-column))
  25555. beg end pos)
  25556. (beginning-of-line 1) (setq beg (point))
  25557. (beginning-of-line 2) (setq end (point))
  25558. (beginning-of-line (- arg))
  25559. (setq pos (move-marker (make-marker) (point)))
  25560. (insert (delete-and-extract-region beg end))
  25561. (goto-char pos)
  25562. (move-to-column col)))
  25563. (defun org-replace-escapes (string table)
  25564. "Replace %-escapes in STRING with values in TABLE.
  25565. TABLE is an association list with keys like \"%a\" and string values.
  25566. The sequences in STRING may contain normal field width and padding information,
  25567. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25568. so values can contain further %-escapes if they are define later in TABLE."
  25569. (let ((case-fold-search nil)
  25570. e re rpl)
  25571. (while (setq e (pop table))
  25572. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25573. (while (string-match re string)
  25574. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25575. (cdr e)))
  25576. (setq string (replace-match rpl t t string))))
  25577. string))
  25578. (defun org-sublist (list start end)
  25579. "Return a section of LIST, from START to END.
  25580. Counting starts at 1."
  25581. (let (rtn (c start))
  25582. (setq list (nthcdr (1- start) list))
  25583. (while (and list (<= c end))
  25584. (push (pop list) rtn)
  25585. (setq c (1+ c)))
  25586. (nreverse rtn)))
  25587. (defun org-find-base-buffer-visiting (file)
  25588. "Like `find-buffer-visiting' but alway return the base buffer and
  25589. not an indirect buffer"
  25590. (let ((buf (find-buffer-visiting file)))
  25591. (if buf
  25592. (or (buffer-base-buffer buf) buf)
  25593. nil)))
  25594. (defun org-image-file-name-regexp ()
  25595. "Return regexp matching the file names of images."
  25596. (if (fboundp 'image-file-name-regexp)
  25597. (image-file-name-regexp)
  25598. (let ((image-file-name-extensions
  25599. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25600. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25601. (concat "\\."
  25602. (regexp-opt (nconc (mapcar 'upcase
  25603. image-file-name-extensions)
  25604. image-file-name-extensions)
  25605. t)
  25606. "\\'"))))
  25607. (defun org-file-image-p (file)
  25608. "Return non-nil if FILE is an image."
  25609. (save-match-data
  25610. (string-match (org-image-file-name-regexp) file)))
  25611. ;;; Paragraph filling stuff.
  25612. ;; We want this to be just right, so use the full arsenal.
  25613. (defun org-indent-line-function ()
  25614. "Indent line like previous, but further if previous was headline or item."
  25615. (interactive)
  25616. (let* ((pos (point))
  25617. (itemp (org-at-item-p))
  25618. column bpos bcol tpos tcol bullet btype bullet-type)
  25619. ;; Find the previous relevant line
  25620. (beginning-of-line 1)
  25621. (cond
  25622. ((looking-at "#") (setq column 0))
  25623. ((looking-at "\\*+ ") (setq column 0))
  25624. (t
  25625. (beginning-of-line 0)
  25626. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25627. (beginning-of-line 0))
  25628. (cond
  25629. ((looking-at "\\*+[ \t]+")
  25630. (goto-char (match-end 0))
  25631. (setq column (current-column)))
  25632. ((org-in-item-p)
  25633. (org-beginning-of-item)
  25634. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25635. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25636. (setq bpos (match-beginning 1) tpos (match-end 0)
  25637. bcol (progn (goto-char bpos) (current-column))
  25638. tcol (progn (goto-char tpos) (current-column))
  25639. bullet (match-string 1)
  25640. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25641. (if (not itemp)
  25642. (setq column tcol)
  25643. (goto-char pos)
  25644. (beginning-of-line 1)
  25645. (if (looking-at "\\S-")
  25646. (progn
  25647. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25648. (setq bullet (match-string 1)
  25649. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25650. (setq column (if (equal btype bullet-type) bcol tcol)))
  25651. (setq column (org-get-indentation)))))
  25652. (t (setq column (org-get-indentation))))))
  25653. (goto-char pos)
  25654. (if (<= (current-column) (current-indentation))
  25655. (indent-line-to column)
  25656. (save-excursion (indent-line-to column)))
  25657. (setq column (current-column))
  25658. (beginning-of-line 1)
  25659. (if (looking-at
  25660. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25661. (replace-match (concat "\\1" (format org-property-format
  25662. (match-string 2) (match-string 3)))
  25663. t nil))
  25664. (move-to-column column)))
  25665. (defun org-set-autofill-regexps ()
  25666. (interactive)
  25667. ;; In the paragraph separator we include headlines, because filling
  25668. ;; text in a line directly attached to a headline would otherwise
  25669. ;; fill the headline as well.
  25670. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25671. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25672. ;; The paragraph starter includes hand-formatted lists.
  25673. (org-set-local 'paragraph-start
  25674. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25675. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25676. ;; But only if the user has not turned off tables or fixed-width regions
  25677. (org-set-local
  25678. 'auto-fill-inhibit-regexp
  25679. (concat "\\*+ \\|#\\+"
  25680. "\\|[ \t]*" org-keyword-time-regexp
  25681. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25682. (concat
  25683. "\\|[ \t]*["
  25684. (if org-enable-table-editor "|" "")
  25685. (if org-enable-fixed-width-editor ":" "")
  25686. "]"))))
  25687. ;; We use our own fill-paragraph function, to make sure that tables
  25688. ;; and fixed-width regions are not wrapped. That function will pass
  25689. ;; through to `fill-paragraph' when appropriate.
  25690. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25691. ; Adaptive filling: To get full control, first make sure that
  25692. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25693. (org-set-local 'adaptive-fill-regexp "\000")
  25694. (org-set-local 'adaptive-fill-function
  25695. 'org-adaptive-fill-function)
  25696. (org-set-local
  25697. 'align-mode-rules-list
  25698. '((org-in-buffer-settings
  25699. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25700. (modes . '(org-mode))))))
  25701. (defun org-fill-paragraph (&optional justify)
  25702. "Re-align a table, pass through to fill-paragraph if no table."
  25703. (let ((table-p (org-at-table-p))
  25704. (table.el-p (org-at-table.el-p)))
  25705. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25706. (save-excursion (goto-char (point-at-bol))
  25707. (looking-at outline-regexp)))
  25708. t) ; skip headlines
  25709. (table.el-p t) ; skip table.el tables
  25710. (table-p (org-table-align) t) ; align org-mode tables
  25711. (t nil)))) ; call paragraph-fill
  25712. ;; For reference, this is the default value of adaptive-fill-regexp
  25713. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25714. (defun org-adaptive-fill-function ()
  25715. "Return a fill prefix for org-mode files.
  25716. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25717. work correctly."
  25718. (cond ((looking-at "#[ \t]+")
  25719. (match-string 0))
  25720. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25721. (save-excursion
  25722. (goto-char (match-end 0))
  25723. (make-string (current-column) ?\ )))
  25724. (t nil)))
  25725. ;;;; Functions extending outline functionality
  25726. (defun org-beginning-of-line (&optional arg)
  25727. "Go to the beginning of the current line. If that is invisible, continue
  25728. to a visible line beginning. This makes the function of C-a more intuitive.
  25729. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25730. first attempt, and only move to after the tags when the cursor is already
  25731. beyond the end of the headline."
  25732. (interactive "P")
  25733. (let ((pos (point)))
  25734. (beginning-of-line 1)
  25735. (if (bobp)
  25736. nil
  25737. (backward-char 1)
  25738. (if (org-invisible-p)
  25739. (while (and (not (bobp)) (org-invisible-p))
  25740. (backward-char 1)
  25741. (beginning-of-line 1))
  25742. (forward-char 1)))
  25743. (when org-special-ctrl-a/e
  25744. (cond
  25745. ((and (looking-at org-todo-line-regexp)
  25746. (= (char-after (match-end 1)) ?\ ))
  25747. (goto-char
  25748. (if (eq org-special-ctrl-a/e t)
  25749. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25750. ((= pos (point)) (match-beginning 3))
  25751. (t (point)))
  25752. (cond ((> pos (point)) (point))
  25753. ((not (eq last-command this-command)) (point))
  25754. (t (match-beginning 3))))))
  25755. ((org-at-item-p)
  25756. (goto-char
  25757. (if (eq org-special-ctrl-a/e t)
  25758. (cond ((> pos (match-end 4)) (match-end 4))
  25759. ((= pos (point)) (match-end 4))
  25760. (t (point)))
  25761. (cond ((> pos (point)) (point))
  25762. ((not (eq last-command this-command)) (point))
  25763. (t (match-end 4))))))))))
  25764. (defun org-end-of-line (&optional arg)
  25765. "Go to the end of the line.
  25766. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25767. first attempt, and only move to after the tags when the cursor is already
  25768. beyond the end of the headline."
  25769. (interactive "P")
  25770. (if (or (not org-special-ctrl-a/e)
  25771. (not (org-on-heading-p)))
  25772. (end-of-line arg)
  25773. (let ((pos (point)))
  25774. (beginning-of-line 1)
  25775. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25776. (if (eq org-special-ctrl-a/e t)
  25777. (if (or (< pos (match-beginning 1))
  25778. (= pos (match-end 0)))
  25779. (goto-char (match-beginning 1))
  25780. (goto-char (match-end 0)))
  25781. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25782. (goto-char (match-end 0))
  25783. (goto-char (match-beginning 1))))
  25784. (end-of-line arg)))))
  25785. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25786. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25787. (defun org-kill-line (&optional arg)
  25788. "Kill line, to tags or end of line."
  25789. (interactive "P")
  25790. (cond
  25791. ((or (not org-special-ctrl-k)
  25792. (bolp)
  25793. (not (org-on-heading-p)))
  25794. (call-interactively 'kill-line))
  25795. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  25796. (kill-region (point) (match-beginning 1))
  25797. (org-set-tags nil t))
  25798. (t (kill-region (point) (point-at-eol)))))
  25799. (define-key org-mode-map "\C-k" 'org-kill-line)
  25800. (defun org-invisible-p ()
  25801. "Check if point is at a character currently not visible."
  25802. ;; Early versions of noutline don't have `outline-invisible-p'.
  25803. (if (fboundp 'outline-invisible-p)
  25804. (outline-invisible-p)
  25805. (get-char-property (point) 'invisible)))
  25806. (defun org-invisible-p2 ()
  25807. "Check if point is at a character currently not visible."
  25808. (save-excursion
  25809. (if (and (eolp) (not (bobp))) (backward-char 1))
  25810. ;; Early versions of noutline don't have `outline-invisible-p'.
  25811. (if (fboundp 'outline-invisible-p)
  25812. (outline-invisible-p)
  25813. (get-char-property (point) 'invisible))))
  25814. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25815. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25816. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25817. (defun org-at-heading-or-item-p ()
  25818. (or (org-on-heading-p) (org-at-item-p)))
  25819. (defun org-on-target-p ()
  25820. (or (org-in-regexp org-radio-target-regexp)
  25821. (org-in-regexp org-target-regexp)))
  25822. (defun org-up-heading-all (arg)
  25823. "Move to the heading line of which the present line is a subheading.
  25824. This function considers both visible and invisible heading lines.
  25825. With argument, move up ARG levels."
  25826. (if (fboundp 'outline-up-heading-all)
  25827. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25828. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25829. (defun org-up-heading-safe ()
  25830. "Move to the heading line of which the present line is a subheading.
  25831. This version will not throw an error. It will return the level of the
  25832. headline found, or nil if no higher level is found."
  25833. (let ((pos (point)) start-level level
  25834. (re (concat "^" outline-regexp)))
  25835. (catch 'exit
  25836. (outline-back-to-heading t)
  25837. (setq start-level (funcall outline-level))
  25838. (if (equal start-level 1) (throw 'exit nil))
  25839. (while (re-search-backward re nil t)
  25840. (setq level (funcall outline-level))
  25841. (if (< level start-level) (throw 'exit level)))
  25842. nil)))
  25843. (defun org-first-sibling-p ()
  25844. "Is this heading the first child of its parents?"
  25845. (interactive)
  25846. (let ((re (concat "^" outline-regexp))
  25847. level l)
  25848. (unless (org-at-heading-p t)
  25849. (error "Not at a heading"))
  25850. (setq level (funcall outline-level))
  25851. (save-excursion
  25852. (if (not (re-search-backward re nil t))
  25853. t
  25854. (setq l (funcall outline-level))
  25855. (< l level)))))
  25856. (defun org-goto-sibling (&optional previous)
  25857. "Goto the next sibling, even if it is invisible.
  25858. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25859. when a sibling was found. When none is found, return nil and don't
  25860. move point."
  25861. (let ((fun (if previous 're-search-backward 're-search-forward))
  25862. (pos (point))
  25863. (re (concat "^" outline-regexp))
  25864. level l)
  25865. (when (condition-case nil (org-back-to-heading t) (error nil))
  25866. (setq level (funcall outline-level))
  25867. (catch 'exit
  25868. (or previous (forward-char 1))
  25869. (while (funcall fun re nil t)
  25870. (setq l (funcall outline-level))
  25871. (when (< l level) (goto-char pos) (throw 'exit nil))
  25872. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25873. (goto-char pos)
  25874. nil))))
  25875. (defun org-show-siblings ()
  25876. "Show all siblings of the current headline."
  25877. (save-excursion
  25878. (while (org-goto-sibling) (org-flag-heading nil)))
  25879. (save-excursion
  25880. (while (org-goto-sibling 'previous)
  25881. (org-flag-heading nil))))
  25882. (defun org-show-hidden-entry ()
  25883. "Show an entry where even the heading is hidden."
  25884. (save-excursion
  25885. (org-show-entry)))
  25886. (defun org-flag-heading (flag &optional entry)
  25887. "Flag the current heading. FLAG non-nil means make invisible.
  25888. When ENTRY is non-nil, show the entire entry."
  25889. (save-excursion
  25890. (org-back-to-heading t)
  25891. ;; Check if we should show the entire entry
  25892. (if entry
  25893. (progn
  25894. (org-show-entry)
  25895. (save-excursion
  25896. (and (outline-next-heading)
  25897. (org-flag-heading nil))))
  25898. (outline-flag-region (max (point-min) (1- (point)))
  25899. (save-excursion (outline-end-of-heading) (point))
  25900. flag))))
  25901. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25902. ;; This is an exact copy of the original function, but it uses
  25903. ;; `org-back-to-heading', to make it work also in invisible
  25904. ;; trees. And is uses an invisible-OK argument.
  25905. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25906. (org-back-to-heading invisible-OK)
  25907. (let ((first t)
  25908. (level (funcall outline-level)))
  25909. (while (and (not (eobp))
  25910. (or first (> (funcall outline-level) level)))
  25911. (setq first nil)
  25912. (outline-next-heading))
  25913. (unless to-heading
  25914. (if (memq (preceding-char) '(?\n ?\^M))
  25915. (progn
  25916. ;; Go to end of line before heading
  25917. (forward-char -1)
  25918. (if (memq (preceding-char) '(?\n ?\^M))
  25919. ;; leave blank line before heading
  25920. (forward-char -1))))))
  25921. (point))
  25922. (defun org-show-subtree ()
  25923. "Show everything after this heading at deeper levels."
  25924. (outline-flag-region
  25925. (point)
  25926. (save-excursion
  25927. (outline-end-of-subtree) (outline-next-heading) (point))
  25928. nil))
  25929. (defun org-show-entry ()
  25930. "Show the body directly following this heading.
  25931. Show the heading too, if it is currently invisible."
  25932. (interactive)
  25933. (save-excursion
  25934. (condition-case nil
  25935. (progn
  25936. (org-back-to-heading t)
  25937. (outline-flag-region
  25938. (max (point-min) (1- (point)))
  25939. (save-excursion
  25940. (re-search-forward
  25941. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25942. (or (match-beginning 1) (point-max)))
  25943. nil))
  25944. (error nil))))
  25945. (defun org-make-options-regexp (kwds)
  25946. "Make a regular expression for keyword lines."
  25947. (concat
  25948. "^"
  25949. "#?[ \t]*\\+\\("
  25950. (mapconcat 'regexp-quote kwds "\\|")
  25951. "\\):[ \t]*"
  25952. "\\(.+\\)"))
  25953. ;; Make isearch reveal the necessary context
  25954. (defun org-isearch-end ()
  25955. "Reveal context after isearch exits."
  25956. (when isearch-success ; only if search was successful
  25957. (if (featurep 'xemacs)
  25958. ;; Under XEmacs, the hook is run in the correct place,
  25959. ;; we directly show the context.
  25960. (org-show-context 'isearch)
  25961. ;; In Emacs the hook runs *before* restoring the overlays.
  25962. ;; So we have to use a one-time post-command-hook to do this.
  25963. ;; (Emacs 22 has a special variable, see function `org-mode')
  25964. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25965. isearch-mode-end-hook-quit)
  25966. ;; Only when the isearch was not quitted.
  25967. (org-add-hook 'post-command-hook 'org-isearch-post-command
  25968. 'append 'local)))))
  25969. (defun org-isearch-post-command ()
  25970. "Remove self from hook, and show context."
  25971. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  25972. (org-show-context 'isearch))
  25973. ;;;; Integration with and fixes for other packages
  25974. ;;; Imenu support
  25975. (defvar org-imenu-markers nil
  25976. "All markers currently used by Imenu.")
  25977. (make-variable-buffer-local 'org-imenu-markers)
  25978. (defun org-imenu-new-marker (&optional pos)
  25979. "Return a new marker for use by Imenu, and remember the marker."
  25980. (let ((m (make-marker)))
  25981. (move-marker m (or pos (point)))
  25982. (push m org-imenu-markers)
  25983. m))
  25984. (defun org-imenu-get-tree ()
  25985. "Produce the index for Imenu."
  25986. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  25987. (setq org-imenu-markers nil)
  25988. (let* ((n org-imenu-depth)
  25989. (re (concat "^" outline-regexp))
  25990. (subs (make-vector (1+ n) nil))
  25991. (last-level 0)
  25992. m tree level head)
  25993. (save-excursion
  25994. (save-restriction
  25995. (widen)
  25996. (goto-char (point-max))
  25997. (while (re-search-backward re nil t)
  25998. (setq level (org-reduced-level (funcall outline-level)))
  25999. (when (<= level n)
  26000. (looking-at org-complex-heading-regexp)
  26001. (setq head (org-match-string-no-properties 4)
  26002. m (org-imenu-new-marker))
  26003. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26004. (if (>= level last-level)
  26005. (push (cons head m) (aref subs level))
  26006. (push (cons head (aref subs (1+ level))) (aref subs level))
  26007. (loop for i from (1+ level) to n do (aset subs i nil)))
  26008. (setq last-level level)))))
  26009. (aref subs 1)))
  26010. (eval-after-load "imenu"
  26011. '(progn
  26012. (add-hook 'imenu-after-jump-hook
  26013. (lambda () (org-show-context 'org-goto)))))
  26014. ;; Speedbar support
  26015. (defun org-speedbar-set-agenda-restriction ()
  26016. "Restrict future agenda commands to the location at point in speedbar.
  26017. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26018. (interactive)
  26019. (let (p m tp np dir txt w)
  26020. (cond
  26021. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26022. 'org-imenu t))
  26023. (setq m (get-text-property p 'org-imenu-marker))
  26024. (save-excursion
  26025. (save-restriction
  26026. (set-buffer (marker-buffer m))
  26027. (goto-char m)
  26028. (org-agenda-set-restriction-lock 'subtree))))
  26029. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26030. 'speedbar-function 'speedbar-find-file))
  26031. (setq tp (previous-single-property-change
  26032. (1+ p) 'speedbar-function)
  26033. np (next-single-property-change
  26034. tp 'speedbar-function)
  26035. dir (speedbar-line-directory)
  26036. txt (buffer-substring-no-properties (or tp (point-min))
  26037. (or np (point-max))))
  26038. (save-excursion
  26039. (save-restriction
  26040. (set-buffer (find-file-noselect
  26041. (let ((default-directory dir))
  26042. (expand-file-name txt))))
  26043. (unless (org-mode-p)
  26044. (error "Cannot restrict to non-Org-mode file"))
  26045. (org-agenda-set-restriction-lock 'file))))
  26046. (t (error "Don't know how to restrict Org-mode's agenda")))
  26047. (org-move-overlay org-speedbar-restriction-lock-overlay
  26048. (point-at-bol) (point-at-eol))
  26049. (setq current-prefix-arg nil)
  26050. (org-agenda-maybe-redo)))
  26051. (eval-after-load "speedbar"
  26052. '(progn
  26053. (speedbar-add-supported-extension ".org")
  26054. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26055. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26056. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26057. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26058. (add-hook 'speedbar-visiting-tag-hook
  26059. (lambda () (org-show-context 'org-goto)))))
  26060. ;;; Fixes and Hacks
  26061. ;; Make flyspell not check words in links, to not mess up our keymap
  26062. (defun org-mode-flyspell-verify ()
  26063. "Don't let flyspell put overlays at active buttons."
  26064. (not (get-text-property (point) 'keymap)))
  26065. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26066. (eval-after-load "bookmark"
  26067. '(if (boundp 'bookmark-after-jump-hook)
  26068. ;; We can use the hook
  26069. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26070. ;; Hook not available, use advice
  26071. (defadvice bookmark-jump (after org-make-visible activate)
  26072. "Make the position visible."
  26073. (org-bookmark-jump-unhide))))
  26074. (defun org-bookmark-jump-unhide ()
  26075. "Unhide the current position, to show the bookmark location."
  26076. (and (org-mode-p)
  26077. (or (org-invisible-p)
  26078. (save-excursion (goto-char (max (point-min) (1- (point))))
  26079. (org-invisible-p)))
  26080. (org-show-context 'bookmark-jump)))
  26081. ;; Fix a bug in htmlize where there are text properties (face nil)
  26082. (eval-after-load "htmlize"
  26083. '(progn
  26084. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26085. "Make sure there are no nil faces"
  26086. (setq ad-return-value (delq nil ad-return-value)))))
  26087. ;; Make session.el ignore our circular variable
  26088. (eval-after-load "session"
  26089. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26090. ;;;; Experimental code
  26091. (defun org-closed-in-range ()
  26092. "Sparse tree of items closed in a certain time range.
  26093. Still experimental, may disappear in the future."
  26094. (interactive)
  26095. ;; Get the time interval from the user.
  26096. (let* ((time1 (time-to-seconds
  26097. (org-read-date nil 'to-time nil "Starting date: ")))
  26098. (time2 (time-to-seconds
  26099. (org-read-date nil 'to-time nil "End date:")))
  26100. ;; callback function
  26101. (callback (lambda ()
  26102. (let ((time
  26103. (time-to-seconds
  26104. (apply 'encode-time
  26105. (org-parse-time-string
  26106. (match-string 1))))))
  26107. ;; check if time in interval
  26108. (and (>= time time1) (<= time time2))))))
  26109. ;; make tree, check each match with the callback
  26110. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26111. ;;;; Finish up
  26112. (provide 'org)
  26113. (run-hooks 'org-load-hook)
  26114. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26115. ;;; org.el ends here