org.el 1.0 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345
  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. (defcustom org-load-hook '(org-load-default-extensions)
  138. "Hook that is run after org.el has been loaded.
  139. This happens also after `org' has been provided, so
  140. requiring something in this hook that does a (require 'org) is ok."
  141. :group 'org
  142. :type 'hook)
  143. (defcustom org-default-extensions '(org-irc)
  144. "Extensions that should always be loaded together with org.el
  145. If the description starts with <A>, this means the extension
  146. will be autoloaded when needed, to preloading is not necessary."
  147. :group 'org
  148. :type
  149. '(set :greedy t
  150. (const :tag " Mouse support (org-mouse.el)" org-mouse)
  151. (const :tag "<A> Publishing (org-publish.el)" org-publish)
  152. (const :tag "<A> LaTeX export (org-export-latex.el)" org-export-latex)
  153. (const :tag " IRC/ERC links (org-irc.el)" org-irc)
  154. (const :tag " Apple Mail message links under OS X (org-mac-message.el)" org-mac-message)))
  155. (defun org-load-default-extensions ()
  156. "Load all extensions that are listed in `org-default-extensions'."
  157. (mapc 'require org-default-extensions))
  158. ;; FIXME: Needs a separate group...
  159. (defcustom org-completion-fallback-command 'hippie-expand
  160. "The expansion command called by \\[org-complete] in normal context.
  161. Normal means, no org-mode-specific context."
  162. :group 'org
  163. :type 'function)
  164. (defgroup org-startup nil
  165. "Options concerning startup of Org-mode."
  166. :tag "Org Startup"
  167. :group 'org)
  168. (defcustom org-startup-folded t
  169. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  170. This can also be configured on a per-file basis by adding one of
  171. the following lines anywhere in the buffer:
  172. #+STARTUP: fold
  173. #+STARTUP: nofold
  174. #+STARTUP: content"
  175. :group 'org-startup
  176. :type '(choice
  177. (const :tag "nofold: show all" nil)
  178. (const :tag "fold: overview" t)
  179. (const :tag "content: all headlines" content)))
  180. (defcustom org-startup-truncated t
  181. "Non-nil means, entering Org-mode will set `truncate-lines'.
  182. This is useful since some lines containing links can be very long and
  183. uninteresting. Also tables look terrible when wrapped."
  184. :group 'org-startup
  185. :type 'boolean)
  186. (defcustom org-startup-align-all-tables nil
  187. "Non-nil means, align all tables when visiting a file.
  188. This is useful when the column width in tables is forced with <N> cookies
  189. in table fields. Such tables will look correct only after the first re-align.
  190. This can also be configured on a per-file basis by adding one of
  191. the following lines anywhere in the buffer:
  192. #+STARTUP: align
  193. #+STARTUP: noalign"
  194. :group 'org-startup
  195. :type 'boolean)
  196. (defcustom org-insert-mode-line-in-empty-file nil
  197. "Non-nil means insert the first line setting Org-mode in empty files.
  198. When the function `org-mode' is called interactively in an empty file, this
  199. normally means that the file name does not automatically trigger Org-mode.
  200. To ensure that the file will always be in Org-mode in the future, a
  201. line enforcing Org-mode will be inserted into the buffer, if this option
  202. has been set."
  203. :group 'org-startup
  204. :type 'boolean)
  205. (defcustom org-replace-disputed-keys nil
  206. "Non-nil means use alternative key bindings for some keys.
  207. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  208. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  209. If you want to use Org-mode together with one of these other modes,
  210. or more generally if you would like to move some Org-mode commands to
  211. other keys, set this variable and configure the keys with the variable
  212. `org-disputed-keys'.
  213. This option is only relevant at load-time of Org-mode, and must be set
  214. *before* org.el is loaded. Changing it requires a restart of Emacs to
  215. become effective."
  216. :group 'org-startup
  217. :type 'boolean)
  218. (if (fboundp 'defvaralias)
  219. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  220. (defcustom org-disputed-keys
  221. '(([(shift up)] . [(meta p)])
  222. ([(shift down)] . [(meta n)])
  223. ([(shift left)] . [(meta -)])
  224. ([(shift right)] . [(meta +)])
  225. ([(control shift right)] . [(meta shift +)])
  226. ([(control shift left)] . [(meta shift -)]))
  227. "Keys for which Org-mode and other modes compete.
  228. This is an alist, cars are the default keys, second element specifies
  229. the alternative to use when `org-replace-disputed-keys' is t.
  230. Keys can be specified in any syntax supported by `define-key'.
  231. The value of this option takes effect only at Org-mode's startup,
  232. therefore you'll have to restart Emacs to apply it after changing."
  233. :group 'org-startup
  234. :type 'alist)
  235. (defun org-key (key)
  236. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  237. Or return the original if not disputed."
  238. (if org-replace-disputed-keys
  239. (let* ((nkey (key-description key))
  240. (x (org-find-if (lambda (x)
  241. (equal (key-description (car x)) nkey))
  242. org-disputed-keys)))
  243. (if x (cdr x) key))
  244. key))
  245. (defun org-find-if (predicate seq)
  246. (catch 'exit
  247. (while seq
  248. (if (funcall predicate (car seq))
  249. (throw 'exit (car seq))
  250. (pop seq)))))
  251. (defun org-defkey (keymap key def)
  252. "Define a key, possibly translated, as returned by `org-key'."
  253. (define-key keymap (org-key key) def))
  254. (defcustom org-ellipsis nil
  255. "The ellipsis to use in the Org-mode outline.
  256. When nil, just use the standard three dots. When a string, use that instead,
  257. When a face, use the standart 3 dots, but with the specified face.
  258. The change affects only Org-mode (which will then use its own display table).
  259. Changing this requires executing `M-x org-mode' in a buffer to become
  260. effective."
  261. :group 'org-startup
  262. :type '(choice (const :tag "Default" nil)
  263. (face :tag "Face" :value org-warning)
  264. (string :tag "String" :value "...#")))
  265. (defvar org-display-table nil
  266. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  267. (defgroup org-keywords nil
  268. "Keywords in Org-mode."
  269. :tag "Org Keywords"
  270. :group 'org)
  271. (defcustom org-deadline-string "DEADLINE:"
  272. "String to mark deadline entries.
  273. A deadline is this string, followed by a time stamp. Should be a word,
  274. terminated by a colon. You can insert a schedule keyword and
  275. a timestamp with \\[org-deadline].
  276. Changes become only effective after restarting Emacs."
  277. :group 'org-keywords
  278. :type 'string)
  279. (defcustom org-scheduled-string "SCHEDULED:"
  280. "String to mark scheduled TODO entries.
  281. A schedule is this string, followed by a time stamp. Should be a word,
  282. terminated by a colon. You can insert a schedule keyword and
  283. a timestamp with \\[org-schedule].
  284. Changes become only effective after restarting Emacs."
  285. :group 'org-keywords
  286. :type 'string)
  287. (defcustom org-closed-string "CLOSED:"
  288. "String used as the prefix for timestamps logging closing a TODO entry."
  289. :group 'org-keywords
  290. :type 'string)
  291. (defcustom org-clock-string "CLOCK:"
  292. "String used as prefix for timestamps clocking work hours on an item."
  293. :group 'org-keywords
  294. :type 'string)
  295. (defcustom org-comment-string "COMMENT"
  296. "Entries starting with this keyword will never be exported.
  297. An entry can be toggled between COMMENT and normal with
  298. \\[org-toggle-comment].
  299. Changes become only effective after restarting Emacs."
  300. :group 'org-keywords
  301. :type 'string)
  302. (defcustom org-quote-string "QUOTE"
  303. "Entries starting with this keyword will be exported in fixed-width font.
  304. Quoting applies only to the text in the entry following the headline, and does
  305. not extend beyond the next headline, even if that is lower level.
  306. An entry can be toggled between QUOTE and normal with
  307. \\[org-toggle-fixed-width-section]."
  308. :group 'org-keywords
  309. :type 'string)
  310. (defconst org-repeat-re
  311. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  312. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  313. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  314. "Regular expression for specifying repeated events.
  315. After a match, group 1 contains the repeat expression.")
  316. (defgroup org-structure nil
  317. "Options concerning the general structure of Org-mode files."
  318. :tag "Org Structure"
  319. :group 'org)
  320. (defgroup org-reveal-location nil
  321. "Options about how to make context of a location visible."
  322. :tag "Org Reveal Location"
  323. :group 'org-structure)
  324. (defconst org-context-choice
  325. '(choice
  326. (const :tag "Always" t)
  327. (const :tag "Never" nil)
  328. (repeat :greedy t :tag "Individual contexts"
  329. (cons
  330. (choice :tag "Context"
  331. (const agenda)
  332. (const org-goto)
  333. (const occur-tree)
  334. (const tags-tree)
  335. (const link-search)
  336. (const mark-goto)
  337. (const bookmark-jump)
  338. (const isearch)
  339. (const default))
  340. (boolean))))
  341. "Contexts for the reveal options.")
  342. (defcustom org-show-hierarchy-above '((default . t))
  343. "Non-nil means, show full hierarchy when revealing a location.
  344. Org-mode often shows locations in an org-mode file which might have
  345. been invisible before. When this is set, the hierarchy of headings
  346. above the exposed location is shown.
  347. Turning this off for example for sparse trees makes them very compact.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. Valid contexts are
  350. agenda when exposing an entry from the agenda
  351. org-goto when using the command `org-goto' on key C-c C-j
  352. occur-tree when using the command `org-occur' on key C-c /
  353. tags-tree when constructing a sparse tree based on tags matches
  354. link-search when exposing search matches associated with a link
  355. mark-goto when exposing the jump goal of a mark
  356. bookmark-jump when exposing a bookmark location
  357. isearch when exiting from an incremental search
  358. default default for all contexts not set explicitly"
  359. :group 'org-reveal-location
  360. :type org-context-choice)
  361. (defcustom org-show-following-heading '((default . nil))
  362. "Non-nil means, show following heading when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the heading following the
  365. match is shown.
  366. Turning this off for example for sparse trees makes them very compact,
  367. but makes it harder to edit the location of the match. In such a case,
  368. use the command \\[org-reveal] to show more context.
  369. Instead of t, this can also be an alist specifying this option for different
  370. contexts. See `org-show-hierarchy-above' for valid contexts."
  371. :group 'org-reveal-location
  372. :type org-context-choice)
  373. (defcustom org-show-siblings '((default . nil) (isearch t))
  374. "Non-nil means, show all sibling heading when revealing a location.
  375. Org-mode often shows locations in an org-mode file which might have
  376. been invisible before. When this is set, the sibling of the current entry
  377. heading are all made visible. If `org-show-hierarchy-above' is t,
  378. the same happens on each level of the hierarchy above the current entry.
  379. By default this is on for the isearch context, off for all other contexts.
  380. Turning this off for example for sparse trees makes them very compact,
  381. but makes it harder to edit the location of the match. In such a case,
  382. use the command \\[org-reveal] to show more context.
  383. Instead of t, this can also be an alist specifying this option for different
  384. contexts. See `org-show-hierarchy-above' for valid contexts."
  385. :group 'org-reveal-location
  386. :type org-context-choice)
  387. (defcustom org-show-entry-below '((default . nil))
  388. "Non-nil means, show the entry below a headline when revealing a location.
  389. Org-mode often shows locations in an org-mode file which might have
  390. been invisible before. When this is set, the text below the headline that is
  391. exposed is also shown.
  392. By default this is off for all contexts.
  393. Instead of t, this can also be an alist specifying this option for different
  394. contexts. See `org-show-hierarchy-above' for valid contexts."
  395. :group 'org-reveal-location
  396. :type org-context-choice)
  397. (defgroup org-cycle nil
  398. "Options concerning visibility cycling in Org-mode."
  399. :tag "Org Cycle"
  400. :group 'org-structure)
  401. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  402. "Names of drawers. Drawers are not opened by cycling on the headline above.
  403. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  404. this:
  405. :DRAWERNAME:
  406. .....
  407. :END:
  408. The drawer \"PROPERTIES\" is special for capturing properties through
  409. the property API.
  410. Drawers can be defined on the per-file basis with a line like:
  411. #+DRAWERS: HIDDEN STATE PROPERTIES"
  412. :group 'org-structure
  413. :type '(repeat (string :tag "Drawer Name")))
  414. (defcustom org-cycle-global-at-bob nil
  415. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  416. This makes it possible to do global cycling without having to use S-TAB or
  417. C-u TAB. For this special case to work, the first line of the buffer
  418. must not be a headline - it may be empty ot some other text. When used in
  419. this way, `org-cycle-hook' is disables temporarily, to make sure the
  420. cursor stays at the beginning of the buffer.
  421. When this option is nil, don't do anything special at the beginning
  422. of the buffer."
  423. :group 'org-cycle
  424. :type 'boolean)
  425. (defcustom org-cycle-emulate-tab t
  426. "Where should `org-cycle' emulate TAB.
  427. nil Never
  428. white Only in completely white lines
  429. whitestart Only at the beginning of lines, before the first non-white char
  430. t Everywhere except in headlines
  431. exc-hl-bol Everywhere except at the start of a headline
  432. If TAB is used in a place where it does not emulate TAB, the current subtree
  433. visibility is cycled."
  434. :group 'org-cycle
  435. :type '(choice (const :tag "Never" nil)
  436. (const :tag "Only in completely white lines" white)
  437. (const :tag "Before first char in a line" whitestart)
  438. (const :tag "Everywhere except in headlines" t)
  439. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  440. ))
  441. (defcustom org-cycle-separator-lines 2
  442. "Number of empty lines needed to keep an empty line between collapsed trees.
  443. If you leave an empty line between the end of a subtree and the following
  444. headline, this empty line is hidden when the subtree is folded.
  445. Org-mode will leave (exactly) one empty line visible if the number of
  446. empty lines is equal or larger to the number given in this variable.
  447. So the default 2 means, at least 2 empty lines after the end of a subtree
  448. are needed to produce free space between a collapsed subtree and the
  449. following headline.
  450. Special case: when 0, never leave empty lines in collapsed view."
  451. :group 'org-cycle
  452. :type 'integer)
  453. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  454. org-cycle-hide-drawers
  455. org-cycle-show-empty-lines
  456. org-optimize-window-after-visibility-change)
  457. "Hook that is run after `org-cycle' has changed the buffer visibility.
  458. The function(s) in this hook must accept a single argument which indicates
  459. the new state that was set by the most recent `org-cycle' command. The
  460. argument is a symbol. After a global state change, it can have the values
  461. `overview', `content', or `all'. After a local state change, it can have
  462. the values `folded', `children', or `subtree'."
  463. :group 'org-cycle
  464. :type 'hook)
  465. (defgroup org-edit-structure nil
  466. "Options concerning structure editing in Org-mode."
  467. :tag "Org Edit Structure"
  468. :group 'org-structure)
  469. (defcustom org-odd-levels-only nil
  470. "Non-nil means, skip even levels and only use odd levels for the outline.
  471. This has the effect that two stars are being added/taken away in
  472. promotion/demotion commands. It also influences how levels are
  473. handled by the exporters.
  474. Changing it requires restart of `font-lock-mode' to become effective
  475. for fontification also in regions already fontified.
  476. You may also set this on a per-file basis by adding one of the following
  477. lines to the buffer:
  478. #+STARTUP: odd
  479. #+STARTUP: oddeven"
  480. :group 'org-edit-structure
  481. :group 'org-font-lock
  482. :type 'boolean)
  483. (defcustom org-adapt-indentation t
  484. "Non-nil means, adapt indentation when promoting and demoting.
  485. When this is set and the *entire* text in an entry is indented, the
  486. indentation is increased by one space in a demotion command, and
  487. decreased by one in a promotion command. If any line in the entry
  488. body starts at column 0, indentation is not changed at all."
  489. :group 'org-edit-structure
  490. :type 'boolean)
  491. (defcustom org-special-ctrl-a/e nil
  492. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  493. When t, `C-a' will bring back the cursor to the beginning of the
  494. headline text, i.e. after the stars and after a possible TODO keyword.
  495. In an item, this will be the position after the bullet.
  496. When the cursor is already at that position, another `C-a' will bring
  497. it to the beginning of the line.
  498. `C-e' will jump to the end of the headline, ignoring the presence of tags
  499. in the headline. A second `C-e' will then jump to the true end of the
  500. line, after any tags.
  501. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  502. and only a directly following, identical keypress will bring the cursor
  503. to the special positions."
  504. :group 'org-edit-structure
  505. :type '(choice
  506. (const :tag "off" nil)
  507. (const :tag "after bullet first" t)
  508. (const :tag "border first" reversed)))
  509. (if (fboundp 'defvaralias)
  510. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  511. (defcustom org-special-ctrl-k nil
  512. "Non-nil means `C-k' will behave specially in headlines.
  513. When nil, `C-k' will call the default `kill-line' command.
  514. When t, the following will happen while the cursor is in the headline:
  515. - When the cursor is at the beginning of a headline, kill the entire
  516. line and possible the folded subtree below the line.
  517. - When in the middle of the headline text, kill the headline up to the tags.
  518. - When after the headline text, kill the tags."
  519. :group 'org-edit-structure
  520. :type 'boolean)
  521. (defcustom org-M-RET-may-split-line '((default . t))
  522. "Non-nil means, M-RET will split the line at the cursor position.
  523. When nil, it will go to the end of the line before making a
  524. new line.
  525. You may also set this option in a different way for different
  526. contexts. Valid contexts are:
  527. headline when creating a new headline
  528. item when creating a new item
  529. table in a table field
  530. default the value to be used for all contexts not explicitly
  531. customized"
  532. :group 'org-structure
  533. :group 'org-table
  534. :type '(choice
  535. (const :tag "Always" t)
  536. (const :tag "Never" nil)
  537. (repeat :greedy t :tag "Individual contexts"
  538. (cons
  539. (choice :tag "Context"
  540. (const headline)
  541. (const item)
  542. (const table)
  543. (const default))
  544. (boolean)))))
  545. (defcustom org-blank-before-new-entry '((heading . nil)
  546. (plain-list-item . nil))
  547. "Should `org-insert-heading' leave a blank line before new heading/item?
  548. The value is an alist, with `heading' and `plain-list-item' as car,
  549. and a boolean flag as cdr."
  550. :group 'org-edit-structure
  551. :type '(list
  552. (cons (const heading) (boolean))
  553. (cons (const plain-list-item) (boolean))))
  554. (defcustom org-insert-heading-hook nil
  555. "Hook being run after inserting a new heading."
  556. :group 'org-edit-structure
  557. :type 'hook)
  558. (defcustom org-enable-fixed-width-editor t
  559. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  560. This currently only means, they are never auto-wrapped.
  561. When nil, such lines will be treated like ordinary lines.
  562. See also the QUOTE keyword."
  563. :group 'org-edit-structure
  564. :type 'boolean)
  565. (defcustom org-goto-auto-isearch t
  566. "Non-nil means, typing characters in org-goto starts incremental search."
  567. :group 'org-edit-structure
  568. :type 'boolean)
  569. (defgroup org-sparse-trees nil
  570. "Options concerning sparse trees in Org-mode."
  571. :tag "Org Sparse Trees"
  572. :group 'org-structure)
  573. (defcustom org-highlight-sparse-tree-matches t
  574. "Non-nil means, highlight all matches that define a sparse tree.
  575. The highlights will automatically disappear the next time the buffer is
  576. changed by an edit command."
  577. :group 'org-sparse-trees
  578. :type 'boolean)
  579. (defcustom org-remove-highlights-with-change t
  580. "Non-nil means, any change to the buffer will remove temporary highlights.
  581. Such highlights are created by `org-occur' and `org-clock-display'.
  582. When nil, `C-c C-c needs to be used to get rid of the highlights.
  583. The highlights created by `org-preview-latex-fragment' always need
  584. `C-c C-c' to be removed."
  585. :group 'org-sparse-trees
  586. :group 'org-time
  587. :type 'boolean)
  588. (defcustom org-occur-hook '(org-first-headline-recenter)
  589. "Hook that is run after `org-occur' has constructed a sparse tree.
  590. This can be used to recenter the window to show as much of the structure
  591. as possible."
  592. :group 'org-sparse-trees
  593. :type 'hook)
  594. (defgroup org-plain-lists nil
  595. "Options concerning plain lists in Org-mode."
  596. :tag "Org Plain lists"
  597. :group 'org-structure)
  598. (defcustom org-cycle-include-plain-lists nil
  599. "Non-nil means, include plain lists into visibility cycling.
  600. This means that during cycling, plain list items will *temporarily* be
  601. interpreted as outline headlines with a level given by 1000+i where i is the
  602. indentation of the bullet. In all other operations, plain list items are
  603. not seen as headlines. For example, you cannot assign a TODO keyword to
  604. such an item."
  605. :group 'org-plain-lists
  606. :type 'boolean)
  607. (defcustom org-plain-list-ordered-item-terminator t
  608. "The character that makes a line with leading number an ordered list item.
  609. Valid values are ?. and ?\). To get both terminators, use t. While
  610. ?. may look nicer, it creates the danger that a line with leading
  611. number may be incorrectly interpreted as an item. ?\) therefore is
  612. the safe choice."
  613. :group 'org-plain-lists
  614. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  615. (const :tag "paren like in \"2)\"" ?\))
  616. (const :tab "both" t)))
  617. (defcustom org-auto-renumber-ordered-lists t
  618. "Non-nil means, automatically renumber ordered plain lists.
  619. Renumbering happens when the sequence have been changed with
  620. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  621. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  622. :group 'org-plain-lists
  623. :type 'boolean)
  624. (defcustom org-provide-checkbox-statistics t
  625. "Non-nil means, update checkbox statistics after insert and toggle.
  626. When this is set, checkbox statistics is updated each time you either insert
  627. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  628. with \\[org-ctrl-c-ctrl-c\\]."
  629. :group 'org-plain-lists
  630. :type 'boolean)
  631. (defgroup org-archive nil
  632. "Options concerning archiving in Org-mode."
  633. :tag "Org Archive"
  634. :group 'org-structure)
  635. (defcustom org-archive-tag "ARCHIVE"
  636. "The tag that marks a subtree as archived.
  637. An archived subtree does not open during visibility cycling, and does
  638. not contribute to the agenda listings.
  639. After changing this, font-lock must be restarted in the relevant buffers to
  640. get the proper fontification."
  641. :group 'org-archive
  642. :group 'org-keywords
  643. :type 'string)
  644. (defcustom org-agenda-skip-archived-trees t
  645. "Non-nil means, the agenda will skip any items located in archived trees.
  646. An archived tree is a tree marked with the tag ARCHIVE."
  647. :group 'org-archive
  648. :group 'org-agenda-skip
  649. :type 'boolean)
  650. (defcustom org-cycle-open-archived-trees nil
  651. "Non-nil means, `org-cycle' will open archived trees.
  652. An archived tree is a tree marked with the tag ARCHIVE.
  653. When nil, archived trees will stay folded. You can still open them with
  654. normal outline commands like `show-all', but not with the cycling commands."
  655. :group 'org-archive
  656. :group 'org-cycle
  657. :type 'boolean)
  658. (defcustom org-sparse-tree-open-archived-trees nil
  659. "Non-nil means sparse tree construction shows matches in archived trees.
  660. When nil, matches in these trees are highlighted, but the trees are kept in
  661. collapsed state."
  662. :group 'org-archive
  663. :group 'org-sparse-trees
  664. :type 'boolean)
  665. (defcustom org-archive-location "%s_archive::"
  666. "The location where subtrees should be archived.
  667. This string consists of two parts, separated by a double-colon.
  668. The first part is a file name - when omitted, archiving happens in the same
  669. file. %s will be replaced by the current file name (without directory part).
  670. Archiving to a different file is useful to keep archived entries from
  671. contributing to the Org-mode Agenda.
  672. The part after the double colon is a headline. The archived entries will be
  673. filed under that headline. When omitted, the subtrees are simply filed away
  674. at the end of the file, as top-level entries.
  675. Here are a few examples:
  676. \"%s_archive::\"
  677. If the current file is Projects.org, archive in file
  678. Projects.org_archive, as top-level trees. This is the default.
  679. \"::* Archived Tasks\"
  680. Archive in the current file, under the top-level headline
  681. \"* Archived Tasks\".
  682. \"~/org/archive.org::\"
  683. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  684. \"basement::** Finished Tasks\"
  685. Archive in file ./basement (relative path), as level 3 trees
  686. below the level 2 heading \"** Finished Tasks\".
  687. You may set this option on a per-file basis by adding to the buffer a
  688. line like
  689. #+ARCHIVE: basement::** Finished Tasks"
  690. :group 'org-archive
  691. :type 'string)
  692. (defcustom org-archive-mark-done t
  693. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  694. This can be a string to set the keyword to use. When t, Org-mode will
  695. use the first keyword in its list that means done."
  696. :group 'org-archive
  697. :type '(choice
  698. (const :tag "No" nil)
  699. (const :tag "Yes" t)
  700. (string :tag "Use this keyword")))
  701. (defcustom org-archive-stamp-time t
  702. "Non-nil means, add a time stamp to entries moved to an archive file.
  703. This variable is obsolete and has no effect anymore, instead add ot remove
  704. `time' from the variablle `org-archive-save-context-info'."
  705. :group 'org-archive
  706. :type 'boolean)
  707. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  708. "Parts of context info that should be stored as properties when archiving.
  709. When a subtree is moved to an archive file, it looses information given by
  710. context, like inherited tags, the category, and possibly also the TODO
  711. state (depending on the variable `org-archive-mark-done').
  712. This variable can be a list of any of the following symbols:
  713. time The time of archiving.
  714. file The file where the entry originates.
  715. itags The local tags, in the headline of the subtree.
  716. ltags The tags the subtree inherits from further up the hierarchy.
  717. todo The pre-archive TODO state.
  718. category The category, taken from file name or #+CATEGORY lines.
  719. olpath The outline path to the item. These are all headlines above
  720. the current item, separated by /, like a file path.
  721. For each symbol present in the list, a property will be created in
  722. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  723. information."
  724. :group 'org-archive
  725. :type '(set :greedy t
  726. (const :tag "Time" time)
  727. (const :tag "File" file)
  728. (const :tag "Category" category)
  729. (const :tag "TODO state" todo)
  730. (const :tag "TODO state" priority)
  731. (const :tag "Inherited tags" itags)
  732. (const :tag "Outline path" olpath)
  733. (const :tag "Local tags" ltags)))
  734. (defgroup org-imenu-and-speedbar nil
  735. "Options concerning imenu and speedbar in Org-mode."
  736. :tag "Org Imenu and Speedbar"
  737. :group 'org-structure)
  738. (defcustom org-imenu-depth 2
  739. "The maximum level for Imenu access to Org-mode headlines.
  740. This also applied for speedbar access."
  741. :group 'org-imenu-and-speedbar
  742. :type 'number)
  743. (defgroup org-table nil
  744. "Options concerning tables in Org-mode."
  745. :tag "Org Table"
  746. :group 'org)
  747. (defcustom org-enable-table-editor 'optimized
  748. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  749. When nil, such lines will be treated like ordinary lines.
  750. When equal to the symbol `optimized', the table editor will be optimized to
  751. do the following:
  752. - Automatic overwrite mode in front of whitespace in table fields.
  753. This makes the structure of the table stay in tact as long as the edited
  754. field does not exceed the column width.
  755. - Minimize the number of realigns. Normally, the table is aligned each time
  756. TAB or RET are pressed to move to another field. With optimization this
  757. happens only if changes to a field might have changed the column width.
  758. Optimization requires replacing the functions `self-insert-command',
  759. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  760. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  761. very good at guessing when a re-align will be necessary, but you can always
  762. force one with \\[org-ctrl-c-ctrl-c].
  763. If you would like to use the optimized version in Org-mode, but the
  764. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  765. This variable can be used to turn on and off the table editor during a session,
  766. but in order to toggle optimization, a restart is required.
  767. See also the variable `org-table-auto-blank-field'."
  768. :group 'org-table
  769. :type '(choice
  770. (const :tag "off" nil)
  771. (const :tag "on" t)
  772. (const :tag "on, optimized" optimized)))
  773. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  774. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  775. In the optimized version, the table editor takes over all simple keys that
  776. normally just insert a character. In tables, the characters are inserted
  777. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  778. for empty fields). Outside tables, the correct binding of the keys is
  779. restored.
  780. The default for this option is t if the optimized version is also used in
  781. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  782. this variable requires a restart of Emacs to become effective."
  783. :group 'org-table
  784. :type 'boolean)
  785. (defcustom orgtbl-radio-table-templates
  786. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  787. % END RECEIVE ORGTBL %n
  788. \\begin{comment}
  789. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  790. | | |
  791. \\end{comment}\n")
  792. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  793. @c END RECEIVE ORGTBL %n
  794. @ignore
  795. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  796. | | |
  797. @end ignore\n")
  798. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  799. <!-- END RECEIVE ORGTBL %n -->
  800. <!--
  801. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  802. | | |
  803. -->\n"))
  804. "Templates for radio tables in different major modes.
  805. All occurrences of %n in a template will be replaced with the name of the
  806. table, obtained by prompting the user."
  807. :group 'org-table
  808. :type '(repeat
  809. (list (symbol :tag "Major mode")
  810. (string :tag "Format"))))
  811. (defgroup org-table-settings nil
  812. "Settings for tables in Org-mode."
  813. :tag "Org Table Settings"
  814. :group 'org-table)
  815. (defcustom org-table-default-size "5x2"
  816. "The default size for newly created tables, Columns x Rows."
  817. :group 'org-table-settings
  818. :type 'string)
  819. (defcustom org-table-number-regexp
  820. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  821. "Regular expression for recognizing numbers in table columns.
  822. If a table column contains mostly numbers, it will be aligned to the
  823. right. If not, it will be aligned to the left.
  824. The default value of this option is a regular expression which allows
  825. anything which looks remotely like a number as used in scientific
  826. context. For example, all of the following will be considered a
  827. number:
  828. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  829. Other options offered by the customize interface are more restrictive."
  830. :group 'org-table-settings
  831. :type '(choice
  832. (const :tag "Positive Integers"
  833. "^[0-9]+$")
  834. (const :tag "Integers"
  835. "^[-+]?[0-9]+$")
  836. (const :tag "Floating Point Numbers"
  837. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  838. (const :tag "Floating Point Number or Integer"
  839. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  840. (const :tag "Exponential, Floating point, Integer"
  841. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  842. (const :tag "Very General Number-Like, including hex"
  843. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  844. (string :tag "Regexp:")))
  845. (defcustom org-table-number-fraction 0.5
  846. "Fraction of numbers in a column required to make the column align right.
  847. In a column all non-white fields are considered. If at least this
  848. fraction of fields is matched by `org-table-number-fraction',
  849. alignment to the right border applies."
  850. :group 'org-table-settings
  851. :type 'number)
  852. (defgroup org-table-editing nil
  853. "Behavior of tables during editing in Org-mode."
  854. :tag "Org Table Editing"
  855. :group 'org-table)
  856. (defcustom org-table-automatic-realign t
  857. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  858. When nil, aligning is only done with \\[org-table-align], or after column
  859. removal/insertion."
  860. :group 'org-table-editing
  861. :type 'boolean)
  862. (defcustom org-table-auto-blank-field t
  863. "Non-nil means, automatically blank table field when starting to type into it.
  864. This only happens when typing immediately after a field motion
  865. command (TAB, S-TAB or RET).
  866. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  867. :group 'org-table-editing
  868. :type 'boolean)
  869. (defcustom org-table-tab-jumps-over-hlines t
  870. "Non-nil means, tab in the last column of a table with jump over a hline.
  871. If a horizontal separator line is following the current line,
  872. `org-table-next-field' can either create a new row before that line, or jump
  873. over the line. When this option is nil, a new line will be created before
  874. this line."
  875. :group 'org-table-editing
  876. :type 'boolean)
  877. (defcustom org-table-tab-recognizes-table.el t
  878. "Non-nil means, TAB will automatically notice a table.el table.
  879. When it sees such a table, it moves point into it and - if necessary -
  880. calls `table-recognize-table'."
  881. :group 'org-table-editing
  882. :type 'boolean)
  883. (defgroup org-table-calculation nil
  884. "Options concerning tables in Org-mode."
  885. :tag "Org Table Calculation"
  886. :group 'org-table)
  887. (defcustom org-table-use-standard-references t
  888. "Should org-mode work with table refrences like B3 instead of @3$2?
  889. Possible values are:
  890. nil never use them
  891. from accept as input, do not present for editing
  892. t: accept as input and present for editing"
  893. :group 'org-table-calculation
  894. :type '(choice
  895. (const :tag "Never, don't even check unser input for them" nil)
  896. (const :tag "Always, both as user input, and when editing" t)
  897. (const :tag "Convert user input, don't offer during editing" 'from)))
  898. (defcustom org-table-copy-increment t
  899. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  900. :group 'org-table-calculation
  901. :type 'boolean)
  902. (defcustom org-calc-default-modes
  903. '(calc-internal-prec 12
  904. calc-float-format (float 5)
  905. calc-angle-mode deg
  906. calc-prefer-frac nil
  907. calc-symbolic-mode nil
  908. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  909. calc-display-working-message t
  910. )
  911. "List with Calc mode settings for use in calc-eval for table formulas.
  912. The list must contain alternating symbols (Calc modes variables and values).
  913. Don't remove any of the default settings, just change the values. Org-mode
  914. relies on the variables to be present in the list."
  915. :group 'org-table-calculation
  916. :type 'plist)
  917. (defcustom org-table-formula-evaluate-inline t
  918. "Non-nil means, TAB and RET evaluate a formula in current table field.
  919. If the current field starts with an equal sign, it is assumed to be a formula
  920. which should be evaluated as described in the manual and in the documentation
  921. string of the command `org-table-eval-formula'. This feature requires the
  922. Emacs calc package.
  923. When this variable is nil, formula calculation is only available through
  924. the command \\[org-table-eval-formula]."
  925. :group 'org-table-calculation
  926. :type 'boolean)
  927. (defcustom org-table-formula-use-constants t
  928. "Non-nil means, interpret constants in formulas in tables.
  929. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  930. by the value given in `org-table-formula-constants', or by a value obtained
  931. from the `constants.el' package."
  932. :group 'org-table-calculation
  933. :type 'boolean)
  934. (defcustom org-table-formula-constants nil
  935. "Alist with constant names and values, for use in table formulas.
  936. The car of each element is a name of a constant, without the `$' before it.
  937. The cdr is the value as a string. For example, if you'd like to use the
  938. speed of light in a formula, you would configure
  939. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  940. and then use it in an equation like `$1*$c'.
  941. Constants can also be defined on a per-file basis using a line like
  942. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  943. :group 'org-table-calculation
  944. :type '(repeat
  945. (cons (string :tag "name")
  946. (string :tag "value"))))
  947. (defvar org-table-formula-constants-local nil
  948. "Local version of `org-table-formula-constants'.")
  949. (make-variable-buffer-local 'org-table-formula-constants-local)
  950. (defcustom org-table-allow-automatic-line-recalculation t
  951. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  952. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  953. :group 'org-table-calculation
  954. :type 'boolean)
  955. (defgroup org-link nil
  956. "Options concerning links in Org-mode."
  957. :tag "Org Link"
  958. :group 'org)
  959. (defvar org-link-abbrev-alist-local nil
  960. "Buffer-local version of `org-link-abbrev-alist', which see.
  961. The value of this is taken from the #+LINK lines.")
  962. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  963. (defcustom org-link-abbrev-alist nil
  964. "Alist of link abbreviations.
  965. The car of each element is a string, to be replaced at the start of a link.
  966. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  967. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  968. [[linkkey:tag][description]]
  969. If REPLACE is a string, the tag will simply be appended to create the link.
  970. If the string contains \"%s\", the tag will be inserted there.
  971. REPLACE may also be a function that will be called with the tag as the
  972. only argument to create the link, which should be returned as a string.
  973. See the manual for examples."
  974. :group 'org-link
  975. :type 'alist)
  976. (defcustom org-descriptive-links t
  977. "Non-nil means, hide link part and only show description of bracket links.
  978. Bracket links are like [[link][descritpion]]. This variable sets the initial
  979. state in new org-mode buffers. The setting can then be toggled on a
  980. per-buffer basis from the Org->Hyperlinks menu."
  981. :group 'org-link
  982. :type 'boolean)
  983. (defcustom org-link-file-path-type 'adaptive
  984. "How the path name in file links should be stored.
  985. Valid values are:
  986. relative Relative to the current directory, i.e. the directory of the file
  987. into which the link is being inserted.
  988. absolute Absolute path, if possible with ~ for home directory.
  989. noabbrev Absolute path, no abbreviation of home directory.
  990. adaptive Use relative path for files in the current directory and sub-
  991. directories of it. For other files, use an absolute path."
  992. :group 'org-link
  993. :type '(choice
  994. (const relative)
  995. (const absolute)
  996. (const noabbrev)
  997. (const adaptive)))
  998. (defcustom org-activate-links '(bracket angle plain radio tag date)
  999. "Types of links that should be activated in Org-mode files.
  1000. This is a list of symbols, each leading to the activation of a certain link
  1001. type. In principle, it does not hurt to turn on most link types - there may
  1002. be a small gain when turning off unused link types. The types are:
  1003. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1004. angular Links in angular brackes that may contain whitespace like
  1005. <bbdb:Carsten Dominik>.
  1006. plain Plain links in normal text, no whitespace, like http://google.com.
  1007. radio Text that is matched by a radio target, see manual for details.
  1008. tag Tag settings in a headline (link to tag search).
  1009. date Time stamps (link to calendar).
  1010. Changing this variable requires a restart of Emacs to become effective."
  1011. :group 'org-link
  1012. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1013. (const :tag "Angular bracket links (old style)" angular)
  1014. (const :tag "Plain text links" plain)
  1015. (const :tag "Radio target matches" radio)
  1016. (const :tag "Tags" tag)
  1017. (const :tag "Timestamps" date)))
  1018. (defgroup org-link-store nil
  1019. "Options concerning storing links in Org-mode"
  1020. :tag "Org Store Link"
  1021. :group 'org-link)
  1022. (defcustom org-email-link-description-format "Email %c: %.30s"
  1023. "Format of the description part of a link to an email or usenet message.
  1024. The following %-excapes will be replaced by corresponding information:
  1025. %F full \"From\" field
  1026. %f name, taken from \"From\" field, address if no name
  1027. %T full \"To\" field
  1028. %t first name in \"To\" field, address if no name
  1029. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1030. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1031. %s subject
  1032. %m message-id.
  1033. You may use normal field width specification between the % and the letter.
  1034. This is for example useful to limit the length of the subject.
  1035. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1036. :group 'org-link-store
  1037. :type 'string)
  1038. (defcustom org-from-is-user-regexp
  1039. (let (r1 r2)
  1040. (when (and user-mail-address (not (string= user-mail-address "")))
  1041. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1042. (when (and user-full-name (not (string= user-full-name "")))
  1043. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1044. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1045. "Regexp mached against the \"From:\" header of an email or usenet message.
  1046. It should match if the message is from the user him/herself."
  1047. :group 'org-link-store
  1048. :type 'regexp)
  1049. (defcustom org-context-in-file-links t
  1050. "Non-nil means, file links from `org-store-link' contain context.
  1051. A search string will be added to the file name with :: as separator and
  1052. used to find the context when the link is activated by the command
  1053. `org-open-at-point'.
  1054. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1055. negates this setting for the duration of the command."
  1056. :group 'org-link-store
  1057. :type 'boolean)
  1058. (defcustom org-keep-stored-link-after-insertion nil
  1059. "Non-nil means, keep link in list for entire session.
  1060. The command `org-store-link' adds a link pointing to the current
  1061. location to an internal list. These links accumulate during a session.
  1062. The command `org-insert-link' can be used to insert links into any
  1063. Org-mode file (offering completion for all stored links). When this
  1064. option is nil, every link which has been inserted once using \\[org-insert-link]
  1065. will be removed from the list, to make completing the unused links
  1066. more efficient."
  1067. :group 'org-link-store
  1068. :type 'boolean)
  1069. (defcustom org-usenet-links-prefer-google nil
  1070. "Non-nil means, `org-store-link' will create web links to Google groups.
  1071. When nil, Gnus will be used for such links.
  1072. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1073. negates this setting for the duration of the command."
  1074. :group 'org-link-store
  1075. :type 'boolean)
  1076. (defgroup org-link-follow nil
  1077. "Options concerning following links in Org-mode"
  1078. :tag "Org Follow Link"
  1079. :group 'org-link)
  1080. (defcustom org-tab-follows-link nil
  1081. "Non-nil means, on links TAB will follow the link.
  1082. Needs to be set before org.el is loaded."
  1083. :group 'org-link-follow
  1084. :type 'boolean)
  1085. (defcustom org-return-follows-link nil
  1086. "Non-nil means, on links RET will follow the link.
  1087. Needs to be set before org.el is loaded."
  1088. :group 'org-link-follow
  1089. :type 'boolean)
  1090. (defcustom org-mouse-1-follows-link t
  1091. "Non-nil means, mouse-1 on a link will follow the link.
  1092. A longer mouse click will still set point. Does not wortk on XEmacs.
  1093. Needs to be set before org.el is loaded."
  1094. :group 'org-link-follow
  1095. :type 'boolean)
  1096. (defcustom org-mark-ring-length 4
  1097. "Number of different positions to be recorded in the ring
  1098. Changing this requires a restart of Emacs to work correctly."
  1099. :group 'org-link-follow
  1100. :type 'interger)
  1101. (defcustom org-link-frame-setup
  1102. '((vm . vm-visit-folder-other-frame)
  1103. (gnus . gnus-other-frame)
  1104. (file . find-file-other-window))
  1105. "Setup the frame configuration for following links.
  1106. When following a link with Emacs, it may often be useful to display
  1107. this link in another window or frame. This variable can be used to
  1108. set this up for the different types of links.
  1109. For VM, use any of
  1110. `vm-visit-folder'
  1111. `vm-visit-folder-other-frame'
  1112. For Gnus, use any of
  1113. `gnus'
  1114. `gnus-other-frame'
  1115. For FILE, use any of
  1116. `find-file'
  1117. `find-file-other-window'
  1118. `find-file-other-frame'
  1119. For the calendar, use the variable `calendar-setup'.
  1120. For BBDB, it is currently only possible to display the matches in
  1121. another window."
  1122. :group 'org-link-follow
  1123. :type '(list
  1124. (cons (const vm)
  1125. (choice
  1126. (const vm-visit-folder)
  1127. (const vm-visit-folder-other-window)
  1128. (const vm-visit-folder-other-frame)))
  1129. (cons (const gnus)
  1130. (choice
  1131. (const gnus)
  1132. (const gnus-other-frame)))
  1133. (cons (const file)
  1134. (choice
  1135. (const find-file)
  1136. (const find-file-other-window)
  1137. (const find-file-other-frame)))))
  1138. (defcustom org-display-internal-link-with-indirect-buffer nil
  1139. "Non-nil means, use indirect buffer to display infile links.
  1140. Activating internal links (from one location in a file to another location
  1141. in the same file) normally just jumps to the location. When the link is
  1142. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1143. another window. When this option is set, the other window actually displays
  1144. an indirect buffer clone of the current buffer, to avoid any visibility
  1145. changes to the current buffer."
  1146. :group 'org-link-follow
  1147. :type 'boolean)
  1148. (defcustom org-open-non-existing-files nil
  1149. "Non-nil means, `org-open-file' will open non-existing files.
  1150. When nil, an error will be generated."
  1151. :group 'org-link-follow
  1152. :type 'boolean)
  1153. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1154. "Function and arguments to call for following mailto links.
  1155. This is a list with the first element being a lisp function, and the
  1156. remaining elements being arguments to the function. In string arguments,
  1157. %a will be replaced by the address, and %s will be replaced by the subject
  1158. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1159. :group 'org-link-follow
  1160. :type '(choice
  1161. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1162. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1163. (const :tag "message-mail" (message-mail "%a" "%s"))
  1164. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1165. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1166. "Non-nil means, ask for confirmation before executing shell links.
  1167. Shell links can be dangerous: just think about a link
  1168. [[shell:rm -rf ~/*][Google Search]]
  1169. This link would show up in your Org-mode document as \"Google Search\",
  1170. but really it would remove your entire home directory.
  1171. Therefore we advise against setting this variable to nil.
  1172. Just change it to `y-or-n-p' of you want to confirm with a
  1173. single keystroke rather than having to type \"yes\"."
  1174. :group 'org-link-follow
  1175. :type '(choice
  1176. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1177. (const :tag "with y-or-n (faster)" y-or-n-p)
  1178. (const :tag "no confirmation (dangerous)" nil)))
  1179. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1180. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1181. Elisp links can be dangerous: just think about a link
  1182. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1183. This link would show up in your Org-mode document as \"Google Search\",
  1184. but really it would remove your entire home directory.
  1185. Therefore we advise against setting this variable to nil.
  1186. Just change it to `y-or-n-p' of you want to confirm with a
  1187. single keystroke rather than having to type \"yes\"."
  1188. :group 'org-link-follow
  1189. :type '(choice
  1190. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1191. (const :tag "with y-or-n (faster)" y-or-n-p)
  1192. (const :tag "no confirmation (dangerous)" nil)))
  1193. (defconst org-file-apps-defaults-gnu
  1194. '((remote . emacs)
  1195. (t . mailcap))
  1196. "Default file applications on a UNIX or GNU/Linux system.
  1197. See `org-file-apps'.")
  1198. (defconst org-file-apps-defaults-macosx
  1199. '((remote . emacs)
  1200. (t . "open %s")
  1201. ("ps" . "gv %s")
  1202. ("ps.gz" . "gv %s")
  1203. ("eps" . "gv %s")
  1204. ("eps.gz" . "gv %s")
  1205. ("dvi" . "xdvi %s")
  1206. ("fig" . "xfig %s"))
  1207. "Default file applications on a MacOS X system.
  1208. The system \"open\" is known as a default, but we use X11 applications
  1209. for some files for which the OS does not have a good default.
  1210. See `org-file-apps'.")
  1211. (defconst org-file-apps-defaults-windowsnt
  1212. (list
  1213. '(remote . emacs)
  1214. (cons t
  1215. (list (if (featurep 'xemacs)
  1216. 'mswindows-shell-execute
  1217. 'w32-shell-execute)
  1218. "open" 'file)))
  1219. "Default file applications on a Windows NT system.
  1220. The system \"open\" is used for most files.
  1221. See `org-file-apps'.")
  1222. (defcustom org-file-apps
  1223. '(
  1224. ("txt" . emacs)
  1225. ("tex" . emacs)
  1226. ("ltx" . emacs)
  1227. ("org" . emacs)
  1228. ("el" . emacs)
  1229. ("bib" . emacs)
  1230. )
  1231. "External applications for opening `file:path' items in a document.
  1232. Org-mode uses system defaults for different file types, but
  1233. you can use this variable to set the application for a given file
  1234. extension. The entries in this list are cons cells where the car identifies
  1235. files and the cdr the corresponding command. Possible values for the
  1236. file identifier are
  1237. \"ext\" A string identifying an extension
  1238. `directory' Matches a directory
  1239. `remote' Matches a remote file, accessible through tramp or efs.
  1240. Remote files most likely should be visited through Emacs
  1241. because external applications cannot handle such paths.
  1242. t Default for all remaining files
  1243. Possible values for the command are:
  1244. `emacs' The file will be visited by the current Emacs process.
  1245. `default' Use the default application for this file type.
  1246. string A command to be executed by a shell; %s will be replaced
  1247. by the path to the file.
  1248. sexp A Lisp form which will be evaluated. The file path will
  1249. be available in the Lisp variable `file'.
  1250. For more examples, see the system specific constants
  1251. `org-file-apps-defaults-macosx'
  1252. `org-file-apps-defaults-windowsnt'
  1253. `org-file-apps-defaults-gnu'."
  1254. :group 'org-link-follow
  1255. :type '(repeat
  1256. (cons (choice :value ""
  1257. (string :tag "Extension")
  1258. (const :tag "Default for unrecognized files" t)
  1259. (const :tag "Remote file" remote)
  1260. (const :tag "Links to a directory" directory))
  1261. (choice :value ""
  1262. (const :tag "Visit with Emacs" emacs)
  1263. (const :tag "Use system default" default)
  1264. (string :tag "Command")
  1265. (sexp :tag "Lisp form")))))
  1266. (defcustom org-mhe-search-all-folders nil
  1267. "Non-nil means, that the search for the mh-message will be extended to
  1268. all folders if the message cannot be found in the folder given in the link.
  1269. Searching all folders is very efficient with one of the search engines
  1270. supported by MH-E, but will be slow with pick."
  1271. :group 'org-link-follow
  1272. :type 'boolean)
  1273. (defgroup org-remember nil
  1274. "Options concerning interaction with remember.el."
  1275. :tag "Org Remember"
  1276. :group 'org)
  1277. (defcustom org-directory "~/org"
  1278. "Directory with org files.
  1279. This directory will be used as default to prompt for org files.
  1280. Used by the hooks for remember.el."
  1281. :group 'org-remember
  1282. :type 'directory)
  1283. (defcustom org-default-notes-file "~/.notes"
  1284. "Default target for storing notes.
  1285. Used by the hooks for remember.el. This can be a string, or nil to mean
  1286. the value of `remember-data-file'.
  1287. You can set this on a per-template basis with the variable
  1288. `org-remember-templates'."
  1289. :group 'org-remember
  1290. :type '(choice
  1291. (const :tag "Default from remember-data-file" nil)
  1292. file))
  1293. (defcustom org-remember-store-without-prompt t
  1294. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1295. In this case, you need `C-u C-c C-c' to get the prompts for
  1296. note file and headline.
  1297. When this variable is nil, `C-c C-c' give you the prompts, and
  1298. `C-u C-c C-c' trigger the fasttrack."
  1299. :group 'org-remember
  1300. :type 'boolean)
  1301. (defcustom org-remember-interactive-interface 'refile
  1302. "The interface to be used for interactive filing of remember notes.
  1303. This is only used when the interactive mode for selecting a filing
  1304. location is used (see the variable `org-remember-store-without-prompt').
  1305. Allowed vaues are:
  1306. outline The interface shows an outline of the relevant file
  1307. and the correct heading is found by moving through
  1308. the outline or by searching with incremental search.
  1309. outline-path-completion Headlines in the current buffer are offered via
  1310. completion.
  1311. refile Use the refile interface, and offer headlines,
  1312. possibly from different buffers."
  1313. :group 'org-remember
  1314. :type '(choice
  1315. (const :tag "Refile" refile)
  1316. (const :tag "Outline" outline)
  1317. (const :tag "Outline-path-completion" outline-path-completion)))
  1318. (defcustom org-goto-interface 'outline
  1319. "The default interface to be used for `org-goto'.
  1320. Allowed vaues are:
  1321. outline The interface shows an outline of the relevant file
  1322. and the correct heading is found by moving through
  1323. the outline or by searching with incremental search.
  1324. outline-path-completion Headlines in the current buffer are offered via
  1325. completion."
  1326. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1327. :type '(choice
  1328. (const :tag "Outline" outline)
  1329. (const :tag "Outline-path-completion" outline-path-completion)))
  1330. (defcustom org-remember-default-headline ""
  1331. "The headline that should be the default location in the notes file.
  1332. When filing remember notes, the cursor will start at that position.
  1333. You can set this on a per-template basis with the variable
  1334. `org-remember-templates'."
  1335. :group 'org-remember
  1336. :type 'string)
  1337. (defcustom org-remember-templates nil
  1338. "Templates for the creation of remember buffers.
  1339. When nil, just let remember make the buffer.
  1340. When not nil, this is a list of 5-element lists. In each entry, the first
  1341. element is the name of the template, which should be a single short word.
  1342. The second element is a character, a unique key to select this template.
  1343. The third element is the template. The fourth element is optional and can
  1344. specify a destination file for remember items created with this template.
  1345. The default file is given by `org-default-notes-file'. An optional fifth
  1346. element can specify the headline in that file that should be offered
  1347. first when the user is asked to file the entry. The default headline is
  1348. given in the variable `org-remember-default-headline'.
  1349. The template specifies the structure of the remember buffer. It should have
  1350. a first line starting with a star, to act as the org-mode headline.
  1351. Furthermore, the following %-escapes will be replaced with content:
  1352. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1353. A default value and a completion table ca be specified like this:
  1354. %^{prompt|default|completion2|completion3|...}
  1355. %t time stamp, date only
  1356. %T time stamp with date and time
  1357. %u, %U like the above, but inactive time stamps
  1358. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1359. You may define a prompt like %^{Please specify birthday}t
  1360. %n user name (taken from `user-full-name')
  1361. %a annotation, normally the link created with org-store-link
  1362. %i initial content, the region active. If %i is indented,
  1363. the entire inserted text will be indented as well.
  1364. %c content of the clipboard, or current kill ring head
  1365. %^g prompt for tags, with completion on tags in target file
  1366. %^G prompt for tags, with completion all tags in all agenda files
  1367. %:keyword specific information for certain link types, see below
  1368. %[pathname] insert the contents of the file given by `pathname'
  1369. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1370. %! Store this note immediately after filling the template
  1371. %? After completing the template, position cursor here.
  1372. Apart from these general escapes, you can access information specific to the
  1373. link type that is created. For example, calling `remember' in emails or gnus
  1374. will record the author and the subject of the message, which you can access
  1375. with %:author and %:subject, respectively. Here is a complete list of what
  1376. is recorded for each link type.
  1377. Link type | Available information
  1378. -------------------+------------------------------------------------------
  1379. bbdb | %:type %:name %:company
  1380. vm, wl, mh, rmail | %:type %:subject %:message-id
  1381. | %:from %:fromname %:fromaddress
  1382. | %:to %:toname %:toaddress
  1383. | %:fromto (either \"to NAME\" or \"from NAME\")
  1384. gnus | %:group, for messages also all email fields
  1385. w3, w3m | %:type %:url
  1386. info | %:type %:file %:node
  1387. calendar | %:type %:date"
  1388. :group 'org-remember
  1389. :get (lambda (var) ; Make sure all entries have 5 elements
  1390. (mapcar (lambda (x)
  1391. (if (not (stringp (car x))) (setq x (cons "" x)))
  1392. (cond ((= (length x) 4) (append x '("")))
  1393. ((= (length x) 3) (append x '("" "")))
  1394. (t x)))
  1395. (default-value var)))
  1396. :type '(repeat
  1397. :tag "enabled"
  1398. (list :value ("" ?a "\n" nil nil)
  1399. (string :tag "Name")
  1400. (character :tag "Selection Key")
  1401. (string :tag "Template")
  1402. (choice
  1403. (file :tag "Destination file")
  1404. (const :tag "Prompt for file" nil))
  1405. (choice
  1406. (string :tag "Destination headline")
  1407. (const :tag "Selection interface for heading")))))
  1408. (defcustom org-reverse-note-order nil
  1409. "Non-nil means, store new notes at the beginning of a file or entry.
  1410. When nil, new notes will be filed to the end of a file or entry.
  1411. This can also be a list with cons cells of regular expressions that
  1412. are matched against file names, and values."
  1413. :group 'org-remember
  1414. :type '(choice
  1415. (const :tag "Reverse always" t)
  1416. (const :tag "Reverse never" nil)
  1417. (repeat :tag "By file name regexp"
  1418. (cons regexp boolean))))
  1419. (defcustom org-refile-targets nil
  1420. "Targets for refiling entries with \\[org-refile].
  1421. This is list of cons cells. Each cell contains:
  1422. - a specification of the files to be considered, either a list of files,
  1423. or a symbol whose function or value fields will be used to retrieve
  1424. a file name or a list of file names. Nil means, refile to a different
  1425. heading in the current buffer.
  1426. - A specification of how to find candidate refile targets. This may be
  1427. any of
  1428. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1429. This tag has to be present in all target headlines, inheritance will
  1430. not be considered.
  1431. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1432. todo keyword.
  1433. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1434. headlines that are refiling targets.
  1435. - a cons cell (:level . N). Any headline of level N is considered a target.
  1436. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1437. ;; FIXME: what if there are a var and func with same name???
  1438. :group 'org-remember
  1439. :type '(repeat
  1440. (cons
  1441. (choice :value org-agenda-files
  1442. (const :tag "All agenda files" org-agenda-files)
  1443. (const :tag "Current buffer" nil)
  1444. (function) (variable) (file))
  1445. (choice :tag "Identify target headline by"
  1446. (cons :tag "Specific tag" (const :tag) (string))
  1447. (cons :tag "TODO keyword" (const :todo) (string))
  1448. (cons :tag "Regular expression" (const :regexp) (regexp))
  1449. (cons :tag "Level number" (const :level) (integer))
  1450. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1451. (defcustom org-refile-use-outline-path nil
  1452. "Non-nil means, provide refile targets as paths.
  1453. So a level 3 headline will be available as level1/level2/level3.
  1454. When the value is `file', also include the file name (without directory)
  1455. into the path. When `full-file-path', include the full file path."
  1456. :group 'org-remember
  1457. :type '(choice
  1458. (const :tag "Not" nil)
  1459. (const :tag "Yes" t)
  1460. (const :tag "Start with file name" file)
  1461. (const :tag "Start with full file path" full-file-path)))
  1462. (defgroup org-todo nil
  1463. "Options concerning TODO items in Org-mode."
  1464. :tag "Org TODO"
  1465. :group 'org)
  1466. (defgroup org-progress nil
  1467. "Options concerning Progress logging in Org-mode."
  1468. :tag "Org Progress"
  1469. :group 'org-time)
  1470. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1471. "List of TODO entry keyword sequences and their interpretation.
  1472. \\<org-mode-map>This is a list of sequences.
  1473. Each sequence starts with a symbol, either `sequence' or `type',
  1474. indicating if the keywords should be interpreted as a sequence of
  1475. action steps, or as different types of TODO items. The first
  1476. keywords are states requiring action - these states will select a headline
  1477. for inclusion into the global TODO list Org-mode produces. If one of
  1478. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1479. signify that no further action is necessary. If \"|\" is not found,
  1480. the last keyword is treated as the only DONE state of the sequence.
  1481. The command \\[org-todo] cycles an entry through these states, and one
  1482. additional state where no keyword is present. For details about this
  1483. cycling, see the manual.
  1484. TODO keywords and interpretation can also be set on a per-file basis with
  1485. the special #+SEQ_TODO and #+TYP_TODO lines.
  1486. Each keyword can optionally specify a character for fast state selection
  1487. \(in combination with the variable `org-use-fast-todo-selection')
  1488. and specifiers for state change logging, using the same syntax
  1489. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1490. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1491. indicates to record a time stamp each time this state is selected.
  1492. \"WAIT(w@)\" says that the user should in addition be prompted for a
  1493. note, and \"WAIT(w@/@)\" says that a note should be taken both when
  1494. entering and when leaving this state.
  1495. For backward compatibility, this variable may also be just a list
  1496. of keywords - in this case the interptetation (sequence or type) will be
  1497. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1498. :group 'org-todo
  1499. :group 'org-keywords
  1500. :type '(choice
  1501. (repeat :tag "Old syntax, just keywords"
  1502. (string :tag "Keyword"))
  1503. (repeat :tag "New syntax"
  1504. (cons
  1505. (choice
  1506. :tag "Interpretation"
  1507. (const :tag "Sequence (cycling hits every state)" sequence)
  1508. (const :tag "Type (cycling directly to DONE)" type))
  1509. (repeat
  1510. (string :tag "Keyword"))))))
  1511. (defvar org-todo-keywords-1 nil
  1512. "All TODO and DONE keywords active in a buffer.")
  1513. (make-variable-buffer-local 'org-todo-keywords-1)
  1514. (defvar org-todo-keywords-for-agenda nil)
  1515. (defvar org-done-keywords-for-agenda nil)
  1516. (defvar org-not-done-keywords nil)
  1517. (make-variable-buffer-local 'org-not-done-keywords)
  1518. (defvar org-done-keywords nil)
  1519. (make-variable-buffer-local 'org-done-keywords)
  1520. (defvar org-todo-heads nil)
  1521. (make-variable-buffer-local 'org-todo-heads)
  1522. (defvar org-todo-sets nil)
  1523. (make-variable-buffer-local 'org-todo-sets)
  1524. (defvar org-todo-log-states nil)
  1525. (make-variable-buffer-local 'org-todo-log-states)
  1526. (defvar org-todo-kwd-alist nil)
  1527. (make-variable-buffer-local 'org-todo-kwd-alist)
  1528. (defvar org-todo-key-alist nil)
  1529. (make-variable-buffer-local 'org-todo-key-alist)
  1530. (defvar org-todo-key-trigger nil)
  1531. (make-variable-buffer-local 'org-todo-key-trigger)
  1532. (defcustom org-todo-interpretation 'sequence
  1533. "Controls how TODO keywords are interpreted.
  1534. This variable is in principle obsolete and is only used for
  1535. backward compatibility, if the interpretation of todo keywords is
  1536. not given already in `org-todo-keywords'. See that variable for
  1537. more information."
  1538. :group 'org-todo
  1539. :group 'org-keywords
  1540. :type '(choice (const sequence)
  1541. (const type)))
  1542. (defcustom org-use-fast-todo-selection 'prefix
  1543. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1544. This variable describes if and under what circumstances the cycling
  1545. mechanism for TODO keywords will be replaced by a single-key, direct
  1546. selection scheme.
  1547. When nil, fast selection is never used.
  1548. When the symbol `prefix', it will be used when `org-todo' is called with
  1549. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1550. in an agenda buffer.
  1551. When t, fast selection is used by default. In this case, the prefix
  1552. argument forces cycling instead.
  1553. In all cases, the special interface is only used if access keys have actually
  1554. been assigned by the user, i.e. if keywords in the configuration are followed
  1555. by a letter in parenthesis, like TODO(t)."
  1556. :group 'org-todo
  1557. :type '(choice
  1558. (const :tag "Never" nil)
  1559. (const :tag "By default" t)
  1560. (const :tag "Only with C-u C-c C-t" prefix)))
  1561. (defcustom org-after-todo-state-change-hook nil
  1562. "Hook which is run after the state of a TODO item was changed.
  1563. The new state (a string with a TODO keyword, or nil) is available in the
  1564. Lisp variable `state'."
  1565. :group 'org-todo
  1566. :type 'hook)
  1567. (defcustom org-log-done nil
  1568. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1569. When equal to the list (done), also prompt for a closing note.
  1570. This can also be configured on a per-file basis by adding one of
  1571. the following lines anywhere in the buffer:
  1572. #+STARTUP: logdone
  1573. #+STARTUP: lognotedone
  1574. #+STARTUP: nologdone"
  1575. :group 'org-todo
  1576. :group 'org-progress
  1577. :type '(choice
  1578. (const :tag "No logging" nil)
  1579. (const :tag "Record CLOSED timestamp" time)
  1580. (const :tag "Record CLOSED timestamp with closing note." note)))
  1581. ;; Normalize old uses of org-log-done.
  1582. (cond
  1583. ((eq org-log-done t) (setq org-log-done 'time))
  1584. ((and (listp org-log-done) (memq 'done org-log-done))
  1585. (setq org-log-done 'note)))
  1586. ;; FIXME: document
  1587. (defcustom org-log-note-clock-out nil
  1588. "Non-nil means, recored a note when clocking out of an item.
  1589. This can also be configured on a per-file basis by adding one of
  1590. the following lines anywhere in the buffer:
  1591. #+STARTUP: lognoteclock-out
  1592. #+STARTUP: nolognoteclock-out"
  1593. :group 'org-todo
  1594. :group 'org-progress
  1595. :type 'boolean)
  1596. (defcustom org-log-done-with-time t
  1597. "Non-nil means, the CLOSED time stamp will contain date and time.
  1598. When nil, only the date will be recorded."
  1599. :group 'org-progress
  1600. :type 'boolean)
  1601. (defcustom org-log-note-headings
  1602. '((done . "CLOSING NOTE %t")
  1603. (state . "State %-12s %t")
  1604. (clock-out . ""))
  1605. "Headings for notes added when clocking out or closing TODO items.
  1606. The value is an alist, with the car being a symbol indicating the note
  1607. context, and the cdr is the heading to be used. The heading may also be the
  1608. empty string.
  1609. %t in the heading will be replaced by a time stamp.
  1610. %s will be replaced by the new TODO state, in double quotes.
  1611. %u will be replaced by the user name.
  1612. %U will be replaced by the full user name."
  1613. :group 'org-todo
  1614. :group 'org-progress
  1615. :type '(list :greedy t
  1616. (cons (const :tag "Heading when closing an item" done) string)
  1617. (cons (const :tag
  1618. "Heading when changing todo state (todo sequence only)"
  1619. state) string)
  1620. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1621. (defcustom org-log-states-order-reversed t
  1622. "Non-nil means, the latest state change note will be directly after heading.
  1623. When nil, the notes will be orderer according to time."
  1624. :group 'org-todo
  1625. :group 'org-progress
  1626. :type 'boolean)
  1627. (defcustom org-log-repeat 'time
  1628. "Non-nil means, record moving through the DONE state when triggering repeat.
  1629. An auto-repeating tasks is immediately switched back to TODO when marked
  1630. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1631. the TODO keyword definition, or recording a cloing note by setting
  1632. `org-log-done', there will be no record of the task moving trhough DONE.
  1633. This variable forces taking a note anyway. Possible values are:
  1634. nil Don't force a record
  1635. time Record a time stamp
  1636. note Record a note
  1637. This option can also be set with on a per-file-basis with
  1638. #+STARTUP: logrepeat
  1639. #+STARTUP: lognoterepeat
  1640. #+STARTUP: nologrepeat
  1641. You can have local logging settings for a subtree by setting the LOGGING
  1642. property to one or more of these keywords."
  1643. :group 'org-todo
  1644. :group 'org-progress
  1645. :type '(choice
  1646. (const :tag "Don't force a record" nil)
  1647. (const :tag "Force recording the DONE state" time)
  1648. (const :tag "Force recording a note with the DONE state" note)))
  1649. (defcustom org-clock-into-drawer 2
  1650. "Should clocking info be wrapped into a drawer?
  1651. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1652. If necessary, the drawer will be created.
  1653. When nil, the drawer will not be created, but used when present.
  1654. When an integer and the number of clocking entries in an item
  1655. reaches or exceeds this number, a drawer will be created."
  1656. :group 'org-todo
  1657. :group 'org-progress
  1658. :type '(choice
  1659. (const :tag "Always" t)
  1660. (const :tag "Only when drawer exists" nil)
  1661. (integer :tag "When at least N clock entries")))
  1662. (defcustom org-clock-out-when-done t
  1663. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1664. Nil means, clock will keep running until stopped explicitly with
  1665. `C-c C-x C-o', or until the clock is started in a different item."
  1666. :group 'org-progress
  1667. :type 'boolean)
  1668. (defcustom org-clock-in-switch-to-state nil
  1669. "Set task to a special todo state while clocking it.
  1670. The value should be the state to which the entry should be switched."
  1671. :group 'org-progress
  1672. :group 'org-todo
  1673. :type '(choice
  1674. (const :tag "Don't force a state" nil)
  1675. (string :tag "State")))
  1676. (defgroup org-priorities nil
  1677. "Priorities in Org-mode."
  1678. :tag "Org Priorities"
  1679. :group 'org-todo)
  1680. (defcustom org-highest-priority ?A
  1681. "The highest priority of TODO items. A character like ?A, ?B etc.
  1682. Must have a smaller ASCII number than `org-lowest-priority'."
  1683. :group 'org-priorities
  1684. :type 'character)
  1685. (defcustom org-lowest-priority ?C
  1686. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1687. Must have a larger ASCII number than `org-highest-priority'."
  1688. :group 'org-priorities
  1689. :type 'character)
  1690. (defcustom org-default-priority ?B
  1691. "The default priority of TODO items.
  1692. This is the priority an item get if no explicit priority is given."
  1693. :group 'org-priorities
  1694. :type 'character)
  1695. (defcustom org-priority-start-cycle-with-default t
  1696. "Non-nil means, start with default priority when starting to cycle.
  1697. When this is nil, the first step in the cycle will be (depending on the
  1698. command used) one higher or lower that the default priority."
  1699. :group 'org-priorities
  1700. :type 'boolean)
  1701. (defgroup org-time nil
  1702. "Options concerning time stamps and deadlines in Org-mode."
  1703. :tag "Org Time"
  1704. :group 'org)
  1705. (defcustom org-insert-labeled-timestamps-at-point nil
  1706. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1707. When nil, these labeled time stamps are forces into the second line of an
  1708. entry, just after the headline. When scheduling from the global TODO list,
  1709. the time stamp will always be forced into the second line."
  1710. :group 'org-time
  1711. :type 'boolean)
  1712. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1713. "Formats for `format-time-string' which are used for time stamps.
  1714. It is not recommended to change this constant.")
  1715. (defcustom org-time-stamp-rounding-minutes 0
  1716. "Number of minutes to round time stamps to upon insertion.
  1717. When zero, insert the time unmodified. Useful rounding numbers
  1718. should be factors of 60, so for example 5, 10, 15.
  1719. When this is not zero, you can still force an exact time-stamp by using
  1720. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1721. :group 'org-time
  1722. :type 'integer)
  1723. (defcustom org-display-custom-times nil
  1724. "Non-nil means, overlay custom formats over all time stamps.
  1725. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1726. To turn this on on a per-file basis, insert anywhere in the file:
  1727. #+STARTUP: customtime"
  1728. :group 'org-time
  1729. :set 'set-default
  1730. :type 'sexp)
  1731. (make-variable-buffer-local 'org-display-custom-times)
  1732. (defcustom org-time-stamp-custom-formats
  1733. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1734. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1735. These are overlayed over the default ISO format if the variable
  1736. `org-display-custom-times' is set. Time like %H:%M should be at the
  1737. end of the second format."
  1738. :group 'org-time
  1739. :type 'sexp)
  1740. (defun org-time-stamp-format (&optional long inactive)
  1741. "Get the right format for a time string."
  1742. (let ((f (if long (cdr org-time-stamp-formats)
  1743. (car org-time-stamp-formats))))
  1744. (if inactive
  1745. (concat "[" (substring f 1 -1) "]")
  1746. f)))
  1747. (defcustom org-read-date-prefer-future t
  1748. "Non-nil means, assume future for incomplete date input from user.
  1749. This affects the following situations:
  1750. 1. The user gives a day, but no month.
  1751. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1752. read this as the third of *next* month. However, if you enter \"17\",
  1753. it will be considered as *this* month.
  1754. 2. The user gives a month but not a year.
  1755. For example, if it is april and you enter \"feb 2\", this will be read
  1756. as feb 2, *next* year. \"May 5\", however, will be this year.
  1757. When this option is nil, the current month and year will always be used
  1758. as defaults."
  1759. :group 'org-time
  1760. :type 'boolean)
  1761. (defcustom org-read-date-display-live t
  1762. "Non-nil means, display current interpretation of date prompt live.
  1763. This display will be in an overlay, in the minibuffer."
  1764. :group 'org-time
  1765. :type 'boolean)
  1766. (defcustom org-read-date-popup-calendar t
  1767. "Non-nil means, pop up a calendar when prompting for a date.
  1768. In the calendar, the date can be selected with mouse-1. However, the
  1769. minibuffer will also be active, and you can simply enter the date as well.
  1770. When nil, only the minibuffer will be available."
  1771. :group 'org-time
  1772. :type 'boolean)
  1773. (if (fboundp 'defvaralias)
  1774. (defvaralias 'org-popup-calendar-for-date-prompt
  1775. 'org-read-date-popup-calendar))
  1776. (defcustom org-extend-today-until 0
  1777. "The hour when your day really ends.
  1778. This has influence for the following applications:
  1779. - When switching the agenda to \"today\". It it is still earlier than
  1780. the time given here, the day recognized as TODAY is actually yesterday.
  1781. - When a date is read from the user and it is still before the time given
  1782. here, the current date and time will be assumed to be yesterday, 23:59.
  1783. FIXME:
  1784. IMPORTANT: This is still a very experimental feature, it may disappear
  1785. again or it may be extended to mean more things."
  1786. :group 'org-time
  1787. :type 'number)
  1788. (defcustom org-edit-timestamp-down-means-later nil
  1789. "Non-nil means, S-down will increase the time in a time stamp.
  1790. When nil, S-up will increase."
  1791. :group 'org-time
  1792. :type 'boolean)
  1793. (defcustom org-calendar-follow-timestamp-change t
  1794. "Non-nil means, make the calendar window follow timestamp changes.
  1795. When a timestamp is modified and the calendar window is visible, it will be
  1796. moved to the new date."
  1797. :group 'org-time
  1798. :type 'boolean)
  1799. (defcustom org-clock-heading-function nil
  1800. "When non-nil, should be a function to create `org-clock-heading'.
  1801. This is the string shown in the mode line when a clock is running.
  1802. The function is called with point at the beginning of the headline."
  1803. :group 'org-time ; FIXME: Should we have a separate group????
  1804. :type 'function)
  1805. (defgroup org-tags nil
  1806. "Options concerning tags in Org-mode."
  1807. :tag "Org Tags"
  1808. :group 'org)
  1809. (defcustom org-tag-alist nil
  1810. "List of tags allowed in Org-mode files.
  1811. When this list is nil, Org-mode will base TAG input on what is already in the
  1812. buffer.
  1813. The value of this variable is an alist, the car of each entry must be a
  1814. keyword as a string, the cdr may be a character that is used to select
  1815. that tag through the fast-tag-selection interface.
  1816. See the manual for details."
  1817. :group 'org-tags
  1818. :type '(repeat
  1819. (choice
  1820. (cons (string :tag "Tag name")
  1821. (character :tag "Access char"))
  1822. (const :tag "Start radio group" (:startgroup))
  1823. (const :tag "End radio group" (:endgroup)))))
  1824. (defcustom org-use-fast-tag-selection 'auto
  1825. "Non-nil means, use fast tag selection scheme.
  1826. This is a special interface to select and deselect tags with single keys.
  1827. When nil, fast selection is never used.
  1828. When the symbol `auto', fast selection is used if and only if selection
  1829. characters for tags have been configured, either through the variable
  1830. `org-tag-alist' or through a #+TAGS line in the buffer.
  1831. When t, fast selection is always used and selection keys are assigned
  1832. automatically if necessary."
  1833. :group 'org-tags
  1834. :type '(choice
  1835. (const :tag "Always" t)
  1836. (const :tag "Never" nil)
  1837. (const :tag "When selection characters are configured" 'auto)))
  1838. (defcustom org-fast-tag-selection-single-key nil
  1839. "Non-nil means, fast tag selection exits after first change.
  1840. When nil, you have to press RET to exit it.
  1841. During fast tag selection, you can toggle this flag with `C-c'.
  1842. This variable can also have the value `expert'. In this case, the window
  1843. displaying the tags menu is not even shown, until you press C-c again."
  1844. :group 'org-tags
  1845. :type '(choice
  1846. (const :tag "No" nil)
  1847. (const :tag "Yes" t)
  1848. (const :tag "Expert" expert)))
  1849. (defvar org-fast-tag-selection-include-todo nil
  1850. "Non-nil means, fast tags selection interface will also offer TODO states.
  1851. This is an undocumented feature, you should not rely on it.")
  1852. (defcustom org-tags-column -80
  1853. "The column to which tags should be indented in a headline.
  1854. If this number is positive, it specifies the column. If it is negative,
  1855. it means that the tags should be flushright to that column. For example,
  1856. -80 works well for a normal 80 character screen."
  1857. :group 'org-tags
  1858. :type 'integer)
  1859. (defcustom org-auto-align-tags t
  1860. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1861. These operations change the length of a headline and therefore shift
  1862. the tags around. With this options turned on, after each such operation
  1863. the tags are again aligned to `org-tags-column'."
  1864. :group 'org-tags
  1865. :type 'boolean)
  1866. (defcustom org-use-tag-inheritance t
  1867. "Non-nil means, tags in levels apply also for sublevels.
  1868. When nil, only the tags directly given in a specific line apply there.
  1869. If you turn off this option, you very likely want to turn on the
  1870. companion option `org-tags-match-list-sublevels'."
  1871. :group 'org-tags
  1872. :type 'boolean)
  1873. (defcustom org-tags-match-list-sublevels nil
  1874. "Non-nil means list also sublevels of headlines matching tag search.
  1875. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1876. the sublevels of a headline matching a tag search often also match
  1877. the same search. Listing all of them can create very long lists.
  1878. Setting this variable to nil causes subtrees of a match to be skipped.
  1879. This option is off by default, because inheritance in on. If you turn
  1880. inheritance off, you very likely want to turn this option on.
  1881. As a special case, if the tag search is restricted to TODO items, the
  1882. value of this variable is ignored and sublevels are always checked, to
  1883. make sure all corresponding TODO items find their way into the list."
  1884. :group 'org-tags
  1885. :type 'boolean)
  1886. (defvar org-tags-history nil
  1887. "History of minibuffer reads for tags.")
  1888. (defvar org-last-tags-completion-table nil
  1889. "The last used completion table for tags.")
  1890. (defvar org-after-tags-change-hook nil
  1891. "Hook that is run after the tags in a line have changed.")
  1892. (defgroup org-properties nil
  1893. "Options concerning properties in Org-mode."
  1894. :tag "Org Properties"
  1895. :group 'org)
  1896. (defcustom org-property-format "%-10s %s"
  1897. "How property key/value pairs should be formatted by `indent-line'.
  1898. When `indent-line' hits a property definition, it will format the line
  1899. according to this format, mainly to make sure that the values are
  1900. lined-up with respect to each other."
  1901. :group 'org-properties
  1902. :type 'string)
  1903. (defcustom org-use-property-inheritance nil
  1904. "Non-nil means, properties apply also for sublevels.
  1905. This setting is only relevant during property searches, not when querying
  1906. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1907. you need to call `org-entry-get' with the inheritance flag.
  1908. Turning this on can cause significant overhead when doing a search, so
  1909. this is turned off by default.
  1910. When nil, only the properties directly given in the current entry count.
  1911. The value may also be a list of properties that shouldhave inheritance.
  1912. However, note that some special properties use inheritance under special
  1913. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1914. and the properties ending in \"_ALL\" when they are used as descriptor
  1915. for valid values of a property."
  1916. :group 'org-properties
  1917. :type '(choice
  1918. (const :tag "Not" nil)
  1919. (const :tag "Always" nil)
  1920. (repeat :tag "Specific properties" (string :tag "Property"))))
  1921. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1922. "The default column format, if no other format has been defined.
  1923. This variable can be set on the per-file basis by inserting a line
  1924. #+COLUMNS: %25ITEM ....."
  1925. :group 'org-properties
  1926. :type 'string)
  1927. (defcustom org-global-properties nil
  1928. "List of property/value pairs that can be inherited by any entry.
  1929. You can set buffer-local values for this by adding lines like
  1930. #+PROPERTY: NAME VALUE"
  1931. :group 'org-properties
  1932. :type '(repeat
  1933. (cons (string :tag "Property")
  1934. (string :tag "Value"))))
  1935. (defvar org-local-properties nil
  1936. "List of property/value pairs that can be inherited by any entry.
  1937. Valid for the current buffer.
  1938. This variable is populated from #+PROPERTY lines.")
  1939. (defgroup org-agenda nil
  1940. "Options concerning agenda views in Org-mode."
  1941. :tag "Org Agenda"
  1942. :group 'org)
  1943. (defvar org-category nil
  1944. "Variable used by org files to set a category for agenda display.
  1945. Such files should use a file variable to set it, for example
  1946. # -*- mode: org; org-category: \"ELisp\"
  1947. or contain a special line
  1948. #+CATEGORY: ELisp
  1949. If the file does not specify a category, then file's base name
  1950. is used instead.")
  1951. (make-variable-buffer-local 'org-category)
  1952. (defcustom org-agenda-files nil
  1953. "The files to be used for agenda display.
  1954. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1955. \\[org-remove-file]. You can also use customize to edit the list.
  1956. If an entry is a directory, all files in that directory that are matched by
  1957. `org-agenda-file-regexp' will be part of the file list.
  1958. If the value of the variable is not a list but a single file name, then
  1959. the list of agenda files is actually stored and maintained in that file, one
  1960. agenda file per line."
  1961. :group 'org-agenda
  1962. :type '(choice
  1963. (repeat :tag "List of files and directories" file)
  1964. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1965. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1966. "Regular expression to match files for `org-agenda-files'.
  1967. If any element in the list in that variable contains a directory instead
  1968. of a normal file, all files in that directory that are matched by this
  1969. regular expression will be included."
  1970. :group 'org-agenda
  1971. :type 'regexp)
  1972. (defcustom org-agenda-skip-unavailable-files nil
  1973. "t means to just skip non-reachable files in `org-agenda-files'.
  1974. Nil means to remove them, after a query, from the list."
  1975. :group 'org-agenda
  1976. :type 'boolean)
  1977. (defcustom org-agenda-multi-occur-extra-files nil
  1978. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1979. The files in `org-agenda-files' are always searched."
  1980. :group 'org-agenda
  1981. :type '(repeat file))
  1982. (defcustom org-agenda-confirm-kill 1
  1983. "When set, remote killing from the agenda buffer needs confirmation.
  1984. When t, a confirmation is always needed. When a number N, confirmation is
  1985. only needed when the text to be killed contains more than N non-white lines."
  1986. :group 'org-agenda
  1987. :type '(choice
  1988. (const :tag "Never" nil)
  1989. (const :tag "Always" t)
  1990. (number :tag "When more than N lines")))
  1991. (defcustom org-calendar-to-agenda-key [?c]
  1992. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1993. The command `org-calendar-goto-agenda' will be bound to this key. The
  1994. default is the character `c' because then `c' can be used to switch back and
  1995. forth between agenda and calendar."
  1996. :group 'org-agenda
  1997. :type 'sexp)
  1998. (defcustom org-agenda-compact-blocks nil
  1999. "Non-nil means, make the block agenda more compact.
  2000. This is done by leaving out unnecessary lines."
  2001. :group 'org-agenda
  2002. :type nil)
  2003. (defgroup org-agenda-export nil
  2004. "Options concerning exporting agenda views in Org-mode."
  2005. :tag "Org Agenda Export"
  2006. :group 'org-agenda)
  2007. (defcustom org-agenda-with-colors t
  2008. "Non-nil means, use colors in agenda views."
  2009. :group 'org-agenda-export
  2010. :type 'boolean)
  2011. (defcustom org-agenda-exporter-settings nil
  2012. "Alist of variable/value pairs that should be active during agenda export.
  2013. This is a good place to set uptions for ps-print and for htmlize."
  2014. :group 'org-agenda-export
  2015. :type '(repeat
  2016. (list
  2017. (variable)
  2018. (sexp :tag "Value"))))
  2019. (defcustom org-agenda-export-html-style ""
  2020. "The style specification for exported HTML Agenda files.
  2021. If this variable contains a string, it will replace the default <style>
  2022. section as produced by `htmlize'.
  2023. Since there are different ways of setting style information, this variable
  2024. needs to contain the full HTML structure to provide a style, including the
  2025. surrounding HTML tags. The style specifications should include definitions
  2026. the fonts used by the agenda, here is an example:
  2027. <style type=\"text/css\">
  2028. p { font-weight: normal; color: gray; }
  2029. .org-agenda-structure {
  2030. font-size: 110%;
  2031. color: #003399;
  2032. font-weight: 600;
  2033. }
  2034. .org-todo {
  2035. color: #cc6666;Week-agenda:
  2036. font-weight: bold;
  2037. }
  2038. .org-done {
  2039. color: #339933;
  2040. }
  2041. .title { text-align: center; }
  2042. .todo, .deadline { color: red; }
  2043. .done { color: green; }
  2044. </style>
  2045. or, if you want to keep the style in a file,
  2046. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2047. As the value of this option simply gets inserted into the HTML <head> header,
  2048. you can \"misuse\" it to also add other text to the header. However,
  2049. <style>...</style> is required, if not present the variable will be ignored."
  2050. :group 'org-agenda-export
  2051. :group 'org-export-html
  2052. :type 'string)
  2053. (defgroup org-agenda-custom-commands nil
  2054. "Options concerning agenda views in Org-mode."
  2055. :tag "Org Agenda Custom Commands"
  2056. :group 'org-agenda)
  2057. (defcustom org-agenda-custom-commands nil
  2058. "Custom commands for the agenda.
  2059. These commands will be offered on the splash screen displayed by the
  2060. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2061. (key desc type match options files)
  2062. key The key (one or more characters as a string) to be associated
  2063. with the command.
  2064. desc A description of the commend, when omitted or nil, a default
  2065. description is built using MATCH.
  2066. type The command type, any of the following symbols:
  2067. todo Entries with a specific TODO keyword, in all agenda files.
  2068. tags Tags match in all agenda files.
  2069. tags-todo Tags match in all agenda files, TODO entries only.
  2070. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2071. tags-tree Sparse tree with all tags matches in *current* file.
  2072. occur-tree Occur sparse tree for *current* file.
  2073. ... A user-defined function.
  2074. match What to search for:
  2075. - a single keyword for TODO keyword searches
  2076. - a tags match expression for tags searches
  2077. - a regular expression for occur searches
  2078. options A list of option settings, similar to that in a let form, so like
  2079. this: ((opt1 val1) (opt2 val2) ...)
  2080. files A list of files file to write the produced agenda buffer to
  2081. with the command `org-store-agenda-views'.
  2082. If a file name ends in \".html\", an HTML version of the buffer
  2083. is written out. If it ends in \".ps\", a postscript version is
  2084. produced. Otherwide, only the plain text is written to the file.
  2085. You can also define a set of commands, to create a composite agenda buffer.
  2086. In this case, an entry looks like this:
  2087. (key desc (cmd1 cmd2 ...) general-options file)
  2088. where
  2089. desc A description string to be displayed in the dispatcher menu.
  2090. cmd An agenda command, similar to the above. However, tree commands
  2091. are no allowed, but instead you can get agenda and global todo list.
  2092. So valid commands for a set are:
  2093. (agenda)
  2094. (alltodo)
  2095. (stuck)
  2096. (todo \"match\" options files)
  2097. (tags \"match\" options files)
  2098. (tags-todo \"match\" options files)
  2099. Each command can carry a list of options, and another set of options can be
  2100. given for the whole set of commands. Individual command options take
  2101. precedence over the general options.
  2102. When using several characters as key to a command, the first characters
  2103. are prefix commands. For the dispatcher to display useful information, you
  2104. should provide a description for the prefix, like
  2105. (setq org-agenda-custom-commands
  2106. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2107. (\"hl\" tags \"+HOME+Lisa\")
  2108. (\"hp\" tags \"+HOME+Peter\")
  2109. (\"hk\" tags \"+HOME+Kim\")))"
  2110. :group 'org-agenda-custom-commands
  2111. :type '(repeat
  2112. (choice :value ("a" "" tags "" nil)
  2113. (list :tag "Single command"
  2114. (string :tag "Access Key(s) ")
  2115. (option (string :tag "Description"))
  2116. (choice
  2117. (const :tag "Agenda" agenda)
  2118. (const :tag "TODO list" alltodo)
  2119. (const :tag "Stuck projects" stuck)
  2120. (const :tag "Tags search (all agenda files)" tags)
  2121. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2122. (const :tag "TODO keyword search (all agenda files)" todo)
  2123. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2124. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2125. (const :tag "Occur tree (current buffer)" occur-tree)
  2126. (sexp :tag "Other, user-defined function"))
  2127. (string :tag "Match")
  2128. (repeat :tag "Local options"
  2129. (list (variable :tag "Option") (sexp :tag "Value")))
  2130. (option (repeat :tag "Export" (file :tag "Export to"))))
  2131. (list :tag "Command series, all agenda files"
  2132. (string :tag "Access Key(s)")
  2133. (string :tag "Description ")
  2134. (repeat
  2135. (choice
  2136. (const :tag "Agenda" (agenda))
  2137. (const :tag "TODO list" (alltodo))
  2138. (const :tag "Stuck projects" (stuck))
  2139. (list :tag "Tags search"
  2140. (const :format "" tags)
  2141. (string :tag "Match")
  2142. (repeat :tag "Local options"
  2143. (list (variable :tag "Option")
  2144. (sexp :tag "Value"))))
  2145. (list :tag "Tags search, TODO entries only"
  2146. (const :format "" tags-todo)
  2147. (string :tag "Match")
  2148. (repeat :tag "Local options"
  2149. (list (variable :tag "Option")
  2150. (sexp :tag "Value"))))
  2151. (list :tag "TODO keyword search"
  2152. (const :format "" todo)
  2153. (string :tag "Match")
  2154. (repeat :tag "Local options"
  2155. (list (variable :tag "Option")
  2156. (sexp :tag "Value"))))
  2157. (list :tag "Other, user-defined function"
  2158. (symbol :tag "function")
  2159. (string :tag "Match")
  2160. (repeat :tag "Local options"
  2161. (list (variable :tag "Option")
  2162. (sexp :tag "Value"))))))
  2163. (repeat :tag "General options"
  2164. (list (variable :tag "Option")
  2165. (sexp :tag "Value")))
  2166. (option (repeat :tag "Export" (file :tag "Export to"))))
  2167. (cons :tag "Prefix key documentation"
  2168. (string :tag "Access Key(s)")
  2169. (string :tag "Description ")))))
  2170. (defcustom org-stuck-projects
  2171. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2172. "How to identify stuck projects.
  2173. This is a list of four items:
  2174. 1. A tags/todo matcher string that is used to identify a project.
  2175. The entire tree below a headline matched by this is considered one project.
  2176. 2. A list of TODO keywords identifying non-stuck projects.
  2177. If the project subtree contains any headline with one of these todo
  2178. keywords, the project is considered to be not stuck. If you specify
  2179. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2180. 3. A list of tags identifying non-stuck projects.
  2181. If the project subtree contains any headline with one of these tags,
  2182. the project is considered to be not stuck. If you specify \"*\" as
  2183. a tag, any tag will mark the project unstuck.
  2184. 4. An arbitrary regular expression matching non-stuck projects.
  2185. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2186. or `C-c a #' to produce the list."
  2187. :group 'org-agenda-custom-commands
  2188. :type '(list
  2189. (string :tag "Tags/TODO match to identify a project")
  2190. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2191. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2192. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2193. (defgroup org-agenda-skip nil
  2194. "Options concerning skipping parts of agenda files."
  2195. :tag "Org Agenda Skip"
  2196. :group 'org-agenda)
  2197. (defcustom org-agenda-todo-list-sublevels t
  2198. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2199. When nil, the sublevels of a TODO entry are not checked, resulting in
  2200. potentially much shorter TODO lists."
  2201. :group 'org-agenda-skip
  2202. :group 'org-todo
  2203. :type 'boolean)
  2204. (defcustom org-agenda-todo-ignore-with-date nil
  2205. "Non-nil means, don't show entries with a date in the global todo list.
  2206. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2207. but don't want them to show up in the TODO list.
  2208. When this is set, it also covers deadlines and scheduled items, the settings
  2209. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2210. will be ignored."
  2211. :group 'org-agenda-skip
  2212. :group 'org-todo
  2213. :type 'boolean)
  2214. (defcustom org-agenda-todo-ignore-scheduled nil
  2215. "Non-nil means, don't show scheduled entries in the global todo list.
  2216. The idea behind this is that by scheduling it, you have already taken care
  2217. of this item.
  2218. See also `org-agenda-todo-ignore-with-date'."
  2219. :group 'org-agenda-skip
  2220. :group 'org-todo
  2221. :type 'boolean)
  2222. (defcustom org-agenda-todo-ignore-deadlines nil
  2223. "Non-nil means, don't show near deadline entries in the global todo list.
  2224. Near means closer than `org-deadline-warning-days' days.
  2225. The idea behind this is that such items will appear in the agenda anyway.
  2226. See also `org-agenda-todo-ignore-with-date'."
  2227. :group 'org-agenda-skip
  2228. :group 'org-todo
  2229. :type 'boolean)
  2230. (defcustom org-agenda-skip-scheduled-if-done nil
  2231. "Non-nil means don't show scheduled items in agenda when they are done.
  2232. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2233. it applies only to the actual date of the scheduling. Warnings about
  2234. an item with a past scheduling dates are always turned off when the item
  2235. is DONE."
  2236. :group 'org-agenda-skip
  2237. :type 'boolean)
  2238. (defcustom org-agenda-skip-deadline-if-done nil
  2239. "Non-nil means don't show deadines when the corresponding item is done.
  2240. When nil, the deadline is still shown and should give you a happy feeling.
  2241. This is relevant for the daily/weekly agenda. And it applied only to the
  2242. actualy date of the deadline. Warnings about approching and past-due
  2243. deadlines are always turned off when the item is DONE."
  2244. :group 'org-agenda-skip
  2245. :type 'boolean)
  2246. (defcustom org-agenda-skip-timestamp-if-done nil
  2247. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2248. :group 'org-agenda-skip
  2249. :type 'boolean)
  2250. (defcustom org-timeline-show-empty-dates 3
  2251. "Non-nil means, `org-timeline' also shows dates without an entry.
  2252. When nil, only the days which actually have entries are shown.
  2253. When t, all days between the first and the last date are shown.
  2254. When an integer, show also empty dates, but if there is a gap of more than
  2255. N days, just insert a special line indicating the size of the gap."
  2256. :group 'org-agenda-skip
  2257. :type '(choice
  2258. (const :tag "None" nil)
  2259. (const :tag "All" t)
  2260. (number :tag "at most")))
  2261. (defgroup org-agenda-startup nil
  2262. "Options concerning initial settings in the Agenda in Org Mode."
  2263. :tag "Org Agenda Startup"
  2264. :group 'org-agenda)
  2265. (defcustom org-finalize-agenda-hook nil
  2266. "Hook run just before displaying an agenda buffer."
  2267. :group 'org-agenda-startup
  2268. :type 'hook)
  2269. (defcustom org-agenda-mouse-1-follows-link nil
  2270. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2271. A longer mouse click will still set point. Does not wortk on XEmacs.
  2272. Needs to be set before org.el is loaded."
  2273. :group 'org-agenda-startup
  2274. :type 'boolean)
  2275. (defcustom org-agenda-start-with-follow-mode nil
  2276. "The initial value of follow-mode in a newly created agenda window."
  2277. :group 'org-agenda-startup
  2278. :type 'boolean)
  2279. (defgroup org-agenda-windows nil
  2280. "Options concerning the windows used by the Agenda in Org Mode."
  2281. :tag "Org Agenda Windows"
  2282. :group 'org-agenda)
  2283. (defcustom org-agenda-window-setup 'reorganize-frame
  2284. "How the agenda buffer should be displayed.
  2285. Possible values for this option are:
  2286. current-window Show agenda in the current window, keeping all other windows.
  2287. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2288. other-window Use `switch-to-buffer-other-window' to display agenda.
  2289. reorganize-frame Show only two windows on the current frame, the current
  2290. window and the agenda.
  2291. See also the variable `org-agenda-restore-windows-after-quit'."
  2292. :group 'org-agenda-windows
  2293. :type '(choice
  2294. (const current-window)
  2295. (const other-frame)
  2296. (const other-window)
  2297. (const reorganize-frame)))
  2298. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2299. "The min and max height of the agenda window as a fraction of frame height.
  2300. The value of the variable is a cons cell with two numbers between 0 and 1.
  2301. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2302. :group 'org-agenda-windows
  2303. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2304. (defcustom org-agenda-restore-windows-after-quit nil
  2305. "Non-nil means, restore window configuration open exiting agenda.
  2306. Before the window configuration is changed for displaying the agenda,
  2307. the current status is recorded. When the agenda is exited with
  2308. `q' or `x' and this option is set, the old state is restored. If
  2309. `org-agenda-window-setup' is `other-frame', the value of this
  2310. option will be ignored.."
  2311. :group 'org-agenda-windows
  2312. :type 'boolean)
  2313. (defcustom org-indirect-buffer-display 'other-window
  2314. "How should indirect tree buffers be displayed?
  2315. This applies to indirect buffers created with the commands
  2316. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2317. Valid values are:
  2318. current-window Display in the current window
  2319. other-window Just display in another window.
  2320. dedicated-frame Create one new frame, and re-use it each time.
  2321. new-frame Make a new frame each time. Note that in this case
  2322. previously-made indirect buffers are kept, and you need to
  2323. kill these buffers yourself."
  2324. :group 'org-structure
  2325. :group 'org-agenda-windows
  2326. :type '(choice
  2327. (const :tag "In current window" current-window)
  2328. (const :tag "In current frame, other window" other-window)
  2329. (const :tag "Each time a new frame" new-frame)
  2330. (const :tag "One dedicated frame" dedicated-frame)))
  2331. (defgroup org-agenda-daily/weekly nil
  2332. "Options concerning the daily/weekly agenda."
  2333. :tag "Org Agenda Daily/Weekly"
  2334. :group 'org-agenda)
  2335. (defcustom org-agenda-ndays 7
  2336. "Number of days to include in overview display.
  2337. Should be 1 or 7."
  2338. :group 'org-agenda-daily/weekly
  2339. :type 'number)
  2340. (defcustom org-agenda-start-on-weekday 1
  2341. "Non-nil means, start the overview always on the specified weekday.
  2342. 0 denotes Sunday, 1 denotes Monday etc.
  2343. When nil, always start on the current day."
  2344. :group 'org-agenda-daily/weekly
  2345. :type '(choice (const :tag "Today" nil)
  2346. (number :tag "Weekday No.")))
  2347. (defcustom org-agenda-show-all-dates t
  2348. "Non-nil means, `org-agenda' shows every day in the selected range.
  2349. When nil, only the days which actually have entries are shown."
  2350. :group 'org-agenda-daily/weekly
  2351. :type 'boolean)
  2352. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2353. "Format string for displaying dates in the agenda.
  2354. Used by the daily/weekly agenda and by the timeline. This should be
  2355. a format string understood by `format-time-string', or a function returning
  2356. the formatted date as a string. The function must take a single argument,
  2357. a calendar-style date list like (month day year)."
  2358. :group 'org-agenda-daily/weekly
  2359. :type '(choice
  2360. (string :tag "Format string")
  2361. (function :tag "Function")))
  2362. (defun org-agenda-format-date-aligned (date)
  2363. "Format a date string for display in the daily/weekly agenda, or timeline.
  2364. This function makes sure that dates are aligned for easy reading."
  2365. (format "%-9s %2d %s %4d"
  2366. (calendar-day-name date)
  2367. (extract-calendar-day date)
  2368. (calendar-month-name (extract-calendar-month date))
  2369. (extract-calendar-year date)))
  2370. (defcustom org-agenda-include-diary nil
  2371. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2372. :group 'org-agenda-daily/weekly
  2373. :type 'boolean)
  2374. (defcustom org-agenda-include-all-todo nil
  2375. "Set means weekly/daily agenda will always contain all TODO entries.
  2376. The TODO entries will be listed at the top of the agenda, before
  2377. the entries for specific days."
  2378. :group 'org-agenda-daily/weekly
  2379. :type 'boolean)
  2380. (defcustom org-agenda-repeating-timestamp-show-all t
  2381. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2382. When nil, only one occurence is shown, either today or the
  2383. nearest into the future."
  2384. :group 'org-agenda-daily/weekly
  2385. :type 'boolean)
  2386. (defcustom org-deadline-warning-days 14
  2387. "No. of days before expiration during which a deadline becomes active.
  2388. This variable governs the display in sparse trees and in the agenda.
  2389. When 0 or negative, it means use this number (the absolute value of it)
  2390. even if a deadline has a different individual lead time specified."
  2391. :group 'org-time
  2392. :group 'org-agenda-daily/weekly
  2393. :type 'number)
  2394. (defcustom org-scheduled-past-days 10000
  2395. "No. of days to continue listing scheduled items that are not marked DONE.
  2396. When an item is scheduled on a date, it shows up in the agenda on this
  2397. day and will be listed until it is marked done for the number of days
  2398. given here."
  2399. :group 'org-agenda-daily/weekly
  2400. :type 'number)
  2401. (defgroup org-agenda-time-grid nil
  2402. "Options concerning the time grid in the Org-mode Agenda."
  2403. :tag "Org Agenda Time Grid"
  2404. :group 'org-agenda)
  2405. (defcustom org-agenda-use-time-grid t
  2406. "Non-nil means, show a time grid in the agenda schedule.
  2407. A time grid is a set of lines for specific times (like every two hours between
  2408. 8:00 and 20:00). The items scheduled for a day at specific times are
  2409. sorted in between these lines.
  2410. For details about when the grid will be shown, and what it will look like, see
  2411. the variable `org-agenda-time-grid'."
  2412. :group 'org-agenda-time-grid
  2413. :type 'boolean)
  2414. (defcustom org-agenda-time-grid
  2415. '((daily today require-timed)
  2416. "----------------"
  2417. (800 1000 1200 1400 1600 1800 2000))
  2418. "The settings for time grid for agenda display.
  2419. This is a list of three items. The first item is again a list. It contains
  2420. symbols specifying conditions when the grid should be displayed:
  2421. daily if the agenda shows a single day
  2422. weekly if the agenda shows an entire week
  2423. today show grid on current date, independent of daily/weekly display
  2424. require-timed show grid only if at least one item has a time specification
  2425. The second item is a string which will be places behing the grid time.
  2426. The third item is a list of integers, indicating the times that should have
  2427. a grid line."
  2428. :group 'org-agenda-time-grid
  2429. :type
  2430. '(list
  2431. (set :greedy t :tag "Grid Display Options"
  2432. (const :tag "Show grid in single day agenda display" daily)
  2433. (const :tag "Show grid in weekly agenda display" weekly)
  2434. (const :tag "Always show grid for today" today)
  2435. (const :tag "Show grid only if any timed entries are present"
  2436. require-timed)
  2437. (const :tag "Skip grid times already present in an entry"
  2438. remove-match))
  2439. (string :tag "Grid String")
  2440. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2441. (defgroup org-agenda-sorting nil
  2442. "Options concerning sorting in the Org-mode Agenda."
  2443. :tag "Org Agenda Sorting"
  2444. :group 'org-agenda)
  2445. (defconst org-sorting-choice
  2446. '(choice
  2447. (const time-up) (const time-down)
  2448. (const category-keep) (const category-up) (const category-down)
  2449. (const tag-down) (const tag-up)
  2450. (const priority-up) (const priority-down))
  2451. "Sorting choices.")
  2452. (defcustom org-agenda-sorting-strategy
  2453. '((agenda time-up category-keep priority-down)
  2454. (todo category-keep priority-down)
  2455. (tags category-keep priority-down))
  2456. "Sorting structure for the agenda items of a single day.
  2457. This is a list of symbols which will be used in sequence to determine
  2458. if an entry should be listed before another entry. The following
  2459. symbols are recognized:
  2460. time-up Put entries with time-of-day indications first, early first
  2461. time-down Put entries with time-of-day indications first, late first
  2462. category-keep Keep the default order of categories, corresponding to the
  2463. sequence in `org-agenda-files'.
  2464. category-up Sort alphabetically by category, A-Z.
  2465. category-down Sort alphabetically by category, Z-A.
  2466. tag-up Sort alphabetically by last tag, A-Z.
  2467. tag-down Sort alphabetically by last tag, Z-A.
  2468. priority-up Sort numerically by priority, high priority last.
  2469. priority-down Sort numerically by priority, high priority first.
  2470. The different possibilities will be tried in sequence, and testing stops
  2471. if one comparison returns a \"not-equal\". For example, the default
  2472. '(time-up category-keep priority-down)
  2473. means: Pull out all entries having a specified time of day and sort them,
  2474. in order to make a time schedule for the current day the first thing in the
  2475. agenda listing for the day. Of the entries without a time indication, keep
  2476. the grouped in categories, don't sort the categories, but keep them in
  2477. the sequence given in `org-agenda-files'. Within each category sort by
  2478. priority.
  2479. Leaving out `category-keep' would mean that items will be sorted across
  2480. categories by priority.
  2481. Instead of a single list, this can also be a set of list for specific
  2482. contents, with a context symbol in the car of the list, any of
  2483. `agenda', `todo', `tags' for the corresponding agenda views."
  2484. :group 'org-agenda-sorting
  2485. :type `(choice
  2486. (repeat :tag "General" ,org-sorting-choice)
  2487. (list :tag "Individually"
  2488. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2489. (repeat ,org-sorting-choice))
  2490. (cons (const :tag "Strategy for TODO lists" todo)
  2491. (repeat ,org-sorting-choice))
  2492. (cons (const :tag "Strategy for Tags matches" tags)
  2493. (repeat ,org-sorting-choice)))))
  2494. (defcustom org-sort-agenda-notime-is-late t
  2495. "Non-nil means, items without time are considered late.
  2496. This is only relevant for sorting. When t, items which have no explicit
  2497. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2498. do have a time. When nil, the default time is before 0:00. You can use this
  2499. option to decide if the schedule for today should come before or after timeless
  2500. agenda entries."
  2501. :group 'org-agenda-sorting
  2502. :type 'boolean)
  2503. (defgroup org-agenda-line-format nil
  2504. "Options concerning the entry prefix in the Org-mode agenda display."
  2505. :tag "Org Agenda Line Format"
  2506. :group 'org-agenda)
  2507. (defcustom org-agenda-prefix-format
  2508. '((agenda . " %-12:c%?-12t% s")
  2509. (timeline . " % s")
  2510. (todo . " %-12:c")
  2511. (tags . " %-12:c"))
  2512. "Format specifications for the prefix of items in the agenda views.
  2513. An alist with four entries, for the different agenda types. The keys to the
  2514. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2515. are format strings.
  2516. This format works similar to a printf format, with the following meaning:
  2517. %c the category of the item, \"Diary\" for entries from the diary, or
  2518. as given by the CATEGORY keyword or derived from the file name.
  2519. %T the *last* tag of the item. Last because inherited tags come
  2520. first in the list.
  2521. %t the time-of-day specification if one applies to the entry, in the
  2522. format HH:MM
  2523. %s Scheduling/Deadline information, a short string
  2524. All specifiers work basically like the standard `%s' of printf, but may
  2525. contain two additional characters: A question mark just after the `%' and
  2526. a whitespace/punctuation character just before the final letter.
  2527. If the first character after `%' is a question mark, the entire field
  2528. will only be included if the corresponding value applies to the
  2529. current entry. This is useful for fields which should have fixed
  2530. width when present, but zero width when absent. For example,
  2531. \"%?-12t\" will result in a 12 character time field if a time of the
  2532. day is specified, but will completely disappear in entries which do
  2533. not contain a time.
  2534. If there is punctuation or whitespace character just before the final
  2535. format letter, this character will be appended to the field value if
  2536. the value is not empty. For example, the format \"%-12:c\" leads to
  2537. \"Diary: \" if the category is \"Diary\". If the category were be
  2538. empty, no additional colon would be interted.
  2539. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2540. - Indent the line with two space characters
  2541. - Give the category in a 12 chars wide field, padded with whitespace on
  2542. the right (because of `-'). Append a colon if there is a category
  2543. (because of `:').
  2544. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2545. time, don't put in an empty field, just skip it (because of '?').
  2546. - Finally, put the scheduling information and append a whitespace.
  2547. As another example, if you don't want the time-of-day of entries in
  2548. the prefix, you could use:
  2549. (setq org-agenda-prefix-format \" %-11:c% s\")
  2550. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2551. `org-agenda-remove-tags'."
  2552. :type '(choice
  2553. (string :tag "General format")
  2554. (list :greedy t :tag "View dependent"
  2555. (cons (const agenda) (string :tag "Format"))
  2556. (cons (const timeline) (string :tag "Format"))
  2557. (cons (const todo) (string :tag "Format"))
  2558. (cons (const tags) (string :tag "Format"))))
  2559. :group 'org-agenda-line-format)
  2560. (defvar org-prefix-format-compiled nil
  2561. "The compiled version of the most recently used prefix format.
  2562. See the variable `org-agenda-prefix-format'.")
  2563. (defcustom org-agenda-todo-keyword-format "%-1s"
  2564. "Format for the TODO keyword in agenda lines.
  2565. Set this to something like \"%-12s\" if you want all TODO keywords
  2566. to occupy a fixed space in the agenda display."
  2567. :group 'org-agenda-line-format
  2568. :type 'string)
  2569. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2570. "Text preceeding scheduled items in the agenda view.
  2571. This is a list with two strings. The first applies when the item is
  2572. scheduled on the current day. The second applies when it has been scheduled
  2573. previously, it may contain a %d to capture how many days ago the item was
  2574. scheduled."
  2575. :group 'org-agenda-line-format
  2576. :type '(list
  2577. (string :tag "Scheduled today ")
  2578. (string :tag "Scheduled previously")))
  2579. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2580. "Text preceeding deadline items in the agenda view.
  2581. This is a list with two strings. The first applies when the item has its
  2582. deadline on the current day. The second applies when it is in the past or
  2583. in the future, it may contain %d to capture how many days away the deadline
  2584. is (was)."
  2585. :group 'org-agenda-line-format
  2586. :type '(list
  2587. (string :tag "Deadline today ")
  2588. (string :tag "Deadline relative")))
  2589. (defcustom org-agenda-remove-times-when-in-prefix t
  2590. "Non-nil means, remove duplicate time specifications in agenda items.
  2591. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2592. time-of-day specification in a headline or diary entry is extracted and
  2593. placed into the prefix. If this option is non-nil, the original specification
  2594. \(a timestamp or -range, or just a plain time(range) specification like
  2595. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2596. cluttered.
  2597. The option can be t or nil. It may also be the symbol `beg', indicating
  2598. that the time should only be removed what it is located at the beginning of
  2599. the headline/diary entry."
  2600. :group 'org-agenda-line-format
  2601. :type '(choice
  2602. (const :tag "Always" t)
  2603. (const :tag "Never" nil)
  2604. (const :tag "When at beginning of entry" beg)))
  2605. (defcustom org-agenda-default-appointment-duration nil
  2606. "Default duration for appointments that only have a starting time.
  2607. When nil, no duration is specified in such cases.
  2608. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2609. :group 'org-agenda-line-format
  2610. :type '(choice
  2611. (integer :tag "Minutes")
  2612. (const :tag "No default duration")))
  2613. (defcustom org-agenda-remove-tags nil
  2614. "Non-nil means, remove the tags from the headline copy in the agenda.
  2615. When this is the symbol `prefix', only remove tags when
  2616. `org-agenda-prefix-format' contains a `%T' specifier."
  2617. :group 'org-agenda-line-format
  2618. :type '(choice
  2619. (const :tag "Always" t)
  2620. (const :tag "Never" nil)
  2621. (const :tag "When prefix format contains %T" prefix)))
  2622. (if (fboundp 'defvaralias)
  2623. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2624. 'org-agenda-remove-tags))
  2625. (defcustom org-agenda-tags-column -80
  2626. "Shift tags in agenda items to this column.
  2627. If this number is positive, it specifies the column. If it is negative,
  2628. it means that the tags should be flushright to that column. For example,
  2629. -80 works well for a normal 80 character screen."
  2630. :group 'org-agenda-line-format
  2631. :type 'integer)
  2632. (if (fboundp 'defvaralias)
  2633. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2634. (defcustom org-agenda-fontify-priorities t
  2635. "Non-nil means, highlight low and high priorities in agenda.
  2636. When t, the highest priority entries are bold, lowest priority italic.
  2637. This may also be an association list of priority faces. The face may be
  2638. a names face, or a list like `(:background \"Red\")'."
  2639. :group 'org-agenda-line-format
  2640. :type '(choice
  2641. (const :tag "Never" nil)
  2642. (const :tag "Defaults" t)
  2643. (repeat :tag "Specify"
  2644. (list (character :tag "Priority" :value ?A)
  2645. (sexp :tag "face")))))
  2646. (defgroup org-latex nil
  2647. "Options for embedding LaTeX code into Org-mode"
  2648. :tag "Org LaTeX"
  2649. :group 'org)
  2650. (defcustom org-format-latex-options
  2651. '(:foreground default :background default :scale 1.0
  2652. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2653. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2654. "Options for creating images from LaTeX fragments.
  2655. This is a property list with the following properties:
  2656. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2657. `default' means use the forground of the default face.
  2658. :background the background color, or \"Transparent\".
  2659. `default' means use the background of the default face.
  2660. :scale a scaling factor for the size of the images
  2661. :html-foreground, :html-background, :html-scale
  2662. The same numbers for HTML export.
  2663. :matchers a list indicating which matchers should be used to
  2664. find LaTeX fragments. Valid members of this list are:
  2665. \"begin\" find environments
  2666. \"$\" find math expressions surrounded by $...$
  2667. \"$$\" find math expressions surrounded by $$....$$
  2668. \"\\(\" find math expressions surrounded by \\(...\\)
  2669. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2670. :group 'org-latex
  2671. :type 'plist)
  2672. (defcustom org-format-latex-header "\\documentclass{article}
  2673. \\usepackage{fullpage} % do not remove
  2674. \\usepackage{amssymb}
  2675. \\usepackage[usenames]{color}
  2676. \\usepackage{amsmath}
  2677. \\usepackage{latexsym}
  2678. \\usepackage[mathscr]{eucal}
  2679. \\pagestyle{empty} % do not remove"
  2680. "The document header used for processing LaTeX fragments."
  2681. :group 'org-latex
  2682. :type 'string)
  2683. (defgroup org-export nil
  2684. "Options for exporting org-listings."
  2685. :tag "Org Export"
  2686. :group 'org)
  2687. (defgroup org-export-general nil
  2688. "General options for exporting Org-mode files."
  2689. :tag "Org Export General"
  2690. :group 'org-export)
  2691. ;; FIXME
  2692. (defvar org-export-publishing-directory nil)
  2693. (defcustom org-export-with-special-strings t
  2694. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2695. When this option is turned on, these strings will be exported as:
  2696. Org HTML LaTeX
  2697. -----+----------+--------
  2698. \\- &shy; \\-
  2699. -- &ndash; --
  2700. --- &mdash; ---
  2701. ... &hellip; \ldots
  2702. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2703. :group 'org-export-translation
  2704. :type 'boolean)
  2705. (defcustom org-export-language-setup
  2706. '(("en" "Author" "Date" "Table of Contents")
  2707. ("cs" "Autor" "Datum" "Obsah")
  2708. ("da" "Ophavsmand" "Dato" "Indhold")
  2709. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2710. ("es" "Autor" "Fecha" "\xcdndice")
  2711. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2712. ("it" "Autore" "Data" "Indice")
  2713. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2714. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2715. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2716. "Terms used in export text, translated to different languages.
  2717. Use the variable `org-export-default-language' to set the language,
  2718. or use the +OPTION lines for a per-file setting."
  2719. :group 'org-export-general
  2720. :type '(repeat
  2721. (list
  2722. (string :tag "HTML language tag")
  2723. (string :tag "Author")
  2724. (string :tag "Date")
  2725. (string :tag "Table of Contents"))))
  2726. (defcustom org-export-default-language "en"
  2727. "The default language of HTML export, as a string.
  2728. This should have an association in `org-export-language-setup'."
  2729. :group 'org-export-general
  2730. :type 'string)
  2731. (defcustom org-export-skip-text-before-1st-heading t
  2732. "Non-nil means, skip all text before the first headline when exporting.
  2733. When nil, that text is exported as well."
  2734. :group 'org-export-general
  2735. :type 'boolean)
  2736. (defcustom org-export-headline-levels 3
  2737. "The last level which is still exported as a headline.
  2738. Inferior levels will produce itemize lists when exported.
  2739. Note that a numeric prefix argument to an exporter function overrides
  2740. this setting.
  2741. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2742. :group 'org-export-general
  2743. :type 'number)
  2744. (defcustom org-export-with-section-numbers t
  2745. "Non-nil means, add section numbers to headlines when exporting.
  2746. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2747. :group 'org-export-general
  2748. :type 'boolean)
  2749. (defcustom org-export-with-toc t
  2750. "Non-nil means, create a table of contents in exported files.
  2751. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2752. When an integer, include levels up to N in the toc, this may then be
  2753. different from `org-export-headline-levels', but it will not be allowed
  2754. to be larger than the number of headline levels.
  2755. When nil, no table of contents is made.
  2756. Headlines which contain any TODO items will be marked with \"(*)\" in
  2757. ASCII export, and with red color in HTML output, if the option
  2758. `org-export-mark-todo-in-toc' is set.
  2759. In HTML output, the TOC will be clickable.
  2760. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2761. or \"toc:3\"."
  2762. :group 'org-export-general
  2763. :type '(choice
  2764. (const :tag "No Table of Contents" nil)
  2765. (const :tag "Full Table of Contents" t)
  2766. (integer :tag "TOC to level")))
  2767. (defcustom org-export-mark-todo-in-toc nil
  2768. "Non-nil means, mark TOC lines that contain any open TODO items."
  2769. :group 'org-export-general
  2770. :type 'boolean)
  2771. (defcustom org-export-preserve-breaks nil
  2772. "Non-nil means, preserve all line breaks when exporting.
  2773. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2774. export, line breaks will always be preserved, regardless of this variable.
  2775. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2776. :group 'org-export-general
  2777. :type 'boolean)
  2778. (defcustom org-export-with-archived-trees 'headline
  2779. "Whether subtrees with the ARCHIVE tag should be exported.
  2780. This can have three different values
  2781. nil Do not export, pretend this tree is not present
  2782. t Do export the entire tree
  2783. headline Only export the headline, but skip the tree below it."
  2784. :group 'org-export-general
  2785. :group 'org-archive
  2786. :type '(choice
  2787. (const :tag "not at all" nil)
  2788. (const :tag "headline only" 'headline)
  2789. (const :tag "entirely" t)))
  2790. (defcustom org-export-author-info t
  2791. "Non-nil means, insert author name and email into the exported file.
  2792. This option can also be set with the +OPTIONS line,
  2793. e.g. \"author-info:nil\"."
  2794. :group 'org-export-general
  2795. :type 'boolean)
  2796. (defcustom org-export-time-stamp-file t
  2797. "Non-nil means, insert a time stamp into the exported file.
  2798. The time stamp shows when the file was created.
  2799. This option can also be set with the +OPTIONS line,
  2800. e.g. \"timestamp:nil\"."
  2801. :group 'org-export-general
  2802. :type 'boolean)
  2803. (defcustom org-export-with-timestamps t
  2804. "If nil, do not export time stamps and associated keywords."
  2805. :group 'org-export-general
  2806. :type 'boolean)
  2807. (defcustom org-export-remove-timestamps-from-toc t
  2808. "If nil, remove timestamps from the table of contents entries."
  2809. :group 'org-export-general
  2810. :type 'boolean)
  2811. (defcustom org-export-with-tags 'not-in-toc
  2812. "If nil, do not export tags, just remove them from headlines.
  2813. If this is the symbol `not-in-toc', tags will be removed from table of
  2814. contents entries, but still be shown in the headlines of the document.
  2815. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2816. :group 'org-export-general
  2817. :type '(choice
  2818. (const :tag "Off" nil)
  2819. (const :tag "Not in TOC" not-in-toc)
  2820. (const :tag "On" t)))
  2821. (defcustom org-export-with-drawers nil
  2822. "Non-nil means, export with drawers like the property drawer.
  2823. When t, all drawers are exported. This may also be a list of
  2824. drawer names to export."
  2825. :group 'org-export-general
  2826. :type '(choice
  2827. (const :tag "All drawers" t)
  2828. (const :tag "None" nil)
  2829. (repeat :tag "Selected drawers"
  2830. (string :tag "Drawer name"))))
  2831. (defgroup org-export-translation nil
  2832. "Options for translating special ascii sequences for the export backends."
  2833. :tag "Org Export Translation"
  2834. :group 'org-export)
  2835. (defcustom org-export-with-emphasize t
  2836. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2837. If the export target supports emphasizing text, the word will be
  2838. typeset in bold, italic, or underlined, respectively. Works only for
  2839. single words, but you can say: I *really* *mean* *this*.
  2840. Not all export backends support this.
  2841. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2842. :group 'org-export-translation
  2843. :type 'boolean)
  2844. (defcustom org-export-with-footnotes t
  2845. "If nil, export [1] as a footnote marker.
  2846. Lines starting with [1] will be formatted as footnotes.
  2847. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2848. :group 'org-export-translation
  2849. :type 'boolean)
  2850. (defcustom org-export-with-sub-superscripts t
  2851. "Non-nil means, interpret \"_\" and \"^\" for export.
  2852. When this option is turned on, you can use TeX-like syntax for sub- and
  2853. superscripts. Several characters after \"_\" or \"^\" will be
  2854. considered as a single item - so grouping with {} is normally not
  2855. needed. For example, the following things will be parsed as single
  2856. sub- or superscripts.
  2857. 10^24 or 10^tau several digits will be considered 1 item.
  2858. 10^-12 or 10^-tau a leading sign with digits or a word
  2859. x^2-y^3 will be read as x^2 - y^3, because items are
  2860. terminated by almost any nonword/nondigit char.
  2861. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2862. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2863. sub/superscript. If you set this variable to the symbol `{}',
  2864. the braces are *required* in order to trigger interpretations as
  2865. sub/superscript. This can be helpful in documents that need \"_\"
  2866. frequently in plain text.
  2867. Not all export backends support this, but HTML does.
  2868. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2869. :group 'org-export-translation
  2870. :type '(choice
  2871. (const :tag "Always interpret" t)
  2872. (const :tag "Only with braces" {})
  2873. (const :tag "Never interpret" nil)))
  2874. (defcustom org-export-with-special-strings t
  2875. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2876. When this option is turned on, these strings will be exported as:
  2877. \\- : &shy;
  2878. -- : &ndash;
  2879. --- : &mdash;
  2880. Not all export backends support this, but HTML does.
  2881. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2882. :group 'org-export-translation
  2883. :type 'boolean)
  2884. (defcustom org-export-with-TeX-macros t
  2885. "Non-nil means, interpret simple TeX-like macros when exporting.
  2886. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2887. No only real TeX macros will work here, but the standard HTML entities
  2888. for math can be used as macro names as well. For a list of supported
  2889. names in HTML export, see the constant `org-html-entities'.
  2890. Not all export backends support this.
  2891. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2892. :group 'org-export-translation
  2893. :group 'org-export-latex
  2894. :type 'boolean)
  2895. (defcustom org-export-with-LaTeX-fragments nil
  2896. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2897. When set, the exporter will find LaTeX environments if the \\begin line is
  2898. the first non-white thing on a line. It will also find the math delimiters
  2899. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2900. display math.
  2901. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2902. :group 'org-export-translation
  2903. :group 'org-export-latex
  2904. :type 'boolean)
  2905. (defcustom org-export-with-fixed-width t
  2906. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2907. This can be used to have pre-formatted text, fragments of code etc. For
  2908. example:
  2909. : ;; Some Lisp examples
  2910. : (while (defc cnt)
  2911. : (ding))
  2912. will be looking just like this in also HTML. See also the QUOTE keyword.
  2913. Not all export backends support this.
  2914. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2915. :group 'org-export-translation
  2916. :type 'boolean)
  2917. (defcustom org-match-sexp-depth 3
  2918. "Number of stacked braces for sub/superscript matching.
  2919. This has to be set before loading org.el to be effective."
  2920. :group 'org-export-translation
  2921. :type 'integer)
  2922. (defgroup org-export-tables nil
  2923. "Options for exporting tables in Org-mode."
  2924. :tag "Org Export Tables"
  2925. :group 'org-export)
  2926. (defcustom org-export-with-tables t
  2927. "If non-nil, lines starting with \"|\" define a table.
  2928. For example:
  2929. | Name | Address | Birthday |
  2930. |-------------+----------+-----------|
  2931. | Arthur Dent | England | 29.2.2100 |
  2932. Not all export backends support this.
  2933. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2934. :group 'org-export-tables
  2935. :type 'boolean)
  2936. (defcustom org-export-highlight-first-table-line t
  2937. "Non-nil means, highlight the first table line.
  2938. In HTML export, this means use <th> instead of <td>.
  2939. In tables created with table.el, this applies to the first table line.
  2940. In Org-mode tables, all lines before the first horizontal separator
  2941. line will be formatted with <th> tags."
  2942. :group 'org-export-tables
  2943. :type 'boolean)
  2944. (defcustom org-export-table-remove-special-lines t
  2945. "Remove special lines and marking characters in calculating tables.
  2946. This removes the special marking character column from tables that are set
  2947. up for spreadsheet calculations. It also removes the entire lines
  2948. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2949. the values of constants may be useful to have."
  2950. :group 'org-export-tables
  2951. :type 'boolean)
  2952. (defcustom org-export-prefer-native-exporter-for-tables nil
  2953. "Non-nil means, always export tables created with table.el natively.
  2954. Natively means, use the HTML code generator in table.el.
  2955. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2956. the table does not use row- or column-spanning). This has the
  2957. advantage, that the automatic HTML conversions for math symbols and
  2958. sub/superscripts can be applied. Org-mode's HTML generator is also
  2959. much faster."
  2960. :group 'org-export-tables
  2961. :type 'boolean)
  2962. (defgroup org-export-ascii nil
  2963. "Options specific for ASCII export of Org-mode files."
  2964. :tag "Org Export ASCII"
  2965. :group 'org-export)
  2966. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2967. "Characters for underlining headings in ASCII export.
  2968. In the given sequence, these characters will be used for level 1, 2, ..."
  2969. :group 'org-export-ascii
  2970. :type '(repeat character))
  2971. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2972. "Bullet characters for headlines converted to lists in ASCII export.
  2973. The first character is used for the first lest level generated in this
  2974. way, and so on. If there are more levels than characters given here,
  2975. the list will be repeated.
  2976. Note that plain lists will keep the same bullets as the have in the
  2977. Org-mode file."
  2978. :group 'org-export-ascii
  2979. :type '(repeat character))
  2980. (defgroup org-export-xml nil
  2981. "Options specific for XML export of Org-mode files."
  2982. :tag "Org Export XML"
  2983. :group 'org-export)
  2984. (defgroup org-export-html nil
  2985. "Options specific for HTML export of Org-mode files."
  2986. :tag "Org Export HTML"
  2987. :group 'org-export)
  2988. (defcustom org-export-html-coding-system nil
  2989. ""
  2990. :group 'org-export-html
  2991. :type 'coding-system)
  2992. (defcustom org-export-html-extension "html"
  2993. "The extension for exported HTML files."
  2994. :group 'org-export-html
  2995. :type 'string)
  2996. (defcustom org-export-html-style
  2997. "<style type=\"text/css\">
  2998. html {
  2999. font-family: Times, serif;
  3000. font-size: 12pt;
  3001. }
  3002. .title { text-align: center; }
  3003. .todo { color: red; }
  3004. .done { color: green; }
  3005. .timestamp { color: grey }
  3006. .timestamp-kwd { color: CadetBlue }
  3007. .tag { background-color:lightblue; font-weight:normal }
  3008. .target { background-color: lavender; }
  3009. pre {
  3010. border: 1pt solid #AEBDCC;
  3011. background-color: #F3F5F7;
  3012. padding: 5pt;
  3013. font-family: courier, monospace;
  3014. }
  3015. table { border-collapse: collapse; }
  3016. td, th {
  3017. vertical-align: top;
  3018. <!--border: 1pt solid #ADB9CC;-->
  3019. }
  3020. </style>"
  3021. "The default style specification for exported HTML files.
  3022. Since there are different ways of setting style information, this variable
  3023. needs to contain the full HTML structure to provide a style, including the
  3024. surrounding HTML tags. The style specifications should include definitions
  3025. for new classes todo, done, title, and deadline. For example, legal values
  3026. would be:
  3027. <style type=\"text/css\">
  3028. p { font-weight: normal; color: gray; }
  3029. h1 { color: black; }
  3030. .title { text-align: center; }
  3031. .todo, .deadline { color: red; }
  3032. .done { color: green; }
  3033. </style>
  3034. or, if you want to keep the style in a file,
  3035. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3036. As the value of this option simply gets inserted into the HTML <head> header,
  3037. you can \"misuse\" it to add arbitrary text to the header."
  3038. :group 'org-export-html
  3039. :type 'string)
  3040. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3041. "Format for typesetting the document title in HTML export."
  3042. :group 'org-export-html
  3043. :type 'string)
  3044. (defcustom org-export-html-toplevel-hlevel 2
  3045. "The <H> level for level 1 headings in HTML export."
  3046. :group 'org-export-html
  3047. :type 'string)
  3048. (defcustom org-export-html-link-org-files-as-html t
  3049. "Non-nil means, make file links to `file.org' point to `file.html'.
  3050. When org-mode is exporting an org-mode file to HTML, links to
  3051. non-html files are directly put into a href tag in HTML.
  3052. However, links to other Org-mode files (recognized by the
  3053. extension `.org.) should become links to the corresponding html
  3054. file, assuming that the linked org-mode file will also be
  3055. converted to HTML.
  3056. When nil, the links still point to the plain `.org' file."
  3057. :group 'org-export-html
  3058. :type 'boolean)
  3059. (defcustom org-export-html-inline-images 'maybe
  3060. "Non-nil means, inline images into exported HTML pages.
  3061. This is done using an <img> tag. When nil, an anchor with href is used to
  3062. link to the image. If this option is `maybe', then images in links with
  3063. an empty description will be inlined, while images with a description will
  3064. be linked only."
  3065. :group 'org-export-html
  3066. :type '(choice (const :tag "Never" nil)
  3067. (const :tag "Always" t)
  3068. (const :tag "When there is no description" maybe)))
  3069. ;; FIXME: rename
  3070. (defcustom org-export-html-expand t
  3071. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3072. When nil, these tags will be exported as plain text and therefore
  3073. not be interpreted by a browser.
  3074. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3075. :group 'org-export-html
  3076. :type 'boolean)
  3077. (defcustom org-export-html-table-tag
  3078. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3079. "The HTML tag that is used to start a table.
  3080. This must be a <table> tag, but you may change the options like
  3081. borders and spacing."
  3082. :group 'org-export-html
  3083. :type 'string)
  3084. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3085. "The opening tag for table header fields.
  3086. This is customizable so that alignment options can be specified."
  3087. :group 'org-export-tables
  3088. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3089. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3090. "The opening tag for table data fields.
  3091. This is customizable so that alignment options can be specified."
  3092. :group 'org-export-tables
  3093. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3094. (defcustom org-export-html-with-timestamp nil
  3095. "If non-nil, write `org-export-html-html-helper-timestamp'
  3096. into the exported HTML text. Otherwise, the buffer will just be saved
  3097. to a file."
  3098. :group 'org-export-html
  3099. :type 'boolean)
  3100. (defcustom org-export-html-html-helper-timestamp
  3101. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3102. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3103. :group 'org-export-html
  3104. :type 'string)
  3105. (defgroup org-export-icalendar nil
  3106. "Options specific for iCalendar export of Org-mode files."
  3107. :tag "Org Export iCalendar"
  3108. :group 'org-export)
  3109. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3110. "The file name for the iCalendar file covering all agenda files.
  3111. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3112. The file name should be absolute, the file will be overwritten without warning."
  3113. :group 'org-export-icalendar
  3114. :type 'file)
  3115. (defcustom org-icalendar-include-todo nil
  3116. "Non-nil means, export to iCalendar files should also cover TODO items."
  3117. :group 'org-export-icalendar
  3118. :type '(choice
  3119. (const :tag "None" nil)
  3120. (const :tag "Unfinished" t)
  3121. (const :tag "All" all)))
  3122. (defcustom org-icalendar-include-sexps t
  3123. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3124. These are entries like in the diary, but directly in an Org-mode file."
  3125. :group 'org-export-icalendar
  3126. :type 'boolean)
  3127. (defcustom org-icalendar-include-body 100
  3128. "Amount of text below headline to be included in iCalendar export.
  3129. This is a number of characters that should maximally be included.
  3130. Properties, scheduling and clocking lines will always be removed.
  3131. The text will be inserted into the DESCRIPTION field."
  3132. :group 'org-export-icalendar
  3133. :type '(choice
  3134. (const :tag "Nothing" nil)
  3135. (const :tag "Everything" t)
  3136. (integer :tag "Max characters")))
  3137. (defcustom org-icalendar-combined-name "OrgMode"
  3138. "Calendar name for the combined iCalendar representing all agenda files."
  3139. :group 'org-export-icalendar
  3140. :type 'string)
  3141. (defgroup org-font-lock nil
  3142. "Font-lock settings for highlighting in Org-mode."
  3143. :tag "Org Font Lock"
  3144. :group 'org)
  3145. (defcustom org-level-color-stars-only nil
  3146. "Non-nil means fontify only the stars in each headline.
  3147. When nil, the entire headline is fontified.
  3148. Changing it requires restart of `font-lock-mode' to become effective
  3149. also in regions already fontified."
  3150. :group 'org-font-lock
  3151. :type 'boolean)
  3152. (defcustom org-hide-leading-stars nil
  3153. "Non-nil means, hide the first N-1 stars in a headline.
  3154. This works by using the face `org-hide' for these stars. This
  3155. face is white for a light background, and black for a dark
  3156. background. You may have to customize the face `org-hide' to
  3157. make this work.
  3158. Changing it requires restart of `font-lock-mode' to become effective
  3159. also in regions already fontified.
  3160. You may also set this on a per-file basis by adding one of the following
  3161. lines to the buffer:
  3162. #+STARTUP: hidestars
  3163. #+STARTUP: showstars"
  3164. :group 'org-font-lock
  3165. :type 'boolean)
  3166. (defcustom org-fontify-done-headline nil
  3167. "Non-nil means, change the face of a headline if it is marked DONE.
  3168. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3169. When this is non-nil, the headline after the keyword is set to the
  3170. `org-headline-done' as an additional indication."
  3171. :group 'org-font-lock
  3172. :type 'boolean)
  3173. (defcustom org-fontify-emphasized-text t
  3174. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3175. Changing this variable requires a restart of Emacs to take effect."
  3176. :group 'org-font-lock
  3177. :type 'boolean)
  3178. (defcustom org-highlight-latex-fragments-and-specials nil
  3179. "Non-nil means, fontify what is treated specially by the exporters."
  3180. :group 'org-font-lock
  3181. :type 'boolean)
  3182. (defcustom org-hide-emphasis-markers nil
  3183. "Non-nil mean font-lock should hide the emphasis marker characters."
  3184. :group 'org-font-lock
  3185. :type 'boolean)
  3186. (defvar org-emph-re nil
  3187. "Regular expression for matching emphasis.")
  3188. (defvar org-verbatim-re nil
  3189. "Regular expression for matching verbatim text.")
  3190. (defvar org-emphasis-regexp-components) ; defined just below
  3191. (defvar org-emphasis-alist) ; defined just below
  3192. (defun org-set-emph-re (var val)
  3193. "Set variable and compute the emphasis regular expression."
  3194. (set var val)
  3195. (when (and (boundp 'org-emphasis-alist)
  3196. (boundp 'org-emphasis-regexp-components)
  3197. org-emphasis-alist org-emphasis-regexp-components)
  3198. (let* ((e org-emphasis-regexp-components)
  3199. (pre (car e))
  3200. (post (nth 1 e))
  3201. (border (nth 2 e))
  3202. (body (nth 3 e))
  3203. (nl (nth 4 e))
  3204. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3205. (body1 (concat body "*?"))
  3206. (markers (mapconcat 'car org-emphasis-alist ""))
  3207. (vmarkers (mapconcat
  3208. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3209. org-emphasis-alist "")))
  3210. ;; make sure special characters appear at the right position in the class
  3211. (if (string-match "\\^" markers)
  3212. (setq markers (concat (replace-match "" t t markers) "^")))
  3213. (if (string-match "-" markers)
  3214. (setq markers (concat (replace-match "" t t markers) "-")))
  3215. (if (string-match "\\^" vmarkers)
  3216. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3217. (if (string-match "-" vmarkers)
  3218. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3219. (if (> nl 0)
  3220. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3221. (int-to-string nl) "\\}")))
  3222. ;; Make the regexp
  3223. (setq org-emph-re
  3224. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3225. "\\("
  3226. "\\([" markers "]\\)"
  3227. "\\("
  3228. "[^" border "]\\|"
  3229. "[^" border (if (and nil stacked) markers) "]"
  3230. body1
  3231. "[^" border (if (and nil stacked) markers) "]"
  3232. "\\)"
  3233. "\\3\\)"
  3234. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3235. (setq org-verbatim-re
  3236. (concat "\\([" pre "]\\|^\\)"
  3237. "\\("
  3238. "\\([" vmarkers "]\\)"
  3239. "\\("
  3240. "[^" border "]\\|"
  3241. "[^" border "]"
  3242. body1
  3243. "[^" border "]"
  3244. "\\)"
  3245. "\\3\\)"
  3246. "\\([" post "]\\|$\\)")))))
  3247. (defcustom org-emphasis-regexp-components
  3248. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3249. "Components used to build the regular expression for emphasis.
  3250. This is a list with 6 entries. Terminology: In an emphasis string
  3251. like \" *strong word* \", we call the initial space PREMATCH, the final
  3252. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3253. and \"trong wor\" is the body. The different components in this variable
  3254. specify what is allowed/forbidden in each part:
  3255. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3256. post Chars allowed as postmatch. End of line will be allowed too.
  3257. border The chars *forbidden* as border characters.
  3258. body-regexp A regexp like \".\" to match a body character. Don't use
  3259. non-shy groups here, and don't allow newline here.
  3260. newline The maximum number of newlines allowed in an emphasis exp.
  3261. Use customize to modify this, or restart Emacs after changing it."
  3262. :group 'org-font-lock
  3263. :set 'org-set-emph-re
  3264. :type '(list
  3265. (sexp :tag "Allowed chars in pre ")
  3266. (sexp :tag "Allowed chars in post ")
  3267. (sexp :tag "Forbidden chars in border ")
  3268. (sexp :tag "Regexp for body ")
  3269. (integer :tag "number of newlines allowed")
  3270. (option (boolean :tag "Stacking (DISABLED) "))))
  3271. (defcustom org-emphasis-alist
  3272. '(("*" bold "<b>" "</b>")
  3273. ("/" italic "<i>" "</i>")
  3274. ("_" underline "<u>" "</u>")
  3275. ("=" org-code "<code>" "</code>" verbatim)
  3276. ("~" org-verbatim "" "" verbatim)
  3277. ("+" (:strike-through t) "<del>" "</del>")
  3278. )
  3279. "Special syntax for emphasized text.
  3280. Text starting and ending with a special character will be emphasized, for
  3281. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3282. characters, the face to be used by font-lock for highlighting in Org-mode
  3283. Emacs buffers, and the HTML tags to be used for this.
  3284. Use customize to modify this, or restart Emacs after changing it."
  3285. :group 'org-font-lock
  3286. :set 'org-set-emph-re
  3287. :type '(repeat
  3288. (list
  3289. (string :tag "Marker character")
  3290. (choice
  3291. (face :tag "Font-lock-face")
  3292. (plist :tag "Face property list"))
  3293. (string :tag "HTML start tag")
  3294. (string :tag "HTML end tag")
  3295. (option (const verbatim)))))
  3296. ;;; The faces
  3297. (defgroup org-faces nil
  3298. "Faces in Org-mode."
  3299. :tag "Org Faces"
  3300. :group 'org-font-lock)
  3301. (defun org-compatible-face (inherits specs)
  3302. "Make a compatible face specification.
  3303. If INHERITS is an existing face and if the Emacs version supports it,
  3304. just inherit the face. If not, use SPECS to define the face.
  3305. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3306. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3307. to the top of the list. The `min-colors' attribute will be removed from
  3308. any other entries, and any resulting duplicates will be removed entirely."
  3309. (cond
  3310. ((and inherits (facep inherits)
  3311. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3312. ;; In Emacs 23, we use inheritance where possible.
  3313. ;; We only do this in Emacs 23, because only there the outline
  3314. ;; faces have been changed to the original org-mode-level-faces.
  3315. (list (list t :inherit inherits)))
  3316. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3317. ;; These do not understand the `min-colors' attribute.
  3318. (let (r e a)
  3319. (while (setq e (pop specs))
  3320. (cond
  3321. ((memq (car e) '(t default)) (push e r))
  3322. ((setq a (member '(min-colors 8) (car e)))
  3323. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3324. (cdr e)))))
  3325. ((setq a (assq 'min-colors (car e)))
  3326. (setq e (cons (delq a (car e)) (cdr e)))
  3327. (or (assoc (car e) r) (push e r)))
  3328. (t (or (assoc (car e) r) (push e r)))))
  3329. (nreverse r)))
  3330. (t specs)))
  3331. (put 'org-compatible-face 'lisp-indent-function 1)
  3332. (defface org-hide
  3333. '((((background light)) (:foreground "white"))
  3334. (((background dark)) (:foreground "black")))
  3335. "Face used to hide leading stars in headlines.
  3336. The forground color of this face should be equal to the background
  3337. color of the frame."
  3338. :group 'org-faces)
  3339. (defface org-level-1 ;; font-lock-function-name-face
  3340. (org-compatible-face 'outline-1
  3341. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3342. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3343. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3344. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3345. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3346. (t (:bold t))))
  3347. "Face used for level 1 headlines."
  3348. :group 'org-faces)
  3349. (defface org-level-2 ;; font-lock-variable-name-face
  3350. (org-compatible-face 'outline-2
  3351. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3352. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3353. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3354. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3355. (t (:bold t))))
  3356. "Face used for level 2 headlines."
  3357. :group 'org-faces)
  3358. (defface org-level-3 ;; font-lock-keyword-face
  3359. (org-compatible-face 'outline-3
  3360. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3361. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3362. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3363. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3364. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3365. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3366. (t (:bold t))))
  3367. "Face used for level 3 headlines."
  3368. :group 'org-faces)
  3369. (defface org-level-4 ;; font-lock-comment-face
  3370. (org-compatible-face 'outline-4
  3371. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3372. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3373. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3374. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3375. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3376. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3377. (t (:bold t))))
  3378. "Face used for level 4 headlines."
  3379. :group 'org-faces)
  3380. (defface org-level-5 ;; font-lock-type-face
  3381. (org-compatible-face 'outline-5
  3382. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3383. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3384. (((class color) (min-colors 8)) (:foreground "green"))))
  3385. "Face used for level 5 headlines."
  3386. :group 'org-faces)
  3387. (defface org-level-6 ;; font-lock-constant-face
  3388. (org-compatible-face 'outline-6
  3389. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3390. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3391. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3392. "Face used for level 6 headlines."
  3393. :group 'org-faces)
  3394. (defface org-level-7 ;; font-lock-builtin-face
  3395. (org-compatible-face 'outline-7
  3396. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3397. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3398. (((class color) (min-colors 8)) (:foreground "blue"))))
  3399. "Face used for level 7 headlines."
  3400. :group 'org-faces)
  3401. (defface org-level-8 ;; font-lock-string-face
  3402. (org-compatible-face 'outline-8
  3403. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3404. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3405. (((class color) (min-colors 8)) (:foreground "green"))))
  3406. "Face used for level 8 headlines."
  3407. :group 'org-faces)
  3408. (defface org-special-keyword ;; font-lock-string-face
  3409. (org-compatible-face nil
  3410. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3411. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3412. (t (:italic t))))
  3413. "Face used for special keywords."
  3414. :group 'org-faces)
  3415. (defface org-drawer ;; font-lock-function-name-face
  3416. (org-compatible-face nil
  3417. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3418. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3419. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3420. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3421. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3422. (t (:bold t))))
  3423. "Face used for drawers."
  3424. :group 'org-faces)
  3425. (defface org-property-value nil
  3426. "Face used for the value of a property."
  3427. :group 'org-faces)
  3428. (defface org-column
  3429. (org-compatible-face nil
  3430. '((((class color) (min-colors 16) (background light))
  3431. (:background "grey90"))
  3432. (((class color) (min-colors 16) (background dark))
  3433. (:background "grey30"))
  3434. (((class color) (min-colors 8))
  3435. (:background "cyan" :foreground "black"))
  3436. (t (:inverse-video t))))
  3437. "Face for column display of entry properties."
  3438. :group 'org-faces)
  3439. (when (fboundp 'set-face-attribute)
  3440. ;; Make sure that a fixed-width face is used when we have a column table.
  3441. (set-face-attribute 'org-column nil
  3442. :height (face-attribute 'default :height)
  3443. :family (face-attribute 'default :family)))
  3444. (defface org-warning
  3445. (org-compatible-face 'font-lock-warning-face
  3446. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3447. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3448. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3449. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3450. (t (:bold t))))
  3451. "Face for deadlines and TODO keywords."
  3452. :group 'org-faces)
  3453. (defface org-archived ; similar to shadow
  3454. (org-compatible-face 'shadow
  3455. '((((class color grayscale) (min-colors 88) (background light))
  3456. (:foreground "grey50"))
  3457. (((class color grayscale) (min-colors 88) (background dark))
  3458. (:foreground "grey70"))
  3459. (((class color) (min-colors 8) (background light))
  3460. (:foreground "green"))
  3461. (((class color) (min-colors 8) (background dark))
  3462. (:foreground "yellow"))))
  3463. "Face for headline with the ARCHIVE tag."
  3464. :group 'org-faces)
  3465. (defface org-link
  3466. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3467. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3468. (t (:underline t)))
  3469. "Face for links."
  3470. :group 'org-faces)
  3471. (defface org-ellipsis
  3472. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3473. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3474. (t (:strike-through t)))
  3475. "Face for the ellipsis in folded text."
  3476. :group 'org-faces)
  3477. (defface org-target
  3478. '((((class color) (background light)) (:underline t))
  3479. (((class color) (background dark)) (:underline t))
  3480. (t (:underline t)))
  3481. "Face for links."
  3482. :group 'org-faces)
  3483. (defface org-date
  3484. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3485. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3486. (t (:underline t)))
  3487. "Face for links."
  3488. :group 'org-faces)
  3489. (defface org-sexp-date
  3490. '((((class color) (background light)) (:foreground "Purple"))
  3491. (((class color) (background dark)) (:foreground "Cyan"))
  3492. (t (:underline t)))
  3493. "Face for links."
  3494. :group 'org-faces)
  3495. (defface org-tag
  3496. '((t (:bold t)))
  3497. "Face for tags."
  3498. :group 'org-faces)
  3499. (defface org-todo ; font-lock-warning-face
  3500. (org-compatible-face nil
  3501. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3502. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3503. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3504. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3505. (t (:inverse-video t :bold t))))
  3506. "Face for TODO keywords."
  3507. :group 'org-faces)
  3508. (defface org-done ;; font-lock-type-face
  3509. (org-compatible-face nil
  3510. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3511. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3512. (((class color) (min-colors 8)) (:foreground "green"))
  3513. (t (:bold t))))
  3514. "Face used for todo keywords that indicate DONE items."
  3515. :group 'org-faces)
  3516. (defface org-headline-done ;; font-lock-string-face
  3517. (org-compatible-face nil
  3518. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3519. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3520. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3521. "Face used to indicate that a headline is DONE.
  3522. This face is only used if `org-fontify-done-headline' is set. If applies
  3523. to the part of the headline after the DONE keyword."
  3524. :group 'org-faces)
  3525. (defcustom org-todo-keyword-faces nil
  3526. "Faces for specific TODO keywords.
  3527. This is a list of cons cells, with TODO keywords in the car
  3528. and faces in the cdr. The face can be a symbol, or a property
  3529. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3530. :group 'org-faces
  3531. :group 'org-todo
  3532. :type '(repeat
  3533. (cons
  3534. (string :tag "keyword")
  3535. (sexp :tag "face"))))
  3536. (defface org-table ;; font-lock-function-name-face
  3537. (org-compatible-face nil
  3538. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3539. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3540. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3541. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3542. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3543. (((class color) (min-colors 8) (background dark)))))
  3544. "Face used for tables."
  3545. :group 'org-faces)
  3546. (defface org-formula
  3547. (org-compatible-face nil
  3548. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3549. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3550. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3551. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3552. (t (:bold t :italic t))))
  3553. "Face for formulas."
  3554. :group 'org-faces)
  3555. (defface org-code
  3556. (org-compatible-face nil
  3557. '((((class color grayscale) (min-colors 88) (background light))
  3558. (:foreground "grey50"))
  3559. (((class color grayscale) (min-colors 88) (background dark))
  3560. (:foreground "grey70"))
  3561. (((class color) (min-colors 8) (background light))
  3562. (:foreground "green"))
  3563. (((class color) (min-colors 8) (background dark))
  3564. (:foreground "yellow"))))
  3565. "Face for fixed-with text like code snippets."
  3566. :group 'org-faces
  3567. :version "22.1")
  3568. (defface org-verbatim
  3569. (org-compatible-face nil
  3570. '((((class color grayscale) (min-colors 88) (background light))
  3571. (:foreground "grey50" :underline t))
  3572. (((class color grayscale) (min-colors 88) (background dark))
  3573. (:foreground "grey70" :underline t))
  3574. (((class color) (min-colors 8) (background light))
  3575. (:foreground "green" :underline t))
  3576. (((class color) (min-colors 8) (background dark))
  3577. (:foreground "yellow" :underline t))))
  3578. "Face for fixed-with text like code snippets."
  3579. :group 'org-faces
  3580. :version "22.1")
  3581. (defface org-agenda-structure ;; font-lock-function-name-face
  3582. (org-compatible-face nil
  3583. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3584. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3585. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3586. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3587. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3588. (t (:bold t))))
  3589. "Face used in agenda for captions and dates."
  3590. :group 'org-faces)
  3591. (defface org-scheduled-today
  3592. (org-compatible-face nil
  3593. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3594. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3595. (((class color) (min-colors 8)) (:foreground "green"))
  3596. (t (:bold t :italic t))))
  3597. "Face for items scheduled for a certain day."
  3598. :group 'org-faces)
  3599. (defface org-scheduled-previously
  3600. (org-compatible-face nil
  3601. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3602. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3603. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3604. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3605. (t (:bold t))))
  3606. "Face for items scheduled previously, and not yet done."
  3607. :group 'org-faces)
  3608. (defface org-upcoming-deadline
  3609. (org-compatible-face nil
  3610. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3611. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3612. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3613. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3614. (t (:bold t))))
  3615. "Face for items scheduled previously, and not yet done."
  3616. :group 'org-faces)
  3617. (defcustom org-agenda-deadline-faces
  3618. '((1.0 . org-warning)
  3619. (0.5 . org-upcoming-deadline)
  3620. (0.0 . default))
  3621. "Faces for showing deadlines in the agenda.
  3622. This is a list of cons cells. The cdr of each cell is a face to be used,
  3623. and it can also just be like '(:foreground \"yellow\").
  3624. Each car is a fraction of the head-warning time that must have passed for
  3625. this the face in the cdr to be used for display. The numbers must be
  3626. given in descending order. The head-warning time is normally taken
  3627. from `org-deadline-warning-days', but can also be specified in the deadline
  3628. timestamp itself, like this:
  3629. DEADLINE: <2007-08-13 Mon -8d>
  3630. You may use d for days, w for weeks, m for months and y for years. Months
  3631. and years will only be treated in an approximate fashion (30.4 days for a
  3632. month and 365.24 days for a year)."
  3633. :group 'org-faces
  3634. :group 'org-agenda-daily/weekly
  3635. :type '(repeat
  3636. (cons
  3637. (number :tag "Fraction of head-warning time passed")
  3638. (sexp :tag "Face"))))
  3639. ;; FIXME: this is not a good face yet.
  3640. (defface org-agenda-restriction-lock
  3641. (org-compatible-face nil
  3642. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3643. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3644. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3645. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3646. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3647. (t (:inverse-video t))))
  3648. "Face for showing the agenda restriction lock."
  3649. :group 'org-faces)
  3650. (defface org-time-grid ;; font-lock-variable-name-face
  3651. (org-compatible-face nil
  3652. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3653. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3654. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3655. "Face used for time grids."
  3656. :group 'org-faces)
  3657. (defconst org-level-faces
  3658. '(org-level-1 org-level-2 org-level-3 org-level-4
  3659. org-level-5 org-level-6 org-level-7 org-level-8
  3660. ))
  3661. (defcustom org-n-level-faces (length org-level-faces)
  3662. "The number of different faces to be used for headlines.
  3663. Org-mode defines 8 different headline faces, so this can be at most 8.
  3664. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3665. :type 'number
  3666. :group 'org-faces)
  3667. ;;; Functions and variables from ther packages
  3668. ;; Declared here to avoid compiler warnings
  3669. (eval-and-compile
  3670. (unless (fboundp 'declare-function)
  3671. (defmacro declare-function (fn file &optional arglist fileonly))))
  3672. ;; XEmacs only
  3673. (defvar outline-mode-menu-heading)
  3674. (defvar outline-mode-menu-show)
  3675. (defvar outline-mode-menu-hide)
  3676. (defvar zmacs-regions) ; XEmacs regions
  3677. ;; Emacs only
  3678. (defvar mark-active)
  3679. ;; Various packages
  3680. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3681. (declare-function add-to-diary-list "diary-lib"
  3682. (date string specifier &optional marker globcolor literal))
  3683. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3684. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3685. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3686. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3687. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3688. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3689. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3690. (declare-function bbdb-record-name "ext:bbdb" (record))
  3691. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3692. (declare-function bibtex-generate-autokey "bibtex" ())
  3693. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3694. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3695. (defvar calc-embedded-close-formula)
  3696. (defvar calc-embedded-open-formula)
  3697. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3698. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3699. (declare-function calendar-check-holidays "holidays" (date))
  3700. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3701. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3702. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3703. (declare-function calendar-forward-day "cal-move" (arg))
  3704. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3705. (declare-function calendar-goto-date "cal-move" (date))
  3706. (declare-function calendar-goto-today "cal-move" ())
  3707. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3708. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3709. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3710. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3711. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3712. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3713. (defvar calendar-mode-map)
  3714. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3715. (declare-function cdlatex-tab "ext:cdlatex" ())
  3716. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3717. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3718. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3719. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3720. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3721. (defvar font-lock-unfontify-region-function)
  3722. (declare-function gnus-article-show-summary "gnus-art" ())
  3723. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3724. (defvar gnus-other-frame-object)
  3725. (defvar gnus-group-name)
  3726. (defvar gnus-article-current)
  3727. (defvar Info-current-file)
  3728. (defvar Info-current-node)
  3729. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3730. (declare-function mh-find-path "mh-utils" ())
  3731. (declare-function mh-get-header-field "mh-utils" (field))
  3732. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3733. (declare-function mh-header-display "mh-show" ())
  3734. (declare-function mh-index-previous-folder "mh-search" ())
  3735. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3736. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3737. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3738. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3739. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3740. (declare-function mh-show-header-display "mh-show" t t)
  3741. (declare-function mh-show-msg "mh-show" (msg))
  3742. (declare-function mh-show-show "mh-show" t t)
  3743. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3744. (defvar mh-progs)
  3745. (defvar mh-current-folder)
  3746. (defvar mh-show-folder-buffer)
  3747. (defvar mh-index-folder)
  3748. (defvar mh-searcher)
  3749. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3750. (declare-function parse-time-string "parse-time" (string))
  3751. (declare-function remember "remember" (&optional initial))
  3752. (declare-function remember-buffer-desc "remember" ())
  3753. (declare-function remember-finalize "remember" ())
  3754. (defvar remember-save-after-remembering)
  3755. (defvar remember-data-file)
  3756. (defvar remember-register)
  3757. (defvar remember-buffer)
  3758. (defvar remember-handler-functions)
  3759. (defvar remember-annotation-functions)
  3760. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3761. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3762. (declare-function rmail-what-message "rmail" ())
  3763. (defvar rmail-current-message)
  3764. (defvar texmathp-why)
  3765. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3766. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3767. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3768. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3769. (declare-function vm-isearch-update "ext:vm-search" ())
  3770. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3771. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3772. (declare-function vm-su-subject "ext:vm-summary" (m))
  3773. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3774. (defvar vm-message-pointer)
  3775. (defvar vm-folder-directory)
  3776. (defvar w3m-current-url)
  3777. (defvar w3m-current-title)
  3778. ;; backward compatibility to old version of wl
  3779. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3780. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3781. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3782. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3783. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3784. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3785. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3786. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3787. (defvar wl-summary-buffer-elmo-folder)
  3788. (defvar wl-summary-buffer-folder-name)
  3789. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3790. (defvar org-latex-regexps)
  3791. (defvar constants-unit-system)
  3792. ;;; Variables for pre-computed regular expressions, all buffer local
  3793. (defvar org-drawer-regexp nil
  3794. "Matches first line of a hidden block.")
  3795. (make-variable-buffer-local 'org-drawer-regexp)
  3796. (defvar org-todo-regexp nil
  3797. "Matches any of the TODO state keywords.")
  3798. (make-variable-buffer-local 'org-todo-regexp)
  3799. (defvar org-not-done-regexp nil
  3800. "Matches any of the TODO state keywords except the last one.")
  3801. (make-variable-buffer-local 'org-not-done-regexp)
  3802. (defvar org-todo-line-regexp nil
  3803. "Matches a headline and puts TODO state into group 2 if present.")
  3804. (make-variable-buffer-local 'org-todo-line-regexp)
  3805. (defvar org-complex-heading-regexp nil
  3806. "Matches a headline and puts everything into groups:
  3807. group 1: the stars
  3808. group 2: The todo keyword, maybe
  3809. group 3: Priority cookie
  3810. group 4: True headline
  3811. group 5: Tags")
  3812. (make-variable-buffer-local 'org-complex-heading-regexp)
  3813. (defvar org-todo-line-tags-regexp nil
  3814. "Matches a headline and puts TODO state into group 2 if present.
  3815. Also put tags into group 4 if tags are present.")
  3816. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3817. (defvar org-nl-done-regexp nil
  3818. "Matches newline followed by a headline with the DONE keyword.")
  3819. (make-variable-buffer-local 'org-nl-done-regexp)
  3820. (defvar org-looking-at-done-regexp nil
  3821. "Matches the DONE keyword a point.")
  3822. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3823. (defvar org-ds-keyword-length 12
  3824. "Maximum length of the Deadline and SCHEDULED keywords.")
  3825. (make-variable-buffer-local 'org-ds-keyword-length)
  3826. (defvar org-deadline-regexp nil
  3827. "Matches the DEADLINE keyword.")
  3828. (make-variable-buffer-local 'org-deadline-regexp)
  3829. (defvar org-deadline-time-regexp nil
  3830. "Matches the DEADLINE keyword together with a time stamp.")
  3831. (make-variable-buffer-local 'org-deadline-time-regexp)
  3832. (defvar org-deadline-line-regexp nil
  3833. "Matches the DEADLINE keyword and the rest of the line.")
  3834. (make-variable-buffer-local 'org-deadline-line-regexp)
  3835. (defvar org-scheduled-regexp nil
  3836. "Matches the SCHEDULED keyword.")
  3837. (make-variable-buffer-local 'org-scheduled-regexp)
  3838. (defvar org-scheduled-time-regexp nil
  3839. "Matches the SCHEDULED keyword together with a time stamp.")
  3840. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3841. (defvar org-closed-time-regexp nil
  3842. "Matches the CLOSED keyword together with a time stamp.")
  3843. (make-variable-buffer-local 'org-closed-time-regexp)
  3844. (defvar org-keyword-time-regexp nil
  3845. "Matches any of the 4 keywords, together with the time stamp.")
  3846. (make-variable-buffer-local 'org-keyword-time-regexp)
  3847. (defvar org-keyword-time-not-clock-regexp nil
  3848. "Matches any of the 3 keywords, together with the time stamp.")
  3849. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3850. (defvar org-maybe-keyword-time-regexp nil
  3851. "Matches a timestamp, possibly preceeded by a keyword.")
  3852. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3853. (defvar org-planning-or-clock-line-re nil
  3854. "Matches a line with planning or clock info.")
  3855. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3856. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3857. rear-nonsticky t mouse-map t fontified t)
  3858. "Properties to remove when a string without properties is wanted.")
  3859. (defsubst org-match-string-no-properties (num &optional string)
  3860. (if (featurep 'xemacs)
  3861. (let ((s (match-string num string)))
  3862. (remove-text-properties 0 (length s) org-rm-props s)
  3863. s)
  3864. (match-string-no-properties num string)))
  3865. (defsubst org-no-properties (s)
  3866. (if (fboundp 'set-text-properties)
  3867. (set-text-properties 0 (length s) nil s)
  3868. (remove-text-properties 0 (length s) org-rm-props s))
  3869. s)
  3870. (defsubst org-get-alist-option (option key)
  3871. (cond ((eq key t) t)
  3872. ((eq option t) t)
  3873. ((assoc key option) (cdr (assoc key option)))
  3874. (t (cdr (assq 'default option)))))
  3875. (defsubst org-inhibit-invisibility ()
  3876. "Modified `buffer-invisibility-spec' for Emacs 21.
  3877. Some ops with invisible text do not work correctly on Emacs 21. For these
  3878. we turn off invisibility temporarily. Use this in a `let' form."
  3879. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3880. (defsubst org-set-local (var value)
  3881. "Make VAR local in current buffer and set it to VALUE."
  3882. (set (make-variable-buffer-local var) value))
  3883. (defsubst org-mode-p ()
  3884. "Check if the current buffer is in Org-mode."
  3885. (eq major-mode 'org-mode))
  3886. (defsubst org-last (list)
  3887. "Return the last element of LIST."
  3888. (car (last list)))
  3889. (defun org-let (list &rest body)
  3890. (eval (cons 'let (cons list body))))
  3891. (put 'org-let 'lisp-indent-function 1)
  3892. (defun org-let2 (list1 list2 &rest body)
  3893. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3894. (put 'org-let2 'lisp-indent-function 2)
  3895. (defconst org-startup-options
  3896. '(("fold" org-startup-folded t)
  3897. ("overview" org-startup-folded t)
  3898. ("nofold" org-startup-folded nil)
  3899. ("showall" org-startup-folded nil)
  3900. ("content" org-startup-folded content)
  3901. ("hidestars" org-hide-leading-stars t)
  3902. ("showstars" org-hide-leading-stars nil)
  3903. ("odd" org-odd-levels-only t)
  3904. ("oddeven" org-odd-levels-only nil)
  3905. ("align" org-startup-align-all-tables t)
  3906. ("noalign" org-startup-align-all-tables nil)
  3907. ("customtime" org-display-custom-times t)
  3908. ("logdone" org-log-done time)
  3909. ("lognotedone" org-log-done note)
  3910. ("nologdone" org-log-done nil)
  3911. ("lognoteclock-out" org-log-note-clock-out t)
  3912. ("nolognoteclock-out" org-log-note-clock-out nil)
  3913. ("logrepeat" org-log-repeat state)
  3914. ("lognoterepeat" org-log-repeat note)
  3915. ("nologrepeat" org-log-repeat nil)
  3916. ("constcgs" constants-unit-system cgs)
  3917. ("constSI" constants-unit-system SI))
  3918. "Variable associated with STARTUP options for org-mode.
  3919. Each element is a list of three items: The startup options as written
  3920. in the #+STARTUP line, the corresponding variable, and the value to
  3921. set this variable to if the option is found. An optional forth element PUSH
  3922. means to push this value onto the list in the variable.")
  3923. (defun org-set-regexps-and-options ()
  3924. "Precompute regular expressions for current buffer."
  3925. (when (org-mode-p)
  3926. (org-set-local 'org-todo-kwd-alist nil)
  3927. (org-set-local 'org-todo-key-alist nil)
  3928. (org-set-local 'org-todo-key-trigger nil)
  3929. (org-set-local 'org-todo-keywords-1 nil)
  3930. (org-set-local 'org-done-keywords nil)
  3931. (org-set-local 'org-todo-heads nil)
  3932. (org-set-local 'org-todo-sets nil)
  3933. (org-set-local 'org-todo-log-states nil)
  3934. (let ((re (org-make-options-regexp
  3935. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3936. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3937. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3938. (splitre "[ \t]+")
  3939. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3940. tail sep kws1 prio props drawers)
  3941. (save-excursion
  3942. (save-restriction
  3943. (widen)
  3944. (goto-char (point-min))
  3945. (while (re-search-forward re nil t)
  3946. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3947. (cond
  3948. ((equal key "CATEGORY")
  3949. (if (string-match "[ \t]+$" value)
  3950. (setq value (replace-match "" t t value)))
  3951. (setq cat value))
  3952. ((member key '("SEQ_TODO" "TODO"))
  3953. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3954. ((equal key "TYP_TODO")
  3955. (push (cons 'type (org-split-string value splitre)) kwds))
  3956. ((equal key "TAGS")
  3957. (setq tags (append tags (org-split-string value splitre))))
  3958. ((equal key "COLUMNS")
  3959. (org-set-local 'org-columns-default-format value))
  3960. ((equal key "LINK")
  3961. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3962. (push (cons (match-string 1 value)
  3963. (org-trim (match-string 2 value)))
  3964. links)))
  3965. ((equal key "PRIORITIES")
  3966. (setq prio (org-split-string value " +")))
  3967. ((equal key "PROPERTY")
  3968. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3969. (push (cons (match-string 1 value) (match-string 2 value))
  3970. props)))
  3971. ((equal key "DRAWERS")
  3972. (setq drawers (org-split-string value splitre)))
  3973. ((equal key "CONSTANTS")
  3974. (setq const (append const (org-split-string value splitre))))
  3975. ((equal key "STARTUP")
  3976. (let ((opts (org-split-string value splitre))
  3977. l var val)
  3978. (while (setq l (pop opts))
  3979. (when (setq l (assoc l org-startup-options))
  3980. (setq var (nth 1 l) val (nth 2 l))
  3981. (if (not (nth 3 l))
  3982. (set (make-local-variable var) val)
  3983. (if (not (listp (symbol-value var)))
  3984. (set (make-local-variable var) nil))
  3985. (set (make-local-variable var) (symbol-value var))
  3986. (add-to-list var val))))))
  3987. ((equal key "ARCHIVE")
  3988. (string-match " *$" value)
  3989. (setq arch (replace-match "" t t value))
  3990. (remove-text-properties 0 (length arch)
  3991. '(face t fontified t) arch)))
  3992. )))
  3993. (when cat
  3994. (org-set-local 'org-category (intern cat))
  3995. (push (cons "CATEGORY" cat) props))
  3996. (when prio
  3997. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3998. (setq prio (mapcar 'string-to-char prio))
  3999. (org-set-local 'org-highest-priority (nth 0 prio))
  4000. (org-set-local 'org-lowest-priority (nth 1 prio))
  4001. (org-set-local 'org-default-priority (nth 2 prio)))
  4002. (and props (org-set-local 'org-local-properties (nreverse props)))
  4003. (and drawers (org-set-local 'org-drawers drawers))
  4004. (and arch (org-set-local 'org-archive-location arch))
  4005. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4006. ;; Process the TODO keywords
  4007. (unless kwds
  4008. ;; Use the global values as if they had been given locally.
  4009. (setq kwds (default-value 'org-todo-keywords))
  4010. (if (stringp (car kwds))
  4011. (setq kwds (list (cons org-todo-interpretation
  4012. (default-value 'org-todo-keywords)))))
  4013. (setq kwds (reverse kwds)))
  4014. (setq kwds (nreverse kwds))
  4015. (let (inter kws kw)
  4016. (while (setq kws (pop kwds))
  4017. (setq inter (pop kws) sep (member "|" kws)
  4018. kws0 (delete "|" (copy-sequence kws))
  4019. kwsa nil
  4020. kws1 (mapcar
  4021. (lambda (x)
  4022. ;; 1 2
  4023. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4024. (progn
  4025. (setq kw (match-string 1 x)
  4026. key (and (match-end 2) (match-string 2 x))
  4027. log (org-extract-log-state-settings x))
  4028. (push (cons kw (and key (string-to-char key))) kwsa)
  4029. (and log (push log org-todo-log-states))
  4030. kw)
  4031. (error "Invalid TODO keyword %s" x)))
  4032. kws0)
  4033. kwsa (if kwsa (append '((:startgroup))
  4034. (nreverse kwsa)
  4035. '((:endgroup))))
  4036. hw (car kws1)
  4037. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4038. tail (list inter hw (car dws) (org-last dws)))
  4039. (add-to-list 'org-todo-heads hw 'append)
  4040. (push kws1 org-todo-sets)
  4041. (setq org-done-keywords (append org-done-keywords dws nil))
  4042. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4043. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4044. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4045. (setq org-todo-sets (nreverse org-todo-sets)
  4046. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4047. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4048. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4049. ;; Process the constants
  4050. (when const
  4051. (let (e cst)
  4052. (while (setq e (pop const))
  4053. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4054. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4055. (setq org-table-formula-constants-local cst)))
  4056. ;; Process the tags.
  4057. (when tags
  4058. (let (e tgs)
  4059. (while (setq e (pop tags))
  4060. (cond
  4061. ((equal e "{") (push '(:startgroup) tgs))
  4062. ((equal e "}") (push '(:endgroup) tgs))
  4063. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4064. (push (cons (match-string 1 e)
  4065. (string-to-char (match-string 2 e)))
  4066. tgs))
  4067. (t (push (list e) tgs))))
  4068. (org-set-local 'org-tag-alist nil)
  4069. (while (setq e (pop tgs))
  4070. (or (and (stringp (car e))
  4071. (assoc (car e) org-tag-alist))
  4072. (push e org-tag-alist))))))
  4073. ;; Compute the regular expressions and other local variables
  4074. (if (not org-done-keywords)
  4075. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4076. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4077. (length org-scheduled-string)))
  4078. org-drawer-regexp
  4079. (concat "^[ \t]*:\\("
  4080. (mapconcat 'regexp-quote org-drawers "\\|")
  4081. "\\):[ \t]*$")
  4082. org-not-done-keywords
  4083. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4084. org-todo-regexp
  4085. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4086. "\\|") "\\)\\>")
  4087. org-not-done-regexp
  4088. (concat "\\<\\("
  4089. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4090. "\\)\\>")
  4091. org-todo-line-regexp
  4092. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4093. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4094. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4095. org-complex-heading-regexp
  4096. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4097. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4098. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4099. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4100. org-nl-done-regexp
  4101. (concat "\n\\*+[ \t]+"
  4102. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4103. "\\)" "\\>")
  4104. org-todo-line-tags-regexp
  4105. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4106. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4107. (org-re
  4108. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4109. org-looking-at-done-regexp
  4110. (concat "^" "\\(?:"
  4111. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4112. "\\>")
  4113. org-deadline-regexp (concat "\\<" org-deadline-string)
  4114. org-deadline-time-regexp
  4115. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4116. org-deadline-line-regexp
  4117. (concat "\\<\\(" org-deadline-string "\\).*")
  4118. org-scheduled-regexp
  4119. (concat "\\<" org-scheduled-string)
  4120. org-scheduled-time-regexp
  4121. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4122. org-closed-time-regexp
  4123. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4124. org-keyword-time-regexp
  4125. (concat "\\<\\(" org-scheduled-string
  4126. "\\|" org-deadline-string
  4127. "\\|" org-closed-string
  4128. "\\|" org-clock-string "\\)"
  4129. " *[[<]\\([^]>]+\\)[]>]")
  4130. org-keyword-time-not-clock-regexp
  4131. (concat "\\<\\(" org-scheduled-string
  4132. "\\|" org-deadline-string
  4133. "\\|" org-closed-string
  4134. "\\)"
  4135. " *[[<]\\([^]>]+\\)[]>]")
  4136. org-maybe-keyword-time-regexp
  4137. (concat "\\(\\<\\(" org-scheduled-string
  4138. "\\|" org-deadline-string
  4139. "\\|" org-closed-string
  4140. "\\|" org-clock-string "\\)\\)?"
  4141. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4142. org-planning-or-clock-line-re
  4143. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4144. "\\|" org-deadline-string
  4145. "\\|" org-closed-string "\\|" org-clock-string
  4146. "\\)\\>\\)")
  4147. )
  4148. (org-compute-latex-and-specials-regexp)
  4149. (org-set-font-lock-defaults)))
  4150. (defun org-extract-log-state-settings (x)
  4151. "Extract the log state setting from a TODO keyword string.
  4152. This will extract info from a string like \"WAIT(w@/!)\"."
  4153. (let (kw key log1 log2)
  4154. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4155. (setq kw (match-string 1 x)
  4156. key (and (match-end 2) (match-string 2 x))
  4157. log1 (and (match-end 3) (match-string 3 x))
  4158. log2 (and (match-end 4) (match-string 4 x)))
  4159. (and (or log1 log2)
  4160. (list kw
  4161. (and log1 (if (equal log1 "!") 'time 'note))
  4162. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4163. (defun org-remove-keyword-keys (list)
  4164. "Remove a pair of parenthesis at the end of each string in LIST."
  4165. (mapcar (lambda (x)
  4166. (if (string-match "(.*)$" x)
  4167. (substring x 0 (match-beginning 0))
  4168. x))
  4169. list))
  4170. ;; FIXME: this could be done much better, using second characters etc.
  4171. (defun org-assign-fast-keys (alist)
  4172. "Assign fast keys to a keyword-key alist.
  4173. Respect keys that are already there."
  4174. (let (new e k c c1 c2 (char ?a))
  4175. (while (setq e (pop alist))
  4176. (cond
  4177. ((equal e '(:startgroup)) (push e new))
  4178. ((equal e '(:endgroup)) (push e new))
  4179. (t
  4180. (setq k (car e) c2 nil)
  4181. (if (cdr e)
  4182. (setq c (cdr e))
  4183. ;; automatically assign a character.
  4184. (setq c1 (string-to-char
  4185. (downcase (substring
  4186. k (if (= (string-to-char k) ?@) 1 0)))))
  4187. (if (or (rassoc c1 new) (rassoc c1 alist))
  4188. (while (or (rassoc char new) (rassoc char alist))
  4189. (setq char (1+ char)))
  4190. (setq c2 c1))
  4191. (setq c (or c2 char)))
  4192. (push (cons k c) new))))
  4193. (nreverse new)))
  4194. ;;; Some variables ujsed in various places
  4195. (defvar org-window-configuration nil
  4196. "Used in various places to store a window configuration.")
  4197. (defvar org-finish-function nil
  4198. "Function to be called when `C-c C-c' is used.
  4199. This is for getting out of special buffers like remember.")
  4200. ;; FIXME: Occasionally check by commenting these, to make sure
  4201. ;; no other functions uses these, forgetting to let-bind them.
  4202. (defvar entry)
  4203. (defvar state)
  4204. (defvar last-state)
  4205. (defvar date)
  4206. (defvar description)
  4207. ;; Defined somewhere in this file, but used before definition.
  4208. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4209. (defvar org-agenda-buffer-name)
  4210. (defvar org-agenda-undo-list)
  4211. (defvar org-agenda-pending-undo-list)
  4212. (defvar org-agenda-overriding-header)
  4213. (defvar orgtbl-mode)
  4214. (defvar org-html-entities)
  4215. (defvar org-struct-menu)
  4216. (defvar org-org-menu)
  4217. (defvar org-tbl-menu)
  4218. (defvar org-agenda-keymap)
  4219. ;;;; Emacs/XEmacs compatibility
  4220. ;; Overlay compatibility functions
  4221. (defun org-make-overlay (beg end &optional buffer)
  4222. (if (featurep 'xemacs)
  4223. (make-extent beg end buffer)
  4224. (make-overlay beg end buffer)))
  4225. (defun org-delete-overlay (ovl)
  4226. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4227. (defun org-detach-overlay (ovl)
  4228. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4229. (defun org-move-overlay (ovl beg end &optional buffer)
  4230. (if (featurep 'xemacs)
  4231. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4232. (move-overlay ovl beg end buffer)))
  4233. (defun org-overlay-put (ovl prop value)
  4234. (if (featurep 'xemacs)
  4235. (set-extent-property ovl prop value)
  4236. (overlay-put ovl prop value)))
  4237. (defun org-overlay-display (ovl text &optional face evap)
  4238. "Make overlay OVL display TEXT with face FACE."
  4239. (if (featurep 'xemacs)
  4240. (let ((gl (make-glyph text)))
  4241. (and face (set-glyph-face gl face))
  4242. (set-extent-property ovl 'invisible t)
  4243. (set-extent-property ovl 'end-glyph gl))
  4244. (overlay-put ovl 'display text)
  4245. (if face (overlay-put ovl 'face face))
  4246. (if evap (overlay-put ovl 'evaporate t))))
  4247. (defun org-overlay-before-string (ovl text &optional face evap)
  4248. "Make overlay OVL display TEXT with face FACE."
  4249. (if (featurep 'xemacs)
  4250. (let ((gl (make-glyph text)))
  4251. (and face (set-glyph-face gl face))
  4252. (set-extent-property ovl 'begin-glyph gl))
  4253. (if face (org-add-props text nil 'face face))
  4254. (overlay-put ovl 'before-string text)
  4255. (if evap (overlay-put ovl 'evaporate t))))
  4256. (defun org-overlay-get (ovl prop)
  4257. (if (featurep 'xemacs)
  4258. (extent-property ovl prop)
  4259. (overlay-get ovl prop)))
  4260. (defun org-overlays-at (pos)
  4261. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4262. (defun org-overlays-in (&optional start end)
  4263. (if (featurep 'xemacs)
  4264. (extent-list nil start end)
  4265. (overlays-in start end)))
  4266. (defun org-overlay-start (o)
  4267. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4268. (defun org-overlay-end (o)
  4269. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4270. (defun org-find-overlays (prop &optional pos delete)
  4271. "Find all overlays specifying PROP at POS or point.
  4272. If DELETE is non-nil, delete all those overlays."
  4273. (let ((overlays (org-overlays-at (or pos (point))))
  4274. ov found)
  4275. (while (setq ov (pop overlays))
  4276. (if (org-overlay-get ov prop)
  4277. (if delete (org-delete-overlay ov) (push ov found))))
  4278. found))
  4279. ;; Region compatibility
  4280. (defun org-add-hook (hook function &optional append local)
  4281. "Add-hook, compatible with both Emacsen."
  4282. (if (and local (featurep 'xemacs))
  4283. (add-local-hook hook function append)
  4284. (add-hook hook function append local)))
  4285. (defvar org-ignore-region nil
  4286. "To temporarily disable the active region.")
  4287. (defun org-region-active-p ()
  4288. "Is `transient-mark-mode' on and the region active?
  4289. Works on both Emacs and XEmacs."
  4290. (if org-ignore-region
  4291. nil
  4292. (if (featurep 'xemacs)
  4293. (and zmacs-regions (region-active-p))
  4294. (if (fboundp 'use-region-p)
  4295. (use-region-p)
  4296. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4297. ;; Invisibility compatibility
  4298. (defun org-add-to-invisibility-spec (arg)
  4299. "Add elements to `buffer-invisibility-spec'.
  4300. See documentation for `buffer-invisibility-spec' for the kind of elements
  4301. that can be added."
  4302. (cond
  4303. ((fboundp 'add-to-invisibility-spec)
  4304. (add-to-invisibility-spec arg))
  4305. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4306. (setq buffer-invisibility-spec (list arg)))
  4307. (t
  4308. (setq buffer-invisibility-spec
  4309. (cons arg buffer-invisibility-spec)))))
  4310. (defun org-remove-from-invisibility-spec (arg)
  4311. "Remove elements from `buffer-invisibility-spec'."
  4312. (if (fboundp 'remove-from-invisibility-spec)
  4313. (remove-from-invisibility-spec arg)
  4314. (if (consp buffer-invisibility-spec)
  4315. (setq buffer-invisibility-spec
  4316. (delete arg buffer-invisibility-spec)))))
  4317. (defun org-in-invisibility-spec-p (arg)
  4318. "Is ARG a member of `buffer-invisibility-spec'?"
  4319. (if (consp buffer-invisibility-spec)
  4320. (member arg buffer-invisibility-spec)
  4321. nil))
  4322. ;;;; Define the Org-mode
  4323. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4324. (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."))
  4325. ;; We use a before-change function to check if a table might need
  4326. ;; an update.
  4327. (defvar org-table-may-need-update t
  4328. "Indicates that a table might need an update.
  4329. This variable is set by `org-before-change-function'.
  4330. `org-table-align' sets it back to nil.")
  4331. (defvar org-mode-map)
  4332. (defvar org-mode-hook nil)
  4333. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4334. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4335. (defvar org-table-buffer-is-an nil)
  4336. (defconst org-outline-regexp "\\*+ ")
  4337. ;;;###autoload
  4338. (define-derived-mode org-mode outline-mode "Org"
  4339. "Outline-based notes management and organizer, alias
  4340. \"Carsten's outline-mode for keeping track of everything.\"
  4341. Org-mode develops organizational tasks around a NOTES file which
  4342. contains information about projects as plain text. Org-mode is
  4343. implemented on top of outline-mode, which is ideal to keep the content
  4344. of large files well structured. It supports ToDo items, deadlines and
  4345. time stamps, which magically appear in the diary listing of the Emacs
  4346. calendar. Tables are easily created with a built-in table editor.
  4347. Plain text URL-like links connect to websites, emails (VM), Usenet
  4348. messages (Gnus), BBDB entries, and any files related to the project.
  4349. For printing and sharing of notes, an Org-mode file (or a part of it)
  4350. can be exported as a structured ASCII or HTML file.
  4351. The following commands are available:
  4352. \\{org-mode-map}"
  4353. ;; Get rid of Outline menus, they are not needed
  4354. ;; Need to do this here because define-derived-mode sets up
  4355. ;; the keymap so late. Still, it is a waste to call this each time
  4356. ;; we switch another buffer into org-mode.
  4357. (if (featurep 'xemacs)
  4358. (when (boundp 'outline-mode-menu-heading)
  4359. ;; Assume this is Greg's port, it used easymenu
  4360. (easy-menu-remove outline-mode-menu-heading)
  4361. (easy-menu-remove outline-mode-menu-show)
  4362. (easy-menu-remove outline-mode-menu-hide))
  4363. (define-key org-mode-map [menu-bar headings] 'undefined)
  4364. (define-key org-mode-map [menu-bar hide] 'undefined)
  4365. (define-key org-mode-map [menu-bar show] 'undefined))
  4366. (easy-menu-add org-org-menu)
  4367. (easy-menu-add org-tbl-menu)
  4368. (org-install-agenda-files-menu)
  4369. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4370. (org-add-to-invisibility-spec '(org-cwidth))
  4371. (when (featurep 'xemacs)
  4372. (org-set-local 'line-move-ignore-invisible t))
  4373. (org-set-local 'outline-regexp org-outline-regexp)
  4374. (org-set-local 'outline-level 'org-outline-level)
  4375. (when (and org-ellipsis
  4376. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4377. (fboundp 'make-glyph-code))
  4378. (unless org-display-table
  4379. (setq org-display-table (make-display-table)))
  4380. (set-display-table-slot
  4381. org-display-table 4
  4382. (vconcat (mapcar
  4383. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4384. org-ellipsis)))
  4385. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4386. (setq buffer-display-table org-display-table))
  4387. (org-set-regexps-and-options)
  4388. ;; Calc embedded
  4389. (org-set-local 'calc-embedded-open-mode "# ")
  4390. (modify-syntax-entry ?# "<")
  4391. (modify-syntax-entry ?@ "w")
  4392. (if org-startup-truncated (setq truncate-lines t))
  4393. (org-set-local 'font-lock-unfontify-region-function
  4394. 'org-unfontify-region)
  4395. ;; Activate before-change-function
  4396. (org-set-local 'org-table-may-need-update t)
  4397. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4398. 'local)
  4399. ;; Check for running clock before killing a buffer
  4400. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4401. ;; Paragraphs and auto-filling
  4402. (org-set-autofill-regexps)
  4403. (setq indent-line-function 'org-indent-line-function)
  4404. (org-update-radio-target-regexp)
  4405. ;; Comment characters
  4406. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4407. (org-set-local 'comment-padding " ")
  4408. ;; Align options lines
  4409. (org-set-local
  4410. 'align-mode-rules-list
  4411. '((org-in-buffer-settings
  4412. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4413. (modes . '(org-mode)))))
  4414. ;; Imenu
  4415. (org-set-local 'imenu-create-index-function
  4416. 'org-imenu-get-tree)
  4417. ;; Make isearch reveal context
  4418. (if (or (featurep 'xemacs)
  4419. (not (boundp 'outline-isearch-open-invisible-function)))
  4420. ;; Emacs 21 and XEmacs make use of the hook
  4421. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4422. ;; Emacs 22 deals with this through a special variable
  4423. (org-set-local 'outline-isearch-open-invisible-function
  4424. (lambda (&rest ignore) (org-show-context 'isearch))))
  4425. ;; If empty file that did not turn on org-mode automatically, make it to.
  4426. (if (and org-insert-mode-line-in-empty-file
  4427. (interactive-p)
  4428. (= (point-min) (point-max)))
  4429. (insert "# -*- mode: org -*-\n\n"))
  4430. (unless org-inhibit-startup
  4431. (when org-startup-align-all-tables
  4432. (let ((bmp (buffer-modified-p)))
  4433. (org-table-map-tables 'org-table-align)
  4434. (set-buffer-modified-p bmp)))
  4435. (org-cycle-hide-drawers 'all)
  4436. (cond
  4437. ((eq org-startup-folded t)
  4438. (org-cycle '(4)))
  4439. ((eq org-startup-folded 'content)
  4440. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4441. (org-cycle '(4)) (org-cycle '(4)))))))
  4442. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4443. (defsubst org-call-with-arg (command arg)
  4444. "Call COMMAND interactively, but pretend prefix are was ARG."
  4445. (let ((current-prefix-arg arg)) (call-interactively command)))
  4446. (defsubst org-current-line (&optional pos)
  4447. (save-excursion
  4448. (and pos (goto-char pos))
  4449. ;; works also in narrowed buffer, because we start at 1, not point-min
  4450. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4451. (defun org-current-time ()
  4452. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4453. (if (> org-time-stamp-rounding-minutes 0)
  4454. (let ((r org-time-stamp-rounding-minutes)
  4455. (time (decode-time)))
  4456. (apply 'encode-time
  4457. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4458. (nthcdr 2 time))))
  4459. (current-time)))
  4460. (defun org-add-props (string plist &rest props)
  4461. "Add text properties to entire string, from beginning to end.
  4462. PLIST may be a list of properties, PROPS are individual properties and values
  4463. that will be added to PLIST. Returns the string that was modified."
  4464. (add-text-properties
  4465. 0 (length string) (if props (append plist props) plist) string)
  4466. string)
  4467. (put 'org-add-props 'lisp-indent-function 2)
  4468. ;;;; Font-Lock stuff, including the activators
  4469. (defvar org-mouse-map (make-sparse-keymap))
  4470. (org-defkey org-mouse-map
  4471. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4472. (org-defkey org-mouse-map
  4473. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4474. (when org-mouse-1-follows-link
  4475. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4476. (when org-tab-follows-link
  4477. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4478. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4479. (when org-return-follows-link
  4480. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4481. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4482. (require 'font-lock)
  4483. (defconst org-non-link-chars "]\t\n\r<>")
  4484. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4485. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4486. (defvar org-link-re-with-space nil
  4487. "Matches a link with spaces, optional angular brackets around it.")
  4488. (defvar org-link-re-with-space2 nil
  4489. "Matches a link with spaces, optional angular brackets around it.")
  4490. (defvar org-angle-link-re nil
  4491. "Matches link with angular brackets, spaces are allowed.")
  4492. (defvar org-plain-link-re nil
  4493. "Matches plain link, without spaces.")
  4494. (defvar org-bracket-link-regexp nil
  4495. "Matches a link in double brackets.")
  4496. (defvar org-bracket-link-analytic-regexp nil
  4497. "Regular expression used to analyze links.
  4498. Here is what the match groups contain after a match:
  4499. 1: http:
  4500. 2: http
  4501. 3: path
  4502. 4: [desc]
  4503. 5: desc")
  4504. (defvar org-any-link-re nil
  4505. "Regular expression matching any link.")
  4506. (defun org-make-link-regexps ()
  4507. "Update the link regular expressions.
  4508. This should be called after the variable `org-link-types' has changed."
  4509. (setq org-link-re-with-space
  4510. (concat
  4511. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4512. "\\([^" org-non-link-chars " ]"
  4513. "[^" org-non-link-chars "]*"
  4514. "[^" org-non-link-chars " ]\\)>?")
  4515. org-link-re-with-space2
  4516. (concat
  4517. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4518. "\\([^" org-non-link-chars " ]"
  4519. "[^]\t\n\r]*"
  4520. "[^" org-non-link-chars " ]\\)>?")
  4521. org-angle-link-re
  4522. (concat
  4523. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4524. "\\([^" org-non-link-chars " ]"
  4525. "[^" org-non-link-chars "]*"
  4526. "\\)>")
  4527. org-plain-link-re
  4528. (concat
  4529. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4530. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4531. org-bracket-link-regexp
  4532. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4533. org-bracket-link-analytic-regexp
  4534. (concat
  4535. "\\[\\["
  4536. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4537. "\\([^]]+\\)"
  4538. "\\]"
  4539. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4540. "\\]")
  4541. org-any-link-re
  4542. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4543. org-angle-link-re "\\)\\|\\("
  4544. org-plain-link-re "\\)")))
  4545. (org-make-link-regexps)
  4546. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4547. "Regular expression for fast time stamp matching.")
  4548. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4549. "Regular expression for fast time stamp matching.")
  4550. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4551. "Regular expression matching time strings for analysis.
  4552. This one does not require the space after the date.")
  4553. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4554. "Regular expression matching time strings for analysis.")
  4555. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4556. "Regular expression matching time stamps, with groups.")
  4557. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4558. "Regular expression matching time stamps (also [..]), with groups.")
  4559. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4560. "Regular expression matching a time stamp range.")
  4561. (defconst org-tr-regexp-both
  4562. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4563. "Regular expression matching a time stamp range.")
  4564. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4565. org-ts-regexp "\\)?")
  4566. "Regular expression matching a time stamp or time stamp range.")
  4567. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4568. org-ts-regexp-both "\\)?")
  4569. "Regular expression matching a time stamp or time stamp range.
  4570. The time stamps may be either active or inactive.")
  4571. (defvar org-emph-face nil)
  4572. (defun org-do-emphasis-faces (limit)
  4573. "Run through the buffer and add overlays to links."
  4574. (let (rtn)
  4575. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4576. (if (not (= (char-after (match-beginning 3))
  4577. (char-after (match-beginning 4))))
  4578. (progn
  4579. (setq rtn t)
  4580. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4581. 'face
  4582. (nth 1 (assoc (match-string 3)
  4583. org-emphasis-alist)))
  4584. (add-text-properties (match-beginning 2) (match-end 2)
  4585. '(font-lock-multiline t))
  4586. (when org-hide-emphasis-markers
  4587. (add-text-properties (match-end 4) (match-beginning 5)
  4588. '(invisible org-link))
  4589. (add-text-properties (match-beginning 3) (match-end 3)
  4590. '(invisible org-link)))))
  4591. (backward-char 1))
  4592. rtn))
  4593. (defun org-emphasize (&optional char)
  4594. "Insert or change an emphasis, i.e. a font like bold or italic.
  4595. If there is an active region, change that region to a new emphasis.
  4596. If there is no region, just insert the marker characters and position
  4597. the cursor between them.
  4598. CHAR should be either the marker character, or the first character of the
  4599. HTML tag associated with that emphasis. If CHAR is a space, the means
  4600. to remove the emphasis of the selected region.
  4601. If char is not given (for example in an interactive call) it
  4602. will be prompted for."
  4603. (interactive)
  4604. (let ((eal org-emphasis-alist) e det
  4605. (erc org-emphasis-regexp-components)
  4606. (prompt "")
  4607. (string "") beg end move tag c s)
  4608. (if (org-region-active-p)
  4609. (setq beg (region-beginning) end (region-end)
  4610. string (buffer-substring beg end))
  4611. (setq move t))
  4612. (while (setq e (pop eal))
  4613. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4614. c (aref tag 0))
  4615. (push (cons c (string-to-char (car e))) det)
  4616. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4617. (substring tag 1)))))
  4618. (unless char
  4619. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4620. (setq char (read-char-exclusive)))
  4621. (setq char (or (cdr (assoc char det)) char))
  4622. (if (equal char ?\ )
  4623. (setq s "" move nil)
  4624. (unless (assoc (char-to-string char) org-emphasis-alist)
  4625. (error "No such emphasis marker: \"%c\"" char))
  4626. (setq s (char-to-string char)))
  4627. (while (and (> (length string) 1)
  4628. (equal (substring string 0 1) (substring string -1))
  4629. (assoc (substring string 0 1) org-emphasis-alist))
  4630. (setq string (substring string 1 -1)))
  4631. (setq string (concat s string s))
  4632. (if beg (delete-region beg end))
  4633. (unless (or (bolp)
  4634. (string-match (concat "[" (nth 0 erc) "\n]")
  4635. (char-to-string (char-before (point)))))
  4636. (insert " "))
  4637. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4638. (char-to-string (char-after (point))))
  4639. (insert " ") (backward-char 1))
  4640. (insert string)
  4641. (and move (backward-char 1))))
  4642. (defconst org-nonsticky-props
  4643. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4644. (defun org-activate-plain-links (limit)
  4645. "Run through the buffer and add overlays to links."
  4646. (catch 'exit
  4647. (let (f)
  4648. (while (re-search-forward org-plain-link-re limit t)
  4649. (setq f (get-text-property (match-beginning 0) 'face))
  4650. (if (or (eq f 'org-tag)
  4651. (and (listp f) (memq 'org-tag f)))
  4652. nil
  4653. (add-text-properties (match-beginning 0) (match-end 0)
  4654. (list 'mouse-face 'highlight
  4655. 'rear-nonsticky org-nonsticky-props
  4656. 'keymap org-mouse-map
  4657. ))
  4658. (throw 'exit t))))))
  4659. (defun org-activate-code (limit)
  4660. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4661. (unless (get-text-property (match-beginning 1) 'face)
  4662. (remove-text-properties (match-beginning 0) (match-end 0)
  4663. '(display t invisible t intangible t))
  4664. t)))
  4665. (defun org-activate-angle-links (limit)
  4666. "Run through the buffer and add overlays to links."
  4667. (if (re-search-forward org-angle-link-re limit t)
  4668. (progn
  4669. (add-text-properties (match-beginning 0) (match-end 0)
  4670. (list 'mouse-face 'highlight
  4671. 'rear-nonsticky org-nonsticky-props
  4672. 'keymap org-mouse-map
  4673. ))
  4674. t)))
  4675. (defmacro org-maybe-intangible (props)
  4676. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4677. In emacs 21, invisible text is not avoided by the command loop, so the
  4678. intangible property is needed to make sure point skips this text.
  4679. In Emacs 22, this is not necessary. The intangible text property has
  4680. led to problems with flyspell. These problems are fixed in flyspell.el,
  4681. but we still avoid setting the property in Emacs 22 and later.
  4682. We use a macro so that the test can happen at compilation time."
  4683. (if (< emacs-major-version 22)
  4684. `(append '(intangible t) ,props)
  4685. props))
  4686. (defun org-activate-bracket-links (limit)
  4687. "Run through the buffer and add overlays to bracketed links."
  4688. (if (re-search-forward org-bracket-link-regexp limit t)
  4689. (let* ((help (concat "LINK: "
  4690. (org-match-string-no-properties 1)))
  4691. ;; FIXME: above we should remove the escapes.
  4692. ;; but that requires another match, protecting match data,
  4693. ;; a lot of overhead for font-lock.
  4694. (ip (org-maybe-intangible
  4695. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4696. 'keymap org-mouse-map 'mouse-face 'highlight
  4697. 'font-lock-multiline t 'help-echo help)))
  4698. (vp (list 'rear-nonsticky org-nonsticky-props
  4699. 'keymap org-mouse-map 'mouse-face 'highlight
  4700. ' font-lock-multiline t 'help-echo help)))
  4701. ;; We need to remove the invisible property here. Table narrowing
  4702. ;; may have made some of this invisible.
  4703. (remove-text-properties (match-beginning 0) (match-end 0)
  4704. '(invisible nil))
  4705. (if (match-end 3)
  4706. (progn
  4707. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4708. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4709. (add-text-properties (match-end 3) (match-end 0) ip))
  4710. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4711. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4712. (add-text-properties (match-end 1) (match-end 0) ip))
  4713. t)))
  4714. (defun org-activate-dates (limit)
  4715. "Run through the buffer and add overlays to dates."
  4716. (if (re-search-forward org-tsr-regexp-both limit t)
  4717. (progn
  4718. (add-text-properties (match-beginning 0) (match-end 0)
  4719. (list 'mouse-face 'highlight
  4720. 'rear-nonsticky org-nonsticky-props
  4721. 'keymap org-mouse-map))
  4722. (when org-display-custom-times
  4723. (if (match-end 3)
  4724. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4725. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4726. t)))
  4727. (defvar org-target-link-regexp nil
  4728. "Regular expression matching radio targets in plain text.")
  4729. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4730. "Regular expression matching a link target.")
  4731. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4732. "Regular expression matching a radio target.")
  4733. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4734. "Regular expression matching any target.")
  4735. (defun org-activate-target-links (limit)
  4736. "Run through the buffer and add overlays to target matches."
  4737. (when org-target-link-regexp
  4738. (let ((case-fold-search t))
  4739. (if (re-search-forward org-target-link-regexp limit t)
  4740. (progn
  4741. (add-text-properties (match-beginning 0) (match-end 0)
  4742. (list 'mouse-face 'highlight
  4743. 'rear-nonsticky org-nonsticky-props
  4744. 'keymap org-mouse-map
  4745. 'help-echo "Radio target link"
  4746. 'org-linked-text t))
  4747. t)))))
  4748. (defun org-update-radio-target-regexp ()
  4749. "Find all radio targets in this file and update the regular expression."
  4750. (interactive)
  4751. (when (memq 'radio org-activate-links)
  4752. (setq org-target-link-regexp
  4753. (org-make-target-link-regexp (org-all-targets 'radio)))
  4754. (org-restart-font-lock)))
  4755. (defun org-hide-wide-columns (limit)
  4756. (let (s e)
  4757. (setq s (text-property-any (point) (or limit (point-max))
  4758. 'org-cwidth t))
  4759. (when s
  4760. (setq e (next-single-property-change s 'org-cwidth))
  4761. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4762. (goto-char e)
  4763. t)))
  4764. (defvar org-latex-and-specials-regexp nil
  4765. "Regular expression for highlighting export special stuff.")
  4766. (defvar org-match-substring-regexp)
  4767. (defvar org-match-substring-with-braces-regexp)
  4768. (defvar org-export-html-special-string-regexps)
  4769. (defun org-compute-latex-and-specials-regexp ()
  4770. "Compute regular expression for stuff treated specially by exporters."
  4771. (if (not org-highlight-latex-fragments-and-specials)
  4772. (org-set-local 'org-latex-and-specials-regexp nil)
  4773. (let*
  4774. ((matchers (plist-get org-format-latex-options :matchers))
  4775. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4776. org-latex-regexps)))
  4777. (options (org-combine-plists (org-default-export-plist)
  4778. (org-infile-export-plist)))
  4779. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4780. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4781. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4782. (org-export-html-expand (plist-get options :expand-quoted-html))
  4783. (org-export-with-special-strings (plist-get options :special-strings))
  4784. (re-sub
  4785. (cond
  4786. ((equal org-export-with-sub-superscripts '{})
  4787. (list org-match-substring-with-braces-regexp))
  4788. (org-export-with-sub-superscripts
  4789. (list org-match-substring-regexp))
  4790. (t nil)))
  4791. (re-latex
  4792. (if org-export-with-LaTeX-fragments
  4793. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4794. (re-macros
  4795. (if org-export-with-TeX-macros
  4796. (list (concat "\\\\"
  4797. (regexp-opt
  4798. (append (mapcar 'car org-html-entities)
  4799. (if (boundp 'org-latex-entities)
  4800. org-latex-entities nil))
  4801. 'words))) ; FIXME
  4802. ))
  4803. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4804. (re-special (if org-export-with-special-strings
  4805. (mapcar (lambda (x) (car x))
  4806. org-export-html-special-string-regexps)))
  4807. (re-rest
  4808. (delq nil
  4809. (list
  4810. (if org-export-html-expand "@<[^>\n]+>")
  4811. ))))
  4812. (org-set-local
  4813. 'org-latex-and-specials-regexp
  4814. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4815. re-rest) "\\|")))))
  4816. (defface org-latex-and-export-specials
  4817. (let ((font (cond ((assq :inherit custom-face-attributes)
  4818. '(:inherit underline))
  4819. (t '(:underline t)))))
  4820. `((((class grayscale) (background light))
  4821. (:foreground "DimGray" ,@font))
  4822. (((class grayscale) (background dark))
  4823. (:foreground "LightGray" ,@font))
  4824. (((class color) (background light))
  4825. (:foreground "SaddleBrown"))
  4826. (((class color) (background dark))
  4827. (:foreground "burlywood"))
  4828. (t (,@font))))
  4829. "Face used to highlight math latex and other special exporter stuff."
  4830. :group 'org-faces)
  4831. (defun org-do-latex-and-special-faces (limit)
  4832. "Run through the buffer and add overlays to links."
  4833. (when org-latex-and-specials-regexp
  4834. (let (rtn d)
  4835. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4836. limit t))
  4837. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4838. 'face))
  4839. '(org-code org-verbatim underline)))
  4840. (progn
  4841. (setq rtn t
  4842. d (cond ((member (char-after (1+ (match-beginning 0)))
  4843. '(?_ ?^)) 1)
  4844. (t 0)))
  4845. (font-lock-prepend-text-property
  4846. (+ d (match-beginning 0)) (match-end 0)
  4847. 'face 'org-latex-and-export-specials)
  4848. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4849. '(font-lock-multiline t)))))
  4850. rtn)))
  4851. (defun org-restart-font-lock ()
  4852. "Restart font-lock-mode, to force refontification."
  4853. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4854. (font-lock-mode -1)
  4855. (font-lock-mode 1)))
  4856. (defun org-all-targets (&optional radio)
  4857. "Return a list of all targets in this file.
  4858. With optional argument RADIO, only find radio targets."
  4859. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4860. rtn)
  4861. (save-excursion
  4862. (goto-char (point-min))
  4863. (while (re-search-forward re nil t)
  4864. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4865. rtn)))
  4866. (defun org-make-target-link-regexp (targets)
  4867. "Make regular expression matching all strings in TARGETS.
  4868. The regular expression finds the targets also if there is a line break
  4869. between words."
  4870. (and targets
  4871. (concat
  4872. "\\<\\("
  4873. (mapconcat
  4874. (lambda (x)
  4875. (while (string-match " +" x)
  4876. (setq x (replace-match "\\s-+" t t x)))
  4877. x)
  4878. targets
  4879. "\\|")
  4880. "\\)\\>")))
  4881. (defun org-activate-tags (limit)
  4882. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4883. (progn
  4884. (add-text-properties (match-beginning 1) (match-end 1)
  4885. (list 'mouse-face 'highlight
  4886. 'rear-nonsticky org-nonsticky-props
  4887. 'keymap org-mouse-map))
  4888. t)))
  4889. (defun org-outline-level ()
  4890. (save-excursion
  4891. (looking-at outline-regexp)
  4892. (if (match-beginning 1)
  4893. (+ (org-get-string-indentation (match-string 1)) 1000)
  4894. (1- (- (match-end 0) (match-beginning 0))))))
  4895. (defvar org-font-lock-keywords nil)
  4896. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4897. "Regular expression matching a property line.")
  4898. (defun org-set-font-lock-defaults ()
  4899. (let* ((em org-fontify-emphasized-text)
  4900. (lk org-activate-links)
  4901. (org-font-lock-extra-keywords
  4902. (list
  4903. ;; Headlines
  4904. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4905. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4906. ;; Table lines
  4907. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4908. (1 'org-table t))
  4909. ;; Table internals
  4910. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4911. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4912. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4913. ;; Drawers
  4914. (list org-drawer-regexp '(0 'org-special-keyword t))
  4915. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4916. ;; Properties
  4917. (list org-property-re
  4918. '(1 'org-special-keyword t)
  4919. '(3 'org-property-value t))
  4920. (if org-format-transports-properties-p
  4921. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4922. ;; Links
  4923. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4924. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4925. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4926. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4927. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4928. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4929. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4930. '(org-hide-wide-columns (0 nil append))
  4931. ;; TODO lines
  4932. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4933. '(1 (org-get-todo-face 1) t))
  4934. ;; DONE
  4935. (if org-fontify-done-headline
  4936. (list (concat "^[*]+ +\\<\\("
  4937. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4938. "\\)\\(.*\\)")
  4939. '(2 'org-headline-done t))
  4940. nil)
  4941. ;; Priorities
  4942. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4943. ;; Special keywords
  4944. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4945. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4946. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4947. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4948. ;; Emphasis
  4949. (if em
  4950. (if (featurep 'xemacs)
  4951. '(org-do-emphasis-faces (0 nil append))
  4952. '(org-do-emphasis-faces)))
  4953. ;; Checkboxes
  4954. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4955. 2 'bold prepend)
  4956. (if org-provide-checkbox-statistics
  4957. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4958. (0 (org-get-checkbox-statistics-face) t)))
  4959. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4960. '(1 'org-archived prepend))
  4961. ;; Specials
  4962. '(org-do-latex-and-special-faces)
  4963. ;; Code
  4964. '(org-activate-code (1 'org-code t))
  4965. ;; COMMENT
  4966. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4967. "\\|" org-quote-string "\\)\\>")
  4968. '(1 'org-special-keyword t))
  4969. '("^#.*" (0 'font-lock-comment-face t))
  4970. )))
  4971. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4972. ;; Now set the full font-lock-keywords
  4973. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4974. (org-set-local 'font-lock-defaults
  4975. '(org-font-lock-keywords t nil nil backward-paragraph))
  4976. (kill-local-variable 'font-lock-keywords) nil))
  4977. (defvar org-m nil)
  4978. (defvar org-l nil)
  4979. (defvar org-f nil)
  4980. (defun org-get-level-face (n)
  4981. "Get the right face for match N in font-lock matching of healdines."
  4982. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4983. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4984. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4985. (cond
  4986. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4987. ((eq n 2) org-f)
  4988. (t (if org-level-color-stars-only nil org-f))))
  4989. (defun org-get-todo-face (kwd)
  4990. "Get the right face for a TODO keyword KWD.
  4991. If KWD is a number, get the corresponding match group."
  4992. (if (numberp kwd) (setq kwd (match-string kwd)))
  4993. (or (cdr (assoc kwd org-todo-keyword-faces))
  4994. (and (member kwd org-done-keywords) 'org-done)
  4995. 'org-todo))
  4996. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4997. "Remove fontification and activation overlays from links."
  4998. (font-lock-default-unfontify-region beg end)
  4999. (let* ((buffer-undo-list t)
  5000. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5001. (inhibit-modification-hooks t)
  5002. deactivate-mark buffer-file-name buffer-file-truename)
  5003. (remove-text-properties beg end
  5004. '(mouse-face t keymap t org-linked-text t
  5005. invisible t intangible t))))
  5006. ;;;; Visibility cycling, including org-goto and indirect buffer
  5007. ;;; Cycling
  5008. (defvar org-cycle-global-status nil)
  5009. (make-variable-buffer-local 'org-cycle-global-status)
  5010. (defvar org-cycle-subtree-status nil)
  5011. (make-variable-buffer-local 'org-cycle-subtree-status)
  5012. ;;;###autoload
  5013. (defun org-cycle (&optional arg)
  5014. "Visibility cycling for Org-mode.
  5015. - When this function is called with a prefix argument, rotate the entire
  5016. buffer through 3 states (global cycling)
  5017. 1. OVERVIEW: Show only top-level headlines.
  5018. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5019. 3. SHOW ALL: Show everything.
  5020. - When point is at the beginning of a headline, rotate the subtree started
  5021. by this line through 3 different states (local cycling)
  5022. 1. FOLDED: Only the main headline is shown.
  5023. 2. CHILDREN: The main headline and the direct children are shown.
  5024. From this state, you can move to one of the children
  5025. and zoom in further.
  5026. 3. SUBTREE: Show the entire subtree, including body text.
  5027. - When there is a numeric prefix, go up to a heading with level ARG, do
  5028. a `show-subtree' and return to the previous cursor position. If ARG
  5029. is negative, go up that many levels.
  5030. - When point is not at the beginning of a headline, execute
  5031. `indent-relative', like TAB normally does. See the option
  5032. `org-cycle-emulate-tab' for details.
  5033. - Special case: if point is at the beginning of the buffer and there is
  5034. no headline in line 1, this function will act as if called with prefix arg.
  5035. But only if also the variable `org-cycle-global-at-bob' is t."
  5036. (interactive "P")
  5037. (let* ((outline-regexp
  5038. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5039. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5040. outline-regexp))
  5041. (bob-special (and org-cycle-global-at-bob (bobp)
  5042. (not (looking-at outline-regexp))))
  5043. (org-cycle-hook
  5044. (if bob-special
  5045. (delq 'org-optimize-window-after-visibility-change
  5046. (copy-sequence org-cycle-hook))
  5047. org-cycle-hook))
  5048. (pos (point)))
  5049. (if (or bob-special (equal arg '(4)))
  5050. ;; special case: use global cycling
  5051. (setq arg t))
  5052. (cond
  5053. ((org-at-table-p 'any)
  5054. ;; Enter the table or move to the next field in the table
  5055. (or (org-table-recognize-table.el)
  5056. (progn
  5057. (if arg (org-table-edit-field t)
  5058. (org-table-justify-field-maybe)
  5059. (call-interactively 'org-table-next-field)))))
  5060. ((eq arg t) ;; Global cycling
  5061. (cond
  5062. ((and (eq last-command this-command)
  5063. (eq org-cycle-global-status 'overview))
  5064. ;; We just created the overview - now do table of contents
  5065. ;; This can be slow in very large buffers, so indicate action
  5066. (message "CONTENTS...")
  5067. (org-content)
  5068. (message "CONTENTS...done")
  5069. (setq org-cycle-global-status 'contents)
  5070. (run-hook-with-args 'org-cycle-hook 'contents))
  5071. ((and (eq last-command this-command)
  5072. (eq org-cycle-global-status 'contents))
  5073. ;; We just showed the table of contents - now show everything
  5074. (show-all)
  5075. (message "SHOW ALL")
  5076. (setq org-cycle-global-status 'all)
  5077. (run-hook-with-args 'org-cycle-hook 'all))
  5078. (t
  5079. ;; Default action: go to overview
  5080. (org-overview)
  5081. (message "OVERVIEW")
  5082. (setq org-cycle-global-status 'overview)
  5083. (run-hook-with-args 'org-cycle-hook 'overview))))
  5084. ((and org-drawers org-drawer-regexp
  5085. (save-excursion
  5086. (beginning-of-line 1)
  5087. (looking-at org-drawer-regexp)))
  5088. ;; Toggle block visibility
  5089. (org-flag-drawer
  5090. (not (get-char-property (match-end 0) 'invisible))))
  5091. ((integerp arg)
  5092. ;; Show-subtree, ARG levels up from here.
  5093. (save-excursion
  5094. (org-back-to-heading)
  5095. (outline-up-heading (if (< arg 0) (- arg)
  5096. (- (funcall outline-level) arg)))
  5097. (org-show-subtree)))
  5098. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5099. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5100. ;; At a heading: rotate between three different views
  5101. (org-back-to-heading)
  5102. (let ((goal-column 0) eoh eol eos)
  5103. ;; First, some boundaries
  5104. (save-excursion
  5105. (org-back-to-heading)
  5106. (save-excursion
  5107. (beginning-of-line 2)
  5108. (while (and (not (eobp)) ;; this is like `next-line'
  5109. (get-char-property (1- (point)) 'invisible))
  5110. (beginning-of-line 2)) (setq eol (point)))
  5111. (outline-end-of-heading) (setq eoh (point))
  5112. (org-end-of-subtree t)
  5113. (unless (eobp)
  5114. (skip-chars-forward " \t\n")
  5115. (beginning-of-line 1) ; in case this is an item
  5116. )
  5117. (setq eos (1- (point))))
  5118. ;; Find out what to do next and set `this-command'
  5119. (cond
  5120. ((= eos eoh)
  5121. ;; Nothing is hidden behind this heading
  5122. (message "EMPTY ENTRY")
  5123. (setq org-cycle-subtree-status nil)
  5124. (save-excursion
  5125. (goto-char eos)
  5126. (outline-next-heading)
  5127. (if (org-invisible-p) (org-flag-heading nil))))
  5128. ((or (>= eol eos)
  5129. (not (string-match "\\S-" (buffer-substring eol eos))))
  5130. ;; Entire subtree is hidden in one line: open it
  5131. (org-show-entry)
  5132. (show-children)
  5133. (message "CHILDREN")
  5134. (save-excursion
  5135. (goto-char eos)
  5136. (outline-next-heading)
  5137. (if (org-invisible-p) (org-flag-heading nil)))
  5138. (setq org-cycle-subtree-status 'children)
  5139. (run-hook-with-args 'org-cycle-hook 'children))
  5140. ((and (eq last-command this-command)
  5141. (eq org-cycle-subtree-status 'children))
  5142. ;; We just showed the children, now show everything.
  5143. (org-show-subtree)
  5144. (message "SUBTREE")
  5145. (setq org-cycle-subtree-status 'subtree)
  5146. (run-hook-with-args 'org-cycle-hook 'subtree))
  5147. (t
  5148. ;; Default action: hide the subtree.
  5149. (hide-subtree)
  5150. (message "FOLDED")
  5151. (setq org-cycle-subtree-status 'folded)
  5152. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5153. ;; TAB emulation
  5154. (buffer-read-only (org-back-to-heading))
  5155. ((org-try-cdlatex-tab))
  5156. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5157. (or (not (bolp))
  5158. (not (looking-at outline-regexp))))
  5159. (call-interactively (global-key-binding "\t")))
  5160. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5161. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5162. (or (and (eq org-cycle-emulate-tab 'white)
  5163. (= (match-end 0) (point-at-eol)))
  5164. (and (eq org-cycle-emulate-tab 'whitestart)
  5165. (>= (match-end 0) pos))))
  5166. t
  5167. (eq org-cycle-emulate-tab t))
  5168. ; (if (and (looking-at "[ \n\r\t]")
  5169. ; (string-match "^[ \t]*$" (buffer-substring
  5170. ; (point-at-bol) (point))))
  5171. ; (progn
  5172. ; (beginning-of-line 1)
  5173. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5174. (call-interactively (global-key-binding "\t")))
  5175. (t (save-excursion
  5176. (org-back-to-heading)
  5177. (org-cycle))))))
  5178. ;;;###autoload
  5179. (defun org-global-cycle (&optional arg)
  5180. "Cycle the global visibility. For details see `org-cycle'."
  5181. (interactive "P")
  5182. (let ((org-cycle-include-plain-lists
  5183. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5184. (if (integerp arg)
  5185. (progn
  5186. (show-all)
  5187. (hide-sublevels arg)
  5188. (setq org-cycle-global-status 'contents))
  5189. (org-cycle '(4)))))
  5190. (defun org-overview ()
  5191. "Switch to overview mode, shoing only top-level headlines.
  5192. Really, this shows all headlines with level equal or greater than the level
  5193. of the first headline in the buffer. This is important, because if the
  5194. first headline is not level one, then (hide-sublevels 1) gives confusing
  5195. results."
  5196. (interactive)
  5197. (let ((level (save-excursion
  5198. (goto-char (point-min))
  5199. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5200. (progn
  5201. (goto-char (match-beginning 0))
  5202. (funcall outline-level))))))
  5203. (and level (hide-sublevels level))))
  5204. (defun org-content (&optional arg)
  5205. "Show all headlines in the buffer, like a table of contents.
  5206. With numerical argument N, show content up to level N."
  5207. (interactive "P")
  5208. (save-excursion
  5209. ;; Visit all headings and show their offspring
  5210. (and (integerp arg) (org-overview))
  5211. (goto-char (point-max))
  5212. (catch 'exit
  5213. (while (and (progn (condition-case nil
  5214. (outline-previous-visible-heading 1)
  5215. (error (goto-char (point-min))))
  5216. t)
  5217. (looking-at outline-regexp))
  5218. (if (integerp arg)
  5219. (show-children (1- arg))
  5220. (show-branches))
  5221. (if (bobp) (throw 'exit nil))))))
  5222. (defun org-optimize-window-after-visibility-change (state)
  5223. "Adjust the window after a change in outline visibility.
  5224. This function is the default value of the hook `org-cycle-hook'."
  5225. (when (get-buffer-window (current-buffer))
  5226. (cond
  5227. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5228. ; ((eq state 'overview) (org-beginning-of-line))
  5229. ((eq state 'content) nil)
  5230. ((eq state 'all) nil)
  5231. ((eq state 'folded) nil)
  5232. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5233. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5234. (defun org-compact-display-after-subtree-move ()
  5235. (let (beg end)
  5236. (save-excursion
  5237. (if (org-up-heading-safe)
  5238. (progn
  5239. (hide-subtree)
  5240. (show-entry)
  5241. (show-children)
  5242. (org-cycle-show-empty-lines 'children)
  5243. (org-cycle-hide-drawers 'children))
  5244. (org-overview)))))
  5245. (defun org-cycle-show-empty-lines (state)
  5246. "Show empty lines above all visible headlines.
  5247. The region to be covered depends on STATE when called through
  5248. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5249. entire buffer covered. Note that an empty line is only shown if there
  5250. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5251. (when (> org-cycle-separator-lines 0)
  5252. (save-excursion
  5253. (let* ((n org-cycle-separator-lines)
  5254. (re (cond
  5255. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5256. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5257. (t (let ((ns (number-to-string (- n 2))))
  5258. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5259. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5260. beg end)
  5261. (cond
  5262. ((memq state '(overview contents t))
  5263. (setq beg (point-min) end (point-max)))
  5264. ((memq state '(children folded))
  5265. (setq beg (point) end (progn (org-end-of-subtree t t)
  5266. (beginning-of-line 2)
  5267. (point)))))
  5268. (when beg
  5269. (goto-char beg)
  5270. (while (re-search-forward re end t)
  5271. (if (not (get-char-property (match-end 1) 'invisible))
  5272. (outline-flag-region
  5273. (match-beginning 1) (match-end 1) nil)))))))
  5274. ;; Never hide empty lines at the end of the file.
  5275. (save-excursion
  5276. (goto-char (point-max))
  5277. (outline-previous-heading)
  5278. (outline-end-of-heading)
  5279. (if (and (looking-at "[ \t\n]+")
  5280. (= (match-end 0) (point-max)))
  5281. (outline-flag-region (point) (match-end 0) nil))))
  5282. (defun org-subtree-end-visible-p ()
  5283. "Is the end of the current subtree visible?"
  5284. (pos-visible-in-window-p
  5285. (save-excursion (org-end-of-subtree t) (point))))
  5286. (defun org-first-headline-recenter (&optional N)
  5287. "Move cursor to the first headline and recenter the headline.
  5288. Optional argument N means, put the headline into the Nth line of the window."
  5289. (goto-char (point-min))
  5290. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5291. (beginning-of-line)
  5292. (recenter (prefix-numeric-value N))))
  5293. ;;; Org-goto
  5294. (defvar org-goto-window-configuration nil)
  5295. (defvar org-goto-marker nil)
  5296. (defvar org-goto-map
  5297. (let ((map (make-sparse-keymap)))
  5298. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5299. (while (setq cmd (pop cmds))
  5300. (substitute-key-definition cmd cmd map global-map)))
  5301. (suppress-keymap map)
  5302. (org-defkey map "\C-m" 'org-goto-ret)
  5303. (org-defkey map [(return)] 'org-goto-ret)
  5304. (org-defkey map [(left)] 'org-goto-left)
  5305. (org-defkey map [(right)] 'org-goto-right)
  5306. (org-defkey map [(control ?g)] 'org-goto-quit)
  5307. (org-defkey map "\C-i" 'org-cycle)
  5308. (org-defkey map [(tab)] 'org-cycle)
  5309. (org-defkey map [(down)] 'outline-next-visible-heading)
  5310. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5311. (if org-goto-auto-isearch
  5312. (if (fboundp 'define-key-after)
  5313. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5314. nil)
  5315. (org-defkey map "q" 'org-goto-quit)
  5316. (org-defkey map "n" 'outline-next-visible-heading)
  5317. (org-defkey map "p" 'outline-previous-visible-heading)
  5318. (org-defkey map "f" 'outline-forward-same-level)
  5319. (org-defkey map "b" 'outline-backward-same-level)
  5320. (org-defkey map "u" 'outline-up-heading))
  5321. (org-defkey map "/" 'org-occur)
  5322. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5323. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5324. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5325. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5326. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5327. map))
  5328. (defconst org-goto-help
  5329. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5330. RET=jump to location [Q]uit and return to previous location
  5331. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5332. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5333. (defun org-goto (&optional alternative-interface)
  5334. "Look up a different location in the current file, keeping current visibility.
  5335. When you want look-up or go to a different location in a document, the
  5336. fastest way is often to fold the entire buffer and then dive into the tree.
  5337. This method has the disadvantage, that the previous location will be folded,
  5338. which may not be what you want.
  5339. This command works around this by showing a copy of the current buffer
  5340. in an indirect buffer, in overview mode. You can dive into the tree in
  5341. that copy, use org-occur and incremental search to find a location.
  5342. When pressing RET or `Q', the command returns to the original buffer in
  5343. which the visibility is still unchanged. After RET is will also jump to
  5344. the location selected in the indirect buffer and expose the
  5345. the headline hierarchy above."
  5346. (interactive "P")
  5347. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5348. (org-refile-use-outline-path t)
  5349. (interface
  5350. (if (not alternative-interface)
  5351. org-goto-interface
  5352. (if (eq org-goto-interface 'outline)
  5353. 'outline-path-completion
  5354. 'outline)))
  5355. (org-goto-start-pos (point))
  5356. (selected-point
  5357. (if (eq interface 'outline)
  5358. (car (org-get-location (current-buffer) org-goto-help))
  5359. (nth 3 (org-refile-get-location "Goto: ")))))
  5360. (if selected-point
  5361. (progn
  5362. (org-mark-ring-push org-goto-start-pos)
  5363. (goto-char selected-point)
  5364. (if (or (org-invisible-p) (org-invisible-p2))
  5365. (org-show-context 'org-goto)))
  5366. (message "Quit"))))
  5367. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5368. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5369. (defvar org-goto-local-auto-isearch-map) ; defined below
  5370. (defun org-get-location (buf help)
  5371. "Let the user select a location in the Org-mode buffer BUF.
  5372. This function uses a recursive edit. It returns the selected position
  5373. or nil."
  5374. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5375. (isearch-hide-immediately nil)
  5376. (isearch-search-fun-function
  5377. (lambda () 'org-goto-local-search-forward-headings))
  5378. (org-goto-selected-point org-goto-exit-command))
  5379. (save-excursion
  5380. (save-window-excursion
  5381. (delete-other-windows)
  5382. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5383. (switch-to-buffer
  5384. (condition-case nil
  5385. (make-indirect-buffer (current-buffer) "*org-goto*")
  5386. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5387. (with-output-to-temp-buffer "*Help*"
  5388. (princ help))
  5389. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5390. (setq buffer-read-only nil)
  5391. (let ((org-startup-truncated t)
  5392. (org-startup-folded nil)
  5393. (org-startup-align-all-tables nil))
  5394. (org-mode)
  5395. (org-overview))
  5396. (setq buffer-read-only t)
  5397. (if (and (boundp 'org-goto-start-pos)
  5398. (integer-or-marker-p org-goto-start-pos))
  5399. (let ((org-show-hierarchy-above t)
  5400. (org-show-siblings t)
  5401. (org-show-following-heading t))
  5402. (goto-char org-goto-start-pos)
  5403. (and (org-invisible-p) (org-show-context)))
  5404. (goto-char (point-min)))
  5405. (org-beginning-of-line)
  5406. (message "Select location and press RET")
  5407. (use-local-map org-goto-map)
  5408. (recursive-edit)
  5409. ))
  5410. (kill-buffer "*org-goto*")
  5411. (cons org-goto-selected-point org-goto-exit-command)))
  5412. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5413. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5414. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5415. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5416. (defun org-goto-local-search-forward-headings (string bound noerror)
  5417. "Search and make sure that anu matches are in headlines."
  5418. (catch 'return
  5419. (while (search-forward string bound noerror)
  5420. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5421. (and (member :headline context)
  5422. (not (member :tags context))))
  5423. (throw 'return (point))))))
  5424. (defun org-goto-local-auto-isearch ()
  5425. "Start isearch."
  5426. (interactive)
  5427. (goto-char (point-min))
  5428. (let ((keys (this-command-keys)))
  5429. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5430. (isearch-mode t)
  5431. (isearch-process-search-char (string-to-char keys)))))
  5432. (defun org-goto-ret (&optional arg)
  5433. "Finish `org-goto' by going to the new location."
  5434. (interactive "P")
  5435. (setq org-goto-selected-point (point)
  5436. org-goto-exit-command 'return)
  5437. (throw 'exit nil))
  5438. (defun org-goto-left ()
  5439. "Finish `org-goto' by going to the new location."
  5440. (interactive)
  5441. (if (org-on-heading-p)
  5442. (progn
  5443. (beginning-of-line 1)
  5444. (setq org-goto-selected-point (point)
  5445. org-goto-exit-command 'left)
  5446. (throw 'exit nil))
  5447. (error "Not on a heading")))
  5448. (defun org-goto-right ()
  5449. "Finish `org-goto' by going to the new location."
  5450. (interactive)
  5451. (if (org-on-heading-p)
  5452. (progn
  5453. (setq org-goto-selected-point (point)
  5454. org-goto-exit-command 'right)
  5455. (throw 'exit nil))
  5456. (error "Not on a heading")))
  5457. (defun org-goto-quit ()
  5458. "Finish `org-goto' without cursor motion."
  5459. (interactive)
  5460. (setq org-goto-selected-point nil)
  5461. (setq org-goto-exit-command 'quit)
  5462. (throw 'exit nil))
  5463. ;;; Indirect buffer display of subtrees
  5464. (defvar org-indirect-dedicated-frame nil
  5465. "This is the frame being used for indirect tree display.")
  5466. (defvar org-last-indirect-buffer nil)
  5467. (defun org-tree-to-indirect-buffer (&optional arg)
  5468. "Create indirect buffer and narrow it to current subtree.
  5469. With numerical prefix ARG, go up to this level and then take that tree.
  5470. If ARG is negative, go up that many levels.
  5471. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5472. indirect buffer previously made with this command, to avoid proliferation of
  5473. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5474. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5475. is kept so that you can work with several indirect buffers at the same time.
  5476. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5477. requests that a new frame be made for the new buffer, so that the dedicated
  5478. frame is not changed."
  5479. (interactive "P")
  5480. (let ((cbuf (current-buffer))
  5481. (cwin (selected-window))
  5482. (pos (point))
  5483. beg end level heading ibuf)
  5484. (save-excursion
  5485. (org-back-to-heading t)
  5486. (when (numberp arg)
  5487. (setq level (org-outline-level))
  5488. (if (< arg 0) (setq arg (+ level arg)))
  5489. (while (> (setq level (org-outline-level)) arg)
  5490. (outline-up-heading 1 t)))
  5491. (setq beg (point)
  5492. heading (org-get-heading))
  5493. (org-end-of-subtree t) (setq end (point)))
  5494. (if (and (buffer-live-p org-last-indirect-buffer)
  5495. (not (eq org-indirect-buffer-display 'new-frame))
  5496. (not arg))
  5497. (kill-buffer org-last-indirect-buffer))
  5498. (setq ibuf (org-get-indirect-buffer cbuf)
  5499. org-last-indirect-buffer ibuf)
  5500. (cond
  5501. ((or (eq org-indirect-buffer-display 'new-frame)
  5502. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5503. (select-frame (make-frame))
  5504. (delete-other-windows)
  5505. (switch-to-buffer ibuf)
  5506. (org-set-frame-title heading))
  5507. ((eq org-indirect-buffer-display 'dedicated-frame)
  5508. (raise-frame
  5509. (select-frame (or (and org-indirect-dedicated-frame
  5510. (frame-live-p org-indirect-dedicated-frame)
  5511. org-indirect-dedicated-frame)
  5512. (setq org-indirect-dedicated-frame (make-frame)))))
  5513. (delete-other-windows)
  5514. (switch-to-buffer ibuf)
  5515. (org-set-frame-title (concat "Indirect: " heading)))
  5516. ((eq org-indirect-buffer-display 'current-window)
  5517. (switch-to-buffer ibuf))
  5518. ((eq org-indirect-buffer-display 'other-window)
  5519. (pop-to-buffer ibuf))
  5520. (t (error "Invalid value.")))
  5521. (if (featurep 'xemacs)
  5522. (save-excursion (org-mode) (turn-on-font-lock)))
  5523. (narrow-to-region beg end)
  5524. (show-all)
  5525. (goto-char pos)
  5526. (and (window-live-p cwin) (select-window cwin))))
  5527. (defun org-get-indirect-buffer (&optional buffer)
  5528. (setq buffer (or buffer (current-buffer)))
  5529. (let ((n 1) (base (buffer-name buffer)) bname)
  5530. (while (buffer-live-p
  5531. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5532. (setq n (1+ n)))
  5533. (condition-case nil
  5534. (make-indirect-buffer buffer bname 'clone)
  5535. (error (make-indirect-buffer buffer bname)))))
  5536. (defun org-set-frame-title (title)
  5537. "Set the title of the current frame to the string TITLE."
  5538. ;; FIXME: how to name a single frame in XEmacs???
  5539. (unless (featurep 'xemacs)
  5540. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5541. ;;;; Structure editing
  5542. ;;; Inserting headlines
  5543. (defun org-insert-heading (&optional force-heading)
  5544. "Insert a new heading or item with same depth at point.
  5545. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5546. If point is at the beginning of a headline, insert a sibling before the
  5547. current headline. If point is not at the beginning, do not split the line,
  5548. but create the new hedline after the current line."
  5549. (interactive "P")
  5550. (if (= (buffer-size) 0)
  5551. (insert "\n* ")
  5552. (when (or force-heading (not (org-insert-item)))
  5553. (let* ((head (save-excursion
  5554. (condition-case nil
  5555. (progn
  5556. (org-back-to-heading)
  5557. (match-string 0))
  5558. (error "*"))))
  5559. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5560. pos)
  5561. (cond
  5562. ((and (org-on-heading-p) (bolp)
  5563. (or (bobp)
  5564. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5565. ;; insert before the current line
  5566. (open-line (if blank 2 1)))
  5567. ((and (bolp)
  5568. (or (bobp)
  5569. (save-excursion
  5570. (backward-char 1) (not (org-invisible-p)))))
  5571. ;; insert right here
  5572. nil)
  5573. (t
  5574. ; ;; in the middle of the line
  5575. ; (org-show-entry)
  5576. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5577. ; (if (and
  5578. ; (org-on-heading-p)
  5579. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5580. ; ;; protect the tags
  5581. ;; (let ((tags (match-string 2)) pos)
  5582. ; (delete-region (match-beginning 1) (match-end 1))
  5583. ; (setq pos (point-at-bol))
  5584. ; (newline (if blank 2 1))
  5585. ; (save-excursion
  5586. ; (goto-char pos)
  5587. ; (end-of-line 1)
  5588. ; (insert " " tags)
  5589. ; (org-set-tags nil 'align)))
  5590. ; (newline (if blank 2 1)))
  5591. ; (newline (if blank 2 1))))
  5592. ;; in the middle of the line
  5593. (org-show-entry)
  5594. (let ((split
  5595. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5596. tags pos)
  5597. (if (org-on-heading-p)
  5598. (progn
  5599. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5600. (setq tags (and (match-end 2) (match-string 2)))
  5601. (and (match-end 1)
  5602. (delete-region (match-beginning 1) (match-end 1)))
  5603. (setq pos (point-at-bol))
  5604. (or split (end-of-line 1))
  5605. (delete-horizontal-space)
  5606. (newline (if blank 2 1))
  5607. (when tags
  5608. (save-excursion
  5609. (goto-char pos)
  5610. (end-of-line 1)
  5611. (insert " " tags)
  5612. (org-set-tags nil 'align))))
  5613. (or split (end-of-line 1))
  5614. (newline (if blank 2 1))))))
  5615. (insert head) (just-one-space)
  5616. (setq pos (point))
  5617. (end-of-line 1)
  5618. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5619. (run-hooks 'org-insert-heading-hook)))))
  5620. (defun org-insert-heading-after-current ()
  5621. "Insert a new heading with same level as current, after current subtree."
  5622. (interactive)
  5623. (org-back-to-heading)
  5624. (org-insert-heading)
  5625. (org-move-subtree-down)
  5626. (end-of-line 1))
  5627. (defun org-insert-todo-heading (arg)
  5628. "Insert a new heading with the same level and TODO state as current heading.
  5629. If the heading has no TODO state, or if the state is DONE, use the first
  5630. state (TODO by default). Also with prefix arg, force first state."
  5631. (interactive "P")
  5632. (when (not (org-insert-item 'checkbox))
  5633. (org-insert-heading)
  5634. (save-excursion
  5635. (org-back-to-heading)
  5636. (outline-previous-heading)
  5637. (looking-at org-todo-line-regexp))
  5638. (if (or arg
  5639. (not (match-beginning 2))
  5640. (member (match-string 2) org-done-keywords))
  5641. (insert (car org-todo-keywords-1) " ")
  5642. (insert (match-string 2) " "))))
  5643. (defun org-insert-subheading (arg)
  5644. "Insert a new subheading and demote it.
  5645. Works for outline headings and for plain lists alike."
  5646. (interactive "P")
  5647. (org-insert-heading arg)
  5648. (cond
  5649. ((org-on-heading-p) (org-do-demote))
  5650. ((org-at-item-p) (org-indent-item 1))))
  5651. (defun org-insert-todo-subheading (arg)
  5652. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5653. Works for outline headings and for plain lists alike."
  5654. (interactive "P")
  5655. (org-insert-todo-heading arg)
  5656. (cond
  5657. ((org-on-heading-p) (org-do-demote))
  5658. ((org-at-item-p) (org-indent-item 1))))
  5659. ;;; Promotion and Demotion
  5660. (defun org-promote-subtree ()
  5661. "Promote the entire subtree.
  5662. See also `org-promote'."
  5663. (interactive)
  5664. (save-excursion
  5665. (org-map-tree 'org-promote))
  5666. (org-fix-position-after-promote))
  5667. (defun org-demote-subtree ()
  5668. "Demote the entire subtree. See `org-demote'.
  5669. See also `org-promote'."
  5670. (interactive)
  5671. (save-excursion
  5672. (org-map-tree 'org-demote))
  5673. (org-fix-position-after-promote))
  5674. (defun org-do-promote ()
  5675. "Promote the current heading higher up the tree.
  5676. If the region is active in `transient-mark-mode', promote all headings
  5677. in the region."
  5678. (interactive)
  5679. (save-excursion
  5680. (if (org-region-active-p)
  5681. (org-map-region 'org-promote (region-beginning) (region-end))
  5682. (org-promote)))
  5683. (org-fix-position-after-promote))
  5684. (defun org-do-demote ()
  5685. "Demote the current heading lower down the tree.
  5686. If the region is active in `transient-mark-mode', demote all headings
  5687. in the region."
  5688. (interactive)
  5689. (save-excursion
  5690. (if (org-region-active-p)
  5691. (org-map-region 'org-demote (region-beginning) (region-end))
  5692. (org-demote)))
  5693. (org-fix-position-after-promote))
  5694. (defun org-fix-position-after-promote ()
  5695. "Make sure that after pro/demotion cursor position is right."
  5696. (let ((pos (point)))
  5697. (when (save-excursion
  5698. (beginning-of-line 1)
  5699. (looking-at org-todo-line-regexp)
  5700. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5701. (cond ((eobp) (insert " "))
  5702. ((eolp) (insert " "))
  5703. ((equal (char-after) ?\ ) (forward-char 1))))))
  5704. (defun org-reduced-level (l)
  5705. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5706. (defun org-get-legal-level (level &optional change)
  5707. "Rectify a level change under the influence of `org-odd-levels-only'
  5708. LEVEL is a current level, CHANGE is by how much the level should be
  5709. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5710. even level numbers will become the next higher odd number."
  5711. (if org-odd-levels-only
  5712. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5713. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5714. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5715. (max 1 (+ level change))))
  5716. (defun org-promote ()
  5717. "Promote the current heading higher up the tree.
  5718. If the region is active in `transient-mark-mode', promote all headings
  5719. in the region."
  5720. (org-back-to-heading t)
  5721. (let* ((level (save-match-data (funcall outline-level)))
  5722. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5723. (diff (abs (- level (length up-head) -1))))
  5724. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5725. (replace-match up-head nil t)
  5726. ;; Fixup tag positioning
  5727. (and org-auto-align-tags (org-set-tags nil t))
  5728. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5729. (defun org-demote ()
  5730. "Demote the current heading lower down the tree.
  5731. If the region is active in `transient-mark-mode', demote all headings
  5732. in the region."
  5733. (org-back-to-heading t)
  5734. (let* ((level (save-match-data (funcall outline-level)))
  5735. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5736. (diff (abs (- level (length down-head) -1))))
  5737. (replace-match down-head nil t)
  5738. ;; Fixup tag positioning
  5739. (and org-auto-align-tags (org-set-tags nil t))
  5740. (if org-adapt-indentation (org-fixup-indentation diff))))
  5741. (defun org-map-tree (fun)
  5742. "Call FUN for every heading underneath the current one."
  5743. (org-back-to-heading)
  5744. (let ((level (funcall outline-level)))
  5745. (save-excursion
  5746. (funcall fun)
  5747. (while (and (progn
  5748. (outline-next-heading)
  5749. (> (funcall outline-level) level))
  5750. (not (eobp)))
  5751. (funcall fun)))))
  5752. (defun org-map-region (fun beg end)
  5753. "Call FUN for every heading between BEG and END."
  5754. (let ((org-ignore-region t))
  5755. (save-excursion
  5756. (setq end (copy-marker end))
  5757. (goto-char beg)
  5758. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5759. (< (point) end))
  5760. (funcall fun))
  5761. (while (and (progn
  5762. (outline-next-heading)
  5763. (< (point) end))
  5764. (not (eobp)))
  5765. (funcall fun)))))
  5766. (defun org-fixup-indentation (diff)
  5767. "Change the indentation in the current entry by DIFF
  5768. However, if any line in the current entry has no indentation, or if it
  5769. would end up with no indentation after the change, nothing at all is done."
  5770. (save-excursion
  5771. (let ((end (save-excursion (outline-next-heading)
  5772. (point-marker)))
  5773. (prohibit (if (> diff 0)
  5774. "^\\S-"
  5775. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5776. col)
  5777. (unless (save-excursion (end-of-line 1)
  5778. (re-search-forward prohibit end t))
  5779. (while (and (< (point) end)
  5780. (re-search-forward "^[ \t]+" end t))
  5781. (goto-char (match-end 0))
  5782. (setq col (current-column))
  5783. (if (< diff 0) (replace-match ""))
  5784. (indent-to (+ diff col))))
  5785. (move-marker end nil))))
  5786. (defun org-convert-to-odd-levels ()
  5787. "Convert an org-mode file with all levels allowed to one with odd levels.
  5788. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5789. level 5 etc."
  5790. (interactive)
  5791. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5792. (let ((org-odd-levels-only nil) n)
  5793. (save-excursion
  5794. (goto-char (point-min))
  5795. (while (re-search-forward "^\\*\\*+ " nil t)
  5796. (setq n (- (length (match-string 0)) 2))
  5797. (while (>= (setq n (1- n)) 0)
  5798. (org-demote))
  5799. (end-of-line 1))))))
  5800. (defun org-convert-to-oddeven-levels ()
  5801. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5802. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5803. section with an even level, conversion would destroy the structure of the file. An error
  5804. is signaled in this case."
  5805. (interactive)
  5806. (goto-char (point-min))
  5807. ;; First check if there are no even levels
  5808. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5809. (org-show-context t)
  5810. (error "Not all levels are odd in this file. Conversion not possible."))
  5811. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5812. (let ((org-odd-levels-only nil) n)
  5813. (save-excursion
  5814. (goto-char (point-min))
  5815. (while (re-search-forward "^\\*\\*+ " nil t)
  5816. (setq n (/ (1- (length (match-string 0))) 2))
  5817. (while (>= (setq n (1- n)) 0)
  5818. (org-promote))
  5819. (end-of-line 1))))))
  5820. (defun org-tr-level (n)
  5821. "Make N odd if required."
  5822. (if org-odd-levels-only (1+ (/ n 2)) n))
  5823. ;;; Vertical tree motion, cutting and pasting of subtrees
  5824. (defun org-move-subtree-up (&optional arg)
  5825. "Move the current subtree up past ARG headlines of the same level."
  5826. (interactive "p")
  5827. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5828. (defun org-move-subtree-down (&optional arg)
  5829. "Move the current subtree down past ARG headlines of the same level."
  5830. (interactive "p")
  5831. (setq arg (prefix-numeric-value arg))
  5832. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5833. 'outline-get-last-sibling))
  5834. (ins-point (make-marker))
  5835. (cnt (abs arg))
  5836. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5837. ;; Select the tree
  5838. (org-back-to-heading)
  5839. (setq beg0 (point))
  5840. (save-excursion
  5841. (setq ne-beg (org-back-over-empty-lines))
  5842. (setq beg (point)))
  5843. (save-match-data
  5844. (save-excursion (outline-end-of-heading)
  5845. (setq folded (org-invisible-p)))
  5846. (outline-end-of-subtree))
  5847. (outline-next-heading)
  5848. (setq ne-end (org-back-over-empty-lines))
  5849. (setq end (point))
  5850. (goto-char beg0)
  5851. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5852. ;; include less whitespace
  5853. (save-excursion
  5854. (goto-char beg)
  5855. (forward-line (- ne-beg ne-end))
  5856. (setq beg (point))))
  5857. ;; Find insertion point, with error handling
  5858. (while (> cnt 0)
  5859. (or (and (funcall movfunc) (looking-at outline-regexp))
  5860. (progn (goto-char beg0)
  5861. (error "Cannot move past superior level or buffer limit")))
  5862. (setq cnt (1- cnt)))
  5863. (if (> arg 0)
  5864. ;; Moving forward - still need to move over subtree
  5865. (progn (org-end-of-subtree t t)
  5866. (save-excursion
  5867. (org-back-over-empty-lines)
  5868. (or (bolp) (newline)))))
  5869. (setq ne-ins (org-back-over-empty-lines))
  5870. (move-marker ins-point (point))
  5871. (setq txt (buffer-substring beg end))
  5872. (delete-region beg end)
  5873. (outline-flag-region (1- beg) beg nil)
  5874. (outline-flag-region (1- (point)) (point) nil)
  5875. (insert txt)
  5876. (or (bolp) (insert "\n"))
  5877. (setq ins-end (point))
  5878. (goto-char ins-point)
  5879. (org-skip-whitespace)
  5880. (when (and (< arg 0)
  5881. (org-first-sibling-p)
  5882. (> ne-ins ne-beg))
  5883. ;; Move whitespace back to beginning
  5884. (save-excursion
  5885. (goto-char ins-end)
  5886. (let ((kill-whole-line t))
  5887. (kill-line (- ne-ins ne-beg)) (point)))
  5888. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5889. (move-marker ins-point nil)
  5890. (org-compact-display-after-subtree-move)
  5891. (unless folded
  5892. (org-show-entry)
  5893. (show-children)
  5894. (org-cycle-hide-drawers 'children))))
  5895. (defvar org-subtree-clip ""
  5896. "Clipboard for cut and paste of subtrees.
  5897. This is actually only a copy of the kill, because we use the normal kill
  5898. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5899. (defvar org-subtree-clip-folded nil
  5900. "Was the last copied subtree folded?
  5901. This is used to fold the tree back after pasting.")
  5902. (defun org-cut-subtree (&optional n)
  5903. "Cut the current subtree into the clipboard.
  5904. With prefix arg N, cut this many sequential subtrees.
  5905. This is a short-hand for marking the subtree and then cutting it."
  5906. (interactive "p")
  5907. (org-copy-subtree n 'cut))
  5908. (defun org-copy-subtree (&optional n cut)
  5909. "Cut the current subtree into the clipboard.
  5910. With prefix arg N, cut this many sequential subtrees.
  5911. This is a short-hand for marking the subtree and then copying it.
  5912. If CUT is non-nil, actually cut the subtree."
  5913. (interactive "p")
  5914. (let (beg end folded (beg0 (point)))
  5915. (if (interactive-p)
  5916. (org-back-to-heading nil) ; take what looks like a subtree
  5917. (org-back-to-heading t)) ; take what is really there
  5918. (org-back-over-empty-lines)
  5919. (setq beg (point))
  5920. (skip-chars-forward " \t\r\n")
  5921. (save-match-data
  5922. (save-excursion (outline-end-of-heading)
  5923. (setq folded (org-invisible-p)))
  5924. (condition-case nil
  5925. (outline-forward-same-level (1- n))
  5926. (error nil))
  5927. (org-end-of-subtree t t))
  5928. (org-back-over-empty-lines)
  5929. (setq end (point))
  5930. (goto-char beg0)
  5931. (when (> end beg)
  5932. (setq org-subtree-clip-folded folded)
  5933. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5934. (setq org-subtree-clip (current-kill 0))
  5935. (message "%s: Subtree(s) with %d characters"
  5936. (if cut "Cut" "Copied")
  5937. (length org-subtree-clip)))))
  5938. (defun org-paste-subtree (&optional level tree)
  5939. "Paste the clipboard as a subtree, with modification of headline level.
  5940. The entire subtree is promoted or demoted in order to match a new headline
  5941. level. By default, the new level is derived from the visible headings
  5942. before and after the insertion point, and taken to be the inferior headline
  5943. level of the two. So if the previous visible heading is level 3 and the
  5944. next is level 4 (or vice versa), level 4 will be used for insertion.
  5945. This makes sure that the subtree remains an independent subtree and does
  5946. not swallow low level entries.
  5947. You can also force a different level, either by using a numeric prefix
  5948. argument, or by inserting the heading marker by hand. For example, if the
  5949. cursor is after \"*****\", then the tree will be shifted to level 5.
  5950. If you want to insert the tree as is, just use \\[yank].
  5951. If optional TREE is given, use this text instead of the kill ring."
  5952. (interactive "P")
  5953. (unless (org-kill-is-subtree-p tree)
  5954. (error "%s"
  5955. (substitute-command-keys
  5956. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5957. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5958. (^re (concat "^\\(" outline-regexp "\\)"))
  5959. (re (concat "\\(" outline-regexp "\\)"))
  5960. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5961. (old-level (if (string-match ^re txt)
  5962. (- (match-end 0) (match-beginning 0) 1)
  5963. -1))
  5964. (force-level (cond (level (prefix-numeric-value level))
  5965. ((string-match
  5966. ^re_ (buffer-substring (point-at-bol) (point)))
  5967. (- (match-end 1) (match-beginning 1)))
  5968. (t nil)))
  5969. (previous-level (save-excursion
  5970. (condition-case nil
  5971. (progn
  5972. (outline-previous-visible-heading 1)
  5973. (if (looking-at re)
  5974. (- (match-end 0) (match-beginning 0) 1)
  5975. 1))
  5976. (error 1))))
  5977. (next-level (save-excursion
  5978. (condition-case nil
  5979. (progn
  5980. (or (looking-at outline-regexp)
  5981. (outline-next-visible-heading 1))
  5982. (if (looking-at re)
  5983. (- (match-end 0) (match-beginning 0) 1)
  5984. 1))
  5985. (error 1))))
  5986. (new-level (or force-level (max previous-level next-level)))
  5987. (shift (if (or (= old-level -1)
  5988. (= new-level -1)
  5989. (= old-level new-level))
  5990. 0
  5991. (- new-level old-level)))
  5992. (delta (if (> shift 0) -1 1))
  5993. (func (if (> shift 0) 'org-demote 'org-promote))
  5994. (org-odd-levels-only nil)
  5995. beg end)
  5996. ;; Remove the forced level indicator
  5997. (if force-level
  5998. (delete-region (point-at-bol) (point)))
  5999. ;; Paste
  6000. (beginning-of-line 1)
  6001. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6002. (setq beg (point))
  6003. (insert-before-markers txt) ;; FIXME: correct fix????
  6004. (unless (string-match "\n\\'" txt) (insert "\n"))
  6005. (setq end (point))
  6006. (goto-char beg)
  6007. (skip-chars-forward " \t\n\r")
  6008. (setq beg (point))
  6009. ;; Shift if necessary
  6010. (unless (= shift 0)
  6011. (save-restriction
  6012. (narrow-to-region beg end)
  6013. (while (not (= shift 0))
  6014. (org-map-region func (point-min) (point-max))
  6015. (setq shift (+ delta shift)))
  6016. (goto-char (point-min))))
  6017. (when (interactive-p)
  6018. (message "Clipboard pasted as level %d subtree" new-level))
  6019. (if (and kill-ring
  6020. (eq org-subtree-clip (current-kill 0))
  6021. org-subtree-clip-folded)
  6022. ;; The tree was folded before it was killed/copied
  6023. (hide-subtree))))
  6024. (defun org-kill-is-subtree-p (&optional txt)
  6025. "Check if the current kill is an outline subtree, or a set of trees.
  6026. Returns nil if kill does not start with a headline, or if the first
  6027. headline level is not the largest headline level in the tree.
  6028. So this will actually accept several entries of equal levels as well,
  6029. which is OK for `org-paste-subtree'.
  6030. If optional TXT is given, check this string instead of the current kill."
  6031. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6032. (start-level (and kill
  6033. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6034. org-outline-regexp "\\)")
  6035. kill)
  6036. (- (match-end 2) (match-beginning 2) 1)))
  6037. (re (concat "^" org-outline-regexp))
  6038. (start (1+ (match-beginning 2))))
  6039. (if (not start-level)
  6040. (progn
  6041. nil) ;; does not even start with a heading
  6042. (catch 'exit
  6043. (while (setq start (string-match re kill (1+ start)))
  6044. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6045. (throw 'exit nil)))
  6046. t))))
  6047. (defun org-narrow-to-subtree ()
  6048. "Narrow buffer to the current subtree."
  6049. (interactive)
  6050. (save-excursion
  6051. (save-match-data
  6052. (narrow-to-region
  6053. (progn (org-back-to-heading) (point))
  6054. (progn (org-end-of-subtree t t) (point))))))
  6055. ;;; Outline Sorting
  6056. (defun org-sort (with-case)
  6057. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6058. Optional argument WITH-CASE means sort case-sensitively."
  6059. (interactive "P")
  6060. (if (org-at-table-p)
  6061. (org-call-with-arg 'org-table-sort-lines with-case)
  6062. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6063. (defvar org-priority-regexp) ; defined later in the file
  6064. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6065. "Sort entries on a certain level of an outline tree.
  6066. If there is an active region, the entries in the region are sorted.
  6067. Else, if the cursor is before the first entry, sort the top-level items.
  6068. Else, the children of the entry at point are sorted.
  6069. Sorting can be alphabetically, numerically, and by date/time as given by
  6070. the first time stamp in the entry. The command prompts for the sorting
  6071. type unless it has been given to the function through the SORTING-TYPE
  6072. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6073. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6074. called with point at the beginning of the record. It must return either
  6075. a string or a number that should serve as the sorting key for that record.
  6076. Comparing entries ignores case by default. However, with an optional argument
  6077. WITH-CASE, the sorting considers case as well."
  6078. (interactive "P")
  6079. (let ((case-func (if with-case 'identity 'downcase))
  6080. start beg end stars re re2
  6081. txt what tmp plain-list-p)
  6082. ;; Find beginning and end of region to sort
  6083. (cond
  6084. ((org-region-active-p)
  6085. ;; we will sort the region
  6086. (setq end (region-end)
  6087. what "region")
  6088. (goto-char (region-beginning))
  6089. (if (not (org-on-heading-p)) (outline-next-heading))
  6090. (setq start (point)))
  6091. ((org-at-item-p)
  6092. ;; we will sort this plain list
  6093. (org-beginning-of-item-list) (setq start (point))
  6094. (org-end-of-item-list) (setq end (point))
  6095. (goto-char start)
  6096. (setq plain-list-p t
  6097. what "plain list"))
  6098. ((or (org-on-heading-p)
  6099. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6100. ;; we will sort the children of the current headline
  6101. (org-back-to-heading)
  6102. (setq start (point)
  6103. end (progn (org-end-of-subtree t t)
  6104. (org-back-over-empty-lines)
  6105. (point))
  6106. what "children")
  6107. (goto-char start)
  6108. (show-subtree)
  6109. (outline-next-heading))
  6110. (t
  6111. ;; we will sort the top-level entries in this file
  6112. (goto-char (point-min))
  6113. (or (org-on-heading-p) (outline-next-heading))
  6114. (setq start (point) end (point-max) what "top-level")
  6115. (goto-char start)
  6116. (show-all)))
  6117. (setq beg (point))
  6118. (if (>= beg end) (error "Nothing to sort"))
  6119. (unless plain-list-p
  6120. (looking-at "\\(\\*+\\)")
  6121. (setq stars (match-string 1)
  6122. re (concat "^" (regexp-quote stars) " +")
  6123. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6124. txt (buffer-substring beg end))
  6125. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6126. (if (and (not (equal stars "*")) (string-match re2 txt))
  6127. (error "Region to sort contains a level above the first entry")))
  6128. (unless sorting-type
  6129. (message
  6130. (if plain-list-p
  6131. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6132. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6133. what)
  6134. (setq sorting-type (read-char-exclusive))
  6135. (and (= (downcase sorting-type) ?f)
  6136. (setq getkey-func
  6137. (completing-read "Sort using function: "
  6138. obarray 'fboundp t nil nil))
  6139. (setq getkey-func (intern getkey-func)))
  6140. (and (= (downcase sorting-type) ?r)
  6141. (setq property
  6142. (completing-read "Property: "
  6143. (mapcar 'list (org-buffer-property-keys t))
  6144. nil t))))
  6145. (message "Sorting entries...")
  6146. (save-restriction
  6147. (narrow-to-region start end)
  6148. (let ((dcst (downcase sorting-type))
  6149. (now (current-time)))
  6150. (sort-subr
  6151. (/= dcst sorting-type)
  6152. ;; This function moves to the beginning character of the "record" to
  6153. ;; be sorted.
  6154. (if plain-list-p
  6155. (lambda nil
  6156. (if (org-at-item-p) t (goto-char (point-max))))
  6157. (lambda nil
  6158. (if (re-search-forward re nil t)
  6159. (goto-char (match-beginning 0))
  6160. (goto-char (point-max)))))
  6161. ;; This function moves to the last character of the "record" being
  6162. ;; sorted.
  6163. (if plain-list-p
  6164. 'org-end-of-item
  6165. (lambda nil
  6166. (save-match-data
  6167. (condition-case nil
  6168. (outline-forward-same-level 1)
  6169. (error
  6170. (goto-char (point-max)))))))
  6171. ;; This function returns the value that gets sorted against.
  6172. (if plain-list-p
  6173. (lambda nil
  6174. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6175. (cond
  6176. ((= dcst ?n)
  6177. (string-to-number (buffer-substring (match-end 0)
  6178. (point-at-eol))))
  6179. ((= dcst ?a)
  6180. (buffer-substring (match-end 0) (point-at-eol)))
  6181. ((= dcst ?t)
  6182. (if (re-search-forward org-ts-regexp
  6183. (point-at-eol) t)
  6184. (org-time-string-to-time (match-string 0))
  6185. now))
  6186. ((= dcst ?f)
  6187. (if getkey-func
  6188. (progn
  6189. (setq tmp (funcall getkey-func))
  6190. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6191. tmp)
  6192. (error "Invalid key function `%s'" getkey-func)))
  6193. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6194. (lambda nil
  6195. (cond
  6196. ((= dcst ?n)
  6197. (if (looking-at outline-regexp)
  6198. (string-to-number (buffer-substring (match-end 0)
  6199. (point-at-eol)))
  6200. nil))
  6201. ((= dcst ?a)
  6202. (funcall case-func (buffer-substring (point-at-bol)
  6203. (point-at-eol))))
  6204. ((= dcst ?t)
  6205. (if (re-search-forward org-ts-regexp
  6206. (save-excursion
  6207. (forward-line 2)
  6208. (point)) t)
  6209. (org-time-string-to-time (match-string 0))
  6210. now))
  6211. ((= dcst ?p)
  6212. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6213. (string-to-char (match-string 2))
  6214. org-default-priority))
  6215. ((= dcst ?r)
  6216. (or (org-entry-get nil property) ""))
  6217. ((= dcst ?f)
  6218. (if getkey-func
  6219. (progn
  6220. (setq tmp (funcall getkey-func))
  6221. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6222. tmp)
  6223. (error "Invalid key function `%s'" getkey-func)))
  6224. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6225. nil
  6226. (cond
  6227. ((= dcst ?a) 'string<)
  6228. ((= dcst ?t) 'time-less-p)
  6229. (t nil)))))
  6230. (message "Sorting entries...done")))
  6231. (defun org-do-sort (table what &optional with-case sorting-type)
  6232. "Sort TABLE of WHAT according to SORTING-TYPE.
  6233. The user will be prompted for the SORTING-TYPE if the call to this
  6234. function does not specify it. WHAT is only for the prompt, to indicate
  6235. what is being sorted. The sorting key will be extracted from
  6236. the car of the elements of the table.
  6237. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6238. (unless sorting-type
  6239. (message
  6240. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6241. what)
  6242. (setq sorting-type (read-char-exclusive)))
  6243. (let ((dcst (downcase sorting-type))
  6244. extractfun comparefun)
  6245. ;; Define the appropriate functions
  6246. (cond
  6247. ((= dcst ?n)
  6248. (setq extractfun 'string-to-number
  6249. comparefun (if (= dcst sorting-type) '< '>)))
  6250. ((= dcst ?a)
  6251. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6252. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6253. comparefun (if (= dcst sorting-type)
  6254. 'string<
  6255. (lambda (a b) (and (not (string< a b))
  6256. (not (string= a b)))))))
  6257. ((= dcst ?t)
  6258. (setq extractfun
  6259. (lambda (x)
  6260. (if (string-match org-ts-regexp x)
  6261. (time-to-seconds
  6262. (org-time-string-to-time (match-string 0 x)))
  6263. 0))
  6264. comparefun (if (= dcst sorting-type) '< '>)))
  6265. (t (error "Invalid sorting type `%c'" sorting-type)))
  6266. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6267. table)
  6268. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6269. ;;;; Plain list items, including checkboxes
  6270. ;;; Plain list items
  6271. (defun org-at-item-p ()
  6272. "Is point in a line starting a hand-formatted item?"
  6273. (let ((llt org-plain-list-ordered-item-terminator))
  6274. (save-excursion
  6275. (goto-char (point-at-bol))
  6276. (looking-at
  6277. (cond
  6278. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6279. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6280. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6281. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6282. (defun org-in-item-p ()
  6283. "It the cursor inside a plain list item.
  6284. Does not have to be the first line."
  6285. (save-excursion
  6286. (condition-case nil
  6287. (progn
  6288. (org-beginning-of-item)
  6289. (org-at-item-p)
  6290. t)
  6291. (error nil))))
  6292. (defun org-insert-item (&optional checkbox)
  6293. "Insert a new item at the current level.
  6294. Return t when things worked, nil when we are not in an item."
  6295. (when (save-excursion
  6296. (condition-case nil
  6297. (progn
  6298. (org-beginning-of-item)
  6299. (org-at-item-p)
  6300. (if (org-invisible-p) (error "Invisible item"))
  6301. t)
  6302. (error nil)))
  6303. (let* ((bul (match-string 0))
  6304. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6305. (match-end 0)))
  6306. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6307. pos)
  6308. (cond
  6309. ((and (org-at-item-p) (<= (point) eow))
  6310. ;; before the bullet
  6311. (beginning-of-line 1)
  6312. (open-line (if blank 2 1)))
  6313. ((<= (point) eow)
  6314. (beginning-of-line 1))
  6315. (t
  6316. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6317. (end-of-line 1)
  6318. (delete-horizontal-space))
  6319. (newline (if blank 2 1))))
  6320. (insert bul (if checkbox "[ ]" ""))
  6321. (just-one-space)
  6322. (setq pos (point))
  6323. (end-of-line 1)
  6324. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6325. (org-maybe-renumber-ordered-list)
  6326. (and checkbox (org-update-checkbox-count-maybe))
  6327. t))
  6328. ;;; Checkboxes
  6329. (defun org-at-item-checkbox-p ()
  6330. "Is point at a line starting a plain-list item with a checklet?"
  6331. (and (org-at-item-p)
  6332. (save-excursion
  6333. (goto-char (match-end 0))
  6334. (skip-chars-forward " \t")
  6335. (looking-at "\\[[- X]\\]"))))
  6336. (defun org-toggle-checkbox (&optional arg)
  6337. "Toggle the checkbox in the current line."
  6338. (interactive "P")
  6339. (catch 'exit
  6340. (let (beg end status (firstnew 'unknown))
  6341. (cond
  6342. ((org-region-active-p)
  6343. (setq beg (region-beginning) end (region-end)))
  6344. ((org-on-heading-p)
  6345. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6346. ((org-at-item-checkbox-p)
  6347. (let ((pos (point)))
  6348. (replace-match
  6349. (cond (arg "[-]")
  6350. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6351. (t "[ ]"))
  6352. t t)
  6353. (goto-char pos))
  6354. (throw 'exit t))
  6355. (t (error "Not at a checkbox or heading, and no active region")))
  6356. (save-excursion
  6357. (goto-char beg)
  6358. (while (< (point) end)
  6359. (when (org-at-item-checkbox-p)
  6360. (setq status (equal (match-string 0) "[X]"))
  6361. (when (eq firstnew 'unknown)
  6362. (setq firstnew (not status)))
  6363. (replace-match
  6364. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6365. (beginning-of-line 2)))))
  6366. (org-update-checkbox-count-maybe))
  6367. (defun org-update-checkbox-count-maybe ()
  6368. "Update checkbox statistics unless turned off by user."
  6369. (when org-provide-checkbox-statistics
  6370. (org-update-checkbox-count)))
  6371. (defun org-update-checkbox-count (&optional all)
  6372. "Update the checkbox statistics in the current section.
  6373. This will find all statistic cookies like [57%] and [6/12] and update them
  6374. with the current numbers. With optional prefix argument ALL, do this for
  6375. the whole buffer."
  6376. (interactive "P")
  6377. (save-excursion
  6378. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6379. (beg (condition-case nil
  6380. (progn (outline-back-to-heading) (point))
  6381. (error (point-min))))
  6382. (end (move-marker (make-marker)
  6383. (progn (outline-next-heading) (point))))
  6384. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6385. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6386. (re-find (concat re "\\|" re-box))
  6387. beg-cookie end-cookie is-percent c-on c-off lim
  6388. eline curr-ind next-ind continue-from startsearch
  6389. (cstat 0)
  6390. )
  6391. (when all
  6392. (goto-char (point-min))
  6393. (outline-next-heading)
  6394. (setq beg (point) end (point-max)))
  6395. (goto-char end)
  6396. ;; find each statistic cookie
  6397. (while (re-search-backward re-find beg t)
  6398. (setq beg-cookie (match-beginning 1)
  6399. end-cookie (match-end 1)
  6400. cstat (+ cstat (if end-cookie 1 0))
  6401. startsearch (point-at-eol)
  6402. continue-from (point-at-bol)
  6403. is-percent (match-beginning 2)
  6404. lim (cond
  6405. ((org-on-heading-p) (outline-next-heading) (point))
  6406. ((org-at-item-p) (org-end-of-item) (point))
  6407. (t nil))
  6408. c-on 0
  6409. c-off 0)
  6410. (when lim
  6411. ;; find first checkbox for this cookie and gather
  6412. ;; statistics from all that are at this indentation level
  6413. (goto-char startsearch)
  6414. (if (re-search-forward re-box lim t)
  6415. (progn
  6416. (org-beginning-of-item)
  6417. (setq curr-ind (org-get-indentation))
  6418. (setq next-ind curr-ind)
  6419. (while (= curr-ind next-ind)
  6420. (save-excursion (end-of-line) (setq eline (point)))
  6421. (if (re-search-forward re-box eline t)
  6422. (if (member (match-string 2) '("[ ]" "[-]"))
  6423. (setq c-off (1+ c-off))
  6424. (setq c-on (1+ c-on))
  6425. )
  6426. )
  6427. (org-end-of-item)
  6428. (setq next-ind (org-get-indentation))
  6429. )))
  6430. (goto-char continue-from)
  6431. ;; update cookie
  6432. (when end-cookie
  6433. (delete-region beg-cookie end-cookie)
  6434. (goto-char beg-cookie)
  6435. (insert
  6436. (if is-percent
  6437. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6438. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6439. ;; update items checkbox if it has one
  6440. (when (org-at-item-p)
  6441. (org-beginning-of-item)
  6442. (when (and (> (+ c-on c-off) 0)
  6443. (re-search-forward re-box (point-at-eol) t))
  6444. (setq beg-cookie (match-beginning 2)
  6445. end-cookie (match-end 2))
  6446. (delete-region beg-cookie end-cookie)
  6447. (goto-char beg-cookie)
  6448. (cond ((= c-off 0) (insert "[X]"))
  6449. ((= c-on 0) (insert "[ ]"))
  6450. (t (insert "[-]")))
  6451. )))
  6452. (goto-char continue-from))
  6453. (when (interactive-p)
  6454. (message "Checkbox satistics updated %s (%d places)"
  6455. (if all "in entire file" "in current outline entry") cstat)))))
  6456. (defun org-get-checkbox-statistics-face ()
  6457. "Select the face for checkbox statistics.
  6458. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6459. it will be `org-todo'."
  6460. (if (match-end 1)
  6461. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6462. (if (and (> (match-end 2) (match-beginning 2))
  6463. (equal (match-string 2) (match-string 3)))
  6464. 'org-done
  6465. 'org-todo)))
  6466. (defun org-get-indentation (&optional line)
  6467. "Get the indentation of the current line, interpreting tabs.
  6468. When LINE is given, assume it represents a line and compute its indentation."
  6469. (if line
  6470. (if (string-match "^ *" (org-remove-tabs line))
  6471. (match-end 0))
  6472. (save-excursion
  6473. (beginning-of-line 1)
  6474. (skip-chars-forward " \t")
  6475. (current-column))))
  6476. (defun org-remove-tabs (s &optional width)
  6477. "Replace tabulators in S with spaces.
  6478. Assumes that s is a single line, starting in column 0."
  6479. (setq width (or width tab-width))
  6480. (while (string-match "\t" s)
  6481. (setq s (replace-match
  6482. (make-string
  6483. (- (* width (/ (+ (match-beginning 0) width) width))
  6484. (match-beginning 0)) ?\ )
  6485. t t s)))
  6486. s)
  6487. (defun org-fix-indentation (line ind)
  6488. "Fix indentation in LINE.
  6489. IND is a cons cell with target and minimum indentation.
  6490. If the current indenation in LINE is smaller than the minimum,
  6491. leave it alone. If it is larger than ind, set it to the target."
  6492. (let* ((l (org-remove-tabs line))
  6493. (i (org-get-indentation l))
  6494. (i1 (car ind)) (i2 (cdr ind)))
  6495. (if (>= i i2) (setq l (substring line i2)))
  6496. (if (> i1 0)
  6497. (concat (make-string i1 ?\ ) l)
  6498. l)))
  6499. (defcustom org-empty-line-terminates-plain-lists nil
  6500. "Non-nil means, an empty line ends all plain list levels.
  6501. When nil, empty lines are part of the preceeding item."
  6502. :group 'org-plain-lists
  6503. :type 'boolean)
  6504. (defun org-beginning-of-item ()
  6505. "Go to the beginning of the current hand-formatted item.
  6506. If the cursor is not in an item, throw an error."
  6507. (interactive)
  6508. (let ((pos (point))
  6509. (limit (save-excursion
  6510. (condition-case nil
  6511. (progn
  6512. (org-back-to-heading)
  6513. (beginning-of-line 2) (point))
  6514. (error (point-min)))))
  6515. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6516. ind ind1)
  6517. (if (org-at-item-p)
  6518. (beginning-of-line 1)
  6519. (beginning-of-line 1)
  6520. (skip-chars-forward " \t")
  6521. (setq ind (current-column))
  6522. (if (catch 'exit
  6523. (while t
  6524. (beginning-of-line 0)
  6525. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6526. (if (looking-at "[ \t]*$")
  6527. (setq ind1 ind-empty)
  6528. (skip-chars-forward " \t")
  6529. (setq ind1 (current-column)))
  6530. (if (< ind1 ind)
  6531. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6532. nil
  6533. (goto-char pos)
  6534. (error "Not in an item")))))
  6535. (defun org-end-of-item ()
  6536. "Go to the end of the current hand-formatted item.
  6537. If the cursor is not in an item, throw an error."
  6538. (interactive)
  6539. (let* ((pos (point))
  6540. ind1
  6541. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6542. (limit (save-excursion (outline-next-heading) (point)))
  6543. (ind (save-excursion
  6544. (org-beginning-of-item)
  6545. (skip-chars-forward " \t")
  6546. (current-column)))
  6547. (end (catch 'exit
  6548. (while t
  6549. (beginning-of-line 2)
  6550. (if (eobp) (throw 'exit (point)))
  6551. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6552. (if (looking-at "[ \t]*$")
  6553. (setq ind1 ind-empty)
  6554. (skip-chars-forward " \t")
  6555. (setq ind1 (current-column)))
  6556. (if (<= ind1 ind)
  6557. (throw 'exit (point-at-bol)))))))
  6558. (if end
  6559. (goto-char end)
  6560. (goto-char pos)
  6561. (error "Not in an item"))))
  6562. (defun org-next-item ()
  6563. "Move to the beginning of the next item in the current plain list.
  6564. Error if not at a plain list, or if this is the last item in the list."
  6565. (interactive)
  6566. (let (ind ind1 (pos (point)))
  6567. (org-beginning-of-item)
  6568. (setq ind (org-get-indentation))
  6569. (org-end-of-item)
  6570. (setq ind1 (org-get-indentation))
  6571. (unless (and (org-at-item-p) (= ind ind1))
  6572. (goto-char pos)
  6573. (error "On last item"))))
  6574. (defun org-previous-item ()
  6575. "Move to the beginning of the previous item in the current plain list.
  6576. Error if not at a plain list, or if this is the first item in the list."
  6577. (interactive)
  6578. (let (beg ind ind1 (pos (point)))
  6579. (org-beginning-of-item)
  6580. (setq beg (point))
  6581. (setq ind (org-get-indentation))
  6582. (goto-char beg)
  6583. (catch 'exit
  6584. (while t
  6585. (beginning-of-line 0)
  6586. (if (looking-at "[ \t]*$")
  6587. nil
  6588. (if (<= (setq ind1 (org-get-indentation)) ind)
  6589. (throw 'exit t)))))
  6590. (condition-case nil
  6591. (if (or (not (org-at-item-p))
  6592. (< ind1 (1- ind)))
  6593. (error "")
  6594. (org-beginning-of-item))
  6595. (error (goto-char pos)
  6596. (error "On first item")))))
  6597. (defun org-first-list-item-p ()
  6598. "Is this heading the item in a plain list?"
  6599. (unless (org-at-item-p)
  6600. (error "Not at a plain list item"))
  6601. (org-beginning-of-item)
  6602. (= (point) (save-excursion (org-beginning-of-item-list))))
  6603. (defun org-move-item-down ()
  6604. "Move the plain list item at point down, i.e. swap with following item.
  6605. Subitems (items with larger indentation) are considered part of the item,
  6606. so this really moves item trees."
  6607. (interactive)
  6608. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6609. (org-beginning-of-item)
  6610. (setq beg0 (point))
  6611. (save-excursion
  6612. (setq ne-beg (org-back-over-empty-lines))
  6613. (setq beg (point)))
  6614. (goto-char beg0)
  6615. (setq ind (org-get-indentation))
  6616. (org-end-of-item)
  6617. (setq end0 (point))
  6618. (setq ind1 (org-get-indentation))
  6619. (setq ne-end (org-back-over-empty-lines))
  6620. (setq end (point))
  6621. (goto-char beg0)
  6622. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6623. ;; include less whitespace
  6624. (save-excursion
  6625. (goto-char beg)
  6626. (forward-line (- ne-beg ne-end))
  6627. (setq beg (point))))
  6628. (goto-char end0)
  6629. (if (and (org-at-item-p) (= ind ind1))
  6630. (progn
  6631. (org-end-of-item)
  6632. (org-back-over-empty-lines)
  6633. (setq txt (buffer-substring beg end))
  6634. (save-excursion
  6635. (delete-region beg end))
  6636. (setq pos (point))
  6637. (insert txt)
  6638. (goto-char pos) (org-skip-whitespace)
  6639. (org-maybe-renumber-ordered-list))
  6640. (goto-char pos)
  6641. (error "Cannot move this item further down"))))
  6642. (defun org-move-item-up (arg)
  6643. "Move the plain list item at point up, i.e. swap with previous item.
  6644. Subitems (items with larger indentation) are considered part of the item,
  6645. so this really moves item trees."
  6646. (interactive "p")
  6647. (let (beg beg0 end ind ind1 (pos (point)) txt
  6648. ne-beg ne-ins ins-end)
  6649. (org-beginning-of-item)
  6650. (setq beg0 (point))
  6651. (setq ind (org-get-indentation))
  6652. (save-excursion
  6653. (setq ne-beg (org-back-over-empty-lines))
  6654. (setq beg (point)))
  6655. (goto-char beg0)
  6656. (org-end-of-item)
  6657. (setq end (point))
  6658. (goto-char beg0)
  6659. (catch 'exit
  6660. (while t
  6661. (beginning-of-line 0)
  6662. (if (looking-at "[ \t]*$")
  6663. (if org-empty-line-terminates-plain-lists
  6664. (progn
  6665. (goto-char pos)
  6666. (error "Cannot move this item further up"))
  6667. nil)
  6668. (if (<= (setq ind1 (org-get-indentation)) ind)
  6669. (throw 'exit t)))))
  6670. (condition-case nil
  6671. (org-beginning-of-item)
  6672. (error (goto-char beg)
  6673. (error "Cannot move this item further up")))
  6674. (setq ind1 (org-get-indentation))
  6675. (if (and (org-at-item-p) (= ind ind1))
  6676. (progn
  6677. (setq ne-ins (org-back-over-empty-lines))
  6678. (setq txt (buffer-substring beg end))
  6679. (save-excursion
  6680. (delete-region beg end))
  6681. (setq pos (point))
  6682. (insert txt)
  6683. (setq ins-end (point))
  6684. (goto-char pos) (org-skip-whitespace)
  6685. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6686. ;; Move whitespace back to beginning
  6687. (save-excursion
  6688. (goto-char ins-end)
  6689. (let ((kill-whole-line t))
  6690. (kill-line (- ne-ins ne-beg)) (point)))
  6691. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6692. (org-maybe-renumber-ordered-list))
  6693. (goto-char pos)
  6694. (error "Cannot move this item further up"))))
  6695. (defun org-maybe-renumber-ordered-list ()
  6696. "Renumber the ordered list at point if setup allows it.
  6697. This tests the user option `org-auto-renumber-ordered-lists' before
  6698. doing the renumbering."
  6699. (interactive)
  6700. (when (and org-auto-renumber-ordered-lists
  6701. (org-at-item-p))
  6702. (if (match-beginning 3)
  6703. (org-renumber-ordered-list 1)
  6704. (org-fix-bullet-type))))
  6705. (defun org-maybe-renumber-ordered-list-safe ()
  6706. (condition-case nil
  6707. (save-excursion
  6708. (org-maybe-renumber-ordered-list))
  6709. (error nil)))
  6710. (defun org-cycle-list-bullet (&optional which)
  6711. "Cycle through the different itemize/enumerate bullets.
  6712. This cycle the entire list level through the sequence:
  6713. `-' -> `+' -> `*' -> `1.' -> `1)'
  6714. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6715. 0 meand `-', 1 means `+' etc."
  6716. (interactive "P")
  6717. (org-preserve-lc
  6718. (org-beginning-of-item-list)
  6719. (org-at-item-p)
  6720. (beginning-of-line 1)
  6721. (let ((current (match-string 0))
  6722. (prevp (eq which 'previous))
  6723. new)
  6724. (setq new (cond
  6725. ((and (numberp which)
  6726. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6727. ((string-match "-" current) (if prevp "1)" "+"))
  6728. ((string-match "\\+" current)
  6729. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6730. ((string-match "\\*" current) (if prevp "+" "1."))
  6731. ((string-match "\\." current) (if prevp "*" "1)"))
  6732. ((string-match ")" current) (if prevp "1." "-"))
  6733. (t (error "This should not happen"))))
  6734. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6735. (org-fix-bullet-type)
  6736. (org-maybe-renumber-ordered-list))))
  6737. (defun org-get-string-indentation (s)
  6738. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6739. (let ((n -1) (i 0) (w tab-width) c)
  6740. (catch 'exit
  6741. (while (< (setq n (1+ n)) (length s))
  6742. (setq c (aref s n))
  6743. (cond ((= c ?\ ) (setq i (1+ i)))
  6744. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6745. (t (throw 'exit t)))))
  6746. i))
  6747. (defun org-renumber-ordered-list (arg)
  6748. "Renumber an ordered plain list.
  6749. Cursor needs to be in the first line of an item, the line that starts
  6750. with something like \"1.\" or \"2)\"."
  6751. (interactive "p")
  6752. (unless (and (org-at-item-p)
  6753. (match-beginning 3))
  6754. (error "This is not an ordered list"))
  6755. (let ((line (org-current-line))
  6756. (col (current-column))
  6757. (ind (org-get-string-indentation
  6758. (buffer-substring (point-at-bol) (match-beginning 3))))
  6759. ;; (term (substring (match-string 3) -1))
  6760. ind1 (n (1- arg))
  6761. fmt)
  6762. ;; find where this list begins
  6763. (org-beginning-of-item-list)
  6764. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6765. (setq fmt (concat "%d" (match-string 1)))
  6766. (beginning-of-line 0)
  6767. ;; walk forward and replace these numbers
  6768. (catch 'exit
  6769. (while t
  6770. (catch 'next
  6771. (beginning-of-line 2)
  6772. (if (eobp) (throw 'exit nil))
  6773. (if (looking-at "[ \t]*$") (throw 'next nil))
  6774. (skip-chars-forward " \t") (setq ind1 (current-column))
  6775. (if (> ind1 ind) (throw 'next t))
  6776. (if (< ind1 ind) (throw 'exit t))
  6777. (if (not (org-at-item-p)) (throw 'exit nil))
  6778. (delete-region (match-beginning 2) (match-end 2))
  6779. (goto-char (match-beginning 2))
  6780. (insert (format fmt (setq n (1+ n)))))))
  6781. (goto-line line)
  6782. (move-to-column col)))
  6783. (defun org-fix-bullet-type ()
  6784. "Make sure all items in this list have the same bullet as the firsst item."
  6785. (interactive)
  6786. (unless (org-at-item-p) (error "This is not a list"))
  6787. (let ((line (org-current-line))
  6788. (col (current-column))
  6789. (ind (current-indentation))
  6790. ind1 bullet)
  6791. ;; find where this list begins
  6792. (org-beginning-of-item-list)
  6793. (beginning-of-line 1)
  6794. ;; find out what the bullet type is
  6795. (looking-at "[ \t]*\\(\\S-+\\)")
  6796. (setq bullet (match-string 1))
  6797. ;; walk forward and replace these numbers
  6798. (beginning-of-line 0)
  6799. (catch 'exit
  6800. (while t
  6801. (catch 'next
  6802. (beginning-of-line 2)
  6803. (if (eobp) (throw 'exit nil))
  6804. (if (looking-at "[ \t]*$") (throw 'next nil))
  6805. (skip-chars-forward " \t") (setq ind1 (current-column))
  6806. (if (> ind1 ind) (throw 'next t))
  6807. (if (< ind1 ind) (throw 'exit t))
  6808. (if (not (org-at-item-p)) (throw 'exit nil))
  6809. (skip-chars-forward " \t")
  6810. (looking-at "\\S-+")
  6811. (replace-match bullet))))
  6812. (goto-line line)
  6813. (move-to-column col)
  6814. (if (string-match "[0-9]" bullet)
  6815. (org-renumber-ordered-list 1))))
  6816. (defun org-beginning-of-item-list ()
  6817. "Go to the beginning of the current item list.
  6818. I.e. to the first item in this list."
  6819. (interactive)
  6820. (org-beginning-of-item)
  6821. (let ((pos (point-at-bol))
  6822. (ind (org-get-indentation))
  6823. ind1)
  6824. ;; find where this list begins
  6825. (catch 'exit
  6826. (while t
  6827. (catch 'next
  6828. (beginning-of-line 0)
  6829. (if (looking-at "[ \t]*$")
  6830. (throw (if (bobp) 'exit 'next) t))
  6831. (skip-chars-forward " \t") (setq ind1 (current-column))
  6832. (if (or (< ind1 ind)
  6833. (and (= ind1 ind)
  6834. (not (org-at-item-p)))
  6835. (bobp))
  6836. (throw 'exit t)
  6837. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6838. (goto-char pos)))
  6839. (defun org-end-of-item-list ()
  6840. "Go to the end of the current item list.
  6841. I.e. to the text after the last item."
  6842. (interactive)
  6843. (org-beginning-of-item)
  6844. (let ((pos (point-at-bol))
  6845. (ind (org-get-indentation))
  6846. ind1)
  6847. ;; find where this list begins
  6848. (catch 'exit
  6849. (while t
  6850. (catch 'next
  6851. (beginning-of-line 2)
  6852. (if (looking-at "[ \t]*$")
  6853. (throw (if (eobp) 'exit 'next) t))
  6854. (skip-chars-forward " \t") (setq ind1 (current-column))
  6855. (if (or (< ind1 ind)
  6856. (and (= ind1 ind)
  6857. (not (org-at-item-p)))
  6858. (eobp))
  6859. (progn
  6860. (setq pos (point-at-bol))
  6861. (throw 'exit t))))))
  6862. (goto-char pos)))
  6863. (defvar org-last-indent-begin-marker (make-marker))
  6864. (defvar org-last-indent-end-marker (make-marker))
  6865. (defun org-outdent-item (arg)
  6866. "Outdent a local list item."
  6867. (interactive "p")
  6868. (org-indent-item (- arg)))
  6869. (defun org-indent-item (arg)
  6870. "Indent a local list item."
  6871. (interactive "p")
  6872. (unless (org-at-item-p)
  6873. (error "Not on an item"))
  6874. (save-excursion
  6875. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6876. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6877. (setq beg org-last-indent-begin-marker
  6878. end org-last-indent-end-marker)
  6879. (org-beginning-of-item)
  6880. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6881. (org-end-of-item)
  6882. (setq end (move-marker org-last-indent-end-marker (point))))
  6883. (goto-char beg)
  6884. (setq tmp (org-item-indent-positions)
  6885. ind (car tmp)
  6886. ind-down (nth 2 tmp)
  6887. ind-up (nth 1 tmp)
  6888. delta (if (> arg 0)
  6889. (if ind-down (- ind-down ind) 2)
  6890. (if ind-up (- ind-up ind) -2)))
  6891. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6892. (while (< (point) end)
  6893. (beginning-of-line 1)
  6894. (skip-chars-forward " \t") (setq ind1 (current-column))
  6895. (delete-region (point-at-bol) (point))
  6896. (or (eolp) (indent-to-column (+ ind1 delta)))
  6897. (beginning-of-line 2))))
  6898. (org-fix-bullet-type)
  6899. (org-maybe-renumber-ordered-list-safe)
  6900. (save-excursion
  6901. (beginning-of-line 0)
  6902. (condition-case nil (org-beginning-of-item) (error nil))
  6903. (org-maybe-renumber-ordered-list-safe)))
  6904. (defun org-item-indent-positions ()
  6905. "Return indentation for plain list items.
  6906. This returns a list with three values: The current indentation, the
  6907. parent indentation and the indentation a child should habe.
  6908. Assumes cursor in item line."
  6909. (let* ((bolpos (point-at-bol))
  6910. (ind (org-get-indentation))
  6911. ind-down ind-up pos)
  6912. (save-excursion
  6913. (org-beginning-of-item-list)
  6914. (skip-chars-backward "\n\r \t")
  6915. (when (org-in-item-p)
  6916. (org-beginning-of-item)
  6917. (setq ind-up (org-get-indentation))))
  6918. (setq pos (point))
  6919. (save-excursion
  6920. (cond
  6921. ((and (condition-case nil (progn (org-previous-item) t)
  6922. (error nil))
  6923. (or (forward-char 1) t)
  6924. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6925. (setq ind-down (org-get-indentation)))
  6926. ((and (goto-char pos)
  6927. (org-at-item-p))
  6928. (goto-char (match-end 0))
  6929. (skip-chars-forward " \t")
  6930. (setq ind-down (current-column)))))
  6931. (list ind ind-up ind-down)))
  6932. ;;; The orgstruct minor mode
  6933. ;; Define a minor mode which can be used in other modes in order to
  6934. ;; integrate the org-mode structure editing commands.
  6935. ;; This is really a hack, because the org-mode structure commands use
  6936. ;; keys which normally belong to the major mode. Here is how it
  6937. ;; works: The minor mode defines all the keys necessary to operate the
  6938. ;; structure commands, but wraps the commands into a function which
  6939. ;; tests if the cursor is currently at a headline or a plain list
  6940. ;; item. If that is the case, the structure command is used,
  6941. ;; temporarily setting many Org-mode variables like regular
  6942. ;; expressions for filling etc. However, when any of those keys is
  6943. ;; used at a different location, function uses `key-binding' to look
  6944. ;; up if the key has an associated command in another currently active
  6945. ;; keymap (minor modes, major mode, global), and executes that
  6946. ;; command. There might be problems if any of the keys is otherwise
  6947. ;; used as a prefix key.
  6948. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6949. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6950. ;; addresses this by checking explicitly for both bindings.
  6951. (defvar orgstruct-mode-map (make-sparse-keymap)
  6952. "Keymap for the minor `orgstruct-mode'.")
  6953. (defvar org-local-vars nil
  6954. "List of local variables, for use by `orgstruct-mode'")
  6955. ;;;###autoload
  6956. (define-minor-mode orgstruct-mode
  6957. "Toggle the minor more `orgstruct-mode'.
  6958. This mode is for using Org-mode structure commands in other modes.
  6959. The following key behave as if Org-mode was active, if the cursor
  6960. is on a headline, or on a plain list item (both in the definition
  6961. of Org-mode).
  6962. M-up Move entry/item up
  6963. M-down Move entry/item down
  6964. M-left Promote
  6965. M-right Demote
  6966. M-S-up Move entry/item up
  6967. M-S-down Move entry/item down
  6968. M-S-left Promote subtree
  6969. M-S-right Demote subtree
  6970. M-q Fill paragraph and items like in Org-mode
  6971. C-c ^ Sort entries
  6972. C-c - Cycle list bullet
  6973. TAB Cycle item visibility
  6974. M-RET Insert new heading/item
  6975. S-M-RET Insert new TODO heading / Chekbox item
  6976. C-c C-c Set tags / toggle checkbox"
  6977. nil " OrgStruct" nil
  6978. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6979. ;;;###autoload
  6980. (defun turn-on-orgstruct ()
  6981. "Unconditionally turn on `orgstruct-mode'."
  6982. (orgstruct-mode 1))
  6983. ;;;###autoload
  6984. (defun turn-on-orgstruct++ ()
  6985. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6986. In addition to setting orgstruct-mode, this also exports all indentation and
  6987. autofilling variables from org-mode into the buffer. Note that turning
  6988. off orgstruct-mode will *not* remove these additional settings."
  6989. (orgstruct-mode 1)
  6990. (let (var val)
  6991. (mapc
  6992. (lambda (x)
  6993. (when (string-match
  6994. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6995. (symbol-name (car x)))
  6996. (setq var (car x) val (nth 1 x))
  6997. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6998. org-local-vars)))
  6999. (defun orgstruct-error ()
  7000. "Error when there is no default binding for a structure key."
  7001. (interactive)
  7002. (error "This key has no function outside structure elements"))
  7003. (defun orgstruct-setup ()
  7004. "Setup orgstruct keymaps."
  7005. (let ((nfunc 0)
  7006. (bindings
  7007. (list
  7008. '([(meta up)] org-metaup)
  7009. '([(meta down)] org-metadown)
  7010. '([(meta left)] org-metaleft)
  7011. '([(meta right)] org-metaright)
  7012. '([(meta shift up)] org-shiftmetaup)
  7013. '([(meta shift down)] org-shiftmetadown)
  7014. '([(meta shift left)] org-shiftmetaleft)
  7015. '([(meta shift right)] org-shiftmetaright)
  7016. '([(shift up)] org-shiftup)
  7017. '([(shift down)] org-shiftdown)
  7018. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7019. '("\M-q" fill-paragraph)
  7020. '("\C-c^" org-sort)
  7021. '("\C-c-" org-cycle-list-bullet)))
  7022. elt key fun cmd)
  7023. (while (setq elt (pop bindings))
  7024. (setq nfunc (1+ nfunc))
  7025. (setq key (org-key (car elt))
  7026. fun (nth 1 elt)
  7027. cmd (orgstruct-make-binding fun nfunc key))
  7028. (org-defkey orgstruct-mode-map key cmd))
  7029. ;; Special treatment needed for TAB and RET
  7030. (org-defkey orgstruct-mode-map [(tab)]
  7031. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7032. (org-defkey orgstruct-mode-map "\C-i"
  7033. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7034. (org-defkey orgstruct-mode-map "\M-\C-m"
  7035. (orgstruct-make-binding 'org-insert-heading 105
  7036. "\M-\C-m" [(meta return)]))
  7037. (org-defkey orgstruct-mode-map [(meta return)]
  7038. (orgstruct-make-binding 'org-insert-heading 106
  7039. [(meta return)] "\M-\C-m"))
  7040. (org-defkey orgstruct-mode-map [(shift meta return)]
  7041. (orgstruct-make-binding 'org-insert-todo-heading 107
  7042. [(meta return)] "\M-\C-m"))
  7043. (unless org-local-vars
  7044. (setq org-local-vars (org-get-local-variables)))
  7045. t))
  7046. (defun orgstruct-make-binding (fun n &rest keys)
  7047. "Create a function for binding in the structure minor mode.
  7048. FUN is the command to call inside a table. N is used to create a unique
  7049. command name. KEYS are keys that should be checked in for a command
  7050. to execute outside of tables."
  7051. (eval
  7052. (list 'defun
  7053. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7054. '(arg)
  7055. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7056. "Outside of structure, run the binding of `"
  7057. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7058. "'.")
  7059. '(interactive "p")
  7060. (list 'if
  7061. '(org-context-p 'headline 'item)
  7062. (list 'org-run-like-in-org-mode (list 'quote fun))
  7063. (list 'let '(orgstruct-mode)
  7064. (list 'call-interactively
  7065. (append '(or)
  7066. (mapcar (lambda (k)
  7067. (list 'key-binding k))
  7068. keys)
  7069. '('orgstruct-error))))))))
  7070. (defun org-context-p (&rest contexts)
  7071. "Check if local context is and of CONTEXTS.
  7072. Possible values in the list of contexts are `table', `headline', and `item'."
  7073. (let ((pos (point)))
  7074. (goto-char (point-at-bol))
  7075. (prog1 (or (and (memq 'table contexts)
  7076. (looking-at "[ \t]*|"))
  7077. (and (memq 'headline contexts)
  7078. (looking-at "\\*+"))
  7079. (and (memq 'item contexts)
  7080. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7081. (goto-char pos))))
  7082. (defun org-get-local-variables ()
  7083. "Return a list of all local variables in an org-mode buffer."
  7084. (let (varlist)
  7085. (with-current-buffer (get-buffer-create "*Org tmp*")
  7086. (erase-buffer)
  7087. (org-mode)
  7088. (setq varlist (buffer-local-variables)))
  7089. (kill-buffer "*Org tmp*")
  7090. (delq nil
  7091. (mapcar
  7092. (lambda (x)
  7093. (setq x
  7094. (if (symbolp x)
  7095. (list x)
  7096. (list (car x) (list 'quote (cdr x)))))
  7097. (if (string-match
  7098. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7099. (symbol-name (car x)))
  7100. x nil))
  7101. varlist))))
  7102. ;;;###autoload
  7103. (defun org-run-like-in-org-mode (cmd)
  7104. (unless org-local-vars
  7105. (setq org-local-vars (org-get-local-variables)))
  7106. (eval (list 'let org-local-vars
  7107. (list 'call-interactively (list 'quote cmd)))))
  7108. ;;;; Archiving
  7109. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7110. (defun org-archive-subtree (&optional find-done)
  7111. "Move the current subtree to the archive.
  7112. The archive can be a certain top-level heading in the current file, or in
  7113. a different file. The tree will be moved to that location, the subtree
  7114. heading be marked DONE, and the current time will be added.
  7115. When called with prefix argument FIND-DONE, find whole trees without any
  7116. open TODO items and archive them (after getting confirmation from the user).
  7117. If the cursor is not at a headline when this comand is called, try all level
  7118. 1 trees. If the cursor is on a headline, only try the direct children of
  7119. this heading."
  7120. (interactive "P")
  7121. (if find-done
  7122. (org-archive-all-done)
  7123. ;; Save all relevant TODO keyword-relatex variables
  7124. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7125. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7126. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7127. (tr-org-done-keywords org-done-keywords)
  7128. (tr-org-todo-regexp org-todo-regexp)
  7129. (tr-org-todo-line-regexp org-todo-line-regexp)
  7130. (tr-org-odd-levels-only org-odd-levels-only)
  7131. (this-buffer (current-buffer))
  7132. (org-archive-location org-archive-location)
  7133. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7134. ;; start of variables that will be used for saving context
  7135. ;; The compiler complains about them - keep them anyway!
  7136. (file (abbreviate-file-name (buffer-file-name)))
  7137. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7138. (time (format-time-string
  7139. (substring (cdr org-time-stamp-formats) 1 -1)
  7140. (current-time)))
  7141. afile heading buffer level newfile-p
  7142. category todo priority
  7143. ;; start of variables that will be used for savind context
  7144. ltags itags prop)
  7145. ;; Try to find a local archive location
  7146. (save-excursion
  7147. (save-restriction
  7148. (widen)
  7149. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7150. (if (and prop (string-match "\\S-" prop))
  7151. (setq org-archive-location prop)
  7152. (if (or (re-search-backward re nil t)
  7153. (re-search-forward re nil t))
  7154. (setq org-archive-location (match-string 1))))))
  7155. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7156. (progn
  7157. (setq afile (format (match-string 1 org-archive-location)
  7158. (file-name-nondirectory buffer-file-name))
  7159. heading (match-string 2 org-archive-location)))
  7160. (error "Invalid `org-archive-location'"))
  7161. (if (> (length afile) 0)
  7162. (setq newfile-p (not (file-exists-p afile))
  7163. buffer (find-file-noselect afile))
  7164. (setq buffer (current-buffer)))
  7165. (unless buffer
  7166. (error "Cannot access file \"%s\"" afile))
  7167. (if (and (> (length heading) 0)
  7168. (string-match "^\\*+" heading))
  7169. (setq level (match-end 0))
  7170. (setq heading nil level 0))
  7171. (save-excursion
  7172. (org-back-to-heading t)
  7173. ;; Get context information that will be lost by moving the tree
  7174. (org-refresh-category-properties)
  7175. (setq category (org-get-category)
  7176. todo (and (looking-at org-todo-line-regexp)
  7177. (match-string 2))
  7178. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7179. ltags (org-get-tags)
  7180. itags (org-delete-all ltags (org-get-tags-at)))
  7181. (setq ltags (mapconcat 'identity ltags " ")
  7182. itags (mapconcat 'identity itags " "))
  7183. ;; We first only copy, in case something goes wrong
  7184. ;; we need to protect this-command, to avoid kill-region sets it,
  7185. ;; which would lead to duplication of subtrees
  7186. (let (this-command) (org-copy-subtree))
  7187. (set-buffer buffer)
  7188. ;; Enforce org-mode for the archive buffer
  7189. (if (not (org-mode-p))
  7190. ;; Force the mode for future visits.
  7191. (let ((org-insert-mode-line-in-empty-file t)
  7192. (org-inhibit-startup t))
  7193. (call-interactively 'org-mode)))
  7194. (when newfile-p
  7195. (goto-char (point-max))
  7196. (insert (format "\nArchived entries from file %s\n\n"
  7197. (buffer-file-name this-buffer))))
  7198. ;; Force the TODO keywords of the original buffer
  7199. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7200. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7201. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7202. (org-done-keywords tr-org-done-keywords)
  7203. (org-todo-regexp tr-org-todo-regexp)
  7204. (org-todo-line-regexp tr-org-todo-line-regexp)
  7205. (org-odd-levels-only
  7206. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7207. org-odd-levels-only
  7208. tr-org-odd-levels-only)))
  7209. (goto-char (point-min))
  7210. (if heading
  7211. (progn
  7212. (if (re-search-forward
  7213. (concat "^" (regexp-quote heading)
  7214. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7215. nil t)
  7216. (goto-char (match-end 0))
  7217. ;; Heading not found, just insert it at the end
  7218. (goto-char (point-max))
  7219. (or (bolp) (insert "\n"))
  7220. (insert "\n" heading "\n")
  7221. (end-of-line 0))
  7222. ;; Make the subtree visible
  7223. (show-subtree)
  7224. (org-end-of-subtree t)
  7225. (skip-chars-backward " \t\r\n")
  7226. (and (looking-at "[ \t\r\n]*")
  7227. (replace-match "\n\n")))
  7228. ;; No specific heading, just go to end of file.
  7229. (goto-char (point-max)) (insert "\n"))
  7230. ;; Paste
  7231. (org-paste-subtree (org-get-legal-level level 1))
  7232. ;; Mark the entry as done
  7233. (when (and org-archive-mark-done
  7234. (looking-at org-todo-line-regexp)
  7235. (or (not (match-end 2))
  7236. (not (member (match-string 2) org-done-keywords))))
  7237. (let (org-log-done org-todo-log-states)
  7238. (org-todo
  7239. (car (or (member org-archive-mark-done org-done-keywords)
  7240. org-done-keywords)))))
  7241. ;; Add the context info
  7242. (when org-archive-save-context-info
  7243. (let ((l org-archive-save-context-info) e n v)
  7244. (while (setq e (pop l))
  7245. (when (and (setq v (symbol-value e))
  7246. (stringp v) (string-match "\\S-" v))
  7247. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7248. (org-entry-put (point) n v)))))
  7249. ;; Save the buffer, if it is not the same buffer.
  7250. (if (not (eq this-buffer buffer)) (save-buffer))))
  7251. ;; Here we are back in the original buffer. Everything seems to have
  7252. ;; worked. So now cut the tree and finish up.
  7253. (let (this-command) (org-cut-subtree))
  7254. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7255. (message "Subtree archived %s"
  7256. (if (eq this-buffer buffer)
  7257. (concat "under heading: " heading)
  7258. (concat "in file: " (abbreviate-file-name afile)))))))
  7259. (defun org-refresh-category-properties ()
  7260. "Refresh category text properties in teh buffer."
  7261. (let ((def-cat (cond
  7262. ((null org-category)
  7263. (if buffer-file-name
  7264. (file-name-sans-extension
  7265. (file-name-nondirectory buffer-file-name))
  7266. "???"))
  7267. ((symbolp org-category) (symbol-name org-category))
  7268. (t org-category)))
  7269. beg end cat pos optionp)
  7270. (org-unmodified
  7271. (save-excursion
  7272. (save-restriction
  7273. (widen)
  7274. (goto-char (point-min))
  7275. (put-text-property (point) (point-max) 'org-category def-cat)
  7276. (while (re-search-forward
  7277. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7278. (setq pos (match-end 0)
  7279. optionp (equal (char-after (match-beginning 0)) ?#)
  7280. cat (org-trim (match-string 2)))
  7281. (if optionp
  7282. (setq beg (point-at-bol) end (point-max))
  7283. (org-back-to-heading t)
  7284. (setq beg (point) end (org-end-of-subtree t t)))
  7285. (put-text-property beg end 'org-category cat)
  7286. (goto-char pos)))))))
  7287. (defun org-archive-all-done (&optional tag)
  7288. "Archive sublevels of the current tree without open TODO items.
  7289. If the cursor is not on a headline, try all level 1 trees. If
  7290. it is on a headline, try all direct children.
  7291. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7292. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7293. (rea (concat ".*:" org-archive-tag ":"))
  7294. (begm (make-marker))
  7295. (endm (make-marker))
  7296. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7297. "Move subtree to archive (no open TODO items)? "))
  7298. beg end (cntarch 0))
  7299. (if (org-on-heading-p)
  7300. (progn
  7301. (setq re1 (concat "^" (regexp-quote
  7302. (make-string
  7303. (1+ (- (match-end 0) (match-beginning 0) 1))
  7304. ?*))
  7305. " "))
  7306. (move-marker begm (point))
  7307. (move-marker endm (org-end-of-subtree t)))
  7308. (setq re1 "^* ")
  7309. (move-marker begm (point-min))
  7310. (move-marker endm (point-max)))
  7311. (save-excursion
  7312. (goto-char begm)
  7313. (while (re-search-forward re1 endm t)
  7314. (setq beg (match-beginning 0)
  7315. end (save-excursion (org-end-of-subtree t) (point)))
  7316. (goto-char beg)
  7317. (if (re-search-forward re end t)
  7318. (goto-char end)
  7319. (goto-char beg)
  7320. (if (and (or (not tag) (not (looking-at rea)))
  7321. (y-or-n-p question))
  7322. (progn
  7323. (if tag
  7324. (org-toggle-tag org-archive-tag 'on)
  7325. (org-archive-subtree))
  7326. (setq cntarch (1+ cntarch)))
  7327. (goto-char end)))))
  7328. (message "%d trees archived" cntarch)))
  7329. (defun org-cycle-hide-drawers (state)
  7330. "Re-hide all drawers after a visibility state change."
  7331. (when (and (org-mode-p)
  7332. (not (memq state '(overview folded))))
  7333. (save-excursion
  7334. (let* ((globalp (memq state '(contents all)))
  7335. (beg (if globalp (point-min) (point)))
  7336. (end (if globalp (point-max) (org-end-of-subtree t))))
  7337. (goto-char beg)
  7338. (while (re-search-forward org-drawer-regexp end t)
  7339. (org-flag-drawer t))))))
  7340. (defun org-flag-drawer (flag)
  7341. (save-excursion
  7342. (beginning-of-line 1)
  7343. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7344. (let ((b (match-end 0))
  7345. (outline-regexp org-outline-regexp))
  7346. (if (re-search-forward
  7347. "^[ \t]*:END:"
  7348. (save-excursion (outline-next-heading) (point)) t)
  7349. (outline-flag-region b (point-at-eol) flag)
  7350. (error ":END: line missing"))))))
  7351. (defun org-cycle-hide-archived-subtrees (state)
  7352. "Re-hide all archived subtrees after a visibility state change."
  7353. (when (and (not org-cycle-open-archived-trees)
  7354. (not (memq state '(overview folded))))
  7355. (save-excursion
  7356. (let* ((globalp (memq state '(contents all)))
  7357. (beg (if globalp (point-min) (point)))
  7358. (end (if globalp (point-max) (org-end-of-subtree t))))
  7359. (org-hide-archived-subtrees beg end)
  7360. (goto-char beg)
  7361. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7362. (message "%s" (substitute-command-keys
  7363. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7364. (defun org-force-cycle-archived ()
  7365. "Cycle subtree even if it is archived."
  7366. (interactive)
  7367. (setq this-command 'org-cycle)
  7368. (let ((org-cycle-open-archived-trees t))
  7369. (call-interactively 'org-cycle)))
  7370. (defun org-hide-archived-subtrees (beg end)
  7371. "Re-hide all archived subtrees after a visibility state change."
  7372. (save-excursion
  7373. (let* ((re (concat ":" org-archive-tag ":")))
  7374. (goto-char beg)
  7375. (while (re-search-forward re end t)
  7376. (and (org-on-heading-p) (hide-subtree))
  7377. (org-end-of-subtree t)))))
  7378. (defun org-toggle-tag (tag &optional onoff)
  7379. "Toggle the tag TAG for the current line.
  7380. If ONOFF is `on' or `off', don't toggle but set to this state."
  7381. (unless (org-on-heading-p t) (error "Not on headling"))
  7382. (let (res current)
  7383. (save-excursion
  7384. (beginning-of-line)
  7385. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7386. (point-at-eol) t)
  7387. (progn
  7388. (setq current (match-string 1))
  7389. (replace-match ""))
  7390. (setq current ""))
  7391. (setq current (nreverse (org-split-string current ":")))
  7392. (cond
  7393. ((eq onoff 'on)
  7394. (setq res t)
  7395. (or (member tag current) (push tag current)))
  7396. ((eq onoff 'off)
  7397. (or (not (member tag current)) (setq current (delete tag current))))
  7398. (t (if (member tag current)
  7399. (setq current (delete tag current))
  7400. (setq res t)
  7401. (push tag current))))
  7402. (end-of-line 1)
  7403. (if current
  7404. (progn
  7405. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7406. (org-set-tags nil t))
  7407. (delete-horizontal-space))
  7408. (run-hooks 'org-after-tags-change-hook))
  7409. res))
  7410. (defun org-toggle-archive-tag (&optional arg)
  7411. "Toggle the archive tag for the current headline.
  7412. With prefix ARG, check all children of current headline and offer tagging
  7413. the children that do not contain any open TODO items."
  7414. (interactive "P")
  7415. (if arg
  7416. (org-archive-all-done 'tag)
  7417. (let (set)
  7418. (save-excursion
  7419. (org-back-to-heading t)
  7420. (setq set (org-toggle-tag org-archive-tag))
  7421. (when set (hide-subtree)))
  7422. (and set (beginning-of-line 1))
  7423. (message "Subtree %s" (if set "archived" "unarchived")))))
  7424. ;;;; Tables
  7425. ;;; The table editor
  7426. ;; Watch out: Here we are talking about two different kind of tables.
  7427. ;; Most of the code is for the tables created with the Org-mode table editor.
  7428. ;; Sometimes, we talk about tables created and edited with the table.el
  7429. ;; Emacs package. We call the former org-type tables, and the latter
  7430. ;; table.el-type tables.
  7431. (defun org-before-change-function (beg end)
  7432. "Every change indicates that a table might need an update."
  7433. (setq org-table-may-need-update t))
  7434. (defconst org-table-line-regexp "^[ \t]*|"
  7435. "Detects an org-type table line.")
  7436. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7437. "Detects an org-type table line.")
  7438. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7439. "Detects a table line marked for automatic recalculation.")
  7440. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7441. "Detects a table line marked for automatic recalculation.")
  7442. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7443. "Detects a table line marked for automatic recalculation.")
  7444. (defconst org-table-hline-regexp "^[ \t]*|-"
  7445. "Detects an org-type table hline.")
  7446. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7447. "Detects a table-type table hline.")
  7448. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7449. "Detects an org-type or table-type table.")
  7450. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7451. "Searching from within a table (any type) this finds the first line
  7452. outside the table.")
  7453. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7454. "Searching from within a table (any type) this finds the first line
  7455. outside the table.")
  7456. (defvar org-table-last-highlighted-reference nil)
  7457. (defvar org-table-formula-history nil)
  7458. (defvar org-table-column-names nil
  7459. "Alist with column names, derived from the `!' line.")
  7460. (defvar org-table-column-name-regexp nil
  7461. "Regular expression matching the current column names.")
  7462. (defvar org-table-local-parameters nil
  7463. "Alist with parameter names, derived from the `$' line.")
  7464. (defvar org-table-named-field-locations nil
  7465. "Alist with locations of named fields.")
  7466. (defvar org-table-current-line-types nil
  7467. "Table row types, non-nil only for the duration of a comand.")
  7468. (defvar org-table-current-begin-line nil
  7469. "Table begin line, non-nil only for the duration of a comand.")
  7470. (defvar org-table-current-begin-pos nil
  7471. "Table begin position, non-nil only for the duration of a comand.")
  7472. (defvar org-table-dlines nil
  7473. "Vector of data line line numbers in the current table.")
  7474. (defvar org-table-hlines nil
  7475. "Vector of hline line numbers in the current table.")
  7476. (defconst org-table-range-regexp
  7477. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7478. ;; 1 2 3 4 5
  7479. "Regular expression for matching ranges in formulas.")
  7480. (defconst org-table-range-regexp2
  7481. (concat
  7482. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7483. "\\.\\."
  7484. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7485. "Match a range for reference display.")
  7486. (defconst org-table-translate-regexp
  7487. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7488. "Match a reference that needs translation, for reference display.")
  7489. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7490. (defun org-table-create-with-table.el ()
  7491. "Use the table.el package to insert a new table.
  7492. If there is already a table at point, convert between Org-mode tables
  7493. and table.el tables."
  7494. (interactive)
  7495. (require 'table)
  7496. (cond
  7497. ((org-at-table.el-p)
  7498. (if (y-or-n-p "Convert table to Org-mode table? ")
  7499. (org-table-convert)))
  7500. ((org-at-table-p)
  7501. (if (y-or-n-p "Convert table to table.el table? ")
  7502. (org-table-convert)))
  7503. (t (call-interactively 'table-insert))))
  7504. (defun org-table-create-or-convert-from-region (arg)
  7505. "Convert region to table, or create an empty table.
  7506. If there is an active region, convert it to a table, using the function
  7507. `org-table-convert-region'. See the documentation of that function
  7508. to learn how the prefix argument is interpreted to determine the field
  7509. separator.
  7510. If there is no such region, create an empty table with `org-table-create'."
  7511. (interactive "P")
  7512. (if (org-region-active-p)
  7513. (org-table-convert-region (region-beginning) (region-end) arg)
  7514. (org-table-create arg)))
  7515. (defun org-table-create (&optional size)
  7516. "Query for a size and insert a table skeleton.
  7517. SIZE is a string Columns x Rows like for example \"3x2\"."
  7518. (interactive "P")
  7519. (unless size
  7520. (setq size (read-string
  7521. (concat "Table size Columns x Rows [e.g. "
  7522. org-table-default-size "]: ")
  7523. "" nil org-table-default-size)))
  7524. (let* ((pos (point))
  7525. (indent (make-string (current-column) ?\ ))
  7526. (split (org-split-string size " *x *"))
  7527. (rows (string-to-number (nth 1 split)))
  7528. (columns (string-to-number (car split)))
  7529. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7530. "\n")))
  7531. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7532. (point-at-bol) (point)))
  7533. (beginning-of-line 1)
  7534. (newline))
  7535. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7536. (dotimes (i rows) (insert line))
  7537. (goto-char pos)
  7538. (if (> rows 1)
  7539. ;; Insert a hline after the first row.
  7540. (progn
  7541. (end-of-line 1)
  7542. (insert "\n|-")
  7543. (goto-char pos)))
  7544. (org-table-align)))
  7545. (defun org-table-convert-region (beg0 end0 &optional separator)
  7546. "Convert region to a table.
  7547. The region goes from BEG0 to END0, but these borders will be moved
  7548. slightly, to make sure a beginning of line in the first line is included.
  7549. SEPARATOR specifies the field separator in the lines. It can have the
  7550. following values:
  7551. '(4) Use the comma as a field separator
  7552. '(16) Use a TAB as field separator
  7553. integer When a number, use that many spaces as field separator
  7554. nil When nil, the command tries to be smart and figure out the
  7555. separator in the following way:
  7556. - when each line contains a TAB, assume TAB-separated material
  7557. - when each line contains a comme, assume CSV material
  7558. - else, assume one or more SPACE charcters as separator."
  7559. (interactive "rP")
  7560. (let* ((beg (min beg0 end0))
  7561. (end (max beg0 end0))
  7562. re)
  7563. (goto-char beg)
  7564. (beginning-of-line 1)
  7565. (setq beg (move-marker (make-marker) (point)))
  7566. (goto-char end)
  7567. (if (bolp) (backward-char 1) (end-of-line 1))
  7568. (setq end (move-marker (make-marker) (point)))
  7569. ;; Get the right field separator
  7570. (unless separator
  7571. (goto-char beg)
  7572. (setq separator
  7573. (cond
  7574. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7575. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7576. (t 1))))
  7577. (setq re (cond
  7578. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7579. ((equal separator '(16)) "^\\|\t")
  7580. ((integerp separator)
  7581. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7582. (t (error "This should not happen"))))
  7583. (goto-char beg)
  7584. (while (re-search-forward re end t)
  7585. (replace-match "| " t t))
  7586. (goto-char beg)
  7587. (insert " ")
  7588. (org-table-align)))
  7589. (defun org-table-import (file arg)
  7590. "Import FILE as a table.
  7591. The file is assumed to be tab-separated. Such files can be produced by most
  7592. spreadsheet and database applications. If no tabs (at least one per line)
  7593. are found, lines will be split on whitespace into fields."
  7594. (interactive "f\nP")
  7595. (or (bolp) (newline))
  7596. (let ((beg (point))
  7597. (pm (point-max)))
  7598. (insert-file-contents file)
  7599. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7600. (defun org-table-export ()
  7601. "Export table as a tab-separated file.
  7602. Such a file can be imported into a spreadsheet program like Excel."
  7603. (interactive)
  7604. (let* ((beg (org-table-begin))
  7605. (end (org-table-end))
  7606. (table (buffer-substring beg end))
  7607. (file (read-file-name "Export table to: "))
  7608. buf)
  7609. (unless (or (not (file-exists-p file))
  7610. (y-or-n-p (format "Overwrite file %s? " file)))
  7611. (error "Abort"))
  7612. (with-current-buffer (find-file-noselect file)
  7613. (setq buf (current-buffer))
  7614. (erase-buffer)
  7615. (fundamental-mode)
  7616. (insert table)
  7617. (goto-char (point-min))
  7618. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7619. (replace-match "" t t)
  7620. (end-of-line 1))
  7621. (goto-char (point-min))
  7622. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7623. (replace-match "" t t)
  7624. (goto-char (min (1+ (point)) (point-max))))
  7625. (goto-char (point-min))
  7626. (while (re-search-forward "^-[-+]*$" nil t)
  7627. (replace-match "")
  7628. (if (looking-at "\n")
  7629. (delete-char 1)))
  7630. (goto-char (point-min))
  7631. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7632. (replace-match "\t" t t))
  7633. (save-buffer))
  7634. (kill-buffer buf)))
  7635. (defvar org-table-aligned-begin-marker (make-marker)
  7636. "Marker at the beginning of the table last aligned.
  7637. Used to check if cursor still is in that table, to minimize realignment.")
  7638. (defvar org-table-aligned-end-marker (make-marker)
  7639. "Marker at the end of the table last aligned.
  7640. Used to check if cursor still is in that table, to minimize realignment.")
  7641. (defvar org-table-last-alignment nil
  7642. "List of flags for flushright alignment, from the last re-alignment.
  7643. This is being used to correctly align a single field after TAB or RET.")
  7644. (defvar org-table-last-column-widths nil
  7645. "List of max width of fields in each column.
  7646. This is being used to correctly align a single field after TAB or RET.")
  7647. (defvar org-table-overlay-coordinates nil
  7648. "Overlay coordinates after each align of a table.")
  7649. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7650. (defvar org-last-recalc-line nil)
  7651. (defconst org-narrow-column-arrow "=>"
  7652. "Used as display property in narrowed table columns.")
  7653. (defun org-table-align ()
  7654. "Align the table at point by aligning all vertical bars."
  7655. (interactive)
  7656. (let* (
  7657. ;; Limits of table
  7658. (beg (org-table-begin))
  7659. (end (org-table-end))
  7660. ;; Current cursor position
  7661. (linepos (org-current-line))
  7662. (colpos (org-table-current-column))
  7663. (winstart (window-start))
  7664. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7665. lines (new "") lengths l typenums ty fields maxfields i
  7666. column
  7667. (indent "") cnt frac
  7668. rfmt hfmt
  7669. (spaces '(1 . 1))
  7670. (sp1 (car spaces))
  7671. (sp2 (cdr spaces))
  7672. (rfmt1 (concat
  7673. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7674. (hfmt1 (concat
  7675. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7676. emptystrings links dates emph narrow fmax f1 len c e)
  7677. (untabify beg end)
  7678. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7679. ;; Check if we have links or dates
  7680. (goto-char beg)
  7681. (setq links (re-search-forward org-bracket-link-regexp end t))
  7682. (goto-char beg)
  7683. (setq emph (and org-hide-emphasis-markers
  7684. (re-search-forward org-emph-re end t)))
  7685. (goto-char beg)
  7686. (setq dates (and org-display-custom-times
  7687. (re-search-forward org-ts-regexp-both end t)))
  7688. ;; Make sure the link properties are right
  7689. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7690. ;; Make sure the date properties are right
  7691. (when dates (goto-char beg) (while (org-activate-dates end)))
  7692. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7693. ;; Check if we are narrowing any columns
  7694. (goto-char beg)
  7695. (setq narrow (and org-format-transports-properties-p
  7696. (re-search-forward "<[0-9]+>" end t)))
  7697. ;; Get the rows
  7698. (setq lines (org-split-string
  7699. (buffer-substring beg end) "\n"))
  7700. ;; Store the indentation of the first line
  7701. (if (string-match "^ *" (car lines))
  7702. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7703. ;; Mark the hlines by setting the corresponding element to nil
  7704. ;; At the same time, we remove trailing space.
  7705. (setq lines (mapcar (lambda (l)
  7706. (if (string-match "^ *|-" l)
  7707. nil
  7708. (if (string-match "[ \t]+$" l)
  7709. (substring l 0 (match-beginning 0))
  7710. l)))
  7711. lines))
  7712. ;; Get the data fields by splitting the lines.
  7713. (setq fields (mapcar
  7714. (lambda (l)
  7715. (org-split-string l " *| *"))
  7716. (delq nil (copy-sequence lines))))
  7717. ;; How many fields in the longest line?
  7718. (condition-case nil
  7719. (setq maxfields (apply 'max (mapcar 'length fields)))
  7720. (error
  7721. (kill-region beg end)
  7722. (org-table-create org-table-default-size)
  7723. (error "Empty table - created default table")))
  7724. ;; A list of empty strings to fill any short rows on output
  7725. (setq emptystrings (make-list maxfields ""))
  7726. ;; Check for special formatting.
  7727. (setq i -1)
  7728. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7729. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7730. ;; Check if there is an explicit width specified
  7731. (when narrow
  7732. (setq c column fmax nil)
  7733. (while c
  7734. (setq e (pop c))
  7735. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7736. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7737. ;; Find fields that are wider than fmax, and shorten them
  7738. (when fmax
  7739. (loop for xx in column do
  7740. (when (and (stringp xx)
  7741. (> (org-string-width xx) fmax))
  7742. (org-add-props xx nil
  7743. 'help-echo
  7744. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7745. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7746. (unless (> f1 1)
  7747. (error "Cannot narrow field starting with wide link \"%s\""
  7748. (match-string 0 xx)))
  7749. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7750. (add-text-properties (- f1 2) f1
  7751. (list 'display org-narrow-column-arrow)
  7752. xx)))))
  7753. ;; Get the maximum width for each column
  7754. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7755. ;; Get the fraction of numbers, to decide about alignment of the column
  7756. (setq cnt 0 frac 0.0)
  7757. (loop for x in column do
  7758. (if (equal x "")
  7759. nil
  7760. (setq frac ( / (+ (* frac cnt)
  7761. (if (string-match org-table-number-regexp x) 1 0))
  7762. (setq cnt (1+ cnt))))))
  7763. (push (>= frac org-table-number-fraction) typenums))
  7764. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7765. ;; Store the alignment of this table, for later editing of single fields
  7766. (setq org-table-last-alignment typenums
  7767. org-table-last-column-widths lengths)
  7768. ;; With invisible characters, `format' does not get the field width right
  7769. ;; So we need to make these fields wide by hand.
  7770. (when (or links emph)
  7771. (loop for i from 0 upto (1- maxfields) do
  7772. (setq len (nth i lengths))
  7773. (loop for j from 0 upto (1- (length fields)) do
  7774. (setq c (nthcdr i (car (nthcdr j fields))))
  7775. (if (and (stringp (car c))
  7776. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7777. ; (string-match org-bracket-link-regexp (car c))
  7778. (< (org-string-width (car c)) len))
  7779. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7780. ;; Compute the formats needed for output of the table
  7781. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7782. (while (setq l (pop lengths))
  7783. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7784. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7785. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7786. (setq rfmt (concat rfmt "\n")
  7787. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7788. (setq new (mapconcat
  7789. (lambda (l)
  7790. (if l (apply 'format rfmt
  7791. (append (pop fields) emptystrings))
  7792. hfmt))
  7793. lines ""))
  7794. ;; Replace the old one
  7795. (delete-region beg end)
  7796. (move-marker end nil)
  7797. (move-marker org-table-aligned-begin-marker (point))
  7798. (insert new)
  7799. (move-marker org-table-aligned-end-marker (point))
  7800. (when (and orgtbl-mode (not (org-mode-p)))
  7801. (goto-char org-table-aligned-begin-marker)
  7802. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7803. ;; Try to move to the old location
  7804. (goto-line winstartline)
  7805. (setq winstart (point-at-bol))
  7806. (goto-line linepos)
  7807. (set-window-start (selected-window) winstart 'noforce)
  7808. (org-table-goto-column colpos)
  7809. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7810. (setq org-table-may-need-update nil)
  7811. ))
  7812. (defun org-string-width (s)
  7813. "Compute width of string, ignoring invisible characters.
  7814. This ignores character with invisibility property `org-link', and also
  7815. characters with property `org-cwidth', because these will become invisible
  7816. upon the next fontification round."
  7817. (let (b l)
  7818. (when (or (eq t buffer-invisibility-spec)
  7819. (assq 'org-link buffer-invisibility-spec))
  7820. (while (setq b (text-property-any 0 (length s)
  7821. 'invisible 'org-link s))
  7822. (setq s (concat (substring s 0 b)
  7823. (substring s (or (next-single-property-change
  7824. b 'invisible s) (length s)))))))
  7825. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7826. (setq s (concat (substring s 0 b)
  7827. (substring s (or (next-single-property-change
  7828. b 'org-cwidth s) (length s))))))
  7829. (setq l (string-width s) b -1)
  7830. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7831. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7832. l))
  7833. (defun org-table-begin (&optional table-type)
  7834. "Find the beginning of the table and return its position.
  7835. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7836. (save-excursion
  7837. (if (not (re-search-backward
  7838. (if table-type org-table-any-border-regexp
  7839. org-table-border-regexp)
  7840. nil t))
  7841. (progn (goto-char (point-min)) (point))
  7842. (goto-char (match-beginning 0))
  7843. (beginning-of-line 2)
  7844. (point))))
  7845. (defun org-table-end (&optional table-type)
  7846. "Find the end of the table and return its position.
  7847. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7848. (save-excursion
  7849. (if (not (re-search-forward
  7850. (if table-type org-table-any-border-regexp
  7851. org-table-border-regexp)
  7852. nil t))
  7853. (goto-char (point-max))
  7854. (goto-char (match-beginning 0)))
  7855. (point-marker)))
  7856. (defun org-table-justify-field-maybe (&optional new)
  7857. "Justify the current field, text to left, number to right.
  7858. Optional argument NEW may specify text to replace the current field content."
  7859. (cond
  7860. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7861. ((org-at-table-hline-p))
  7862. ((and (not new)
  7863. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7864. (current-buffer)))
  7865. (< (point) org-table-aligned-begin-marker)
  7866. (>= (point) org-table-aligned-end-marker)))
  7867. ;; This is not the same table, force a full re-align
  7868. (setq org-table-may-need-update t))
  7869. (t ;; realign the current field, based on previous full realign
  7870. (let* ((pos (point)) s
  7871. (col (org-table-current-column))
  7872. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7873. l f n o e)
  7874. (when (> col 0)
  7875. (skip-chars-backward "^|\n")
  7876. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7877. (progn
  7878. (setq s (match-string 1)
  7879. o (match-string 0)
  7880. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7881. e (not (= (match-beginning 2) (match-end 2))))
  7882. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7883. l (if e "|" (setq org-table-may-need-update t) ""))
  7884. n (format f s))
  7885. (if new
  7886. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7887. (setq n (format f new))
  7888. (setq n (concat new "|") org-table-may-need-update t)))
  7889. (or (equal n o)
  7890. (let (org-table-may-need-update)
  7891. (replace-match n t t))))
  7892. (setq org-table-may-need-update t))
  7893. (goto-char pos))))))
  7894. (defun org-table-next-field ()
  7895. "Go to the next field in the current table, creating new lines as needed.
  7896. Before doing so, re-align the table if necessary."
  7897. (interactive)
  7898. (org-table-maybe-eval-formula)
  7899. (org-table-maybe-recalculate-line)
  7900. (if (and org-table-automatic-realign
  7901. org-table-may-need-update)
  7902. (org-table-align))
  7903. (let ((end (org-table-end)))
  7904. (if (org-at-table-hline-p)
  7905. (end-of-line 1))
  7906. (condition-case nil
  7907. (progn
  7908. (re-search-forward "|" end)
  7909. (if (looking-at "[ \t]*$")
  7910. (re-search-forward "|" end))
  7911. (if (and (looking-at "-")
  7912. org-table-tab-jumps-over-hlines
  7913. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7914. (goto-char (match-beginning 1)))
  7915. (if (looking-at "-")
  7916. (progn
  7917. (beginning-of-line 0)
  7918. (org-table-insert-row 'below))
  7919. (if (looking-at " ") (forward-char 1))))
  7920. (error
  7921. (org-table-insert-row 'below)))))
  7922. (defun org-table-previous-field ()
  7923. "Go to the previous field in the table.
  7924. Before doing so, re-align the table if necessary."
  7925. (interactive)
  7926. (org-table-justify-field-maybe)
  7927. (org-table-maybe-recalculate-line)
  7928. (if (and org-table-automatic-realign
  7929. org-table-may-need-update)
  7930. (org-table-align))
  7931. (if (org-at-table-hline-p)
  7932. (end-of-line 1))
  7933. (re-search-backward "|" (org-table-begin))
  7934. (re-search-backward "|" (org-table-begin))
  7935. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7936. (re-search-backward "|" (org-table-begin)))
  7937. (if (looking-at "| ?")
  7938. (goto-char (match-end 0))))
  7939. (defun org-table-next-row ()
  7940. "Go to the next row (same column) in the current table.
  7941. Before doing so, re-align the table if necessary."
  7942. (interactive)
  7943. (org-table-maybe-eval-formula)
  7944. (org-table-maybe-recalculate-line)
  7945. (if (or (looking-at "[ \t]*$")
  7946. (save-excursion (skip-chars-backward " \t") (bolp)))
  7947. (newline)
  7948. (if (and org-table-automatic-realign
  7949. org-table-may-need-update)
  7950. (org-table-align))
  7951. (let ((col (org-table-current-column)))
  7952. (beginning-of-line 2)
  7953. (if (or (not (org-at-table-p))
  7954. (org-at-table-hline-p))
  7955. (progn
  7956. (beginning-of-line 0)
  7957. (org-table-insert-row 'below)))
  7958. (org-table-goto-column col)
  7959. (skip-chars-backward "^|\n\r")
  7960. (if (looking-at " ") (forward-char 1)))))
  7961. (defun org-table-copy-down (n)
  7962. "Copy a field down in the current column.
  7963. If the field at the cursor is empty, copy into it the content of the nearest
  7964. non-empty field above. With argument N, use the Nth non-empty field.
  7965. If the current field is not empty, it is copied down to the next row, and
  7966. the cursor is moved with it. Therefore, repeating this command causes the
  7967. column to be filled row-by-row.
  7968. If the variable `org-table-copy-increment' is non-nil and the field is an
  7969. integer or a timestamp, it will be incremented while copying. In the case of
  7970. a timestamp, if the cursor is on the year, change the year. If it is on the
  7971. month or the day, change that. Point will stay on the current date field
  7972. in order to easily repeat the interval."
  7973. (interactive "p")
  7974. (let* ((colpos (org-table-current-column))
  7975. (col (current-column))
  7976. (field (org-table-get-field))
  7977. (non-empty (string-match "[^ \t]" field))
  7978. (beg (org-table-begin))
  7979. txt)
  7980. (org-table-check-inside-data-field)
  7981. (if non-empty
  7982. (progn
  7983. (setq txt (org-trim field))
  7984. (org-table-next-row)
  7985. (org-table-blank-field))
  7986. (save-excursion
  7987. (setq txt
  7988. (catch 'exit
  7989. (while (progn (beginning-of-line 1)
  7990. (re-search-backward org-table-dataline-regexp
  7991. beg t))
  7992. (org-table-goto-column colpos t)
  7993. (if (and (looking-at
  7994. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7995. (= (setq n (1- n)) 0))
  7996. (throw 'exit (match-string 1))))))))
  7997. (if txt
  7998. (progn
  7999. (if (and org-table-copy-increment
  8000. (string-match "^[0-9]+$" txt))
  8001. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8002. (insert txt)
  8003. (move-to-column col)
  8004. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8005. (org-timestamp-up 1)
  8006. (org-table-maybe-recalculate-line))
  8007. (org-table-align)
  8008. (move-to-column col))
  8009. (error "No non-empty field found"))))
  8010. (defun org-table-check-inside-data-field ()
  8011. "Is point inside a table data field?
  8012. I.e. not on a hline or before the first or after the last column?
  8013. This actually throws an error, so it aborts the current command."
  8014. (if (or (not (org-at-table-p))
  8015. (= (org-table-current-column) 0)
  8016. (org-at-table-hline-p)
  8017. (looking-at "[ \t]*$"))
  8018. (error "Not in table data field")))
  8019. (defvar org-table-clip nil
  8020. "Clipboard for table regions.")
  8021. (defun org-table-blank-field ()
  8022. "Blank the current table field or active region."
  8023. (interactive)
  8024. (org-table-check-inside-data-field)
  8025. (if (and (interactive-p) (org-region-active-p))
  8026. (let (org-table-clip)
  8027. (org-table-cut-region (region-beginning) (region-end)))
  8028. (skip-chars-backward "^|")
  8029. (backward-char 1)
  8030. (if (looking-at "|[^|\n]+")
  8031. (let* ((pos (match-beginning 0))
  8032. (match (match-string 0))
  8033. (len (org-string-width match)))
  8034. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8035. (goto-char (+ 2 pos))
  8036. (substring match 1)))))
  8037. (defun org-table-get-field (&optional n replace)
  8038. "Return the value of the field in column N of current row.
  8039. N defaults to current field.
  8040. If REPLACE is a string, replace field with this value. The return value
  8041. is always the old value."
  8042. (and n (org-table-goto-column n))
  8043. (skip-chars-backward "^|\n")
  8044. (backward-char 1)
  8045. (if (looking-at "|[^|\r\n]*")
  8046. (let* ((pos (match-beginning 0))
  8047. (val (buffer-substring (1+ pos) (match-end 0))))
  8048. (if replace
  8049. (replace-match (concat "|" replace) t t))
  8050. (goto-char (min (point-at-eol) (+ 2 pos)))
  8051. val)
  8052. (forward-char 1) ""))
  8053. (defun org-table-field-info (arg)
  8054. "Show info about the current field, and highlight any reference at point."
  8055. (interactive "P")
  8056. (org-table-get-specials)
  8057. (save-excursion
  8058. (let* ((pos (point))
  8059. (col (org-table-current-column))
  8060. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8061. (name (car (rassoc (list (org-current-line) col)
  8062. org-table-named-field-locations)))
  8063. (eql (org-table-get-stored-formulas))
  8064. (dline (org-table-current-dline))
  8065. (ref (format "@%d$%d" dline col))
  8066. (ref1 (org-table-convert-refs-to-an ref))
  8067. (fequation (or (assoc name eql) (assoc ref eql)))
  8068. (cequation (assoc (int-to-string col) eql))
  8069. (eqn (or fequation cequation)))
  8070. (goto-char pos)
  8071. (condition-case nil
  8072. (org-table-show-reference 'local)
  8073. (error nil))
  8074. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8075. dline col
  8076. (if cname (concat " or $" cname) "")
  8077. dline col ref1
  8078. (if name (concat " or $" name) "")
  8079. ;; FIXME: formula info not correct if special table line
  8080. (if eqn
  8081. (concat ", formula: "
  8082. (org-table-formula-to-user
  8083. (concat
  8084. (if (string-match "^[$@]"(car eqn)) "" "$")
  8085. (car eqn) "=" (cdr eqn))))
  8086. "")))))
  8087. (defun org-table-current-column ()
  8088. "Find out which column we are in.
  8089. When called interactively, column is also displayed in echo area."
  8090. (interactive)
  8091. (if (interactive-p) (org-table-check-inside-data-field))
  8092. (save-excursion
  8093. (let ((cnt 0) (pos (point)))
  8094. (beginning-of-line 1)
  8095. (while (search-forward "|" pos t)
  8096. (setq cnt (1+ cnt)))
  8097. (if (interactive-p) (message "This is table column %d" cnt))
  8098. cnt)))
  8099. (defun org-table-current-dline ()
  8100. "Find out what table data line we are in.
  8101. Only datalins count for this."
  8102. (interactive)
  8103. (if (interactive-p) (org-table-check-inside-data-field))
  8104. (save-excursion
  8105. (let ((cnt 0) (pos (point)))
  8106. (goto-char (org-table-begin))
  8107. (while (<= (point) pos)
  8108. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8109. (beginning-of-line 2))
  8110. (if (interactive-p) (message "This is table line %d" cnt))
  8111. cnt)))
  8112. (defun org-table-goto-column (n &optional on-delim force)
  8113. "Move the cursor to the Nth column in the current table line.
  8114. With optional argument ON-DELIM, stop with point before the left delimiter
  8115. of the field.
  8116. If there are less than N fields, just go to after the last delimiter.
  8117. However, when FORCE is non-nil, create new columns if necessary."
  8118. (interactive "p")
  8119. (let ((pos (point-at-eol)))
  8120. (beginning-of-line 1)
  8121. (when (> n 0)
  8122. (while (and (> (setq n (1- n)) -1)
  8123. (or (search-forward "|" pos t)
  8124. (and force
  8125. (progn (end-of-line 1)
  8126. (skip-chars-backward "^|")
  8127. (insert " | "))))))
  8128. ; (backward-char 2) t)))))
  8129. (when (and force (not (looking-at ".*|")))
  8130. (save-excursion (end-of-line 1) (insert " | ")))
  8131. (if on-delim
  8132. (backward-char 1)
  8133. (if (looking-at " ") (forward-char 1))))))
  8134. (defun org-at-table-p (&optional table-type)
  8135. "Return t if the cursor is inside an org-type table.
  8136. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8137. (if org-enable-table-editor
  8138. (save-excursion
  8139. (beginning-of-line 1)
  8140. (looking-at (if table-type org-table-any-line-regexp
  8141. org-table-line-regexp)))
  8142. nil))
  8143. (defun org-at-table.el-p ()
  8144. "Return t if and only if we are at a table.el table."
  8145. (and (org-at-table-p 'any)
  8146. (save-excursion
  8147. (goto-char (org-table-begin 'any))
  8148. (looking-at org-table1-hline-regexp))))
  8149. (defun org-table-recognize-table.el ()
  8150. "If there is a table.el table nearby, recognize it and move into it."
  8151. (if org-table-tab-recognizes-table.el
  8152. (if (org-at-table.el-p)
  8153. (progn
  8154. (beginning-of-line 1)
  8155. (if (looking-at org-table-dataline-regexp)
  8156. nil
  8157. (if (looking-at org-table1-hline-regexp)
  8158. (progn
  8159. (beginning-of-line 2)
  8160. (if (looking-at org-table-any-border-regexp)
  8161. (beginning-of-line -1)))))
  8162. (if (re-search-forward "|" (org-table-end t) t)
  8163. (progn
  8164. (require 'table)
  8165. (if (table--at-cell-p (point))
  8166. t
  8167. (message "recognizing table.el table...")
  8168. (table-recognize-table)
  8169. (message "recognizing table.el table...done")))
  8170. (error "This should not happen..."))
  8171. t)
  8172. nil)
  8173. nil))
  8174. (defun org-at-table-hline-p ()
  8175. "Return t if the cursor is inside a hline in a table."
  8176. (if org-enable-table-editor
  8177. (save-excursion
  8178. (beginning-of-line 1)
  8179. (looking-at org-table-hline-regexp))
  8180. nil))
  8181. (defun org-table-insert-column ()
  8182. "Insert a new column into the table."
  8183. (interactive)
  8184. (if (not (org-at-table-p))
  8185. (error "Not at a table"))
  8186. (org-table-find-dataline)
  8187. (let* ((col (max 1 (org-table-current-column)))
  8188. (beg (org-table-begin))
  8189. (end (org-table-end))
  8190. ;; Current cursor position
  8191. (linepos (org-current-line))
  8192. (colpos col))
  8193. (goto-char beg)
  8194. (while (< (point) end)
  8195. (if (org-at-table-hline-p)
  8196. nil
  8197. (org-table-goto-column col t)
  8198. (insert "| "))
  8199. (beginning-of-line 2))
  8200. (move-marker end nil)
  8201. (goto-line linepos)
  8202. (org-table-goto-column colpos)
  8203. (org-table-align)
  8204. (org-table-fix-formulas "$" nil (1- col) 1)))
  8205. (defun org-table-find-dataline ()
  8206. "Find a dataline in the current table, which is needed for column commands."
  8207. (if (and (org-at-table-p)
  8208. (not (org-at-table-hline-p)))
  8209. t
  8210. (let ((col (current-column))
  8211. (end (org-table-end)))
  8212. (move-to-column col)
  8213. (while (and (< (point) end)
  8214. (or (not (= (current-column) col))
  8215. (org-at-table-hline-p)))
  8216. (beginning-of-line 2)
  8217. (move-to-column col))
  8218. (if (and (org-at-table-p)
  8219. (not (org-at-table-hline-p)))
  8220. t
  8221. (error
  8222. "Please position cursor in a data line for column operations")))))
  8223. (defun org-table-delete-column ()
  8224. "Delete a column from the table."
  8225. (interactive)
  8226. (if (not (org-at-table-p))
  8227. (error "Not at a table"))
  8228. (org-table-find-dataline)
  8229. (org-table-check-inside-data-field)
  8230. (let* ((col (org-table-current-column))
  8231. (beg (org-table-begin))
  8232. (end (org-table-end))
  8233. ;; Current cursor position
  8234. (linepos (org-current-line))
  8235. (colpos col))
  8236. (goto-char beg)
  8237. (while (< (point) end)
  8238. (if (org-at-table-hline-p)
  8239. nil
  8240. (org-table-goto-column col t)
  8241. (and (looking-at "|[^|\n]+|")
  8242. (replace-match "|")))
  8243. (beginning-of-line 2))
  8244. (move-marker end nil)
  8245. (goto-line linepos)
  8246. (org-table-goto-column colpos)
  8247. (org-table-align)
  8248. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8249. col -1 col)))
  8250. (defun org-table-move-column-right ()
  8251. "Move column to the right."
  8252. (interactive)
  8253. (org-table-move-column nil))
  8254. (defun org-table-move-column-left ()
  8255. "Move column to the left."
  8256. (interactive)
  8257. (org-table-move-column 'left))
  8258. (defun org-table-move-column (&optional left)
  8259. "Move the current column to the right. With arg LEFT, move to the left."
  8260. (interactive "P")
  8261. (if (not (org-at-table-p))
  8262. (error "Not at a table"))
  8263. (org-table-find-dataline)
  8264. (org-table-check-inside-data-field)
  8265. (let* ((col (org-table-current-column))
  8266. (col1 (if left (1- col) col))
  8267. (beg (org-table-begin))
  8268. (end (org-table-end))
  8269. ;; Current cursor position
  8270. (linepos (org-current-line))
  8271. (colpos (if left (1- col) (1+ col))))
  8272. (if (and left (= col 1))
  8273. (error "Cannot move column further left"))
  8274. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8275. (error "Cannot move column further right"))
  8276. (goto-char beg)
  8277. (while (< (point) end)
  8278. (if (org-at-table-hline-p)
  8279. nil
  8280. (org-table-goto-column col1 t)
  8281. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8282. (replace-match "|\\2|\\1|")))
  8283. (beginning-of-line 2))
  8284. (move-marker end nil)
  8285. (goto-line linepos)
  8286. (org-table-goto-column colpos)
  8287. (org-table-align)
  8288. (org-table-fix-formulas
  8289. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8290. (cons (number-to-string colpos) (number-to-string col))))))
  8291. (defun org-table-move-row-down ()
  8292. "Move table row down."
  8293. (interactive)
  8294. (org-table-move-row nil))
  8295. (defun org-table-move-row-up ()
  8296. "Move table row up."
  8297. (interactive)
  8298. (org-table-move-row 'up))
  8299. (defun org-table-move-row (&optional up)
  8300. "Move the current table line down. With arg UP, move it up."
  8301. (interactive "P")
  8302. (let* ((col (current-column))
  8303. (pos (point))
  8304. (hline1p (save-excursion (beginning-of-line 1)
  8305. (looking-at org-table-hline-regexp)))
  8306. (dline1 (org-table-current-dline))
  8307. (dline2 (+ dline1 (if up -1 1)))
  8308. (tonew (if up 0 2))
  8309. txt hline2p)
  8310. (beginning-of-line tonew)
  8311. (unless (org-at-table-p)
  8312. (goto-char pos)
  8313. (error "Cannot move row further"))
  8314. (setq hline2p (looking-at org-table-hline-regexp))
  8315. (goto-char pos)
  8316. (beginning-of-line 1)
  8317. (setq pos (point))
  8318. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8319. (delete-region (point) (1+ (point-at-eol)))
  8320. (beginning-of-line tonew)
  8321. (insert txt)
  8322. (beginning-of-line 0)
  8323. (move-to-column col)
  8324. (unless (or hline1p hline2p)
  8325. (org-table-fix-formulas
  8326. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8327. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8328. (defun org-table-insert-row (&optional arg)
  8329. "Insert a new row above the current line into the table.
  8330. With prefix ARG, insert below the current line."
  8331. (interactive "P")
  8332. (if (not (org-at-table-p))
  8333. (error "Not at a table"))
  8334. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8335. (new (org-table-clean-line line)))
  8336. ;; Fix the first field if necessary
  8337. (if (string-match "^[ \t]*| *[#$] *|" line)
  8338. (setq new (replace-match (match-string 0 line) t t new)))
  8339. (beginning-of-line (if arg 2 1))
  8340. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8341. (beginning-of-line 0)
  8342. (re-search-forward "| ?" (point-at-eol) t)
  8343. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8344. (org-table-align))
  8345. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8346. (defun org-table-insert-hline (&optional above)
  8347. "Insert a horizontal-line below the current line into the table.
  8348. With prefix ABOVE, insert above the current line."
  8349. (interactive "P")
  8350. (if (not (org-at-table-p))
  8351. (error "Not at a table"))
  8352. (let ((line (org-table-clean-line
  8353. (buffer-substring (point-at-bol) (point-at-eol))))
  8354. (col (current-column)))
  8355. (while (string-match "|\\( +\\)|" line)
  8356. (setq line (replace-match
  8357. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8358. ?-) "|") t t line)))
  8359. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8360. (beginning-of-line (if above 1 2))
  8361. (insert line "\n")
  8362. (beginning-of-line (if above 1 -1))
  8363. (move-to-column col)
  8364. (and org-table-overlay-coordinates (org-table-align))))
  8365. (defun org-table-hline-and-move (&optional same-column)
  8366. "Insert a hline and move to the row below that line."
  8367. (interactive "P")
  8368. (let ((col (org-table-current-column)))
  8369. (org-table-maybe-eval-formula)
  8370. (org-table-maybe-recalculate-line)
  8371. (org-table-insert-hline)
  8372. (end-of-line 2)
  8373. (if (looking-at "\n[ \t]*|-")
  8374. (progn (insert "\n|") (org-table-align))
  8375. (org-table-next-field))
  8376. (if same-column (org-table-goto-column col))))
  8377. (defun org-table-clean-line (s)
  8378. "Convert a table line S into a string with only \"|\" and space.
  8379. In particular, this does handle wide and invisible characters."
  8380. (if (string-match "^[ \t]*|-" s)
  8381. ;; It's a hline, just map the characters
  8382. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8383. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8384. (setq s (replace-match
  8385. (concat "|" (make-string (org-string-width (match-string 1 s))
  8386. ?\ ) "|")
  8387. t t s)))
  8388. s))
  8389. (defun org-table-kill-row ()
  8390. "Delete the current row or horizontal line from the table."
  8391. (interactive)
  8392. (if (not (org-at-table-p))
  8393. (error "Not at a table"))
  8394. (let ((col (current-column))
  8395. (dline (org-table-current-dline)))
  8396. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8397. (if (not (org-at-table-p)) (beginning-of-line 0))
  8398. (move-to-column col)
  8399. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8400. dline -1 dline)))
  8401. (defun org-table-sort-lines (with-case &optional sorting-type)
  8402. "Sort table lines according to the column at point.
  8403. The position of point indicates the column to be used for
  8404. sorting, and the range of lines is the range between the nearest
  8405. horizontal separator lines, or the entire table of no such lines
  8406. exist. If point is before the first column, you will be prompted
  8407. for the sorting column. If there is an active region, the mark
  8408. specifies the first line and the sorting column, while point
  8409. should be in the last line to be included into the sorting.
  8410. The command then prompts for the sorting type which can be
  8411. alphabetically, numerically, or by time (as given in a time stamp
  8412. in the field). Sorting in reverse order is also possible.
  8413. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8414. If SORTING-TYPE is specified when this function is called from a Lisp
  8415. program, no prompting will take place. SORTING-TYPE must be a character,
  8416. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8417. should be done in reverse order."
  8418. (interactive "P")
  8419. (let* ((thisline (org-current-line))
  8420. (thiscol (org-table-current-column))
  8421. beg end bcol ecol tend tbeg column lns pos)
  8422. (when (equal thiscol 0)
  8423. (if (interactive-p)
  8424. (setq thiscol
  8425. (string-to-number
  8426. (read-string "Use column N for sorting: ")))
  8427. (setq thiscol 1))
  8428. (org-table-goto-column thiscol))
  8429. (org-table-check-inside-data-field)
  8430. (if (org-region-active-p)
  8431. (progn
  8432. (setq beg (region-beginning) end (region-end))
  8433. (goto-char beg)
  8434. (setq column (org-table-current-column)
  8435. beg (point-at-bol))
  8436. (goto-char end)
  8437. (setq end (point-at-bol 2)))
  8438. (setq column (org-table-current-column)
  8439. pos (point)
  8440. tbeg (org-table-begin)
  8441. tend (org-table-end))
  8442. (if (re-search-backward org-table-hline-regexp tbeg t)
  8443. (setq beg (point-at-bol 2))
  8444. (goto-char tbeg)
  8445. (setq beg (point-at-bol 1)))
  8446. (goto-char pos)
  8447. (if (re-search-forward org-table-hline-regexp tend t)
  8448. (setq end (point-at-bol 1))
  8449. (goto-char tend)
  8450. (setq end (point-at-bol))))
  8451. (setq beg (move-marker (make-marker) beg)
  8452. end (move-marker (make-marker) end))
  8453. (untabify beg end)
  8454. (goto-char beg)
  8455. (org-table-goto-column column)
  8456. (skip-chars-backward "^|")
  8457. (setq bcol (current-column))
  8458. (org-table-goto-column (1+ column))
  8459. (skip-chars-backward "^|")
  8460. (setq ecol (1- (current-column)))
  8461. (org-table-goto-column column)
  8462. (setq lns (mapcar (lambda(x) (cons
  8463. (org-sort-remove-invisible
  8464. (nth (1- column)
  8465. (org-split-string x "[ \t]*|[ \t]*")))
  8466. x))
  8467. (org-split-string (buffer-substring beg end) "\n")))
  8468. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8469. (delete-region beg end)
  8470. (move-marker beg nil)
  8471. (move-marker end nil)
  8472. (insert (mapconcat 'cdr lns "\n") "\n")
  8473. (goto-line thisline)
  8474. (org-table-goto-column thiscol)
  8475. (message "%d lines sorted, based on column %d" (length lns) column)))
  8476. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8477. (defun org-sort-remove-invisible (s)
  8478. (remove-text-properties 0 (length s) org-rm-props s)
  8479. (while (string-match org-bracket-link-regexp s)
  8480. (setq s (replace-match (if (match-end 2)
  8481. (match-string 3 s)
  8482. (match-string 1 s)) t t s)))
  8483. s)
  8484. (defun org-table-cut-region (beg end)
  8485. "Copy region in table to the clipboard and blank all relevant fields."
  8486. (interactive "r")
  8487. (org-table-copy-region beg end 'cut))
  8488. (defun org-table-copy-region (beg end &optional cut)
  8489. "Copy rectangular region in table to clipboard.
  8490. A special clipboard is used which can only be accessed
  8491. with `org-table-paste-rectangle'."
  8492. (interactive "rP")
  8493. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8494. region cols
  8495. (rpl (if cut " " nil)))
  8496. (goto-char beg)
  8497. (org-table-check-inside-data-field)
  8498. (setq l01 (org-current-line)
  8499. c01 (org-table-current-column))
  8500. (goto-char end)
  8501. (org-table-check-inside-data-field)
  8502. (setq l02 (org-current-line)
  8503. c02 (org-table-current-column))
  8504. (setq l1 (min l01 l02) l2 (max l01 l02)
  8505. c1 (min c01 c02) c2 (max c01 c02))
  8506. (catch 'exit
  8507. (while t
  8508. (catch 'nextline
  8509. (if (> l1 l2) (throw 'exit t))
  8510. (goto-line l1)
  8511. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8512. (setq cols nil ic1 c1 ic2 c2)
  8513. (while (< ic1 (1+ ic2))
  8514. (push (org-table-get-field ic1 rpl) cols)
  8515. (setq ic1 (1+ ic1)))
  8516. (push (nreverse cols) region)
  8517. (setq l1 (1+ l1)))))
  8518. (setq org-table-clip (nreverse region))
  8519. (if cut (org-table-align))
  8520. org-table-clip))
  8521. (defun org-table-paste-rectangle ()
  8522. "Paste a rectangular region into a table.
  8523. The upper right corner ends up in the current field. All involved fields
  8524. will be overwritten. If the rectangle does not fit into the present table,
  8525. the table is enlarged as needed. The process ignores horizontal separator
  8526. lines."
  8527. (interactive)
  8528. (unless (and org-table-clip (listp org-table-clip))
  8529. (error "First cut/copy a region to paste!"))
  8530. (org-table-check-inside-data-field)
  8531. (let* ((clip org-table-clip)
  8532. (line (org-current-line))
  8533. (col (org-table-current-column))
  8534. (org-enable-table-editor t)
  8535. (org-table-automatic-realign nil)
  8536. c cols field)
  8537. (while (setq cols (pop clip))
  8538. (while (org-at-table-hline-p) (beginning-of-line 2))
  8539. (if (not (org-at-table-p))
  8540. (progn (end-of-line 0) (org-table-next-field)))
  8541. (setq c col)
  8542. (while (setq field (pop cols))
  8543. (org-table-goto-column c nil 'force)
  8544. (org-table-get-field nil field)
  8545. (setq c (1+ c)))
  8546. (beginning-of-line 2))
  8547. (goto-line line)
  8548. (org-table-goto-column col)
  8549. (org-table-align)))
  8550. (defun org-table-convert ()
  8551. "Convert from `org-mode' table to table.el and back.
  8552. Obviously, this only works within limits. When an Org-mode table is
  8553. converted to table.el, all horizontal separator lines get lost, because
  8554. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8555. A table.el table can be converted to an Org-mode table only if it does not
  8556. do row or column spanning. Multiline cells will become multiple cells.
  8557. Beware, Org-mode does not test if the table can be successfully converted - it
  8558. blindly applies a recipe that works for simple tables."
  8559. (interactive)
  8560. (require 'table)
  8561. (if (org-at-table.el-p)
  8562. ;; convert to Org-mode table
  8563. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8564. (end (move-marker (make-marker) (org-table-end t))))
  8565. (table-unrecognize-region beg end)
  8566. (goto-char beg)
  8567. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8568. (replace-match ""))
  8569. (goto-char beg))
  8570. (if (org-at-table-p)
  8571. ;; convert to table.el table
  8572. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8573. (end (move-marker (make-marker) (org-table-end))))
  8574. ;; first, get rid of all horizontal lines
  8575. (goto-char beg)
  8576. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8577. (replace-match ""))
  8578. ;; insert a hline before first
  8579. (goto-char beg)
  8580. (org-table-insert-hline 'above)
  8581. (beginning-of-line -1)
  8582. ;; insert a hline after each line
  8583. (while (progn (beginning-of-line 3) (< (point) end))
  8584. (org-table-insert-hline))
  8585. (goto-char beg)
  8586. (setq end (move-marker end (org-table-end)))
  8587. ;; replace "+" at beginning and ending of hlines
  8588. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8589. (replace-match "\\1+-"))
  8590. (goto-char beg)
  8591. (while (re-search-forward "-|[ \t]*$" end t)
  8592. (replace-match "-+"))
  8593. (goto-char beg)))))
  8594. (defun org-table-wrap-region (arg)
  8595. "Wrap several fields in a column like a paragraph.
  8596. This is useful if you'd like to spread the contents of a field over several
  8597. lines, in order to keep the table compact.
  8598. If there is an active region, and both point and mark are in the same column,
  8599. the text in the column is wrapped to minimum width for the given number of
  8600. lines. Generally, this makes the table more compact. A prefix ARG may be
  8601. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8602. formats the selected text to two lines. If the region was longer than two
  8603. lines, the remaining lines remain empty. A negative prefix argument reduces
  8604. the current number of lines by that amount. The wrapped text is pasted back
  8605. into the table. If you formatted it to more lines than it was before, fields
  8606. further down in the table get overwritten - so you might need to make space in
  8607. the table first.
  8608. If there is no region, the current field is split at the cursor position and
  8609. the text fragment to the right of the cursor is prepended to the field one
  8610. line down.
  8611. If there is no region, but you specify a prefix ARG, the current field gets
  8612. blank, and the content is appended to the field above."
  8613. (interactive "P")
  8614. (org-table-check-inside-data-field)
  8615. (if (org-region-active-p)
  8616. ;; There is a region: fill as a paragraph
  8617. (let* ((beg (region-beginning))
  8618. (cline (save-excursion (goto-char beg) (org-current-line)))
  8619. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8620. nlines)
  8621. (org-table-cut-region (region-beginning) (region-end))
  8622. (if (> (length (car org-table-clip)) 1)
  8623. (error "Region must be limited to single column"))
  8624. (setq nlines (if arg
  8625. (if (< arg 1)
  8626. (+ (length org-table-clip) arg)
  8627. arg)
  8628. (length org-table-clip)))
  8629. (setq org-table-clip
  8630. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8631. nil nlines)))
  8632. (goto-line cline)
  8633. (org-table-goto-column ccol)
  8634. (org-table-paste-rectangle))
  8635. ;; No region, split the current field at point
  8636. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8637. (skip-chars-forward "^\r\n|"))
  8638. (if arg
  8639. ;; combine with field above
  8640. (let ((s (org-table-blank-field))
  8641. (col (org-table-current-column)))
  8642. (beginning-of-line 0)
  8643. (while (org-at-table-hline-p) (beginning-of-line 0))
  8644. (org-table-goto-column col)
  8645. (skip-chars-forward "^|")
  8646. (skip-chars-backward " ")
  8647. (insert " " (org-trim s))
  8648. (org-table-align))
  8649. ;; split field
  8650. (if (looking-at "\\([^|]+\\)+|")
  8651. (let ((s (match-string 1)))
  8652. (replace-match " |")
  8653. (goto-char (match-beginning 0))
  8654. (org-table-next-row)
  8655. (insert (org-trim s) " ")
  8656. (org-table-align))
  8657. (org-table-next-row)))))
  8658. (defvar org-field-marker nil)
  8659. (defun org-table-edit-field (arg)
  8660. "Edit table field in a different window.
  8661. This is mainly useful for fields that contain hidden parts.
  8662. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8663. it can be edited in place."
  8664. (interactive "P")
  8665. (if arg
  8666. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8667. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8668. (remove-text-properties b e '(org-cwidth t invisible t
  8669. display t intangible t))
  8670. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8671. (font-lock-fontify-block)))
  8672. (let ((pos (move-marker (make-marker) (point)))
  8673. (field (org-table-get-field))
  8674. (cw (current-window-configuration))
  8675. p)
  8676. (org-switch-to-buffer-other-window "*Org tmp*")
  8677. (erase-buffer)
  8678. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8679. (let ((org-inhibit-startup t)) (org-mode))
  8680. (goto-char (setq p (point-max)))
  8681. (insert (org-trim field))
  8682. (remove-text-properties p (point-max)
  8683. '(invisible t org-cwidth t display t
  8684. intangible t))
  8685. (goto-char p)
  8686. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8687. (org-set-local 'org-window-configuration cw)
  8688. (org-set-local 'org-field-marker pos)
  8689. (message "Edit and finish with C-c C-c"))))
  8690. (defun org-table-finish-edit-field ()
  8691. "Finish editing a table data field.
  8692. Remove all newline characters, insert the result into the table, realign
  8693. the table and kill the editing buffer."
  8694. (let ((pos org-field-marker)
  8695. (cw org-window-configuration)
  8696. (cb (current-buffer))
  8697. text)
  8698. (goto-char (point-min))
  8699. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8700. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8701. (replace-match " "))
  8702. (setq text (org-trim (buffer-string)))
  8703. (set-window-configuration cw)
  8704. (kill-buffer cb)
  8705. (select-window (get-buffer-window (marker-buffer pos)))
  8706. (goto-char pos)
  8707. (move-marker pos nil)
  8708. (org-table-check-inside-data-field)
  8709. (org-table-get-field nil text)
  8710. (org-table-align)
  8711. (message "New field value inserted")))
  8712. (defun org-trim (s)
  8713. "Remove whitespace at beginning and end of string."
  8714. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8715. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8716. s)
  8717. (defun org-wrap (string &optional width lines)
  8718. "Wrap string to either a number of lines, or a width in characters.
  8719. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8720. that costs. If there is a word longer than WIDTH, the text is actually
  8721. wrapped to the length of that word.
  8722. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8723. many lines, whatever width that takes.
  8724. The return value is a list of lines, without newlines at the end."
  8725. (let* ((words (org-split-string string "[ \t\n]+"))
  8726. (maxword (apply 'max (mapcar 'org-string-width words)))
  8727. w ll)
  8728. (cond (width
  8729. (org-do-wrap words (max maxword width)))
  8730. (lines
  8731. (setq w maxword)
  8732. (setq ll (org-do-wrap words maxword))
  8733. (if (<= (length ll) lines)
  8734. ll
  8735. (setq ll words)
  8736. (while (> (length ll) lines)
  8737. (setq w (1+ w))
  8738. (setq ll (org-do-wrap words w)))
  8739. ll))
  8740. (t (error "Cannot wrap this")))))
  8741. (defun org-do-wrap (words width)
  8742. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8743. (let (lines line)
  8744. (while words
  8745. (setq line (pop words))
  8746. (while (and words (< (+ (length line) (length (car words))) width))
  8747. (setq line (concat line " " (pop words))))
  8748. (setq lines (push line lines)))
  8749. (nreverse lines)))
  8750. (defun org-split-string (string &optional separators)
  8751. "Splits STRING into substrings at SEPARATORS.
  8752. No empty strings are returned if there are matches at the beginning
  8753. and end of string."
  8754. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8755. (start 0)
  8756. notfirst
  8757. (list nil))
  8758. (while (and (string-match rexp string
  8759. (if (and notfirst
  8760. (= start (match-beginning 0))
  8761. (< start (length string)))
  8762. (1+ start) start))
  8763. (< (match-beginning 0) (length string)))
  8764. (setq notfirst t)
  8765. (or (eq (match-beginning 0) 0)
  8766. (and (eq (match-beginning 0) (match-end 0))
  8767. (eq (match-beginning 0) start))
  8768. (setq list
  8769. (cons (substring string start (match-beginning 0))
  8770. list)))
  8771. (setq start (match-end 0)))
  8772. (or (eq start (length string))
  8773. (setq list
  8774. (cons (substring string start)
  8775. list)))
  8776. (nreverse list)))
  8777. (defun org-table-map-tables (function)
  8778. "Apply FUNCTION to the start of all tables in the buffer."
  8779. (save-excursion
  8780. (save-restriction
  8781. (widen)
  8782. (goto-char (point-min))
  8783. (while (re-search-forward org-table-any-line-regexp nil t)
  8784. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8785. (beginning-of-line 1)
  8786. (if (looking-at org-table-line-regexp)
  8787. (save-excursion (funcall function)))
  8788. (re-search-forward org-table-any-border-regexp nil 1))))
  8789. (message "Mapping tables: done"))
  8790. (defvar org-timecnt) ; dynamically scoped parameter
  8791. (defun org-table-sum (&optional beg end nlast)
  8792. "Sum numbers in region of current table column.
  8793. The result will be displayed in the echo area, and will be available
  8794. as kill to be inserted with \\[yank].
  8795. If there is an active region, it is interpreted as a rectangle and all
  8796. numbers in that rectangle will be summed. If there is no active
  8797. region and point is located in a table column, sum all numbers in that
  8798. column.
  8799. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8800. numbers are assumed to be times as well (in decimal hours) and the
  8801. numbers are added as such.
  8802. If NLAST is a number, only the NLAST fields will actually be summed."
  8803. (interactive)
  8804. (save-excursion
  8805. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8806. (cond
  8807. ((and beg end)) ; beg and end given explicitly
  8808. ((org-region-active-p)
  8809. (setq beg (region-beginning) end (region-end)))
  8810. (t
  8811. (setq col (org-table-current-column))
  8812. (goto-char (org-table-begin))
  8813. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8814. (error "No table data"))
  8815. (org-table-goto-column col)
  8816. (setq beg (point))
  8817. (goto-char (org-table-end))
  8818. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8819. (error "No table data"))
  8820. (org-table-goto-column col)
  8821. (setq end (point))))
  8822. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8823. (items1 (cond ((not nlast) items)
  8824. ((>= nlast (length items)) items)
  8825. (t (setq items (reverse items))
  8826. (setcdr (nthcdr (1- nlast) items) nil)
  8827. (nreverse items))))
  8828. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8829. items1)))
  8830. (res (apply '+ numbers))
  8831. (sres (if (= org-timecnt 0)
  8832. (format "%g" res)
  8833. (setq diff (* 3600 res)
  8834. h (floor (/ diff 3600)) diff (mod diff 3600)
  8835. m (floor (/ diff 60)) diff (mod diff 60)
  8836. s diff)
  8837. (format "%d:%02d:%02d" h m s))))
  8838. (kill-new sres)
  8839. (if (interactive-p)
  8840. (message "%s"
  8841. (substitute-command-keys
  8842. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8843. (length numbers) sres))))
  8844. sres))))
  8845. (defun org-table-get-number-for-summing (s)
  8846. (let (n)
  8847. (if (string-match "^ *|? *" s)
  8848. (setq s (replace-match "" nil nil s)))
  8849. (if (string-match " *|? *$" s)
  8850. (setq s (replace-match "" nil nil s)))
  8851. (setq n (string-to-number s))
  8852. (cond
  8853. ((and (string-match "0" s)
  8854. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8855. ((string-match "\\`[ \t]+\\'" s) nil)
  8856. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8857. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8858. (m (string-to-number (or (match-string 2 s) "0")))
  8859. (s (string-to-number (or (match-string 4 s) "0"))))
  8860. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8861. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8862. ((equal n 0) nil)
  8863. (t n))))
  8864. (defun org-table-current-field-formula (&optional key noerror)
  8865. "Return the formula active for the current field.
  8866. Assumes that specials are in place.
  8867. If KEY is given, return the key to this formula.
  8868. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8869. (let* ((name (car (rassoc (list (org-current-line)
  8870. (org-table-current-column))
  8871. org-table-named-field-locations)))
  8872. (col (org-table-current-column))
  8873. (scol (int-to-string col))
  8874. (ref (format "@%d$%d" (org-table-current-dline) col))
  8875. (stored-list (org-table-get-stored-formulas noerror))
  8876. (ass (or (assoc name stored-list)
  8877. (assoc ref stored-list)
  8878. (assoc scol stored-list))))
  8879. (if key
  8880. (car ass)
  8881. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8882. (cdr ass))))))
  8883. (defun org-table-get-formula (&optional equation named)
  8884. "Read a formula from the minibuffer, offer stored formula as default.
  8885. When NAMED is non-nil, look for a named equation."
  8886. (let* ((stored-list (org-table-get-stored-formulas))
  8887. (name (car (rassoc (list (org-current-line)
  8888. (org-table-current-column))
  8889. org-table-named-field-locations)))
  8890. (ref (format "@%d$%d" (org-table-current-dline)
  8891. (org-table-current-column)))
  8892. (refass (assoc ref stored-list))
  8893. (scol (if named
  8894. (if name name ref)
  8895. (int-to-string (org-table-current-column))))
  8896. (dummy (and (or name refass) (not named)
  8897. (not (y-or-n-p "Replace field formula with column formula? " ))
  8898. (error "Abort")))
  8899. (name (or name ref))
  8900. (org-table-may-need-update nil)
  8901. (stored (cdr (assoc scol stored-list)))
  8902. (eq (cond
  8903. ((and stored equation (string-match "^ *=? *$" equation))
  8904. stored)
  8905. ((stringp equation)
  8906. equation)
  8907. (t (org-table-formula-from-user
  8908. (read-string
  8909. (org-table-formula-to-user
  8910. (format "%s formula %s%s="
  8911. (if named "Field" "Column")
  8912. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8913. scol))
  8914. (if stored (org-table-formula-to-user stored) "")
  8915. 'org-table-formula-history
  8916. )))))
  8917. mustsave)
  8918. (when (not (string-match "\\S-" eq))
  8919. ;; remove formula
  8920. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8921. (org-table-store-formulas stored-list)
  8922. (error "Formula removed"))
  8923. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8924. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8925. (if (and name (not named))
  8926. ;; We set the column equation, delete the named one.
  8927. (setq stored-list (delq (assoc name stored-list) stored-list)
  8928. mustsave t))
  8929. (if stored
  8930. (setcdr (assoc scol stored-list) eq)
  8931. (setq stored-list (cons (cons scol eq) stored-list)))
  8932. (if (or mustsave (not (equal stored eq)))
  8933. (org-table-store-formulas stored-list))
  8934. eq))
  8935. (defun org-table-store-formulas (alist)
  8936. "Store the list of formulas below the current table."
  8937. (setq alist (sort alist 'org-table-formula-less-p))
  8938. (save-excursion
  8939. (goto-char (org-table-end))
  8940. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8941. (progn
  8942. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8943. (goto-char (match-beginning 2))
  8944. (delete-region (match-beginning 2) (match-end 0)))
  8945. (insert "#+TBLFM:"))
  8946. (insert " "
  8947. (mapconcat (lambda (x)
  8948. (concat
  8949. (if (equal (string-to-char (car x)) ?@) "" "$")
  8950. (car x) "=" (cdr x)))
  8951. alist "::")
  8952. "\n")))
  8953. (defsubst org-table-formula-make-cmp-string (a)
  8954. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8955. (concat
  8956. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8957. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8958. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8959. (defun org-table-formula-less-p (a b)
  8960. "Compare two formulas for sorting."
  8961. (let ((as (org-table-formula-make-cmp-string (car a)))
  8962. (bs (org-table-formula-make-cmp-string (car b))))
  8963. (and as bs (string< as bs))))
  8964. (defun org-table-get-stored-formulas (&optional noerror)
  8965. "Return an alist with the stored formulas directly after current table."
  8966. (interactive)
  8967. (let (scol eq eq-alist strings string seen)
  8968. (save-excursion
  8969. (goto-char (org-table-end))
  8970. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8971. (setq strings (org-split-string (match-string 2) " *:: *"))
  8972. (while (setq string (pop strings))
  8973. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8974. (setq scol (if (match-end 2)
  8975. (match-string 2 string)
  8976. (match-string 1 string))
  8977. eq (match-string 3 string)
  8978. eq-alist (cons (cons scol eq) eq-alist))
  8979. (if (member scol seen)
  8980. (if noerror
  8981. (progn
  8982. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8983. (ding)
  8984. (sit-for 2))
  8985. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8986. (push scol seen))))))
  8987. (nreverse eq-alist)))
  8988. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8989. "Modify the equations after the table structure has been edited.
  8990. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8991. For all numbers larger than LIMIT, shift them by DELTA."
  8992. (save-excursion
  8993. (goto-char (org-table-end))
  8994. (when (looking-at "#\\+TBLFM:")
  8995. (let ((re (concat key "\\([0-9]+\\)"))
  8996. (re2
  8997. (when remove
  8998. (if (equal key "$")
  8999. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9000. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9001. s n a)
  9002. (when remove
  9003. (while (re-search-forward re2 (point-at-eol) t)
  9004. (replace-match "")))
  9005. (while (re-search-forward re (point-at-eol) t)
  9006. (setq s (match-string 1) n (string-to-number s))
  9007. (cond
  9008. ((setq a (assoc s replace))
  9009. (replace-match (concat key (cdr a)) t t))
  9010. ((and limit (> n limit))
  9011. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9012. (defun org-table-get-specials ()
  9013. "Get the column names and local parameters for this table."
  9014. (save-excursion
  9015. (let ((beg (org-table-begin)) (end (org-table-end))
  9016. names name fields fields1 field cnt
  9017. c v l line col types dlines hlines)
  9018. (setq org-table-column-names nil
  9019. org-table-local-parameters nil
  9020. org-table-named-field-locations nil
  9021. org-table-current-begin-line nil
  9022. org-table-current-begin-pos nil
  9023. org-table-current-line-types nil)
  9024. (goto-char beg)
  9025. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9026. (setq names (org-split-string (match-string 1) " *| *")
  9027. cnt 1)
  9028. (while (setq name (pop names))
  9029. (setq cnt (1+ cnt))
  9030. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9031. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9032. (setq org-table-column-names (nreverse org-table-column-names))
  9033. (setq org-table-column-name-regexp
  9034. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9035. (goto-char beg)
  9036. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9037. (setq fields (org-split-string (match-string 1) " *| *"))
  9038. (while (setq field (pop fields))
  9039. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9040. (push (cons (match-string 1 field) (match-string 2 field))
  9041. org-table-local-parameters))))
  9042. (goto-char beg)
  9043. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9044. (setq c (match-string 1)
  9045. fields (org-split-string (match-string 2) " *| *"))
  9046. (save-excursion
  9047. (beginning-of-line (if (equal c "_") 2 0))
  9048. (setq line (org-current-line) col 1)
  9049. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9050. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9051. (while (and fields1 (setq field (pop fields)))
  9052. (setq v (pop fields1) col (1+ col))
  9053. (when (and (stringp field) (stringp v)
  9054. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9055. (push (cons field v) org-table-local-parameters)
  9056. (push (list field line col) org-table-named-field-locations))))
  9057. ;; Analyse the line types
  9058. (goto-char beg)
  9059. (setq org-table-current-begin-line (org-current-line)
  9060. org-table-current-begin-pos (point)
  9061. l org-table-current-begin-line)
  9062. (while (looking-at "[ \t]*|\\(-\\)?")
  9063. (push (if (match-end 1) 'hline 'dline) types)
  9064. (if (match-end 1) (push l hlines) (push l dlines))
  9065. (beginning-of-line 2)
  9066. (setq l (1+ l)))
  9067. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9068. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9069. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9070. (defun org-table-maybe-eval-formula ()
  9071. "Check if the current field starts with \"=\" or \":=\".
  9072. If yes, store the formula and apply it."
  9073. ;; We already know we are in a table. Get field will only return a formula
  9074. ;; when appropriate. It might return a separator line, but no problem.
  9075. (when org-table-formula-evaluate-inline
  9076. (let* ((field (org-trim (or (org-table-get-field) "")))
  9077. named eq)
  9078. (when (string-match "^:?=\\(.*\\)" field)
  9079. (setq named (equal (string-to-char field) ?:)
  9080. eq (match-string 1 field))
  9081. (if (or (fboundp 'calc-eval)
  9082. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9083. (org-table-eval-formula (if named '(4) nil)
  9084. (org-table-formula-from-user eq))
  9085. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9086. (defvar org-recalc-commands nil
  9087. "List of commands triggering the recalculation of a line.
  9088. Will be filled automatically during use.")
  9089. (defvar org-recalc-marks
  9090. '((" " . "Unmarked: no special line, no automatic recalculation")
  9091. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9092. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9093. ("!" . "Column name definition line. Reference in formula as $name.")
  9094. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9095. ("_" . "Names for values in row below this one.")
  9096. ("^" . "Names for values in row above this one.")))
  9097. (defun org-table-rotate-recalc-marks (&optional newchar)
  9098. "Rotate the recalculation mark in the first column.
  9099. If in any row, the first field is not consistent with a mark,
  9100. insert a new column for the markers.
  9101. When there is an active region, change all the lines in the region,
  9102. after prompting for the marking character.
  9103. After each change, a message will be displayed indicating the meaning
  9104. of the new mark."
  9105. (interactive)
  9106. (unless (org-at-table-p) (error "Not at a table"))
  9107. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9108. (beg (org-table-begin))
  9109. (end (org-table-end))
  9110. (l (org-current-line))
  9111. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9112. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9113. (have-col
  9114. (save-excursion
  9115. (goto-char beg)
  9116. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9117. (col (org-table-current-column))
  9118. (forcenew (car (assoc newchar org-recalc-marks)))
  9119. epos new)
  9120. (when l1
  9121. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9122. (setq newchar (char-to-string (read-char-exclusive))
  9123. forcenew (car (assoc newchar org-recalc-marks))))
  9124. (if (and newchar (not forcenew))
  9125. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9126. newchar))
  9127. (if l1 (goto-line l1))
  9128. (save-excursion
  9129. (beginning-of-line 1)
  9130. (unless (looking-at org-table-dataline-regexp)
  9131. (error "Not at a table data line")))
  9132. (unless have-col
  9133. (org-table-goto-column 1)
  9134. (org-table-insert-column)
  9135. (org-table-goto-column (1+ col)))
  9136. (setq epos (point-at-eol))
  9137. (save-excursion
  9138. (beginning-of-line 1)
  9139. (org-table-get-field
  9140. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9141. (concat " "
  9142. (setq new (or forcenew
  9143. (cadr (member (match-string 1) marks))))
  9144. " ")
  9145. " # ")))
  9146. (if (and l1 l2)
  9147. (progn
  9148. (goto-line l1)
  9149. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9150. (and (looking-at org-table-dataline-regexp)
  9151. (org-table-get-field 1 (concat " " new " "))))
  9152. (goto-line l1)))
  9153. (if (not (= epos (point-at-eol))) (org-table-align))
  9154. (goto-line l)
  9155. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9156. (defun org-table-maybe-recalculate-line ()
  9157. "Recompute the current line if marked for it, and if we haven't just done it."
  9158. (interactive)
  9159. (and org-table-allow-automatic-line-recalculation
  9160. (not (and (memq last-command org-recalc-commands)
  9161. (equal org-last-recalc-line (org-current-line))))
  9162. (save-excursion (beginning-of-line 1)
  9163. (looking-at org-table-auto-recalculate-regexp))
  9164. (org-table-recalculate) t))
  9165. (defvar org-table-formula-debug nil
  9166. "Non-nil means, debug table formulas.
  9167. When nil, simply write \"#ERROR\" in corrupted fields.")
  9168. (make-variable-buffer-local 'org-table-formula-debug)
  9169. (defvar modes)
  9170. (defsubst org-set-calc-mode (var &optional value)
  9171. (if (stringp var)
  9172. (setq var (assoc var '(("D" calc-angle-mode deg)
  9173. ("R" calc-angle-mode rad)
  9174. ("F" calc-prefer-frac t)
  9175. ("S" calc-symbolic-mode t)))
  9176. value (nth 2 var) var (nth 1 var)))
  9177. (if (memq var modes)
  9178. (setcar (cdr (memq var modes)) value)
  9179. (cons var (cons value modes)))
  9180. modes)
  9181. (defun org-table-eval-formula (&optional arg equation
  9182. suppress-align suppress-const
  9183. suppress-store suppress-analysis)
  9184. "Replace the table field value at the cursor by the result of a calculation.
  9185. This function makes use of Dave Gillespie's Calc package, in my view the
  9186. most exciting program ever written for GNU Emacs. So you need to have Calc
  9187. installed in order to use this function.
  9188. In a table, this command replaces the value in the current field with the
  9189. result of a formula. It also installs the formula as the \"current\" column
  9190. formula, by storing it in a special line below the table. When called
  9191. with a `C-u' prefix, the current field must ba a named field, and the
  9192. formula is installed as valid in only this specific field.
  9193. When called with two `C-u' prefixes, insert the active equation
  9194. for the field back into the current field, so that it can be
  9195. edited there. This is useful in order to use \\[org-table-show-reference]
  9196. to check the referenced fields.
  9197. When called, the command first prompts for a formula, which is read in
  9198. the minibuffer. Previously entered formulas are available through the
  9199. history list, and the last used formula is offered as a default.
  9200. These stored formulas are adapted correctly when moving, inserting, or
  9201. deleting columns with the corresponding commands.
  9202. The formula can be any algebraic expression understood by the Calc package.
  9203. For details, see the Org-mode manual.
  9204. This function can also be called from Lisp programs and offers
  9205. additional arguments: EQUATION can be the formula to apply. If this
  9206. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9207. used to speed-up recursive calls by by-passing unnecessary aligns.
  9208. SUPPRESS-CONST suppresses the interpretation of constants in the
  9209. formula, assuming that this has been done already outside the function.
  9210. SUPPRESS-STORE means the formula should not be stored, either because
  9211. it is already stored, or because it is a modified equation that should
  9212. not overwrite the stored one."
  9213. (interactive "P")
  9214. (org-table-check-inside-data-field)
  9215. (or suppress-analysis (org-table-get-specials))
  9216. (if (equal arg '(16))
  9217. (let ((eq (org-table-current-field-formula)))
  9218. (or eq (error "No equation active for current field"))
  9219. (org-table-get-field nil eq)
  9220. (org-table-align)
  9221. (setq org-table-may-need-update t))
  9222. (let* (fields
  9223. (ndown (if (integerp arg) arg 1))
  9224. (org-table-automatic-realign nil)
  9225. (case-fold-search nil)
  9226. (down (> ndown 1))
  9227. (formula (if (and equation suppress-store)
  9228. equation
  9229. (org-table-get-formula equation (equal arg '(4)))))
  9230. (n0 (org-table-current-column))
  9231. (modes (copy-sequence org-calc-default-modes))
  9232. (numbers nil) ; was a variable, now fixed default
  9233. (keep-empty nil)
  9234. n form form0 bw fmt x ev orig c lispp literal)
  9235. ;; Parse the format string. Since we have a lot of modes, this is
  9236. ;; a lot of work. However, I think calc still uses most of the time.
  9237. (if (string-match ";" formula)
  9238. (let ((tmp (org-split-string formula ";")))
  9239. (setq formula (car tmp)
  9240. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9241. (nth 1 tmp)))
  9242. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9243. (setq c (string-to-char (match-string 1 fmt))
  9244. n (string-to-number (match-string 2 fmt)))
  9245. (if (= c ?p)
  9246. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9247. (setq modes (org-set-calc-mode
  9248. 'calc-float-format
  9249. (list (cdr (assoc c '((?n . float) (?f . fix)
  9250. (?s . sci) (?e . eng))))
  9251. n))))
  9252. (setq fmt (replace-match "" t t fmt)))
  9253. (if (string-match "[NT]" fmt)
  9254. (setq numbers (equal (match-string 0 fmt) "N")
  9255. fmt (replace-match "" t t fmt)))
  9256. (if (string-match "L" fmt)
  9257. (setq literal t
  9258. fmt (replace-match "" t t fmt)))
  9259. (if (string-match "E" fmt)
  9260. (setq keep-empty t
  9261. fmt (replace-match "" t t fmt)))
  9262. (while (string-match "[DRFS]" fmt)
  9263. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9264. (setq fmt (replace-match "" t t fmt)))
  9265. (unless (string-match "\\S-" fmt)
  9266. (setq fmt nil))))
  9267. (if (and (not suppress-const) org-table-formula-use-constants)
  9268. (setq formula (org-table-formula-substitute-names formula)))
  9269. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9270. (while (> ndown 0)
  9271. (setq fields (org-split-string
  9272. (org-no-properties
  9273. (buffer-substring (point-at-bol) (point-at-eol)))
  9274. " *| *"))
  9275. (if (eq numbers t)
  9276. (setq fields (mapcar
  9277. (lambda (x) (number-to-string (string-to-number x)))
  9278. fields)))
  9279. (setq ndown (1- ndown))
  9280. (setq form (copy-sequence formula)
  9281. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9282. (if (and lispp literal) (setq lispp 'literal))
  9283. ;; Check for old vertical references
  9284. (setq form (org-rewrite-old-row-references form))
  9285. ;; Insert complex ranges
  9286. (while (string-match org-table-range-regexp form)
  9287. (setq form
  9288. (replace-match
  9289. (save-match-data
  9290. (org-table-make-reference
  9291. (org-table-get-range (match-string 0 form) nil n0)
  9292. keep-empty numbers lispp))
  9293. t t form)))
  9294. ;; Insert simple ranges
  9295. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9296. (setq form
  9297. (replace-match
  9298. (save-match-data
  9299. (org-table-make-reference
  9300. (org-sublist
  9301. fields (string-to-number (match-string 1 form))
  9302. (string-to-number (match-string 2 form)))
  9303. keep-empty numbers lispp))
  9304. t t form)))
  9305. (setq form0 form)
  9306. ;; Insert the references to fields in same row
  9307. (while (string-match "\\$\\([0-9]+\\)" form)
  9308. (setq n (string-to-number (match-string 1 form))
  9309. x (nth (1- (if (= n 0) n0 n)) fields))
  9310. (unless x (error "Invalid field specifier \"%s\""
  9311. (match-string 0 form)))
  9312. (setq form (replace-match
  9313. (save-match-data
  9314. (org-table-make-reference x nil numbers lispp))
  9315. t t form)))
  9316. (if lispp
  9317. (setq ev (condition-case nil
  9318. (eval (eval (read form)))
  9319. (error "#ERROR"))
  9320. ev (if (numberp ev) (number-to-string ev) ev))
  9321. (or (fboundp 'calc-eval)
  9322. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9323. (setq ev (calc-eval (cons form modes)
  9324. (if numbers 'num))))
  9325. (when org-table-formula-debug
  9326. (with-output-to-temp-buffer "*Substitution History*"
  9327. (princ (format "Substitution history of formula
  9328. Orig: %s
  9329. $xyz-> %s
  9330. @r$c-> %s
  9331. $1-> %s\n" orig formula form0 form))
  9332. (if (listp ev)
  9333. (princ (format " %s^\nError: %s"
  9334. (make-string (car ev) ?\-) (nth 1 ev)))
  9335. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9336. ev (or fmt "NONE")
  9337. (if fmt (format fmt (string-to-number ev)) ev)))))
  9338. (setq bw (get-buffer-window "*Substitution History*"))
  9339. (shrink-window-if-larger-than-buffer bw)
  9340. (unless (and (interactive-p) (not ndown))
  9341. (unless (let (inhibit-redisplay)
  9342. (y-or-n-p "Debugging Formula. Continue to next? "))
  9343. (org-table-align)
  9344. (error "Abort"))
  9345. (delete-window bw)
  9346. (message "")))
  9347. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9348. (org-table-justify-field-maybe
  9349. (if fmt (format fmt (string-to-number ev)) ev))
  9350. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9351. (call-interactively 'org-return)
  9352. (setq ndown 0)))
  9353. (and down (org-table-maybe-recalculate-line))
  9354. (or suppress-align (and org-table-may-need-update
  9355. (org-table-align))))))
  9356. (defun org-table-put-field-property (prop value)
  9357. (save-excursion
  9358. (put-text-property (progn (skip-chars-backward "^|") (point))
  9359. (progn (skip-chars-forward "^|") (point))
  9360. prop value)))
  9361. (defun org-table-get-range (desc &optional tbeg col highlight)
  9362. "Get a calc vector from a column, accorting to descriptor DESC.
  9363. Optional arguments TBEG and COL can give the beginning of the table and
  9364. the current column, to avoid unnecessary parsing.
  9365. HIGHLIGHT means, just highlight the range."
  9366. (if (not (equal (string-to-char desc) ?@))
  9367. (setq desc (concat "@" desc)))
  9368. (save-excursion
  9369. (or tbeg (setq tbeg (org-table-begin)))
  9370. (or col (setq col (org-table-current-column)))
  9371. (let ((thisline (org-current-line))
  9372. beg end c1 c2 r1 r2 rangep tmp)
  9373. (unless (string-match org-table-range-regexp desc)
  9374. (error "Invalid table range specifier `%s'" desc))
  9375. (setq rangep (match-end 3)
  9376. r1 (and (match-end 1) (match-string 1 desc))
  9377. r2 (and (match-end 4) (match-string 4 desc))
  9378. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9379. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9380. (and c1 (setq c1 (+ (string-to-number c1)
  9381. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9382. (and c2 (setq c2 (+ (string-to-number c2)
  9383. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9384. (if (equal r1 "") (setq r1 nil))
  9385. (if (equal r2 "") (setq r2 nil))
  9386. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9387. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9388. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9389. (if (not r1) (setq r1 thisline))
  9390. (if (not r2) (setq r2 thisline))
  9391. (if (not c1) (setq c1 col))
  9392. (if (not c2) (setq c2 col))
  9393. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9394. ;; just one field
  9395. (progn
  9396. (goto-line r1)
  9397. (while (not (looking-at org-table-dataline-regexp))
  9398. (beginning-of-line 2))
  9399. (prog1 (org-trim (org-table-get-field c1))
  9400. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9401. ;; A range, return a vector
  9402. ;; First sort the numbers to get a regular ractangle
  9403. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9404. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9405. (goto-line r1)
  9406. (while (not (looking-at org-table-dataline-regexp))
  9407. (beginning-of-line 2))
  9408. (org-table-goto-column c1)
  9409. (setq beg (point))
  9410. (goto-line r2)
  9411. (while (not (looking-at org-table-dataline-regexp))
  9412. (beginning-of-line 0))
  9413. (org-table-goto-column c2)
  9414. (setq end (point))
  9415. (if highlight
  9416. (org-table-highlight-rectangle
  9417. beg (progn (skip-chars-forward "^|\n") (point))))
  9418. ;; return string representation of calc vector
  9419. (mapcar 'org-trim
  9420. (apply 'append (org-table-copy-region beg end)))))))
  9421. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9422. "Analyze descriptor DESC and retrieve the corresponding line number.
  9423. The cursor is currently in line CLINE, the table begins in line BLINE,
  9424. and TABLE is a vector with line types."
  9425. (if (string-match "^[0-9]+$" desc)
  9426. (aref org-table-dlines (string-to-number desc))
  9427. (setq cline (or cline (org-current-line))
  9428. bline (or bline org-table-current-begin-line)
  9429. table (or table org-table-current-line-types))
  9430. (if (or
  9431. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9432. ;; 1 2 3 4 5 6
  9433. (and (not (match-end 3)) (not (match-end 6)))
  9434. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9435. (error "invalid row descriptor `%s'" desc))
  9436. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9437. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9438. (odir (and (match-end 5) (match-string 5 desc)))
  9439. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9440. (i (- cline bline))
  9441. (rel (and (match-end 6)
  9442. (or (and (match-end 1) (not (match-end 3)))
  9443. (match-end 5)))))
  9444. (if (and hn (not hdir))
  9445. (progn
  9446. (setq i 0 hdir "+")
  9447. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9448. (if (and (not hn) on (not odir))
  9449. (error "should never happen");;(aref org-table-dlines on)
  9450. (if (and hn (> hn 0))
  9451. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9452. (if on
  9453. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9454. (+ bline i)))))
  9455. (defun org-find-row-type (table i type backwards relative n)
  9456. (let ((l (length table)))
  9457. (while (> n 0)
  9458. (while (and (setq i (+ i (if backwards -1 1)))
  9459. (>= i 0) (< i l)
  9460. (not (eq (aref table i) type))
  9461. (if (and relative (eq (aref table i) 'hline))
  9462. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9463. t)))
  9464. (setq n (1- n)))
  9465. (if (or (< i 0) (>= i l))
  9466. (error "Row descriptior leads outside table")
  9467. i)))
  9468. (defun org-rewrite-old-row-references (s)
  9469. (if (string-match "&[-+0-9I]" s)
  9470. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9471. s))
  9472. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9473. "Convert list ELEMENTS to something appropriate to insert into formula.
  9474. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9475. NUMBERS indicates that everything should be converted to numbers.
  9476. LISPP means to return something appropriate for a Lisp list."
  9477. (if (stringp elements) ; just a single val
  9478. (if lispp
  9479. (if (eq lispp 'literal)
  9480. elements
  9481. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9482. (if (equal elements "") (setq elements "0"))
  9483. (if numbers (number-to-string (string-to-number elements)) elements))
  9484. (unless keep-empty
  9485. (setq elements
  9486. (delq nil
  9487. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9488. elements))))
  9489. (setq elements (or elements '("0")))
  9490. (if lispp
  9491. (mapconcat
  9492. (lambda (x)
  9493. (if (eq lispp 'literal)
  9494. x
  9495. (prin1-to-string (if numbers (string-to-number x) x))))
  9496. elements " ")
  9497. (concat "[" (mapconcat
  9498. (lambda (x)
  9499. (if numbers (number-to-string (string-to-number x)) x))
  9500. elements
  9501. ",") "]"))))
  9502. (defun org-table-recalculate (&optional all noalign)
  9503. "Recalculate the current table line by applying all stored formulas.
  9504. With prefix arg ALL, do this for all lines in the table."
  9505. (interactive "P")
  9506. (or (memq this-command org-recalc-commands)
  9507. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9508. (unless (org-at-table-p) (error "Not at a table"))
  9509. (if (equal all '(16))
  9510. (org-table-iterate)
  9511. (org-table-get-specials)
  9512. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9513. (lambda (a b) (string< (car a) (car b)))))
  9514. (inhibit-redisplay (not debug-on-error))
  9515. (line-re org-table-dataline-regexp)
  9516. (thisline (org-current-line))
  9517. (thiscol (org-table-current-column))
  9518. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9519. ;; Insert constants in all formulas
  9520. (setq eqlist
  9521. (mapcar (lambda (x)
  9522. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9523. x)
  9524. eqlist))
  9525. ;; Split the equation list
  9526. (while (setq eq (pop eqlist))
  9527. (if (<= (string-to-char (car eq)) ?9)
  9528. (push eq eqlnum)
  9529. (push eq eqlname)))
  9530. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9531. (if all
  9532. (progn
  9533. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9534. (goto-char (setq beg (org-table-begin)))
  9535. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9536. ;; This is a table with marked lines, compute selected lines
  9537. (setq line-re org-table-recalculate-regexp)
  9538. ;; Move forward to the first non-header line
  9539. (if (and (re-search-forward org-table-dataline-regexp end t)
  9540. (re-search-forward org-table-hline-regexp end t)
  9541. (re-search-forward org-table-dataline-regexp end t))
  9542. (setq beg (match-beginning 0))
  9543. nil))) ;; just leave beg where it is
  9544. (setq beg (point-at-bol)
  9545. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9546. (goto-char beg)
  9547. (and all (message "Re-applying formulas to full table..."))
  9548. ;; First find the named fields, and mark them untouchanble
  9549. (remove-text-properties beg end '(org-untouchable t))
  9550. (while (setq eq (pop eqlname))
  9551. (setq name (car eq)
  9552. a (assoc name org-table-named-field-locations))
  9553. (and (not a)
  9554. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9555. (setq a (list name
  9556. (aref org-table-dlines
  9557. (string-to-number (match-string 1 name)))
  9558. (string-to-number (match-string 2 name)))))
  9559. (when (and a (or all (equal (nth 1 a) thisline)))
  9560. (message "Re-applying formula to field: %s" name)
  9561. (goto-line (nth 1 a))
  9562. (org-table-goto-column (nth 2 a))
  9563. (push (append a (list (cdr eq))) eqlname1)
  9564. (org-table-put-field-property :org-untouchable t)))
  9565. ;; Now evauluate the column formulas, but skip fields covered by
  9566. ;; field formulas
  9567. (goto-char beg)
  9568. (while (re-search-forward line-re end t)
  9569. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9570. ;; Unprotected line, recalculate
  9571. (and all (message "Re-applying formulas to full table...(line %d)"
  9572. (setq cnt (1+ cnt))))
  9573. (setq org-last-recalc-line (org-current-line))
  9574. (setq eql eqlnum)
  9575. (while (setq entry (pop eql))
  9576. (goto-line org-last-recalc-line)
  9577. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9578. (unless (get-text-property (point) :org-untouchable)
  9579. (org-table-eval-formula nil (cdr entry)
  9580. 'noalign 'nocst 'nostore 'noanalysis)))))
  9581. ;; Now evaluate the field formulas
  9582. (while (setq eq (pop eqlname1))
  9583. (message "Re-applying formula to field: %s" (car eq))
  9584. (goto-line (nth 1 eq))
  9585. (org-table-goto-column (nth 2 eq))
  9586. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9587. 'nostore 'noanalysis))
  9588. (goto-line thisline)
  9589. (org-table-goto-column thiscol)
  9590. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9591. (or noalign (and org-table-may-need-update (org-table-align))
  9592. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9593. ;; back to initial position
  9594. (message "Re-applying formulas...done")
  9595. (goto-line thisline)
  9596. (org-table-goto-column thiscol)
  9597. (or noalign (and org-table-may-need-update (org-table-align))
  9598. (and all (message "Re-applying formulas...done"))))))
  9599. (defun org-table-iterate (&optional arg)
  9600. "Recalculate the table until it does not change anymore."
  9601. (interactive "P")
  9602. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9603. (i 0)
  9604. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9605. thistbl)
  9606. (catch 'exit
  9607. (while (< i imax)
  9608. (setq i (1+ i))
  9609. (org-table-recalculate 'all)
  9610. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9611. (if (not (string= lasttbl thistbl))
  9612. (setq lasttbl thistbl)
  9613. (if (> i 1)
  9614. (message "Convergence after %d iterations" i)
  9615. (message "Table was already stable"))
  9616. (throw 'exit t)))
  9617. (error "No convergence after %d iterations" i))))
  9618. (defun org-table-formula-substitute-names (f)
  9619. "Replace $const with values in string F."
  9620. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9621. ;; First, check for column names
  9622. (while (setq start (string-match org-table-column-name-regexp f start))
  9623. (setq start (1+ start))
  9624. (setq a (assoc (match-string 1 f) org-table-column-names))
  9625. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9626. ;; Parameters and constants
  9627. (setq start 0)
  9628. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9629. (setq start (1+ start))
  9630. (if (setq a (save-match-data
  9631. (org-table-get-constant (match-string 1 f))))
  9632. (setq f (replace-match
  9633. (concat (if pp "(") a (if pp ")")) t t f))))
  9634. (if org-table-formula-debug
  9635. (put-text-property 0 (length f) :orig-formula f1 f))
  9636. f))
  9637. (defun org-table-get-constant (const)
  9638. "Find the value for a parameter or constant in a formula.
  9639. Parameters get priority."
  9640. (or (cdr (assoc const org-table-local-parameters))
  9641. (cdr (assoc const org-table-formula-constants-local))
  9642. (cdr (assoc const org-table-formula-constants))
  9643. (and (fboundp 'constants-get) (constants-get const))
  9644. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9645. (org-entry-get nil (substring const 5) 'inherit))
  9646. "#UNDEFINED_NAME"))
  9647. (defvar org-table-fedit-map
  9648. (let ((map (make-sparse-keymap)))
  9649. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9650. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9651. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9652. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9653. (org-defkey map "\C-c?" 'org-table-show-reference)
  9654. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9655. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9656. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9657. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9658. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9659. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9660. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9661. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9662. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9663. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9664. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9665. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9666. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9667. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9668. map))
  9669. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9670. '("Edit-Formulas"
  9671. ["Finish and Install" org-table-fedit-finish t]
  9672. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9673. ["Abort" org-table-fedit-abort t]
  9674. "--"
  9675. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9676. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9677. "--"
  9678. "Shift Reference at Point"
  9679. ["Up" org-table-fedit-ref-up t]
  9680. ["Down" org-table-fedit-ref-down t]
  9681. ["Left" org-table-fedit-ref-left t]
  9682. ["Right" org-table-fedit-ref-right t]
  9683. "-"
  9684. "Change Test Row for Column Formulas"
  9685. ["Up" org-table-fedit-line-up t]
  9686. ["Down" org-table-fedit-line-down t]
  9687. "--"
  9688. ["Scroll Table Window" org-table-fedit-scroll t]
  9689. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9690. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9691. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9692. org-table-overlay-coordinates)]
  9693. "--"
  9694. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9695. :style toggle :selected org-table-buffer-is-an]))
  9696. (defvar org-pos)
  9697. (defun org-table-edit-formulas ()
  9698. "Edit the formulas of the current table in a separate buffer."
  9699. (interactive)
  9700. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9701. (beginning-of-line 0))
  9702. (unless (org-at-table-p) (error "Not at a table"))
  9703. (org-table-get-specials)
  9704. (let ((key (org-table-current-field-formula 'key 'noerror))
  9705. (eql (sort (org-table-get-stored-formulas 'noerror)
  9706. 'org-table-formula-less-p))
  9707. (pos (move-marker (make-marker) (point)))
  9708. (startline 1)
  9709. (wc (current-window-configuration))
  9710. (titles '((column . "# Column Formulas\n")
  9711. (field . "# Field Formulas\n")
  9712. (named . "# Named Field Formulas\n")))
  9713. entry s type title)
  9714. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9715. (erase-buffer)
  9716. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9717. (let ((font-lock-global-modes '(not fundamental-mode)))
  9718. (fundamental-mode))
  9719. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9720. (org-set-local 'org-pos pos)
  9721. (org-set-local 'org-window-configuration wc)
  9722. (use-local-map org-table-fedit-map)
  9723. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9724. (easy-menu-add org-table-fedit-menu)
  9725. (setq startline (org-current-line))
  9726. (while (setq entry (pop eql))
  9727. (setq type (cond
  9728. ((equal (string-to-char (car entry)) ?@) 'field)
  9729. ((string-match "^[0-9]" (car entry)) 'column)
  9730. (t 'named)))
  9731. (when (setq title (assq type titles))
  9732. (or (bobp) (insert "\n"))
  9733. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9734. (setq titles (delq title titles)))
  9735. (if (equal key (car entry)) (setq startline (org-current-line)))
  9736. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9737. (car entry) " = " (cdr entry) "\n"))
  9738. (remove-text-properties 0 (length s) '(face nil) s)
  9739. (insert s))
  9740. (if (eq org-table-use-standard-references t)
  9741. (org-table-fedit-toggle-ref-type))
  9742. (goto-line startline)
  9743. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9744. (defun org-table-fedit-post-command ()
  9745. (when (not (memq this-command '(lisp-complete-symbol)))
  9746. (let ((win (selected-window)))
  9747. (save-excursion
  9748. (condition-case nil
  9749. (org-table-show-reference)
  9750. (error nil))
  9751. (select-window win)))))
  9752. (defun org-table-formula-to-user (s)
  9753. "Convert a formula from internal to user representation."
  9754. (if (eq org-table-use-standard-references t)
  9755. (org-table-convert-refs-to-an s)
  9756. s))
  9757. (defun org-table-formula-from-user (s)
  9758. "Convert a formula from user to internal representation."
  9759. (if org-table-use-standard-references
  9760. (org-table-convert-refs-to-rc s)
  9761. s))
  9762. (defun org-table-convert-refs-to-rc (s)
  9763. "Convert spreadsheet references from AB7 to @7$28.
  9764. Works for single references, but also for entire formulas and even the
  9765. full TBLFM line."
  9766. (let ((start 0))
  9767. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9768. (cond
  9769. ((match-end 3)
  9770. ;; format match, just advance
  9771. (setq start (match-end 0)))
  9772. ((and (> (match-beginning 0) 0)
  9773. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9774. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9775. ;; 3.e5 or something like this.
  9776. (setq start (match-end 0)))
  9777. (t
  9778. (setq start (match-beginning 0)
  9779. s (replace-match
  9780. (if (equal (match-string 2 s) "&")
  9781. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9782. (format "@%d$%d"
  9783. (string-to-number (match-string 2 s))
  9784. (org-letters-to-number (match-string 1 s))))
  9785. t t s)))))
  9786. s))
  9787. (defun org-table-convert-refs-to-an (s)
  9788. "Convert spreadsheet references from to @7$28 to AB7.
  9789. Works for single references, but also for entire formulas and even the
  9790. full TBLFM line."
  9791. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9792. (setq s (replace-match
  9793. (format "%s%d"
  9794. (org-number-to-letters
  9795. (string-to-number (match-string 2 s)))
  9796. (string-to-number (match-string 1 s)))
  9797. t t s)))
  9798. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9799. (setq s (replace-match (concat "\\1"
  9800. (org-number-to-letters
  9801. (string-to-number (match-string 2 s))) "&")
  9802. t nil s)))
  9803. s)
  9804. (defun org-letters-to-number (s)
  9805. "Convert a base 26 number represented by letters into an integer.
  9806. For example: AB -> 28."
  9807. (let ((n 0))
  9808. (setq s (upcase s))
  9809. (while (> (length s) 0)
  9810. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9811. s (substring s 1)))
  9812. n))
  9813. (defun org-number-to-letters (n)
  9814. "Convert an integer into a base 26 number represented by letters.
  9815. For example: 28 -> AB."
  9816. (let ((s ""))
  9817. (while (> n 0)
  9818. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9819. n (/ (1- n) 26)))
  9820. s))
  9821. (defun org-table-fedit-convert-buffer (function)
  9822. "Convert all references in this buffer, using FUNTION."
  9823. (let ((line (org-current-line)))
  9824. (goto-char (point-min))
  9825. (while (not (eobp))
  9826. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9827. (delete-region (point) (point-at-eol))
  9828. (or (eobp) (forward-char 1)))
  9829. (goto-line line)))
  9830. (defun org-table-fedit-toggle-ref-type ()
  9831. "Convert all references in the buffer from B3 to @3$2 and back."
  9832. (interactive)
  9833. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9834. (org-table-fedit-convert-buffer
  9835. (if org-table-buffer-is-an
  9836. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9837. (message "Reference type switched to %s"
  9838. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9839. (defun org-table-fedit-ref-up ()
  9840. "Shift the reference at point one row/hline up."
  9841. (interactive)
  9842. (org-table-fedit-shift-reference 'up))
  9843. (defun org-table-fedit-ref-down ()
  9844. "Shift the reference at point one row/hline down."
  9845. (interactive)
  9846. (org-table-fedit-shift-reference 'down))
  9847. (defun org-table-fedit-ref-left ()
  9848. "Shift the reference at point one field to the left."
  9849. (interactive)
  9850. (org-table-fedit-shift-reference 'left))
  9851. (defun org-table-fedit-ref-right ()
  9852. "Shift the reference at point one field to the right."
  9853. (interactive)
  9854. (org-table-fedit-shift-reference 'right))
  9855. (defun org-table-fedit-shift-reference (dir)
  9856. (cond
  9857. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9858. (if (memq dir '(left right))
  9859. (org-rematch-and-replace 1 (eq dir 'left))
  9860. (error "Cannot shift reference in this direction")))
  9861. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9862. ;; A B3-like reference
  9863. (if (memq dir '(up down))
  9864. (org-rematch-and-replace 2 (eq dir 'up))
  9865. (org-rematch-and-replace 1 (eq dir 'left))))
  9866. ((org-at-regexp-p
  9867. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9868. ;; An internal reference
  9869. (if (memq dir '(up down))
  9870. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9871. (org-rematch-and-replace 5 (eq dir 'left))))))
  9872. (defun org-rematch-and-replace (n &optional decr hline)
  9873. "Re-match the group N, and replace it with the shifted refrence."
  9874. (or (match-end n) (error "Cannot shift reference in this direction"))
  9875. (goto-char (match-beginning n))
  9876. (and (looking-at (regexp-quote (match-string n)))
  9877. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9878. t t)))
  9879. (defun org-shift-refpart (ref &optional decr hline)
  9880. "Shift a refrence part REF.
  9881. If DECR is set, decrease the references row/column, else increase.
  9882. If HLINE is set, this may be a hline reference, it certainly is not
  9883. a translation reference."
  9884. (save-match-data
  9885. (let* ((sign (string-match "^[-+]" ref)) n)
  9886. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9887. (cond
  9888. ((and hline (string-match "^I+" ref))
  9889. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9890. (setq n (+ n (if decr -1 1)))
  9891. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9892. (if sign
  9893. (setq sign (if (< n 0) "-" "+") n (abs n))
  9894. (setq n (max 1 n)))
  9895. (concat sign (make-string n ?I)))
  9896. ((string-match "^[0-9]+" ref)
  9897. (setq n (string-to-number (concat sign ref)))
  9898. (setq n (+ n (if decr -1 1)))
  9899. (if sign
  9900. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9901. (number-to-string (max 1 n))))
  9902. ((string-match "^[a-zA-Z]+" ref)
  9903. (org-number-to-letters
  9904. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9905. (t (error "Cannot shift reference"))))))
  9906. (defun org-table-fedit-toggle-coordinates ()
  9907. "Toggle the display of coordinates in the refrenced table."
  9908. (interactive)
  9909. (let ((pos (marker-position org-pos)))
  9910. (with-current-buffer (marker-buffer org-pos)
  9911. (save-excursion
  9912. (goto-char pos)
  9913. (org-table-toggle-coordinate-overlays)))))
  9914. (defun org-table-fedit-finish (&optional arg)
  9915. "Parse the buffer for formula definitions and install them.
  9916. With prefix ARG, apply the new formulas to the table."
  9917. (interactive "P")
  9918. (org-table-remove-rectangle-highlight)
  9919. (if org-table-use-standard-references
  9920. (progn
  9921. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9922. (setq org-table-buffer-is-an nil)))
  9923. (let ((pos org-pos) eql var form)
  9924. (goto-char (point-min))
  9925. (while (re-search-forward
  9926. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9927. nil t)
  9928. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9929. form (match-string 3))
  9930. (setq form (org-trim form))
  9931. (when (not (equal form ""))
  9932. (while (string-match "[ \t]*\n[ \t]*" form)
  9933. (setq form (replace-match " " t t form)))
  9934. (when (assoc var eql)
  9935. (error "Double formulas for %s" var))
  9936. (push (cons var form) eql)))
  9937. (setq org-pos nil)
  9938. (set-window-configuration org-window-configuration)
  9939. (select-window (get-buffer-window (marker-buffer pos)))
  9940. (goto-char pos)
  9941. (unless (org-at-table-p)
  9942. (error "Lost table position - cannot install formulae"))
  9943. (org-table-store-formulas eql)
  9944. (move-marker pos nil)
  9945. (kill-buffer "*Edit Formulas*")
  9946. (if arg
  9947. (org-table-recalculate 'all)
  9948. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9949. (defun org-table-fedit-abort ()
  9950. "Abort editing formulas, without installing the changes."
  9951. (interactive)
  9952. (org-table-remove-rectangle-highlight)
  9953. (let ((pos org-pos))
  9954. (set-window-configuration org-window-configuration)
  9955. (select-window (get-buffer-window (marker-buffer pos)))
  9956. (goto-char pos)
  9957. (move-marker pos nil)
  9958. (message "Formula editing aborted without installing changes")))
  9959. (defun org-table-fedit-lisp-indent ()
  9960. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9961. (interactive)
  9962. (let ((pos (point)) beg end ind)
  9963. (beginning-of-line 1)
  9964. (cond
  9965. ((looking-at "[ \t]")
  9966. (goto-char pos)
  9967. (call-interactively 'lisp-indent-line))
  9968. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9969. ((not (fboundp 'pp-buffer))
  9970. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9971. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9972. (goto-char (- (match-end 0) 2))
  9973. (setq beg (point))
  9974. (setq ind (make-string (current-column) ?\ ))
  9975. (condition-case nil (forward-sexp 1)
  9976. (error
  9977. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9978. (setq end (point))
  9979. (save-restriction
  9980. (narrow-to-region beg end)
  9981. (if (eq last-command this-command)
  9982. (progn
  9983. (goto-char (point-min))
  9984. (setq this-command nil)
  9985. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9986. (replace-match " ")))
  9987. (pp-buffer)
  9988. (untabify (point-min) (point-max))
  9989. (goto-char (1+ (point-min)))
  9990. (while (re-search-forward "^." nil t)
  9991. (beginning-of-line 1)
  9992. (insert ind))
  9993. (goto-char (point-max))
  9994. (backward-delete-char 1)))
  9995. (goto-char beg))
  9996. (t nil))))
  9997. (defvar org-show-positions nil)
  9998. (defun org-table-show-reference (&optional local)
  9999. "Show the location/value of the $ expression at point."
  10000. (interactive)
  10001. (org-table-remove-rectangle-highlight)
  10002. (catch 'exit
  10003. (let ((pos (if local (point) org-pos))
  10004. (face2 'highlight)
  10005. (org-inhibit-highlight-removal t)
  10006. (win (selected-window))
  10007. (org-show-positions nil)
  10008. var name e what match dest)
  10009. (if local (org-table-get-specials))
  10010. (setq what (cond
  10011. ((or (org-at-regexp-p org-table-range-regexp2)
  10012. (org-at-regexp-p org-table-translate-regexp)
  10013. (org-at-regexp-p org-table-range-regexp))
  10014. (setq match
  10015. (save-match-data
  10016. (org-table-convert-refs-to-rc (match-string 0))))
  10017. 'range)
  10018. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10019. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10020. ((not local) nil)
  10021. (t (error "No reference at point")))
  10022. match (and what (or match (match-string 0))))
  10023. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10024. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10025. 'secondary-selection))
  10026. (org-add-hook 'before-change-functions
  10027. 'org-table-remove-rectangle-highlight)
  10028. (if (eq what 'name) (setq var (substring match 1)))
  10029. (when (eq what 'range)
  10030. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10031. (setq match (org-table-formula-substitute-names match)))
  10032. (unless local
  10033. (save-excursion
  10034. (end-of-line 1)
  10035. (re-search-backward "^\\S-" nil t)
  10036. (beginning-of-line 1)
  10037. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10038. (setq dest
  10039. (save-match-data
  10040. (org-table-convert-refs-to-rc (match-string 1))))
  10041. (org-table-add-rectangle-overlay
  10042. (match-beginning 1) (match-end 1) face2))))
  10043. (if (and (markerp pos) (marker-buffer pos))
  10044. (if (get-buffer-window (marker-buffer pos))
  10045. (select-window (get-buffer-window (marker-buffer pos)))
  10046. (org-switch-to-buffer-other-window (get-buffer-window
  10047. (marker-buffer pos)))))
  10048. (goto-char pos)
  10049. (org-table-force-dataline)
  10050. (when dest
  10051. (setq name (substring dest 1))
  10052. (cond
  10053. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10054. (setq e (assoc name org-table-named-field-locations))
  10055. (goto-line (nth 1 e))
  10056. (org-table-goto-column (nth 2 e)))
  10057. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10058. (let ((l (string-to-number (match-string 1 dest)))
  10059. (c (string-to-number (match-string 2 dest))))
  10060. (goto-line (aref org-table-dlines l))
  10061. (org-table-goto-column c)))
  10062. (t (org-table-goto-column (string-to-number name))))
  10063. (move-marker pos (point))
  10064. (org-table-highlight-rectangle nil nil face2))
  10065. (cond
  10066. ((equal dest match))
  10067. ((not match))
  10068. ((eq what 'range)
  10069. (condition-case nil
  10070. (save-excursion
  10071. (org-table-get-range match nil nil 'highlight))
  10072. (error nil)))
  10073. ((setq e (assoc var org-table-named-field-locations))
  10074. (goto-line (nth 1 e))
  10075. (org-table-goto-column (nth 2 e))
  10076. (org-table-highlight-rectangle (point) (point))
  10077. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10078. ((setq e (assoc var org-table-column-names))
  10079. (org-table-goto-column (string-to-number (cdr e)))
  10080. (org-table-highlight-rectangle (point) (point))
  10081. (goto-char (org-table-begin))
  10082. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10083. (org-table-end) t)
  10084. (progn
  10085. (goto-char (match-beginning 1))
  10086. (org-table-highlight-rectangle)
  10087. (message "Named column (column %s)" (cdr e)))
  10088. (error "Column name not found")))
  10089. ((eq what 'column)
  10090. ;; column number
  10091. (org-table-goto-column (string-to-number (substring match 1)))
  10092. (org-table-highlight-rectangle (point) (point))
  10093. (message "Column %s" (substring match 1)))
  10094. ((setq e (assoc var org-table-local-parameters))
  10095. (goto-char (org-table-begin))
  10096. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10097. (progn
  10098. (goto-char (match-beginning 1))
  10099. (org-table-highlight-rectangle)
  10100. (message "Local parameter."))
  10101. (error "Parameter not found")))
  10102. (t
  10103. (cond
  10104. ((not var) (error "No reference at point"))
  10105. ((setq e (assoc var org-table-formula-constants-local))
  10106. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10107. var (cdr e)))
  10108. ((setq e (assoc var org-table-formula-constants))
  10109. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10110. var (cdr e)))
  10111. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10112. (message "Constant: $%s=%s, from `constants.el'%s."
  10113. var e (format " (%s units)" constants-unit-system)))
  10114. (t (error "Undefined name $%s" var)))))
  10115. (goto-char pos)
  10116. (when (and org-show-positions
  10117. (not (memq this-command '(org-table-fedit-scroll
  10118. org-table-fedit-scroll-down))))
  10119. (push pos org-show-positions)
  10120. (push org-table-current-begin-pos org-show-positions)
  10121. (let ((min (apply 'min org-show-positions))
  10122. (max (apply 'max org-show-positions)))
  10123. (goto-char min) (recenter 0)
  10124. (goto-char max)
  10125. (or (pos-visible-in-window-p max) (recenter -1))))
  10126. (select-window win))))
  10127. (defun org-table-force-dataline ()
  10128. "Make sure the cursor is in a dataline in a table."
  10129. (unless (save-excursion
  10130. (beginning-of-line 1)
  10131. (looking-at org-table-dataline-regexp))
  10132. (let* ((re org-table-dataline-regexp)
  10133. (p1 (save-excursion (re-search-forward re nil 'move)))
  10134. (p2 (save-excursion (re-search-backward re nil 'move))))
  10135. (cond ((and p1 p2)
  10136. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10137. p1 p2)))
  10138. ((or p1 p2) (goto-char (or p1 p2)))
  10139. (t (error "No table dataline around here"))))))
  10140. (defun org-table-fedit-line-up ()
  10141. "Move cursor one line up in the window showing the table."
  10142. (interactive)
  10143. (org-table-fedit-move 'previous-line))
  10144. (defun org-table-fedit-line-down ()
  10145. "Move cursor one line down in the window showing the table."
  10146. (interactive)
  10147. (org-table-fedit-move 'next-line))
  10148. (defun org-table-fedit-move (command)
  10149. "Move the cursor in the window shoinw the table.
  10150. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10151. (let ((org-table-allow-automatic-line-recalculation nil)
  10152. (pos org-pos) (win (selected-window)) p)
  10153. (select-window (get-buffer-window (marker-buffer org-pos)))
  10154. (setq p (point))
  10155. (call-interactively command)
  10156. (while (and (org-at-table-p)
  10157. (org-at-table-hline-p))
  10158. (call-interactively command))
  10159. (or (org-at-table-p) (goto-char p))
  10160. (move-marker pos (point))
  10161. (select-window win)))
  10162. (defun org-table-fedit-scroll (N)
  10163. (interactive "p")
  10164. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10165. (scroll-other-window N)))
  10166. (defun org-table-fedit-scroll-down (N)
  10167. (interactive "p")
  10168. (org-table-fedit-scroll (- N)))
  10169. (defvar org-table-rectangle-overlays nil)
  10170. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10171. "Add a new overlay."
  10172. (let ((ov (org-make-overlay beg end)))
  10173. (org-overlay-put ov 'face (or face 'secondary-selection))
  10174. (push ov org-table-rectangle-overlays)))
  10175. (defun org-table-highlight-rectangle (&optional beg end face)
  10176. "Highlight rectangular region in a table."
  10177. (setq beg (or beg (point)) end (or end (point)))
  10178. (let ((b (min beg end))
  10179. (e (max beg end))
  10180. l1 c1 l2 c2 tmp)
  10181. (and (boundp 'org-show-positions)
  10182. (setq org-show-positions (cons b (cons e org-show-positions))))
  10183. (goto-char (min beg end))
  10184. (setq l1 (org-current-line)
  10185. c1 (org-table-current-column))
  10186. (goto-char (max beg end))
  10187. (setq l2 (org-current-line)
  10188. c2 (org-table-current-column))
  10189. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10190. (goto-line l1)
  10191. (beginning-of-line 1)
  10192. (loop for line from l1 to l2 do
  10193. (when (looking-at org-table-dataline-regexp)
  10194. (org-table-goto-column c1)
  10195. (skip-chars-backward "^|\n") (setq beg (point))
  10196. (org-table-goto-column c2)
  10197. (skip-chars-forward "^|\n") (setq end (point))
  10198. (org-table-add-rectangle-overlay beg end face))
  10199. (beginning-of-line 2))
  10200. (goto-char b))
  10201. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10202. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10203. "Remove the rectangle overlays."
  10204. (unless org-inhibit-highlight-removal
  10205. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10206. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10207. (setq org-table-rectangle-overlays nil)))
  10208. (defvar org-table-coordinate-overlays nil
  10209. "Collects the cooordinate grid overlays, so that they can be removed.")
  10210. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10211. (defun org-table-overlay-coordinates ()
  10212. "Add overlays to the table at point, to show row/column coordinates."
  10213. (interactive)
  10214. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10215. (setq org-table-coordinate-overlays nil)
  10216. (save-excursion
  10217. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10218. (goto-char (org-table-begin))
  10219. (while (org-at-table-p)
  10220. (setq eol (point-at-eol))
  10221. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10222. (push ov org-table-coordinate-overlays)
  10223. (setq hline (looking-at org-table-hline-regexp))
  10224. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10225. (format "%4d" (setq id (1+ id)))))
  10226. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10227. (when hline
  10228. (setq ic 0)
  10229. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10230. (setq beg (1+ (match-beginning 0))
  10231. ic (1+ ic)
  10232. s1 (concat "$" (int-to-string ic))
  10233. s2 (org-number-to-letters ic)
  10234. str (if (eq org-table-use-standard-references t) s2 s1))
  10235. (setq ov (org-make-overlay beg (+ beg (length str))))
  10236. (push ov org-table-coordinate-overlays)
  10237. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10238. (beginning-of-line 2)))))
  10239. (defun org-table-toggle-coordinate-overlays ()
  10240. "Toggle the display of Row/Column numbers in tables."
  10241. (interactive)
  10242. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10243. (message "Row/Column number display turned %s"
  10244. (if org-table-overlay-coordinates "on" "off"))
  10245. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10246. (org-table-align))
  10247. (unless org-table-overlay-coordinates
  10248. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10249. (setq org-table-coordinate-overlays nil)))
  10250. (defun org-table-toggle-formula-debugger ()
  10251. "Toggle the formula debugger in tables."
  10252. (interactive)
  10253. (setq org-table-formula-debug (not org-table-formula-debug))
  10254. (message "Formula debugging has been turned %s"
  10255. (if org-table-formula-debug "on" "off")))
  10256. ;;; The orgtbl minor mode
  10257. ;; Define a minor mode which can be used in other modes in order to
  10258. ;; integrate the org-mode table editor.
  10259. ;; This is really a hack, because the org-mode table editor uses several
  10260. ;; keys which normally belong to the major mode, for example the TAB and
  10261. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10262. ;; necessary to operate the table editor, but wraps the commands into a
  10263. ;; function which tests if the cursor is currently inside a table. If that
  10264. ;; is the case, the table editor command is executed. However, when any of
  10265. ;; those keys is used outside a table, the function uses `key-binding' to
  10266. ;; look up if the key has an associated command in another currently active
  10267. ;; keymap (minor modes, major mode, global), and executes that command.
  10268. ;; There might be problems if any of the keys used by the table editor is
  10269. ;; otherwise used as a prefix key.
  10270. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10271. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10272. ;; addresses this by checking explicitly for both bindings.
  10273. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10274. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10275. ;; Some modes bind other commands to simple characters, for example
  10276. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10277. ;; active, this binding is ignored inside tables and replaced with a
  10278. ;; modified self-insert.
  10279. (defvar orgtbl-mode nil
  10280. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10281. table editor in arbitrary modes.")
  10282. (make-variable-buffer-local 'orgtbl-mode)
  10283. (defvar orgtbl-mode-map (make-keymap)
  10284. "Keymap for `orgtbl-mode'.")
  10285. ;;;###autoload
  10286. (defun turn-on-orgtbl ()
  10287. "Unconditionally turn on `orgtbl-mode'."
  10288. (orgtbl-mode 1))
  10289. (defvar org-old-auto-fill-inhibit-regexp nil
  10290. "Local variable used by `orgtbl-mode'")
  10291. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10292. "Matches a line belonging to an orgtbl.")
  10293. (defconst orgtbl-extra-font-lock-keywords
  10294. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10295. 0 (quote 'org-table) 'prepend))
  10296. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10297. ;;;###autoload
  10298. (defun orgtbl-mode (&optional arg)
  10299. "The `org-mode' table editor as a minor mode for use in other modes."
  10300. (interactive)
  10301. (if (org-mode-p)
  10302. ;; Exit without error, in case some hook functions calls this
  10303. ;; by accident in org-mode.
  10304. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10305. (setq orgtbl-mode
  10306. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10307. (if orgtbl-mode
  10308. (progn
  10309. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10310. ;; Make sure we are first in minor-mode-map-alist
  10311. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10312. (and c (setq minor-mode-map-alist
  10313. (cons c (delq c minor-mode-map-alist)))))
  10314. (org-set-local (quote org-table-may-need-update) t)
  10315. (org-add-hook 'before-change-functions 'org-before-change-function
  10316. nil 'local)
  10317. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10318. auto-fill-inhibit-regexp)
  10319. (org-set-local 'auto-fill-inhibit-regexp
  10320. (if auto-fill-inhibit-regexp
  10321. (concat orgtbl-line-start-regexp "\\|"
  10322. auto-fill-inhibit-regexp)
  10323. orgtbl-line-start-regexp))
  10324. (org-add-to-invisibility-spec '(org-cwidth))
  10325. (when (fboundp 'font-lock-add-keywords)
  10326. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10327. (org-restart-font-lock))
  10328. (easy-menu-add orgtbl-mode-menu)
  10329. (run-hooks 'orgtbl-mode-hook))
  10330. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10331. (org-cleanup-narrow-column-properties)
  10332. (org-remove-from-invisibility-spec '(org-cwidth))
  10333. (remove-hook 'before-change-functions 'org-before-change-function t)
  10334. (when (fboundp 'font-lock-remove-keywords)
  10335. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10336. (org-restart-font-lock))
  10337. (easy-menu-remove orgtbl-mode-menu)
  10338. (force-mode-line-update 'all))))
  10339. (defun org-cleanup-narrow-column-properties ()
  10340. "Remove all properties related to narrow-column invisibility."
  10341. (let ((s 1))
  10342. (while (setq s (text-property-any s (point-max)
  10343. 'display org-narrow-column-arrow))
  10344. (remove-text-properties s (1+ s) '(display t)))
  10345. (setq s 1)
  10346. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10347. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10348. (setq s 1)
  10349. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10350. (remove-text-properties s (1+ s) '(invisible t)))))
  10351. ;; Install it as a minor mode.
  10352. (put 'orgtbl-mode :included t)
  10353. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10354. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10355. (defun orgtbl-make-binding (fun n &rest keys)
  10356. "Create a function for binding in the table minor mode.
  10357. FUN is the command to call inside a table. N is used to create a unique
  10358. command name. KEYS are keys that should be checked in for a command
  10359. to execute outside of tables."
  10360. (eval
  10361. (list 'defun
  10362. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10363. '(arg)
  10364. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10365. "Outside of tables, run the binding of `"
  10366. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10367. "'.")
  10368. '(interactive "p")
  10369. (list 'if
  10370. '(org-at-table-p)
  10371. (list 'call-interactively (list 'quote fun))
  10372. (list 'let '(orgtbl-mode)
  10373. (list 'call-interactively
  10374. (append '(or)
  10375. (mapcar (lambda (k)
  10376. (list 'key-binding k))
  10377. keys)
  10378. '('orgtbl-error))))))))
  10379. (defun orgtbl-error ()
  10380. "Error when there is no default binding for a table key."
  10381. (interactive)
  10382. (error "This key has no function outside tables"))
  10383. (defun orgtbl-setup ()
  10384. "Setup orgtbl keymaps."
  10385. (let ((nfunc 0)
  10386. (bindings
  10387. (list
  10388. '([(meta shift left)] org-table-delete-column)
  10389. '([(meta left)] org-table-move-column-left)
  10390. '([(meta right)] org-table-move-column-right)
  10391. '([(meta shift right)] org-table-insert-column)
  10392. '([(meta shift up)] org-table-kill-row)
  10393. '([(meta shift down)] org-table-insert-row)
  10394. '([(meta up)] org-table-move-row-up)
  10395. '([(meta down)] org-table-move-row-down)
  10396. '("\C-c\C-w" org-table-cut-region)
  10397. '("\C-c\M-w" org-table-copy-region)
  10398. '("\C-c\C-y" org-table-paste-rectangle)
  10399. '("\C-c-" org-table-insert-hline)
  10400. '("\C-c}" org-table-toggle-coordinate-overlays)
  10401. '("\C-c{" org-table-toggle-formula-debugger)
  10402. '("\C-m" org-table-next-row)
  10403. '([(shift return)] org-table-copy-down)
  10404. '("\C-c\C-q" org-table-wrap-region)
  10405. '("\C-c?" org-table-field-info)
  10406. '("\C-c " org-table-blank-field)
  10407. '("\C-c+" org-table-sum)
  10408. '("\C-c=" org-table-eval-formula)
  10409. '("\C-c'" org-table-edit-formulas)
  10410. '("\C-c`" org-table-edit-field)
  10411. '("\C-c*" org-table-recalculate)
  10412. '("\C-c|" org-table-create-or-convert-from-region)
  10413. '("\C-c^" org-table-sort-lines)
  10414. '([(control ?#)] org-table-rotate-recalc-marks)))
  10415. elt key fun cmd)
  10416. (while (setq elt (pop bindings))
  10417. (setq nfunc (1+ nfunc))
  10418. (setq key (org-key (car elt))
  10419. fun (nth 1 elt)
  10420. cmd (orgtbl-make-binding fun nfunc key))
  10421. (org-defkey orgtbl-mode-map key cmd))
  10422. ;; Special treatment needed for TAB and RET
  10423. (org-defkey orgtbl-mode-map [(return)]
  10424. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10425. (org-defkey orgtbl-mode-map "\C-m"
  10426. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10427. (org-defkey orgtbl-mode-map [(tab)]
  10428. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10429. (org-defkey orgtbl-mode-map "\C-i"
  10430. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10431. (org-defkey orgtbl-mode-map [(shift tab)]
  10432. (orgtbl-make-binding 'org-table-previous-field 104
  10433. [(shift tab)] [(tab)] "\C-i"))
  10434. (org-defkey orgtbl-mode-map "\M-\C-m"
  10435. (orgtbl-make-binding 'org-table-wrap-region 105
  10436. "\M-\C-m" [(meta return)]))
  10437. (org-defkey orgtbl-mode-map [(meta return)]
  10438. (orgtbl-make-binding 'org-table-wrap-region 106
  10439. [(meta return)] "\M-\C-m"))
  10440. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10441. (when orgtbl-optimized
  10442. ;; If the user wants maximum table support, we need to hijack
  10443. ;; some standard editing functions
  10444. (org-remap orgtbl-mode-map
  10445. 'self-insert-command 'orgtbl-self-insert-command
  10446. 'delete-char 'org-delete-char
  10447. 'delete-backward-char 'org-delete-backward-char)
  10448. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10449. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10450. '("OrgTbl"
  10451. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10452. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10453. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10454. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10455. "--"
  10456. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10457. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10458. ["Copy Field from Above"
  10459. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10460. "--"
  10461. ("Column"
  10462. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10463. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10464. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10465. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10466. ("Row"
  10467. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10468. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10469. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10470. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10471. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10472. "--"
  10473. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10474. ("Rectangle"
  10475. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10476. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10477. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10478. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10479. "--"
  10480. ("Radio tables"
  10481. ["Insert table template" orgtbl-insert-radio-table
  10482. (assq major-mode orgtbl-radio-table-templates)]
  10483. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10484. "--"
  10485. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10486. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10487. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10488. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10489. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10490. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10491. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10492. ["Sum Column/Rectangle" org-table-sum
  10493. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10494. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10495. ["Debug Formulas"
  10496. org-table-toggle-formula-debugger :active (org-at-table-p)
  10497. :keys "C-c {"
  10498. :style toggle :selected org-table-formula-debug]
  10499. ["Show Col/Row Numbers"
  10500. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10501. :keys "C-c }"
  10502. :style toggle :selected org-table-overlay-coordinates]
  10503. ))
  10504. t))
  10505. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10506. "If the cursor is inside a table, realign the table.
  10507. It it is a table to be sent away to a receiver, do it.
  10508. With prefix arg, also recompute table."
  10509. (interactive "P")
  10510. (let ((pos (point)) action)
  10511. (save-excursion
  10512. (beginning-of-line 1)
  10513. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10514. ((looking-at "[ \t]*|") pos)
  10515. ((looking-at "#\\+TBLFM:") 'recalc))))
  10516. (cond
  10517. ((integerp action)
  10518. (goto-char action)
  10519. (org-table-maybe-eval-formula)
  10520. (if arg
  10521. (call-interactively 'org-table-recalculate)
  10522. (org-table-maybe-recalculate-line))
  10523. (call-interactively 'org-table-align)
  10524. (orgtbl-send-table 'maybe))
  10525. ((eq action 'recalc)
  10526. (save-excursion
  10527. (beginning-of-line 1)
  10528. (skip-chars-backward " \r\n\t")
  10529. (if (org-at-table-p)
  10530. (org-call-with-arg 'org-table-recalculate t))))
  10531. (t (let (orgtbl-mode)
  10532. (call-interactively (key-binding "\C-c\C-c")))))))
  10533. (defun orgtbl-tab (arg)
  10534. "Justification and field motion for `orgtbl-mode'."
  10535. (interactive "P")
  10536. (if arg (org-table-edit-field t)
  10537. (org-table-justify-field-maybe)
  10538. (org-table-next-field)))
  10539. (defun orgtbl-ret ()
  10540. "Justification and field motion for `orgtbl-mode'."
  10541. (interactive)
  10542. (org-table-justify-field-maybe)
  10543. (org-table-next-row))
  10544. (defun orgtbl-self-insert-command (N)
  10545. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10546. If the cursor is in a table looking at whitespace, the whitespace is
  10547. overwritten, and the table is not marked as requiring realignment."
  10548. (interactive "p")
  10549. (if (and (org-at-table-p)
  10550. (or
  10551. (and org-table-auto-blank-field
  10552. (member last-command
  10553. '(orgtbl-hijacker-command-100
  10554. orgtbl-hijacker-command-101
  10555. orgtbl-hijacker-command-102
  10556. orgtbl-hijacker-command-103
  10557. orgtbl-hijacker-command-104
  10558. orgtbl-hijacker-command-105))
  10559. (org-table-blank-field))
  10560. t)
  10561. (eq N 1)
  10562. (looking-at "[^|\n]* +|"))
  10563. (let (org-table-may-need-update)
  10564. (goto-char (1- (match-end 0)))
  10565. (delete-backward-char 1)
  10566. (goto-char (match-beginning 0))
  10567. (self-insert-command N))
  10568. (setq org-table-may-need-update t)
  10569. (let (orgtbl-mode)
  10570. (call-interactively (key-binding (vector last-input-event))))))
  10571. (defun org-force-self-insert (N)
  10572. "Needed to enforce self-insert under remapping."
  10573. (interactive "p")
  10574. (self-insert-command N))
  10575. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10576. "Regula expression matching exponentials as produced by calc.")
  10577. (defvar org-table-clean-did-remove-column nil)
  10578. (defun orgtbl-export (table target)
  10579. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10580. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10581. org-table-last-alignment org-table-last-column-widths
  10582. maxcol column)
  10583. (if (not (fboundp func))
  10584. (error "Cannot export orgtbl table to %s" target))
  10585. (setq lines (org-table-clean-before-export lines))
  10586. (setq table
  10587. (mapcar
  10588. (lambda (x)
  10589. (if (string-match org-table-hline-regexp x)
  10590. 'hline
  10591. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10592. lines))
  10593. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10594. table)))
  10595. (loop for i from (1- maxcol) downto 0 do
  10596. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10597. (setq column (delq nil column))
  10598. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10599. (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))
  10600. (funcall func table nil)))
  10601. (defun orgtbl-send-table (&optional maybe)
  10602. "Send a tranformed version of this table to the receiver position.
  10603. With argument MAYBE, fail quietly if no transformation is defined for
  10604. this table."
  10605. (interactive)
  10606. (catch 'exit
  10607. (unless (org-at-table-p) (error "Not at a table"))
  10608. ;; when non-interactive, we assume align has just happened.
  10609. (when (interactive-p) (org-table-align))
  10610. (save-excursion
  10611. (goto-char (org-table-begin))
  10612. (beginning-of-line 0)
  10613. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10614. (if maybe
  10615. (throw 'exit nil)
  10616. (error "Don't know how to transform this table."))))
  10617. (let* ((name (match-string 1))
  10618. beg
  10619. (transform (intern (match-string 2)))
  10620. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10621. (skip (plist-get params :skip))
  10622. (skipcols (plist-get params :skipcols))
  10623. (txt (buffer-substring-no-properties
  10624. (org-table-begin) (org-table-end)))
  10625. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10626. (lines (org-table-clean-before-export lines))
  10627. (i0 (if org-table-clean-did-remove-column 2 1))
  10628. (table (mapcar
  10629. (lambda (x)
  10630. (if (string-match org-table-hline-regexp x)
  10631. 'hline
  10632. (org-remove-by-index
  10633. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10634. skipcols i0)))
  10635. lines))
  10636. (fun (if (= i0 2) 'cdr 'identity))
  10637. (org-table-last-alignment
  10638. (org-remove-by-index (funcall fun org-table-last-alignment)
  10639. skipcols i0))
  10640. (org-table-last-column-widths
  10641. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10642. skipcols i0)))
  10643. (unless (fboundp transform)
  10644. (error "No such transformation function %s" transform))
  10645. (setq txt (funcall transform table params))
  10646. ;; Find the insertion place
  10647. (save-excursion
  10648. (goto-char (point-min))
  10649. (unless (re-search-forward
  10650. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10651. (error "Don't know where to insert translated table"))
  10652. (goto-char (match-beginning 0))
  10653. (beginning-of-line 2)
  10654. (setq beg (point))
  10655. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10656. (error "Cannot find end of insertion region"))
  10657. (beginning-of-line 1)
  10658. (delete-region beg (point))
  10659. (goto-char beg)
  10660. (insert txt "\n"))
  10661. (message "Table converted and installed at receiver location"))))
  10662. (defun org-remove-by-index (list indices &optional i0)
  10663. "Remove the elements in LIST with indices in INDICES.
  10664. First element has index 0, or I0 if given."
  10665. (if (not indices)
  10666. list
  10667. (if (integerp indices) (setq indices (list indices)))
  10668. (setq i0 (1- (or i0 0)))
  10669. (delq :rm (mapcar (lambda (x)
  10670. (setq i0 (1+ i0))
  10671. (if (memq i0 indices) :rm x))
  10672. list))))
  10673. (defun orgtbl-toggle-comment ()
  10674. "Comment or uncomment the orgtbl at point."
  10675. (interactive)
  10676. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10677. (re2 (concat "^" orgtbl-line-start-regexp))
  10678. (commented (save-excursion (beginning-of-line 1)
  10679. (cond ((looking-at re1) t)
  10680. ((looking-at re2) nil)
  10681. (t (error "Not at an org table")))))
  10682. (re (if commented re1 re2))
  10683. beg end)
  10684. (save-excursion
  10685. (beginning-of-line 1)
  10686. (while (looking-at re) (beginning-of-line 0))
  10687. (beginning-of-line 2)
  10688. (setq beg (point))
  10689. (while (looking-at re) (beginning-of-line 2))
  10690. (setq end (point)))
  10691. (comment-region beg end (if commented '(4) nil))))
  10692. (defun orgtbl-insert-radio-table ()
  10693. "Insert a radio table template appropriate for this major mode."
  10694. (interactive)
  10695. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10696. (txt (nth 1 e))
  10697. name pos)
  10698. (unless e (error "No radio table setup defined for %s" major-mode))
  10699. (setq name (read-string "Table name: "))
  10700. (while (string-match "%n" txt)
  10701. (setq txt (replace-match name t t txt)))
  10702. (or (bolp) (insert "\n"))
  10703. (setq pos (point))
  10704. (insert txt)
  10705. (goto-char pos)))
  10706. (defun org-get-param (params header i sym &optional hsym)
  10707. "Get parameter value for symbol SYM.
  10708. If this is a header line, actually get the value for the symbol with an
  10709. additional \"h\" inserted after the colon.
  10710. If the value is a protperty list, get the element for the current column.
  10711. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10712. (let ((val (plist-get params sym)))
  10713. (and hsym header (setq val (or (plist-get params hsym) val)))
  10714. (if (consp val) (plist-get val i) val)))
  10715. (defun orgtbl-to-generic (table params)
  10716. "Convert the orgtbl-mode TABLE to some other format.
  10717. This generic routine can be used for many standard cases.
  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. For the generic converter, some parameters are obligatory: You need to
  10722. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10723. :splice, you must have :tstart and :tend.
  10724. Valid parameters are
  10725. :tstart String to start the table. Ignored when :splice is t.
  10726. :tend String to end the table. Ignored when :splice is t.
  10727. :splice When set to t, return only table body lines, don't wrap
  10728. them into :tstart and :tend. Default is nil.
  10729. :hline String to be inserted on horizontal separation lines.
  10730. May be nil to ignore hlines.
  10731. :lstart String to start a new table line.
  10732. :lend String to end a table line
  10733. :sep Separator between two fields
  10734. :lfmt Format for entire line, with enough %s to capture all fields.
  10735. If this is present, :lstart, :lend, and :sep are ignored.
  10736. :fmt A format to be used to wrap the field, should contain
  10737. %s for the original field value. For example, to wrap
  10738. everything in dollars, you could use :fmt \"$%s$\".
  10739. This may also be a property list with column numbers and
  10740. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10741. :hlstart :hlend :hlsep :hlfmt :hfmt
  10742. Same as above, specific for the header lines in the table.
  10743. All lines before the first hline are treated as header.
  10744. If any of these is not present, the data line value is used.
  10745. :efmt Use this format to print numbers with exponentials.
  10746. The format should have %s twice for inserting mantissa
  10747. and exponent, for example \"%s\\\\times10^{%s}\". This
  10748. may also be a property list with column numbers and
  10749. formats. :fmt will still be applied after :efmt.
  10750. In addition to this, the parameters :skip and :skipcols are always handled
  10751. directly by `orgtbl-send-table'. See manual."
  10752. (interactive)
  10753. (let* ((p params)
  10754. (splicep (plist-get p :splice))
  10755. (hline (plist-get p :hline))
  10756. rtn line i fm efm lfmt h)
  10757. ;; Do we have a header?
  10758. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10759. (setq h t))
  10760. ;; Put header
  10761. (unless splicep
  10762. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10763. ;; Now loop over all lines
  10764. (while (setq line (pop table))
  10765. (if (eq line 'hline)
  10766. ;; A horizontal separator line
  10767. (progn (if hline (push hline rtn))
  10768. (setq h nil)) ; no longer in header
  10769. ;; A normal line. Convert the fields, push line onto the result list
  10770. (setq i 0)
  10771. (setq line
  10772. (mapcar
  10773. (lambda (f)
  10774. (setq i (1+ i)
  10775. fm (org-get-param p h i :fmt :hfmt)
  10776. efm (org-get-param p h i :efmt))
  10777. (if (and efm (string-match orgtbl-exp-regexp f))
  10778. (setq f (format
  10779. efm (match-string 1 f) (match-string 2 f))))
  10780. (if fm (setq f (format fm f)))
  10781. f)
  10782. line))
  10783. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10784. (push (apply 'format lfmt line) rtn)
  10785. (push (concat
  10786. (org-get-param p h i :lstart :hlstart)
  10787. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10788. (org-get-param p h i :lend :hlend))
  10789. rtn))))
  10790. (unless splicep
  10791. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10792. (mapconcat 'identity (nreverse rtn) "\n")))
  10793. (defun orgtbl-to-latex (table params)
  10794. "Convert the orgtbl-mode TABLE to LaTeX.
  10795. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10796. separator line, or a list of fields for that line.
  10797. PARAMS is a property list of parameters that can influence the conversion.
  10798. Supports all parameters from `orgtbl-to-generic'. Most important for
  10799. LaTeX are:
  10800. :splice When set to t, return only table body lines, don't wrap
  10801. them into a tabular environment. Default is nil.
  10802. :fmt A format to be used to wrap the field, should contain %s for the
  10803. original field value. For example, to wrap everything in dollars,
  10804. use :fmt \"$%s$\". This may also be a property list with column
  10805. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10806. :efmt Format for transforming numbers with exponentials. The format
  10807. should have %s twice for inserting mantissa and exponent, for
  10808. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10809. This may also be a property list with column numbers and formats.
  10810. The general parameters :skip and :skipcols have already been applied when
  10811. this function is called."
  10812. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10813. org-table-last-alignment ""))
  10814. (params2
  10815. (list
  10816. :tstart (concat "\\begin{tabular}{" alignment "}")
  10817. :tend "\\end{tabular}"
  10818. :lstart "" :lend " \\\\" :sep " & "
  10819. :efmt "%s\\,(%s)" :hline "\\hline")))
  10820. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10821. (defun orgtbl-to-html (table params)
  10822. "Convert the orgtbl-mode TABLE to LaTeX.
  10823. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10824. separator line, or a list of fields for that line.
  10825. PARAMS is a property list of parameters that can influence the conversion.
  10826. Currently this function recognizes the following parameters:
  10827. :splice When set to t, return only table body lines, don't wrap
  10828. them into a <table> environment. Default is nil.
  10829. The general parameters :skip and :skipcols have already been applied when
  10830. this function is called. The function does *not* use `orgtbl-to-generic',
  10831. so you cannot specify parameters for it."
  10832. (let* ((splicep (plist-get params :splice))
  10833. html)
  10834. ;; Just call the formatter we already have
  10835. ;; We need to make text lines for it, so put the fields back together.
  10836. (setq html (org-format-org-table-html
  10837. (mapcar
  10838. (lambda (x)
  10839. (if (eq x 'hline)
  10840. "|----+----|"
  10841. (concat "| " (mapconcat 'identity x " | ") " |")))
  10842. table)
  10843. splicep))
  10844. (if (string-match "\n+\\'" html)
  10845. (setq html (replace-match "" t t html)))
  10846. html))
  10847. (defun orgtbl-to-texinfo (table params)
  10848. "Convert the orgtbl-mode TABLE to TeXInfo.
  10849. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10850. separator line, or a list of fields for that line.
  10851. PARAMS is a property list of parameters that can influence the conversion.
  10852. Supports all parameters from `orgtbl-to-generic'. Most important for
  10853. TeXInfo are:
  10854. :splice nil/t When set to t, return only table body lines, don't wrap
  10855. them into a multitable environment. Default is nil.
  10856. :fmt fmt A format to be used to wrap the field, should contain
  10857. %s for the original field value. For example, to wrap
  10858. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10859. This may also be a property list with column numbers and
  10860. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10861. :cf \"f1 f2..\" The column fractions for the table. By default these
  10862. are computed automatically from the width of the columns
  10863. under org-mode.
  10864. The general parameters :skip and :skipcols have already been applied when
  10865. this function is called."
  10866. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10867. (colfrac (or (plist-get params :cf)
  10868. (mapconcat
  10869. (lambda (x) (format "%.3f" (/ (float x) total)))
  10870. org-table-last-column-widths " ")))
  10871. (params2
  10872. (list
  10873. :tstart (concat "@multitable @columnfractions " colfrac)
  10874. :tend "@end multitable"
  10875. :lstart "@item " :lend "" :sep " @tab "
  10876. :hlstart "@headitem ")))
  10877. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10878. ;;;; Link Stuff
  10879. ;;; Link abbreviations
  10880. (defun org-link-expand-abbrev (link)
  10881. "Apply replacements as defined in `org-link-abbrev-alist."
  10882. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10883. (let* ((key (match-string 1 link))
  10884. (as (or (assoc key org-link-abbrev-alist-local)
  10885. (assoc key org-link-abbrev-alist)))
  10886. (tag (and (match-end 2) (match-string 3 link)))
  10887. rpl)
  10888. (if (not as)
  10889. link
  10890. (setq rpl (cdr as))
  10891. (cond
  10892. ((symbolp rpl) (funcall rpl tag))
  10893. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10894. (t (concat rpl tag)))))
  10895. link))
  10896. ;;; Storing and inserting links
  10897. (defvar org-insert-link-history nil
  10898. "Minibuffer history for links inserted with `org-insert-link'.")
  10899. (defvar org-stored-links nil
  10900. "Contains the links stored with `org-store-link'.")
  10901. (defvar org-store-link-plist nil
  10902. "Plist with info about the most recently link created with `org-store-link'.")
  10903. (defvar org-link-protocols nil
  10904. "Link protocols added to Org-mode using `org-add-link-type'.")
  10905. (defvar org-store-link-functions nil
  10906. "List of functions that are called to create and store a link.
  10907. Each function will be called in turn until one returns a non-nil
  10908. value. Each function should check if it is responsible for creating
  10909. this link (for example by looking at the major mode).
  10910. If not, it must exit and return nil.
  10911. If yes, it should return a non-nil value after a calling
  10912. `org-store-link-props' with a list of properties and values.
  10913. Special properties are:
  10914. :type The link prefix. like \"http\". This must be given.
  10915. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10916. This is obligatory as well.
  10917. :description Optional default description for the second pair
  10918. of brackets in an Org-mode link. The user can still change
  10919. this when inserting this link into an Org-mode buffer.
  10920. In addition to these, any additional properties can be specified
  10921. and then used in remember templates.")
  10922. (defun org-add-link-type (type &optional follow publish)
  10923. "Add TYPE to the list of `org-link-types'.
  10924. Re-compute all regular expressions depending on `org-link-types'
  10925. FOLLOW and PUBLISH are two functions. Both take the link path as
  10926. an argument.
  10927. FOLLOW should do whatever is necessary to follow the link, for example
  10928. to find a file or display a mail message.
  10929. PUBLISH takes the path and retuns the string that should be used when
  10930. this document is published. FIMXE: This is actually not yet implemented."
  10931. (add-to-list 'org-link-types type t)
  10932. (org-make-link-regexps)
  10933. (add-to-list 'org-link-protocols
  10934. (list type follow publish)))
  10935. (defun org-add-agenda-custom-command (entry)
  10936. "Replace or add a command in `org-agenda-custom-commands'.
  10937. This is mostly for hacking and trying a new command - once the command
  10938. works you probably want to add it to `org-agenda-custom-commands' for good."
  10939. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10940. (if ass
  10941. (setcdr ass (cdr entry))
  10942. (push entry org-agenda-custom-commands))))
  10943. ;;;###autoload
  10944. (defun org-store-link (arg)
  10945. "\\<org-mode-map>Store an org-link to the current location.
  10946. This link is added to `org-stored-links' and can later be inserted
  10947. into an org-buffer with \\[org-insert-link].
  10948. For some link types, a prefix arg is interpreted:
  10949. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10950. For file links, arg negates `org-context-in-file-links'."
  10951. (interactive "P")
  10952. (setq org-store-link-plist nil) ; reset
  10953. (let (link cpltxt desc description search txt)
  10954. (cond
  10955. ((run-hook-with-args-until-success 'org-store-link-functions)
  10956. (setq link (plist-get org-store-link-plist :link)
  10957. desc (or (plist-get org-store-link-plist :description) link)))
  10958. ((eq major-mode 'bbdb-mode)
  10959. (let ((name (bbdb-record-name (bbdb-current-record)))
  10960. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10961. (setq cpltxt (concat "bbdb:" (or name company))
  10962. link (org-make-link cpltxt))
  10963. (org-store-link-props :type "bbdb" :name name :company company)))
  10964. ((eq major-mode 'Info-mode)
  10965. (setq link (org-make-link "info:"
  10966. (file-name-nondirectory Info-current-file)
  10967. ":" Info-current-node))
  10968. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10969. ":" Info-current-node))
  10970. (org-store-link-props :type "info" :file Info-current-file
  10971. :node Info-current-node))
  10972. ((eq major-mode 'calendar-mode)
  10973. (let ((cd (calendar-cursor-to-date)))
  10974. (setq link
  10975. (format-time-string
  10976. (car org-time-stamp-formats)
  10977. (apply 'encode-time
  10978. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10979. nil nil nil))))
  10980. (org-store-link-props :type "calendar" :date cd)))
  10981. ((or (eq major-mode 'vm-summary-mode)
  10982. (eq major-mode 'vm-presentation-mode))
  10983. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10984. (vm-follow-summary-cursor)
  10985. (save-excursion
  10986. (vm-select-folder-buffer)
  10987. (let* ((message (car vm-message-pointer))
  10988. (folder buffer-file-name)
  10989. (subject (vm-su-subject message))
  10990. (to (vm-get-header-contents message "To"))
  10991. (from (vm-get-header-contents message "From"))
  10992. (message-id (vm-su-message-id message)))
  10993. (org-store-link-props :type "vm" :from from :to to :subject subject
  10994. :message-id message-id)
  10995. (setq message-id (org-remove-angle-brackets message-id))
  10996. (setq folder (abbreviate-file-name folder))
  10997. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10998. folder)
  10999. (setq folder (replace-match "" t t folder)))
  11000. (setq cpltxt (org-email-link-description))
  11001. (setq link (org-make-link "vm:" folder "#" message-id)))))
  11002. ((eq major-mode 'wl-summary-mode)
  11003. (let* ((msgnum (wl-summary-message-number))
  11004. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  11005. msgnum 'message-id))
  11006. (wl-message-entity
  11007. (if (fboundp 'elmo-message-entity)
  11008. (elmo-message-entity
  11009. wl-summary-buffer-elmo-folder msgnum)
  11010. (elmo-msgdb-overview-get-entity
  11011. msgnum (wl-summary-buffer-msgdb))))
  11012. (from (wl-summary-line-from))
  11013. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  11014. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  11015. (wl-summary-line-subject))))
  11016. (org-store-link-props :type "wl" :from from :to to
  11017. :subject subject :message-id message-id)
  11018. (setq message-id (org-remove-angle-brackets message-id))
  11019. (setq cpltxt (org-email-link-description))
  11020. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  11021. "#" message-id))))
  11022. ((or (equal major-mode 'mh-folder-mode)
  11023. (equal major-mode 'mh-show-mode))
  11024. (let ((from (org-mhe-get-header "From:"))
  11025. (to (org-mhe-get-header "To:"))
  11026. (message-id (org-mhe-get-header "Message-Id:"))
  11027. (subject (org-mhe-get-header "Subject:")))
  11028. (org-store-link-props :type "mh" :from from :to to
  11029. :subject subject :message-id message-id)
  11030. (setq cpltxt (org-email-link-description))
  11031. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  11032. (org-remove-angle-brackets message-id)))))
  11033. ((or (eq major-mode 'rmail-mode)
  11034. (eq major-mode 'rmail-summary-mode))
  11035. (save-window-excursion
  11036. (save-restriction
  11037. (when (eq major-mode 'rmail-summary-mode)
  11038. (rmail-show-message rmail-current-message))
  11039. (rmail-narrow-to-non-pruned-header)
  11040. (let ((folder buffer-file-name)
  11041. (message-id (mail-fetch-field "message-id"))
  11042. (from (mail-fetch-field "from"))
  11043. (to (mail-fetch-field "to"))
  11044. (subject (mail-fetch-field "subject")))
  11045. (org-store-link-props
  11046. :type "rmail" :from from :to to
  11047. :subject subject :message-id message-id)
  11048. (setq message-id (org-remove-angle-brackets message-id))
  11049. (setq cpltxt (org-email-link-description))
  11050. (setq link (org-make-link "rmail:" folder "#" message-id)))
  11051. (rmail-show-message rmail-current-message))))
  11052. ((eq major-mode 'gnus-group-mode)
  11053. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  11054. (gnus-group-group-name)) ; version
  11055. ((fboundp 'gnus-group-name)
  11056. (gnus-group-name))
  11057. (t "???"))))
  11058. (unless group (error "Not on a group"))
  11059. (org-store-link-props :type "gnus" :group group)
  11060. (setq cpltxt (concat
  11061. (if (org-xor arg org-usenet-links-prefer-google)
  11062. "http://groups.google.com/groups?group="
  11063. "gnus:")
  11064. group)
  11065. link (org-make-link cpltxt))))
  11066. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  11067. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  11068. (let* ((group gnus-newsgroup-name)
  11069. (article (gnus-summary-article-number))
  11070. (header (gnus-summary-article-header article))
  11071. (from (mail-header-from header))
  11072. (message-id (mail-header-id header))
  11073. (date (mail-header-date header))
  11074. (subject (gnus-summary-subject-string)))
  11075. (org-store-link-props :type "gnus" :from from :subject subject
  11076. :message-id message-id :group group)
  11077. (setq cpltxt (org-email-link-description))
  11078. (if (org-xor arg org-usenet-links-prefer-google)
  11079. (setq link
  11080. (concat
  11081. cpltxt "\n "
  11082. (format "http://groups.google.com/groups?as_umsgid=%s"
  11083. (org-fixup-message-id-for-http message-id))))
  11084. (setq link (org-make-link "gnus:" group
  11085. "#" (number-to-string article))))))
  11086. ((eq major-mode 'w3-mode)
  11087. (setq cpltxt (url-view-url t)
  11088. link (org-make-link cpltxt))
  11089. (org-store-link-props :type "w3" :url (url-view-url t)))
  11090. ((eq major-mode 'w3m-mode)
  11091. (setq cpltxt (or w3m-current-title w3m-current-url)
  11092. link (org-make-link w3m-current-url))
  11093. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11094. ((setq search (run-hook-with-args-until-success
  11095. 'org-create-file-search-functions))
  11096. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11097. "::" search))
  11098. (setq cpltxt (or description link)))
  11099. ((eq major-mode 'image-mode)
  11100. (setq cpltxt (concat "file:"
  11101. (abbreviate-file-name buffer-file-name))
  11102. link (org-make-link cpltxt))
  11103. (org-store-link-props :type "image" :file buffer-file-name))
  11104. ((eq major-mode 'dired-mode)
  11105. ;; link to the file in the current line
  11106. (setq cpltxt (concat "file:"
  11107. (abbreviate-file-name
  11108. (expand-file-name
  11109. (dired-get-filename nil t))))
  11110. link (org-make-link cpltxt)))
  11111. ((and buffer-file-name (org-mode-p))
  11112. ;; Just link to current headline
  11113. (setq cpltxt (concat "file:"
  11114. (abbreviate-file-name buffer-file-name)))
  11115. ;; Add a context search string
  11116. (when (org-xor org-context-in-file-links arg)
  11117. ;; Check if we are on a target
  11118. (if (org-in-regexp "<<\\(.*?\\)>>")
  11119. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11120. (setq txt (cond
  11121. ((org-on-heading-p) nil)
  11122. ((org-region-active-p)
  11123. (buffer-substring (region-beginning) (region-end)))
  11124. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11125. (when (or (null txt) (string-match "\\S-" txt))
  11126. (setq cpltxt
  11127. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11128. desc "NONE"))))
  11129. (if (string-match "::\\'" cpltxt)
  11130. (setq cpltxt (substring cpltxt 0 -2)))
  11131. (setq link (org-make-link cpltxt)))
  11132. ((buffer-file-name (buffer-base-buffer))
  11133. ;; Just link to this file here.
  11134. (setq cpltxt (concat "file:"
  11135. (abbreviate-file-name
  11136. (buffer-file-name (buffer-base-buffer)))))
  11137. ;; Add a context string
  11138. (when (org-xor org-context-in-file-links arg)
  11139. (setq txt (if (org-region-active-p)
  11140. (buffer-substring (region-beginning) (region-end))
  11141. (buffer-substring (point-at-bol) (point-at-eol))))
  11142. ;; Only use search option if there is some text.
  11143. (when (string-match "\\S-" txt)
  11144. (setq cpltxt
  11145. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11146. desc "NONE")))
  11147. (setq link (org-make-link cpltxt)))
  11148. ((interactive-p)
  11149. (error "Cannot link to a buffer which is not visiting a file"))
  11150. (t (setq link nil)))
  11151. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11152. (setq link (or link cpltxt)
  11153. desc (or desc cpltxt))
  11154. (if (equal desc "NONE") (setq desc nil))
  11155. (if (and (interactive-p) link)
  11156. (progn
  11157. (setq org-stored-links
  11158. (cons (list link desc) org-stored-links))
  11159. (message "Stored: %s" (or desc link)))
  11160. (and link (org-make-link-string link desc)))))
  11161. (defun org-store-link-props (&rest plist)
  11162. "Store link properties, extract names and addresses."
  11163. (let (x adr)
  11164. (when (setq x (plist-get plist :from))
  11165. (setq adr (mail-extract-address-components x))
  11166. (plist-put plist :fromname (car adr))
  11167. (plist-put plist :fromaddress (nth 1 adr)))
  11168. (when (setq x (plist-get plist :to))
  11169. (setq adr (mail-extract-address-components x))
  11170. (plist-put plist :toname (car adr))
  11171. (plist-put plist :toaddress (nth 1 adr))))
  11172. (let ((from (plist-get plist :from))
  11173. (to (plist-get plist :to)))
  11174. (when (and from to org-from-is-user-regexp)
  11175. (plist-put plist :fromto
  11176. (if (string-match org-from-is-user-regexp from)
  11177. (concat "to %t")
  11178. (concat "from %f")))))
  11179. (setq org-store-link-plist plist))
  11180. (defun org-email-link-description (&optional fmt)
  11181. "Return the description part of an email link.
  11182. This takes information from `org-store-link-plist' and formats it
  11183. according to FMT (default from `org-email-link-description-format')."
  11184. (setq fmt (or fmt org-email-link-description-format))
  11185. (let* ((p org-store-link-plist)
  11186. (to (plist-get p :toaddress))
  11187. (from (plist-get p :fromaddress))
  11188. (table
  11189. (list
  11190. (cons "%c" (plist-get p :fromto))
  11191. (cons "%F" (plist-get p :from))
  11192. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11193. (cons "%T" (plist-get p :to))
  11194. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11195. (cons "%s" (plist-get p :subject))
  11196. (cons "%m" (plist-get p :message-id)))))
  11197. (when (string-match "%c" fmt)
  11198. ;; Check if the user wrote this message
  11199. (if (and org-from-is-user-regexp from to
  11200. (save-match-data (string-match org-from-is-user-regexp from)))
  11201. (setq fmt (replace-match "to %t" t t fmt))
  11202. (setq fmt (replace-match "from %f" t t fmt))))
  11203. (org-replace-escapes fmt table)))
  11204. (defun org-make-org-heading-search-string (&optional string heading)
  11205. "Make search string for STRING or current headline."
  11206. (interactive)
  11207. (let ((s (or string (org-get-heading))))
  11208. (unless (and string (not heading))
  11209. ;; We are using a headline, clean up garbage in there.
  11210. (if (string-match org-todo-regexp s)
  11211. (setq s (replace-match "" t t s)))
  11212. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11213. (setq s (replace-match "" t t s)))
  11214. (setq s (org-trim s))
  11215. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11216. org-comment-string "\\)") s)
  11217. (setq s (replace-match "" t t s)))
  11218. (while (string-match org-ts-regexp s)
  11219. (setq s (replace-match "" t t s))))
  11220. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11221. (setq s (replace-match " " t t s)))
  11222. (or string (setq s (concat "*" s))) ; Add * for headlines
  11223. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11224. (defun org-make-link (&rest strings)
  11225. "Concatenate STRINGS."
  11226. (apply 'concat strings))
  11227. (defun org-make-link-string (link &optional description)
  11228. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11229. (unless (string-match "\\S-" link)
  11230. (error "Empty link"))
  11231. (when (stringp description)
  11232. ;; Remove brackets from the description, they are fatal.
  11233. (while (string-match "\\[" description)
  11234. (setq description (replace-match "{" t t description)))
  11235. (while (string-match "\\]" description)
  11236. (setq description (replace-match "}" t t description))))
  11237. (when (equal (org-link-escape link) description)
  11238. ;; No description needed, it is identical
  11239. (setq description nil))
  11240. (when (and (not description)
  11241. (not (equal link (org-link-escape link))))
  11242. (setq description link))
  11243. (concat "[[" (org-link-escape link) "]"
  11244. (if description (concat "[" description "]") "")
  11245. "]"))
  11246. (defconst org-link-escape-chars
  11247. '((?\ . "%20")
  11248. (?\[ . "%5B")
  11249. (?\] . "%5D")
  11250. (?\340 . "%E0") ; `a
  11251. (?\342 . "%E2") ; ^a
  11252. (?\347 . "%E7") ; ,c
  11253. (?\350 . "%E8") ; `e
  11254. (?\351 . "%E9") ; 'e
  11255. (?\352 . "%EA") ; ^e
  11256. (?\356 . "%EE") ; ^i
  11257. (?\364 . "%F4") ; ^o
  11258. (?\371 . "%F9") ; `u
  11259. (?\373 . "%FB") ; ^u
  11260. (?\; . "%3B")
  11261. (?? . "%3F")
  11262. (?= . "%3D")
  11263. (?+ . "%2B")
  11264. )
  11265. "Association list of escapes for some characters problematic in links.
  11266. This is the list that is used for internal purposes.")
  11267. (defconst org-link-escape-chars-browser
  11268. '((?\ . "%20")) ; 32 for the SPC char
  11269. "Association list of escapes for some characters problematic in links.
  11270. This is the list that is used before handing over to the browser.")
  11271. (defun org-link-escape (text &optional table)
  11272. "Escape charaters in TEXT that are problematic for links."
  11273. (setq table (or table org-link-escape-chars))
  11274. (when text
  11275. (let ((re (mapconcat (lambda (x) (regexp-quote
  11276. (char-to-string (car x))))
  11277. table "\\|")))
  11278. (while (string-match re text)
  11279. (setq text
  11280. (replace-match
  11281. (cdr (assoc (string-to-char (match-string 0 text))
  11282. table))
  11283. t t text)))
  11284. text)))
  11285. (defun org-link-unescape (text &optional table)
  11286. "Reverse the action of `org-link-escape'."
  11287. (setq table (or table org-link-escape-chars))
  11288. (when text
  11289. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11290. table "\\|")))
  11291. (while (string-match re text)
  11292. (setq text
  11293. (replace-match
  11294. (char-to-string (car (rassoc (match-string 0 text) table)))
  11295. t t text)))
  11296. text)))
  11297. (defun org-xor (a b)
  11298. "Exclusive or."
  11299. (if a (not b) b))
  11300. (defun org-get-header (header)
  11301. "Find a header field in the current buffer."
  11302. (save-excursion
  11303. (goto-char (point-min))
  11304. (let ((case-fold-search t) s)
  11305. (cond
  11306. ((eq header 'from)
  11307. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11308. (setq s (match-string 1)))
  11309. (while (string-match "\"" s)
  11310. (setq s (replace-match "" t t s)))
  11311. (if (string-match "[<(].*" s)
  11312. (setq s (replace-match "" t t s))))
  11313. ((eq header 'message-id)
  11314. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11315. (setq s (match-string 1))))
  11316. ((eq header 'subject)
  11317. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11318. (setq s (match-string 1)))))
  11319. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11320. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11321. s)))
  11322. (defun org-fixup-message-id-for-http (s)
  11323. "Replace special characters in a message id, so it can be used in an http query."
  11324. (while (string-match "<" s)
  11325. (setq s (replace-match "%3C" t t s)))
  11326. (while (string-match ">" s)
  11327. (setq s (replace-match "%3E" t t s)))
  11328. (while (string-match "@" s)
  11329. (setq s (replace-match "%40" t t s)))
  11330. s)
  11331. ;;;###autoload
  11332. (defun org-insert-link-global ()
  11333. "Insert a link like Org-mode does.
  11334. This command can be called in any mode to insert a link in Org-mode syntax."
  11335. (interactive)
  11336. (org-run-like-in-org-mode 'org-insert-link))
  11337. (defun org-insert-link (&optional complete-file)
  11338. "Insert a link. At the prompt, enter the link.
  11339. Completion can be used to select a link previously stored with
  11340. `org-store-link'. When the empty string is entered (i.e. if you just
  11341. press RET at the prompt), the link defaults to the most recently
  11342. stored link. As SPC triggers completion in the minibuffer, you need to
  11343. use M-SPC or C-q SPC to force the insertion of a space character.
  11344. You will also be prompted for a description, and if one is given, it will
  11345. be displayed in the buffer instead of the link.
  11346. If there is already a link at point, this command will allow you to edit link
  11347. and description parts.
  11348. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11349. selected using completion. The path to the file will be relative to
  11350. the current directory if the file is in the current directory or a
  11351. subdirectory. Otherwise, the link will be the absolute path as
  11352. completed in the minibuffer (i.e. normally ~/path/to/file).
  11353. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11354. is in the current directory or below.
  11355. With three \\[universal-argument] prefixes, negate the meaning of
  11356. `org-keep-stored-link-after-insertion'."
  11357. (interactive "P")
  11358. (let* ((wcf (current-window-configuration))
  11359. (region (if (org-region-active-p)
  11360. (buffer-substring (region-beginning) (region-end))))
  11361. (remove (and region (list (region-beginning) (region-end))))
  11362. (desc region)
  11363. tmphist ; byte-compile incorrectly complains about this
  11364. link entry file)
  11365. (cond
  11366. ((org-in-regexp org-bracket-link-regexp 1)
  11367. ;; We do have a link at point, and we are going to edit it.
  11368. (setq remove (list (match-beginning 0) (match-end 0)))
  11369. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11370. (setq link (read-string "Link: "
  11371. (org-link-unescape
  11372. (org-match-string-no-properties 1)))))
  11373. ((or (org-in-regexp org-angle-link-re)
  11374. (org-in-regexp org-plain-link-re))
  11375. ;; Convert to bracket link
  11376. (setq remove (list (match-beginning 0) (match-end 0))
  11377. link (read-string "Link: "
  11378. (org-remove-angle-brackets (match-string 0)))))
  11379. ((equal complete-file '(4))
  11380. ;; Completing read for file names.
  11381. (setq file (read-file-name "File: "))
  11382. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11383. (pwd1 (file-name-as-directory (abbreviate-file-name
  11384. (expand-file-name ".")))))
  11385. (cond
  11386. ((equal complete-file '(16))
  11387. (setq link (org-make-link
  11388. "file:"
  11389. (abbreviate-file-name (expand-file-name file)))))
  11390. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11391. (setq link (org-make-link "file:" (match-string 1 file))))
  11392. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11393. (expand-file-name file))
  11394. (setq link (org-make-link
  11395. "file:" (match-string 1 (expand-file-name file)))))
  11396. (t (setq link (org-make-link "file:" file))))))
  11397. (t
  11398. ;; Read link, with completion for stored links.
  11399. (with-output-to-temp-buffer "*Org Links*"
  11400. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11401. (when org-stored-links
  11402. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11403. (princ (mapconcat
  11404. (lambda (x)
  11405. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11406. (reverse org-stored-links) "\n"))))
  11407. (let ((cw (selected-window)))
  11408. (select-window (get-buffer-window "*Org Links*"))
  11409. (shrink-window-if-larger-than-buffer)
  11410. (setq truncate-lines t)
  11411. (select-window cw))
  11412. ;; Fake a link history, containing the stored links.
  11413. (setq tmphist (append (mapcar 'car org-stored-links)
  11414. org-insert-link-history))
  11415. (unwind-protect
  11416. (setq link (org-completing-read
  11417. "Link: "
  11418. (append
  11419. (mapcar (lambda (x) (list (concat (car x) ":")))
  11420. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11421. (mapcar (lambda (x) (list (concat x ":")))
  11422. org-link-types))
  11423. nil nil nil
  11424. 'tmphist
  11425. (or (car (car org-stored-links)))))
  11426. (set-window-configuration wcf)
  11427. (kill-buffer "*Org Links*"))
  11428. (setq entry (assoc link org-stored-links))
  11429. (or entry (push link org-insert-link-history))
  11430. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11431. (not org-keep-stored-link-after-insertion))
  11432. (setq org-stored-links (delq (assoc link org-stored-links)
  11433. org-stored-links)))
  11434. (setq desc (or desc (nth 1 entry)))))
  11435. (if (string-match org-plain-link-re link)
  11436. ;; URL-like link, normalize the use of angular brackets.
  11437. (setq link (org-make-link (org-remove-angle-brackets link))))
  11438. ;; Check if we are linking to the current file with a search option
  11439. ;; If yes, simplify the link by using only the search option.
  11440. (when (and buffer-file-name
  11441. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11442. (let* ((path (match-string 1 link))
  11443. (case-fold-search nil)
  11444. (search (match-string 2 link)))
  11445. (save-match-data
  11446. (if (equal (file-truename buffer-file-name) (file-truename path))
  11447. ;; We are linking to this same file, with a search option
  11448. (setq link search)))))
  11449. ;; Check if we can/should use a relative path. If yes, simplify the link
  11450. (when (string-match "\\<file:\\(.*\\)" link)
  11451. (let* ((path (match-string 1 link))
  11452. (origpath path)
  11453. (case-fold-search nil))
  11454. (cond
  11455. ((eq org-link-file-path-type 'absolute)
  11456. (setq path (abbreviate-file-name (expand-file-name path))))
  11457. ((eq org-link-file-path-type 'noabbrev)
  11458. (setq path (expand-file-name path)))
  11459. ((eq org-link-file-path-type 'relative)
  11460. (setq path (file-relative-name path)))
  11461. (t
  11462. (save-match-data
  11463. (if (string-match (concat "^" (regexp-quote
  11464. (file-name-as-directory
  11465. (expand-file-name "."))))
  11466. (expand-file-name path))
  11467. ;; We are linking a file with relative path name.
  11468. (setq path (substring (expand-file-name path)
  11469. (match-end 0)))))))
  11470. (setq link (concat "file:" path))
  11471. (if (equal desc origpath)
  11472. (setq desc path))))
  11473. (setq desc (read-string "Description: " desc))
  11474. (unless (string-match "\\S-" desc) (setq desc nil))
  11475. (if remove (apply 'delete-region remove))
  11476. (insert (org-make-link-string link desc))))
  11477. (defun org-completing-read (&rest args)
  11478. (let ((minibuffer-local-completion-map
  11479. (copy-keymap minibuffer-local-completion-map)))
  11480. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11481. (apply 'completing-read args)))
  11482. ;;; Opening/following a link
  11483. (defvar org-link-search-failed nil)
  11484. (defun org-next-link ()
  11485. "Move forward to the next link.
  11486. If the link is in hidden text, expose it."
  11487. (interactive)
  11488. (when (and org-link-search-failed (eq this-command last-command))
  11489. (goto-char (point-min))
  11490. (message "Link search wrapped back to beginning of buffer"))
  11491. (setq org-link-search-failed nil)
  11492. (let* ((pos (point))
  11493. (ct (org-context))
  11494. (a (assoc :link ct)))
  11495. (if a (goto-char (nth 2 a)))
  11496. (if (re-search-forward org-any-link-re nil t)
  11497. (progn
  11498. (goto-char (match-beginning 0))
  11499. (if (org-invisible-p) (org-show-context)))
  11500. (goto-char pos)
  11501. (setq org-link-search-failed t)
  11502. (error "No further link found"))))
  11503. (defun org-previous-link ()
  11504. "Move backward to the previous link.
  11505. If the link is in hidden text, expose it."
  11506. (interactive)
  11507. (when (and org-link-search-failed (eq this-command last-command))
  11508. (goto-char (point-max))
  11509. (message "Link search wrapped back to end of buffer"))
  11510. (setq org-link-search-failed nil)
  11511. (let* ((pos (point))
  11512. (ct (org-context))
  11513. (a (assoc :link ct)))
  11514. (if a (goto-char (nth 1 a)))
  11515. (if (re-search-backward org-any-link-re nil t)
  11516. (progn
  11517. (goto-char (match-beginning 0))
  11518. (if (org-invisible-p) (org-show-context)))
  11519. (goto-char pos)
  11520. (setq org-link-search-failed t)
  11521. (error "No further link found"))))
  11522. (defun org-find-file-at-mouse (ev)
  11523. "Open file link or URL at mouse."
  11524. (interactive "e")
  11525. (mouse-set-point ev)
  11526. (org-open-at-point 'in-emacs))
  11527. (defun org-open-at-mouse (ev)
  11528. "Open file link or URL at mouse."
  11529. (interactive "e")
  11530. (mouse-set-point ev)
  11531. (org-open-at-point))
  11532. (defvar org-window-config-before-follow-link nil
  11533. "The window configuration before following a link.
  11534. This is saved in case the need arises to restore it.")
  11535. (defvar org-open-link-marker (make-marker)
  11536. "Marker pointing to the location where `org-open-at-point; was called.")
  11537. ;;;###autoload
  11538. (defun org-open-at-point-global ()
  11539. "Follow a link like Org-mode does.
  11540. This command can be called in any mode to follow a link that has
  11541. Org-mode syntax."
  11542. (interactive)
  11543. (org-run-like-in-org-mode 'org-open-at-point))
  11544. (defun org-open-at-point (&optional in-emacs)
  11545. "Open link at or after point.
  11546. If there is no link at point, this function will search forward up to
  11547. the end of the current subtree.
  11548. Normally, files will be opened by an appropriate application. If the
  11549. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11550. (interactive "P")
  11551. (catch 'abort
  11552. (move-marker org-open-link-marker (point))
  11553. (setq org-window-config-before-follow-link (current-window-configuration))
  11554. (org-remove-occur-highlights nil nil t)
  11555. (if (org-at-timestamp-p t)
  11556. (org-follow-timestamp-link)
  11557. (let (type path link line search (pos (point)))
  11558. (catch 'match
  11559. (save-excursion
  11560. (skip-chars-forward "^]\n\r")
  11561. (when (org-in-regexp org-bracket-link-regexp)
  11562. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11563. (while (string-match " *\n *" link)
  11564. (setq link (replace-match " " t t link)))
  11565. (setq link (org-link-expand-abbrev link))
  11566. (if (string-match org-link-re-with-space2 link)
  11567. (setq type (match-string 1 link) path (match-string 2 link))
  11568. (setq type "thisfile" path link))
  11569. (throw 'match t)))
  11570. (when (get-text-property (point) 'org-linked-text)
  11571. (setq type "thisfile"
  11572. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11573. (1+ (point)) (point))
  11574. path (buffer-substring
  11575. (previous-single-property-change pos 'org-linked-text)
  11576. (next-single-property-change pos 'org-linked-text)))
  11577. (throw 'match t))
  11578. (save-excursion
  11579. (when (or (org-in-regexp org-angle-link-re)
  11580. (org-in-regexp org-plain-link-re))
  11581. (setq type (match-string 1) path (match-string 2))
  11582. (throw 'match t)))
  11583. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11584. (setq type "tree-match"
  11585. path (match-string 1))
  11586. (throw 'match t))
  11587. (save-excursion
  11588. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11589. (setq type "tags"
  11590. path (match-string 1))
  11591. (while (string-match ":" path)
  11592. (setq path (replace-match "+" t t path)))
  11593. (throw 'match t))))
  11594. (unless path
  11595. (error "No link found"))
  11596. ;; Remove any trailing spaces in path
  11597. (if (string-match " +\\'" path)
  11598. (setq path (replace-match "" t t path)))
  11599. (cond
  11600. ((assoc type org-link-protocols)
  11601. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11602. ((equal type "mailto")
  11603. (let ((cmd (car org-link-mailto-program))
  11604. (args (cdr org-link-mailto-program)) args1
  11605. (address path) (subject "") a)
  11606. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11607. (setq address (match-string 1 path)
  11608. subject (org-link-escape (match-string 2 path))))
  11609. (while args
  11610. (cond
  11611. ((not (stringp (car args))) (push (pop args) args1))
  11612. (t (setq a (pop args))
  11613. (if (string-match "%a" a)
  11614. (setq a (replace-match address t t a)))
  11615. (if (string-match "%s" a)
  11616. (setq a (replace-match subject t t a)))
  11617. (push a args1))))
  11618. (apply cmd (nreverse args1))))
  11619. ((member type '("http" "https" "ftp" "news"))
  11620. (browse-url (concat type ":" (org-link-escape
  11621. path org-link-escape-chars-browser))))
  11622. ((member type '("message"))
  11623. (browse-url (concat type ":" path)))
  11624. ((string= type "tags")
  11625. (org-tags-view in-emacs path))
  11626. ((string= type "thisfile")
  11627. (if in-emacs
  11628. (switch-to-buffer-other-window
  11629. (org-get-buffer-for-internal-link (current-buffer)))
  11630. (org-mark-ring-push))
  11631. (let ((cmd `(org-link-search
  11632. ,path
  11633. ,(cond ((equal in-emacs '(4)) 'occur)
  11634. ((equal in-emacs '(16)) 'org-occur)
  11635. (t nil))
  11636. ,pos)))
  11637. (condition-case nil (eval cmd)
  11638. (error (progn (widen) (eval cmd))))))
  11639. ((string= type "tree-match")
  11640. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11641. ((string= type "file")
  11642. (if (string-match "::\\([0-9]+\\)\\'" path)
  11643. (setq line (string-to-number (match-string 1 path))
  11644. path (substring path 0 (match-beginning 0)))
  11645. (if (string-match "::\\(.+\\)\\'" path)
  11646. (setq search (match-string 1 path)
  11647. path (substring path 0 (match-beginning 0)))))
  11648. (if (string-match "[*?{]" (file-name-nondirectory path))
  11649. (dired path)
  11650. (org-open-file path in-emacs line search)))
  11651. ((string= type "news")
  11652. (org-follow-gnus-link path))
  11653. ((string= type "bbdb")
  11654. (org-follow-bbdb-link path))
  11655. ((string= type "info")
  11656. (org-follow-info-link path))
  11657. ((string= type "gnus")
  11658. (let (group article)
  11659. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11660. (error "Error in Gnus link"))
  11661. (setq group (match-string 1 path)
  11662. article (match-string 3 path))
  11663. (org-follow-gnus-link group article)))
  11664. ((string= type "vm")
  11665. (let (folder article)
  11666. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11667. (error "Error in VM link"))
  11668. (setq folder (match-string 1 path)
  11669. article (match-string 3 path))
  11670. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11671. (org-follow-vm-link folder article in-emacs)))
  11672. ((string= type "wl")
  11673. (let (folder article)
  11674. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11675. (error "Error in Wanderlust link"))
  11676. (setq folder (match-string 1 path)
  11677. article (match-string 3 path))
  11678. (org-follow-wl-link folder article)))
  11679. ((string= type "mhe")
  11680. (let (folder article)
  11681. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11682. (error "Error in MHE link"))
  11683. (setq folder (match-string 1 path)
  11684. article (match-string 3 path))
  11685. (org-follow-mhe-link folder article)))
  11686. ((string= type "rmail")
  11687. (let (folder article)
  11688. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11689. (error "Error in RMAIL link"))
  11690. (setq folder (match-string 1 path)
  11691. article (match-string 3 path))
  11692. (org-follow-rmail-link folder article)))
  11693. ((string= type "shell")
  11694. (let ((cmd path))
  11695. (if (or (not org-confirm-shell-link-function)
  11696. (funcall org-confirm-shell-link-function
  11697. (format "Execute \"%s\" in shell? "
  11698. (org-add-props cmd nil
  11699. 'face 'org-warning))))
  11700. (progn
  11701. (message "Executing %s" cmd)
  11702. (shell-command cmd))
  11703. (error "Abort"))))
  11704. ((string= type "elisp")
  11705. (let ((cmd path))
  11706. (if (or (not org-confirm-elisp-link-function)
  11707. (funcall org-confirm-elisp-link-function
  11708. (format "Execute \"%s\" as elisp? "
  11709. (org-add-props cmd nil
  11710. 'face 'org-warning))))
  11711. (message "%s => %s" cmd (eval (read cmd)))
  11712. (error "Abort"))))
  11713. (t
  11714. (browse-url-at-point)))))
  11715. (move-marker org-open-link-marker nil)))
  11716. ;;; File search
  11717. (defvar org-create-file-search-functions nil
  11718. "List of functions to construct the right search string for a file link.
  11719. These functions are called in turn with point at the location to
  11720. which the link should point.
  11721. A function in the hook should first test if it would like to
  11722. handle this file type, for example by checking the major-mode or
  11723. the file extension. If it decides not to handle this file, it
  11724. should just return nil to give other functions a chance. If it
  11725. does handle the file, it must return the search string to be used
  11726. when following the link. The search string will be part of the
  11727. file link, given after a double colon, and `org-open-at-point'
  11728. will automatically search for it. If special measures must be
  11729. taken to make the search successful, another function should be
  11730. added to the companion hook `org-execute-file-search-functions',
  11731. which see.
  11732. A function in this hook may also use `setq' to set the variable
  11733. `description' to provide a suggestion for the descriptive text to
  11734. be used for this link when it gets inserted into an Org-mode
  11735. buffer with \\[org-insert-link].")
  11736. (defvar org-execute-file-search-functions nil
  11737. "List of functions to execute a file search triggered by a link.
  11738. Functions added to this hook must accept a single argument, the
  11739. search string that was part of the file link, the part after the
  11740. double colon. The function must first check if it would like to
  11741. handle this search, for example by checking the major-mode or the
  11742. file extension. If it decides not to handle this search, it
  11743. should just return nil to give other functions a chance. If it
  11744. does handle the search, it must return a non-nil value to keep
  11745. other functions from trying.
  11746. Each function can access the current prefix argument through the
  11747. variable `current-prefix-argument'. Note that a single prefix is
  11748. used to force opening a link in Emacs, so it may be good to only
  11749. use a numeric or double prefix to guide the search function.
  11750. In case this is needed, a function in this hook can also restore
  11751. the window configuration before `org-open-at-point' was called using:
  11752. (set-window-configuration org-window-config-before-follow-link)")
  11753. (defun org-link-search (s &optional type avoid-pos)
  11754. "Search for a link search option.
  11755. If S is surrounded by forward slashes, it is interpreted as a
  11756. regular expression. In org-mode files, this will create an `org-occur'
  11757. sparse tree. In ordinary files, `occur' will be used to list matches.
  11758. If the current buffer is in `dired-mode', grep will be used to search
  11759. in all files. If AVOID-POS is given, ignore matches near that position."
  11760. (let ((case-fold-search t)
  11761. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11762. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11763. (append '(("") (" ") ("\t") ("\n"))
  11764. org-emphasis-alist)
  11765. "\\|") "\\)"))
  11766. (pos (point))
  11767. (pre "") (post "")
  11768. words re0 re1 re2 re3 re4 re5 re2a reall)
  11769. (cond
  11770. ;; First check if there are any special
  11771. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11772. ;; Now try the builtin stuff
  11773. ((save-excursion
  11774. (goto-char (point-min))
  11775. (and
  11776. (re-search-forward
  11777. (concat "<<" (regexp-quote s0) ">>") nil t)
  11778. (setq pos (match-beginning 0))))
  11779. ;; There is an exact target for this
  11780. (goto-char pos))
  11781. ((string-match "^/\\(.*\\)/$" s)
  11782. ;; A regular expression
  11783. (cond
  11784. ((org-mode-p)
  11785. (org-occur (match-string 1 s)))
  11786. ;;((eq major-mode 'dired-mode)
  11787. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11788. (t (org-do-occur (match-string 1 s)))))
  11789. (t
  11790. ;; A normal search strings
  11791. (when (equal (string-to-char s) ?*)
  11792. ;; Anchor on headlines, post may include tags.
  11793. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11794. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11795. s (substring s 1)))
  11796. (remove-text-properties
  11797. 0 (length s)
  11798. '(face nil mouse-face nil keymap nil fontified nil) s)
  11799. ;; Make a series of regular expressions to find a match
  11800. (setq words (org-split-string s "[ \n\r\t]+")
  11801. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11802. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11803. "\\)" markers)
  11804. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11805. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11806. re1 (concat pre re2 post)
  11807. re3 (concat pre re4 post)
  11808. re5 (concat pre ".*" re4)
  11809. re2 (concat pre re2)
  11810. re2a (concat pre re2a)
  11811. re4 (concat pre re4)
  11812. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11813. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11814. re5 "\\)"
  11815. ))
  11816. (cond
  11817. ((eq type 'org-occur) (org-occur reall))
  11818. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11819. (t (goto-char (point-min))
  11820. (if (or (org-search-not-self 1 re0 nil t)
  11821. (org-search-not-self 1 re1 nil t)
  11822. (org-search-not-self 1 re2 nil t)
  11823. (org-search-not-self 1 re2a nil t)
  11824. (org-search-not-self 1 re3 nil t)
  11825. (org-search-not-self 1 re4 nil t)
  11826. (org-search-not-self 1 re5 nil t)
  11827. )
  11828. (goto-char (match-beginning 1))
  11829. (goto-char pos)
  11830. (error "No match")))))
  11831. (t
  11832. ;; Normal string-search
  11833. (goto-char (point-min))
  11834. (if (search-forward s nil t)
  11835. (goto-char (match-beginning 0))
  11836. (error "No match"))))
  11837. (and (org-mode-p) (org-show-context 'link-search))))
  11838. (defun org-search-not-self (group &rest args)
  11839. "Execute `re-search-forward', but only accept matches that do not
  11840. enclose the position of `org-open-link-marker'."
  11841. (let ((m org-open-link-marker))
  11842. (catch 'exit
  11843. (while (apply 're-search-forward args)
  11844. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11845. (goto-char (match-end group))
  11846. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11847. (> (match-beginning 0) (marker-position m))
  11848. (< (match-end 0) (marker-position m)))
  11849. (save-match-data
  11850. (or (not (org-in-regexp
  11851. org-bracket-link-analytic-regexp 1))
  11852. (not (match-end 4)) ; no description
  11853. (and (<= (match-beginning 4) (point))
  11854. (>= (match-end 4) (point))))))
  11855. (throw 'exit (point))))))))
  11856. (defun org-get-buffer-for-internal-link (buffer)
  11857. "Return a buffer to be used for displaying the link target of internal links."
  11858. (cond
  11859. ((not org-display-internal-link-with-indirect-buffer)
  11860. buffer)
  11861. ((string-match "(Clone)$" (buffer-name buffer))
  11862. (message "Buffer is already a clone, not making another one")
  11863. ;; we also do not modify visibility in this case
  11864. buffer)
  11865. (t ; make a new indirect buffer for displaying the link
  11866. (let* ((bn (buffer-name buffer))
  11867. (ibn (concat bn "(Clone)"))
  11868. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11869. (with-current-buffer ib (org-overview))
  11870. ib))))
  11871. (defun org-do-occur (regexp &optional cleanup)
  11872. "Call the Emacs command `occur'.
  11873. If CLEANUP is non-nil, remove the printout of the regular expression
  11874. in the *Occur* buffer. This is useful if the regex is long and not useful
  11875. to read."
  11876. (occur regexp)
  11877. (when cleanup
  11878. (let ((cwin (selected-window)) win beg end)
  11879. (when (setq win (get-buffer-window "*Occur*"))
  11880. (select-window win))
  11881. (goto-char (point-min))
  11882. (when (re-search-forward "match[a-z]+" nil t)
  11883. (setq beg (match-end 0))
  11884. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11885. (setq end (1- (match-beginning 0)))))
  11886. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11887. (goto-char (point-min))
  11888. (select-window cwin))))
  11889. ;;; The mark ring for links jumps
  11890. (defvar org-mark-ring nil
  11891. "Mark ring for positions before jumps in Org-mode.")
  11892. (defvar org-mark-ring-last-goto nil
  11893. "Last position in the mark ring used to go back.")
  11894. ;; Fill and close the ring
  11895. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11896. (loop for i from 1 to org-mark-ring-length do
  11897. (push (make-marker) org-mark-ring))
  11898. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11899. org-mark-ring)
  11900. (defun org-mark-ring-push (&optional pos buffer)
  11901. "Put the current position or POS into the mark ring and rotate it."
  11902. (interactive)
  11903. (setq pos (or pos (point)))
  11904. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11905. (move-marker (car org-mark-ring)
  11906. (or pos (point))
  11907. (or buffer (current-buffer)))
  11908. (message "%s"
  11909. (substitute-command-keys
  11910. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11911. (defun org-mark-ring-goto (&optional n)
  11912. "Jump to the previous position in the mark ring.
  11913. With prefix arg N, jump back that many stored positions. When
  11914. called several times in succession, walk through the entire ring.
  11915. Org-mode commands jumping to a different position in the current file,
  11916. or to another Org-mode file, automatically push the old position
  11917. onto the ring."
  11918. (interactive "p")
  11919. (let (p m)
  11920. (if (eq last-command this-command)
  11921. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11922. (setq p org-mark-ring))
  11923. (setq org-mark-ring-last-goto p)
  11924. (setq m (car p))
  11925. (switch-to-buffer (marker-buffer m))
  11926. (goto-char m)
  11927. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11928. (defun org-remove-angle-brackets (s)
  11929. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11930. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11931. s)
  11932. (defun org-add-angle-brackets (s)
  11933. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11934. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11935. s)
  11936. ;;; Following specific links
  11937. (defun org-follow-timestamp-link ()
  11938. (cond
  11939. ((org-at-date-range-p t)
  11940. (let ((org-agenda-start-on-weekday)
  11941. (t1 (match-string 1))
  11942. (t2 (match-string 2)))
  11943. (setq t1 (time-to-days (org-time-string-to-time t1))
  11944. t2 (time-to-days (org-time-string-to-time t2)))
  11945. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11946. ((org-at-timestamp-p t)
  11947. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11948. (substring (match-string 1) 0 10)))
  11949. 1))
  11950. (t (error "This should not happen"))))
  11951. (defun org-follow-bbdb-link (name)
  11952. "Follow a BBDB link to NAME."
  11953. (require 'bbdb)
  11954. (let ((inhibit-redisplay (not debug-on-error))
  11955. (bbdb-electric-p nil))
  11956. (catch 'exit
  11957. ;; Exact match on name
  11958. (bbdb-name (concat "\\`" name "\\'") nil)
  11959. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11960. ;; Exact match on name
  11961. (bbdb-company (concat "\\`" name "\\'") nil)
  11962. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11963. ;; Partial match on name
  11964. (bbdb-name name nil)
  11965. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11966. ;; Partial match on company
  11967. (bbdb-company name nil)
  11968. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11969. ;; General match including network address and notes
  11970. (bbdb name nil)
  11971. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11972. (delete-window (get-buffer-window "*BBDB*"))
  11973. (error "No matching BBDB record")))))
  11974. (defun org-follow-info-link (name)
  11975. "Follow an info file & node link to NAME."
  11976. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11977. (string-match "\\(.*\\)" name))
  11978. (progn
  11979. (require 'info)
  11980. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11981. (Info-find-node (match-string 1 name) (match-string 2 name))
  11982. (Info-find-node (match-string 1 name) "Top")))
  11983. (message "Could not open: %s" name)))
  11984. (defun org-follow-gnus-link (&optional group article)
  11985. "Follow a Gnus link to GROUP and ARTICLE."
  11986. (require 'gnus)
  11987. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11988. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11989. (cond ((and group article)
  11990. (gnus-group-read-group 1 nil group)
  11991. (gnus-summary-goto-article (string-to-number article) nil t))
  11992. (group (gnus-group-jump-to-group group))))
  11993. (defun org-follow-vm-link (&optional folder article readonly)
  11994. "Follow a VM link to FOLDER and ARTICLE."
  11995. (require 'vm)
  11996. (setq article (org-add-angle-brackets article))
  11997. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11998. ;; ange-ftp or efs or tramp access
  11999. (let ((user (or (match-string 1 folder) (user-login-name)))
  12000. (host (match-string 2 folder))
  12001. (file (match-string 3 folder)))
  12002. (cond
  12003. ((featurep 'tramp)
  12004. ;; use tramp to access the file
  12005. (if (featurep 'xemacs)
  12006. (setq folder (format "[%s@%s]%s" user host file))
  12007. (setq folder (format "/%s@%s:%s" user host file))))
  12008. (t
  12009. ;; use ange-ftp or efs
  12010. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  12011. (setq folder (format "/%s@%s:%s" user host file))))))
  12012. (when folder
  12013. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  12014. (sit-for 0.1)
  12015. (when article
  12016. (vm-select-folder-buffer)
  12017. (widen)
  12018. (let ((case-fold-search t))
  12019. (goto-char (point-min))
  12020. (if (not (re-search-forward
  12021. (concat "^" "message-id: *" (regexp-quote article))))
  12022. (error "Could not find the specified message in this folder"))
  12023. (vm-isearch-update)
  12024. (vm-isearch-narrow)
  12025. (vm-beginning-of-message)
  12026. (vm-summarize)))))
  12027. (defun org-follow-wl-link (folder article)
  12028. "Follow a Wanderlust link to FOLDER and ARTICLE."
  12029. (if (and (string= folder "%")
  12030. article
  12031. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  12032. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  12033. ;; Thus, we recompose folder and article ids.
  12034. (setq folder (format "%s#%s" folder (match-string 1 article))
  12035. article (match-string 3 article)))
  12036. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  12037. (error "No such folder: %s" folder))
  12038. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  12039. (and article
  12040. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  12041. (wl-summary-redisplay)))
  12042. (defun org-follow-rmail-link (folder article)
  12043. "Follow an RMAIL link to FOLDER and ARTICLE."
  12044. (setq article (org-add-angle-brackets article))
  12045. (let (message-number)
  12046. (save-excursion
  12047. (save-window-excursion
  12048. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12049. (setq message-number
  12050. (save-restriction
  12051. (widen)
  12052. (goto-char (point-max))
  12053. (if (re-search-backward
  12054. (concat "^Message-ID:\\s-+" (regexp-quote
  12055. (or article "")))
  12056. nil t)
  12057. (rmail-what-message))))))
  12058. (if message-number
  12059. (progn
  12060. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12061. (rmail-show-message message-number)
  12062. message-number)
  12063. (error "Message not found"))))
  12064. ;;; mh-e integration based on planner-mode
  12065. (defun org-mhe-get-message-real-folder ()
  12066. "Return the name of the current message real folder, so if you use
  12067. sequences, it will now work."
  12068. (save-excursion
  12069. (let* ((folder
  12070. (if (equal major-mode 'mh-folder-mode)
  12071. mh-current-folder
  12072. ;; Refer to the show buffer
  12073. mh-show-folder-buffer))
  12074. (end-index
  12075. (if (boundp 'mh-index-folder)
  12076. (min (length mh-index-folder) (length folder))))
  12077. )
  12078. ;; a simple test on mh-index-data does not work, because
  12079. ;; mh-index-data is always nil in a show buffer.
  12080. (if (and (boundp 'mh-index-folder)
  12081. (string= mh-index-folder (substring folder 0 end-index)))
  12082. (if (equal major-mode 'mh-show-mode)
  12083. (save-window-excursion
  12084. (let (pop-up-frames)
  12085. (when (buffer-live-p (get-buffer folder))
  12086. (progn
  12087. (pop-to-buffer folder)
  12088. (org-mhe-get-message-folder-from-index)
  12089. )
  12090. )))
  12091. (org-mhe-get-message-folder-from-index)
  12092. )
  12093. folder
  12094. )
  12095. )))
  12096. (defun org-mhe-get-message-folder-from-index ()
  12097. "Returns the name of the message folder in a index folder buffer."
  12098. (save-excursion
  12099. (mh-index-previous-folder)
  12100. (re-search-forward "^\\(+.*\\)$" nil t)
  12101. (message "%s" (match-string 1))))
  12102. (defun org-mhe-get-message-folder ()
  12103. "Return the name of the current message folder. Be careful if you
  12104. use sequences."
  12105. (save-excursion
  12106. (if (equal major-mode 'mh-folder-mode)
  12107. mh-current-folder
  12108. ;; Refer to the show buffer
  12109. mh-show-folder-buffer)))
  12110. (defun org-mhe-get-message-num ()
  12111. "Return the number of the current message. Be careful if you
  12112. use sequences."
  12113. (save-excursion
  12114. (if (equal major-mode 'mh-folder-mode)
  12115. (mh-get-msg-num nil)
  12116. ;; Refer to the show buffer
  12117. (mh-show-buffer-message-number))))
  12118. (defun org-mhe-get-header (header)
  12119. "Return a header of the message in folder mode. This will create a
  12120. show buffer for the corresponding message. If you have a more clever
  12121. idea..."
  12122. (let* ((folder (org-mhe-get-message-folder))
  12123. (num (org-mhe-get-message-num))
  12124. (buffer (get-buffer-create (concat "show-" folder)))
  12125. (header-field))
  12126. (with-current-buffer buffer
  12127. (mh-display-msg num folder)
  12128. (if (equal major-mode 'mh-folder-mode)
  12129. (mh-header-display)
  12130. (mh-show-header-display))
  12131. (set-buffer buffer)
  12132. (setq header-field (mh-get-header-field header))
  12133. (if (equal major-mode 'mh-folder-mode)
  12134. (mh-show)
  12135. (mh-show-show))
  12136. header-field)))
  12137. (defun org-follow-mhe-link (folder article)
  12138. "Follow an MHE link to FOLDER and ARTICLE.
  12139. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12140. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12141. ARTICLE is searched in all folders. Indexed searches (swish++,
  12142. namazu, and others supported by MH-E) will always search in all
  12143. folders."
  12144. (require 'mh-e)
  12145. (require 'mh-search)
  12146. (require 'mh-utils)
  12147. (mh-find-path)
  12148. (if (not article)
  12149. (mh-visit-folder (mh-normalize-folder-name folder))
  12150. (setq article (org-add-angle-brackets article))
  12151. (mh-search-choose)
  12152. (if (equal mh-searcher 'pick)
  12153. (progn
  12154. (mh-search folder (list "--message-id" article))
  12155. (when (and org-mhe-search-all-folders
  12156. (not (org-mhe-get-message-real-folder)))
  12157. (kill-this-buffer)
  12158. (mh-search "+" (list "--message-id" article))))
  12159. (mh-search "+" article))
  12160. (if (org-mhe-get-message-real-folder)
  12161. (mh-show-msg 1)
  12162. (kill-this-buffer)
  12163. (error "Message not found"))))
  12164. ;;; BibTeX links
  12165. ;; Use the custom search meachnism to construct and use search strings for
  12166. ;; file links to BibTeX database entries.
  12167. (defun org-create-file-search-in-bibtex ()
  12168. "Create the search string and description for a BibTeX database entry."
  12169. (when (eq major-mode 'bibtex-mode)
  12170. ;; yes, we want to construct this search string.
  12171. ;; Make a good description for this entry, using names, year and the title
  12172. ;; Put it into the `description' variable which is dynamically scoped.
  12173. (let ((bibtex-autokey-names 1)
  12174. (bibtex-autokey-names-stretch 1)
  12175. (bibtex-autokey-name-case-convert-function 'identity)
  12176. (bibtex-autokey-name-separator " & ")
  12177. (bibtex-autokey-additional-names " et al.")
  12178. (bibtex-autokey-year-length 4)
  12179. (bibtex-autokey-name-year-separator " ")
  12180. (bibtex-autokey-titlewords 3)
  12181. (bibtex-autokey-titleword-separator " ")
  12182. (bibtex-autokey-titleword-case-convert-function 'identity)
  12183. (bibtex-autokey-titleword-length 'infty)
  12184. (bibtex-autokey-year-title-separator ": "))
  12185. (setq description (bibtex-generate-autokey)))
  12186. ;; Now parse the entry, get the key and return it.
  12187. (save-excursion
  12188. (bibtex-beginning-of-entry)
  12189. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12190. (defun org-execute-file-search-in-bibtex (s)
  12191. "Find the link search string S as a key for a database entry."
  12192. (when (eq major-mode 'bibtex-mode)
  12193. ;; Yes, we want to do the search in this file.
  12194. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12195. (goto-char (point-min))
  12196. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12197. (regexp-quote s) "[ \t\n]*,") nil t)
  12198. (goto-char (match-beginning 0)))
  12199. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12200. ;; Use double prefix to indicate that any web link should be browsed
  12201. (let ((b (current-buffer)) (p (point)))
  12202. ;; Restore the window configuration because we just use the web link
  12203. (set-window-configuration org-window-config-before-follow-link)
  12204. (save-excursion (set-buffer b) (goto-char p)
  12205. (bibtex-url)))
  12206. (recenter 0)) ; Move entry start to beginning of window
  12207. ;; return t to indicate that the search is done.
  12208. t))
  12209. ;; Finally add the functions to the right hooks.
  12210. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12211. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12212. ;; end of Bibtex link setup
  12213. ;;; Following file links
  12214. (defun org-open-file (path &optional in-emacs line search)
  12215. "Open the file at PATH.
  12216. First, this expands any special file name abbreviations. Then the
  12217. configuration variable `org-file-apps' is checked if it contains an
  12218. entry for this file type, and if yes, the corresponding command is launched.
  12219. If no application is found, Emacs simply visits the file.
  12220. With optional argument IN-EMACS, Emacs will visit the file.
  12221. Optional LINE specifies a line to go to, optional SEARCH a string to
  12222. search for. If LINE or SEARCH is given, the file will always be
  12223. opened in Emacs.
  12224. If the file does not exist, an error is thrown."
  12225. (setq in-emacs (or in-emacs line search))
  12226. (let* ((file (if (equal path "")
  12227. buffer-file-name
  12228. (substitute-in-file-name (expand-file-name path))))
  12229. (apps (append org-file-apps (org-default-apps)))
  12230. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12231. (dirp (if remp nil (file-directory-p file)))
  12232. (dfile (downcase file))
  12233. (old-buffer (current-buffer))
  12234. (old-pos (point))
  12235. (old-mode major-mode)
  12236. ext cmd)
  12237. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12238. (setq ext (match-string 1 dfile))
  12239. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12240. (setq ext (match-string 1 dfile))))
  12241. (if in-emacs
  12242. (setq cmd 'emacs)
  12243. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12244. (and dirp (cdr (assoc 'directory apps)))
  12245. (cdr (assoc ext apps))
  12246. (cdr (assoc t apps)))))
  12247. (when (eq cmd 'mailcap)
  12248. (require 'mailcap)
  12249. (mailcap-parse-mailcaps)
  12250. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12251. (command (mailcap-mime-info mime-type)))
  12252. (if (stringp command)
  12253. (setq cmd command)
  12254. (setq cmd 'emacs))))
  12255. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12256. (not (file-exists-p file))
  12257. (not org-open-non-existing-files))
  12258. (error "No such file: %s" file))
  12259. (cond
  12260. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12261. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12262. (while (string-match "['\"]%s['\"]" cmd)
  12263. (setq cmd (replace-match "%s" t t cmd)))
  12264. (while (string-match "%s" cmd)
  12265. (setq cmd (replace-match
  12266. (save-match-data (shell-quote-argument file))
  12267. t t cmd)))
  12268. (save-window-excursion
  12269. (start-process-shell-command cmd nil cmd)))
  12270. ((or (stringp cmd)
  12271. (eq cmd 'emacs))
  12272. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12273. (widen)
  12274. (if line (goto-line line)
  12275. (if search (org-link-search search))))
  12276. ((consp cmd)
  12277. (eval cmd))
  12278. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12279. (and (org-mode-p) (eq old-mode 'org-mode)
  12280. (or (not (equal old-buffer (current-buffer)))
  12281. (not (equal old-pos (point))))
  12282. (org-mark-ring-push old-pos old-buffer))))
  12283. (defun org-default-apps ()
  12284. "Return the default applications for this operating system."
  12285. (cond
  12286. ((eq system-type 'darwin)
  12287. org-file-apps-defaults-macosx)
  12288. ((eq system-type 'windows-nt)
  12289. org-file-apps-defaults-windowsnt)
  12290. (t org-file-apps-defaults-gnu)))
  12291. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12292. (defun org-file-remote-p (file)
  12293. "Test whether FILE specifies a location on a remote system.
  12294. Return non-nil if the location is indeed remote.
  12295. For example, the filename \"/user@host:/foo\" specifies a location
  12296. on the system \"/user@host:\"."
  12297. (cond ((fboundp 'file-remote-p)
  12298. (file-remote-p file))
  12299. ((fboundp 'tramp-handle-file-remote-p)
  12300. (tramp-handle-file-remote-p file))
  12301. ((and (boundp 'ange-ftp-name-format)
  12302. (string-match (car ange-ftp-name-format) file))
  12303. t)
  12304. (t nil)))
  12305. ;;;; Hooks for remember.el, and refiling
  12306. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12307. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12308. ;;;###autoload
  12309. (defun org-remember-insinuate ()
  12310. "Setup remember.el for use wiht Org-mode."
  12311. (require 'remember)
  12312. (setq remember-annotation-functions '(org-remember-annotation))
  12313. (setq remember-handler-functions '(org-remember-handler))
  12314. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12315. ;;;###autoload
  12316. (defun org-remember-annotation ()
  12317. "Return a link to the current location as an annotation for remember.el.
  12318. If you are using Org-mode files as target for data storage with
  12319. remember.el, then the annotations should include a link compatible with the
  12320. conventions in Org-mode. This function returns such a link."
  12321. (org-store-link nil))
  12322. (defconst org-remember-help
  12323. "Select a destination location for the note.
  12324. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12325. RET on headline -> Store as sublevel entry to current headline
  12326. RET at beg-of-buf -> Append to file as level 2 headline
  12327. <left>/<right> -> before/after current headline, same headings level")
  12328. (defvar org-remember-previous-location nil)
  12329. (defvar org-force-remember-template-char) ;; dynamically scoped
  12330. (defun org-select-remember-template (&optional use-char)
  12331. (when org-remember-templates
  12332. (let* ((templates (mapcar (lambda (x)
  12333. (if (stringp (car x))
  12334. (append (list (nth 1 x) (car x)) (cddr x))
  12335. (append (list (car x) "") (cdr x))))
  12336. org-remember-templates))
  12337. (char (or use-char
  12338. (cond
  12339. ((= (length templates) 1)
  12340. (caar templates))
  12341. ((and (boundp 'org-force-remember-template-char)
  12342. org-force-remember-template-char)
  12343. (if (stringp org-force-remember-template-char)
  12344. (string-to-char org-force-remember-template-char)
  12345. org-force-remember-template-char))
  12346. (t
  12347. (message "Select template: %s"
  12348. (mapconcat
  12349. (lambda (x)
  12350. (cond
  12351. ((not (string-match "\\S-" (nth 1 x)))
  12352. (format "[%c]" (car x)))
  12353. ((equal (downcase (car x))
  12354. (downcase (aref (nth 1 x) 0)))
  12355. (format "[%c]%s" (car x)
  12356. (substring (nth 1 x) 1)))
  12357. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12358. templates " "))
  12359. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12360. (when (equal char0 ?\C-g)
  12361. (jump-to-register remember-register)
  12362. (kill-buffer remember-buffer))
  12363. char0))))))
  12364. (cddr (assoc char templates)))))
  12365. (defvar x-last-selected-text)
  12366. (defvar x-last-selected-text-primary)
  12367. ;;;###autoload
  12368. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12369. "Initialize *remember* buffer with template, invoke `org-mode'.
  12370. This function should be placed into `remember-mode-hook' and in fact requires
  12371. to be run from that hook to function properly."
  12372. (if org-remember-templates
  12373. (let* ((entry (org-select-remember-template use-char))
  12374. (tpl (car entry))
  12375. (plist-p (if org-store-link-plist t nil))
  12376. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12377. (string-match "\\S-" (nth 1 entry)))
  12378. (nth 1 entry)
  12379. org-default-notes-file))
  12380. (headline (nth 2 entry))
  12381. (v-c (or (and (eq window-system 'x)
  12382. (fboundp 'x-cut-buffer-or-selection-value)
  12383. (x-cut-buffer-or-selection-value))
  12384. (org-bound-and-true-p x-last-selected-text)
  12385. (org-bound-and-true-p x-last-selected-text-primary)
  12386. (and (> (length kill-ring) 0) (current-kill 0))))
  12387. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12388. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12389. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12390. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12391. ;; `initial' and `annotation' are bound in `remember'
  12392. (v-i (if (boundp 'initial) initial))
  12393. (v-a (if (and (boundp 'annotation) annotation)
  12394. (if (equal annotation "[[]]") "" annotation)
  12395. ""))
  12396. (v-A (if (and v-a
  12397. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12398. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12399. v-a))
  12400. (v-n user-full-name)
  12401. (org-startup-folded nil)
  12402. org-time-was-given org-end-time-was-given x
  12403. prompt completions char time pos default histvar)
  12404. (setq org-store-link-plist
  12405. (append (list :annotation v-a :initial v-i)
  12406. org-store-link-plist))
  12407. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12408. (erase-buffer)
  12409. (insert (substitute-command-keys
  12410. (format
  12411. "## Filing location: Select interactively, default, or last used:
  12412. ## %s to select file and header location interactively.
  12413. ## %s \"%s\" -> \"* %s\"
  12414. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12415. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12416. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12417. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12418. (abbreviate-file-name (or file org-default-notes-file))
  12419. (or headline "")
  12420. (or (car org-remember-previous-location) "???")
  12421. (or (cdr org-remember-previous-location) "???"))))
  12422. (insert tpl) (goto-char (point-min))
  12423. ;; Simple %-escapes
  12424. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12425. (when (and initial (equal (match-string 0) "%i"))
  12426. (save-match-data
  12427. (let* ((lead (buffer-substring
  12428. (point-at-bol) (match-beginning 0))))
  12429. (setq v-i (mapconcat 'identity
  12430. (org-split-string initial "\n")
  12431. (concat "\n" lead))))))
  12432. (replace-match
  12433. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12434. t t))
  12435. ;; %[] Insert contents of a file.
  12436. (goto-char (point-min))
  12437. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12438. (let ((start (match-beginning 0))
  12439. (end (match-end 0))
  12440. (filename (expand-file-name (match-string 1))))
  12441. (goto-char start)
  12442. (delete-region start end)
  12443. (condition-case error
  12444. (insert-file-contents filename)
  12445. (error (insert (format "%%![Couldn't insert %s: %s]"
  12446. filename error))))))
  12447. ;; %() embedded elisp
  12448. (goto-char (point-min))
  12449. (while (re-search-forward "%\\((.+)\\)" nil t)
  12450. (goto-char (match-beginning 0))
  12451. (let ((template-start (point)))
  12452. (forward-char 1)
  12453. (let ((result
  12454. (condition-case error
  12455. (eval (read (current-buffer)))
  12456. (error (format "%%![Error: %s]" error)))))
  12457. (delete-region template-start (point))
  12458. (insert result))))
  12459. ;; From the property list
  12460. (when plist-p
  12461. (goto-char (point-min))
  12462. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12463. (and (setq x (or (plist-get org-store-link-plist
  12464. (intern (match-string 1))) ""))
  12465. (replace-match x t t))))
  12466. ;; Turn on org-mode in the remember buffer, set local variables
  12467. (org-mode)
  12468. (org-set-local 'org-finish-function 'org-remember-finalize)
  12469. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12470. (org-set-local 'org-default-notes-file file))
  12471. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12472. (org-set-local 'org-remember-default-headline headline))
  12473. ;; Interactive template entries
  12474. (goto-char (point-min))
  12475. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12476. (setq char (if (match-end 3) (match-string 3))
  12477. prompt (if (match-end 2) (match-string 2)))
  12478. (goto-char (match-beginning 0))
  12479. (replace-match "")
  12480. (setq completions nil default nil)
  12481. (when prompt
  12482. (setq completions (org-split-string prompt "|")
  12483. prompt (pop completions)
  12484. default (car completions)
  12485. histvar (intern (concat
  12486. "org-remember-template-prompt-history::"
  12487. (or prompt "")))
  12488. completions (mapcar 'list completions)))
  12489. (cond
  12490. ((member char '("G" "g"))
  12491. (let* ((org-last-tags-completion-table
  12492. (org-global-tags-completion-table
  12493. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12494. (org-add-colon-after-tag-completion t)
  12495. (ins (completing-read
  12496. (if prompt (concat prompt ": ") "Tags: ")
  12497. 'org-tags-completion-function nil nil nil
  12498. 'org-tags-history)))
  12499. (setq ins (mapconcat 'identity
  12500. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12501. ":"))
  12502. (when (string-match "\\S-" ins)
  12503. (or (equal (char-before) ?:) (insert ":"))
  12504. (insert ins)
  12505. (or (equal (char-after) ?:) (insert ":")))))
  12506. (char
  12507. (setq org-time-was-given (equal (upcase char) char))
  12508. (setq time (org-read-date (equal (upcase char) "U") t nil
  12509. prompt))
  12510. (org-insert-time-stamp time org-time-was-given
  12511. (member char '("u" "U"))
  12512. nil nil (list org-end-time-was-given)))
  12513. (t
  12514. (insert (org-completing-read
  12515. (concat (if prompt prompt "Enter string")
  12516. (if default (concat " [" default "]"))
  12517. ": ")
  12518. completions nil nil nil histvar default)))))
  12519. (goto-char (point-min))
  12520. (if (re-search-forward "%\\?" nil t)
  12521. (replace-match "")
  12522. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12523. (org-mode)
  12524. (org-set-local 'org-finish-function 'org-remember-finalize))
  12525. (when (save-excursion
  12526. (goto-char (point-min))
  12527. (re-search-forward "%!" nil t))
  12528. (replace-match "")
  12529. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12530. (defun org-remember-finish-immediately ()
  12531. "File remember note immediately.
  12532. This should be run in `post-command-hook' and will remove itself
  12533. from that hook."
  12534. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12535. (when org-finish-function
  12536. (funcall org-finish-function)))
  12537. (defvar org-clock-marker) ; Defined below
  12538. (defun org-remember-finalize ()
  12539. "Finalize the remember process."
  12540. (unless (fboundp 'remember-finalize)
  12541. (defalias 'remember-finalize 'remember-buffer))
  12542. (when (and org-clock-marker
  12543. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12544. ;; FIXME: test this, this is w/o notetaking!
  12545. (let (org-log-note-clock-out) (org-clock-out)))
  12546. (when buffer-file-name
  12547. (save-buffer)
  12548. (setq buffer-file-name nil))
  12549. (remember-finalize))
  12550. ;;;###autoload
  12551. (defun org-remember (&optional goto org-force-remember-template-char)
  12552. "Call `remember'. If this is already a remember buffer, re-apply template.
  12553. If there is an active region, make sure remember uses it as initial content
  12554. of the remember buffer.
  12555. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12556. anything, just go to the file/headline where the selected template usually
  12557. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12558. note stored by remember.
  12559. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12560. associated with a template in `org-remember-templates'."
  12561. (interactive "P")
  12562. (cond
  12563. ((equal goto '(4)) (org-go-to-remember-target))
  12564. ((equal goto '(16)) (org-remember-goto-last-stored))
  12565. (t
  12566. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12567. (progn
  12568. (when (< (length org-remember-templates) 2)
  12569. (error "No other template available"))
  12570. (erase-buffer)
  12571. (let ((annotation (plist-get org-store-link-plist :annotation))
  12572. (initial (plist-get org-store-link-plist :initial)))
  12573. (org-remember-apply-template))
  12574. (message "Press C-c C-c to remember data"))
  12575. (if (org-region-active-p)
  12576. (remember (buffer-substring (point) (mark)))
  12577. (call-interactively 'remember))))))
  12578. (defun org-remember-goto-last-stored ()
  12579. "Go to the location where the last remember note was stored."
  12580. (interactive)
  12581. (bookmark-jump "org-remember-last-stored")
  12582. (message "This is the last note stored by remember"))
  12583. (defun org-go-to-remember-target (&optional template-key)
  12584. "Go to the target location of a remember template.
  12585. The user is queried for the template."
  12586. (interactive)
  12587. (let* ((entry (org-select-remember-template template-key))
  12588. (file (nth 1 entry))
  12589. (heading (nth 2 entry))
  12590. visiting)
  12591. (unless (and file (stringp file) (string-match "\\S-" file))
  12592. (setq file org-default-notes-file))
  12593. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12594. (setq heading org-remember-default-headline))
  12595. (setq visiting (org-find-base-buffer-visiting file))
  12596. (if (not visiting) (find-file-noselect file))
  12597. (switch-to-buffer (or visiting (get-file-buffer file)))
  12598. (widen)
  12599. (goto-char (point-min))
  12600. (if (re-search-forward
  12601. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12602. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12603. nil t)
  12604. (goto-char (match-beginning 0))
  12605. (error "Target headline not found: %s" heading))))
  12606. (defvar org-note-abort nil) ; dynamically scoped
  12607. ;;;###autoload
  12608. (defun org-remember-handler ()
  12609. "Store stuff from remember.el into an org file.
  12610. First prompts for an org file. If the user just presses return, the value
  12611. of `org-default-notes-file' is used.
  12612. Then the command offers the headings tree of the selected file in order to
  12613. file the text at a specific location.
  12614. You can either immediately press RET to get the note appended to the
  12615. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12616. find a better place. Then press RET or <left> or <right> in insert the note.
  12617. Key Cursor position Note gets inserted
  12618. -----------------------------------------------------------------------------
  12619. RET buffer-start as level 1 heading at end of file
  12620. RET on headline as sublevel of the heading at cursor
  12621. RET no heading at cursor position, level taken from context.
  12622. Or use prefix arg to specify level manually.
  12623. <left> on headline as same level, before current heading
  12624. <right> on headline as same level, after current heading
  12625. So the fastest way to store the note is to press RET RET to append it to
  12626. the default file. This way your current train of thought is not
  12627. interrupted, in accordance with the principles of remember.el.
  12628. You can also get the fast execution without prompting by using
  12629. C-u C-c C-c to exit the remember buffer. See also the variable
  12630. `org-remember-store-without-prompt'.
  12631. Before being stored away, the function ensures that the text has a
  12632. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12633. is constructed from the current date and some additional data.
  12634. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12635. also indented so that it starts in the same column as the headline
  12636. \(i.e. after the stars).
  12637. See also the variable `org-reverse-note-order'."
  12638. (goto-char (point-min))
  12639. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12640. (replace-match ""))
  12641. (goto-char (point-max))
  12642. (beginning-of-line 1)
  12643. (while (looking-at "[ \t]*$\\|##.*")
  12644. (delete-region (1- (point)) (point-max))
  12645. (beginning-of-line 1))
  12646. (catch 'quit
  12647. (if org-note-abort (throw 'quit nil))
  12648. (let* ((txt (buffer-substring (point-min) (point-max)))
  12649. (fastp (org-xor (equal current-prefix-arg '(4))
  12650. org-remember-store-without-prompt))
  12651. (file (cond
  12652. (fastp org-default-notes-file)
  12653. ((and (eq org-remember-interactive-interface 'refile)
  12654. org-refile-targets)
  12655. org-default-notes-file)
  12656. ((not (and (equal current-prefix-arg '(16))
  12657. org-remember-previous-location))
  12658. (org-get-org-file))))
  12659. (heading org-remember-default-headline)
  12660. (visiting (and file (org-find-base-buffer-visiting file)))
  12661. (org-startup-folded nil)
  12662. (org-startup-align-all-tables nil)
  12663. (org-goto-start-pos 1)
  12664. spos exitcmd level indent reversed)
  12665. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12666. (setq file (car org-remember-previous-location)
  12667. heading (cdr org-remember-previous-location)
  12668. fastp t))
  12669. (setq current-prefix-arg nil)
  12670. (if (string-match "[ \t\n]+\\'" txt)
  12671. (setq txt (replace-match "" t t txt)))
  12672. ;; Modify text so that it becomes a nice subtree which can be inserted
  12673. ;; into an org tree.
  12674. (let* ((lines (split-string txt "\n"))
  12675. first)
  12676. (setq first (car lines) lines (cdr lines))
  12677. (if (string-match "^\\*+ " first)
  12678. ;; Is already a headline
  12679. (setq indent nil)
  12680. ;; We need to add a headline: Use time and first buffer line
  12681. (setq lines (cons first lines)
  12682. first (concat "* " (current-time-string)
  12683. " (" (remember-buffer-desc) ")")
  12684. indent " "))
  12685. (if (and org-adapt-indentation indent)
  12686. (setq lines (mapcar
  12687. (lambda (x)
  12688. (if (string-match "\\S-" x)
  12689. (concat indent x) x))
  12690. lines)))
  12691. (setq txt (concat first "\n"
  12692. (mapconcat 'identity lines "\n"))))
  12693. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12694. (setq txt (replace-match "\n\n" t t txt))
  12695. (if (string-match "[ \t\n]*\\'" txt)
  12696. (setq txt (replace-match "\n" t t txt))))
  12697. ;; Put the modified text back into the remember buffer, for refile.
  12698. (erase-buffer)
  12699. (insert txt)
  12700. (goto-char (point-min))
  12701. (when (and (eq org-remember-interactive-interface 'refile)
  12702. (not fastp))
  12703. (org-refile nil (or visiting (find-file-noselect file)))
  12704. (throw 'quit t))
  12705. ;; Find the file
  12706. (if (not visiting) (find-file-noselect file))
  12707. (with-current-buffer (or visiting (get-file-buffer file))
  12708. (unless (org-mode-p)
  12709. (error "Target files for remember notes must be in Org-mode"))
  12710. (save-excursion
  12711. (save-restriction
  12712. (widen)
  12713. (and (goto-char (point-min))
  12714. (not (re-search-forward "^\\* " nil t))
  12715. (insert "\n* " (or heading "Notes") "\n"))
  12716. (setq reversed (org-notes-order-reversed-p))
  12717. ;; Find the default location
  12718. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12719. (goto-char (point-min))
  12720. (if (re-search-forward
  12721. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12722. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12723. nil t)
  12724. (setq org-goto-start-pos (match-beginning 0))
  12725. (when fastp
  12726. (goto-char (point-max))
  12727. (unless (bolp) (newline))
  12728. (insert "* " heading "\n")
  12729. (setq org-goto-start-pos (point-at-bol 0)))))
  12730. ;; Ask the User for a location, using the appropriate interface
  12731. (cond
  12732. (fastp (setq spos org-goto-start-pos
  12733. exitcmd 'return))
  12734. ((eq org-remember-interactive-interface 'outline)
  12735. (setq spos (org-get-location (current-buffer)
  12736. org-remember-help)
  12737. exitcmd (cdr spos)
  12738. spos (car spos)))
  12739. ((eq org-remember-interactive-interface 'outline-path-completion)
  12740. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12741. (org-refile-use-outline-path t))
  12742. (setq spos (org-refile-get-location "Heading: ")
  12743. exitcmd 'return
  12744. spos (nth 3 spos))))
  12745. (t (error "this should not hapen")))
  12746. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12747. ; not handle this note
  12748. (goto-char spos)
  12749. (cond ((org-on-heading-p t)
  12750. (org-back-to-heading t)
  12751. (setq level (funcall outline-level))
  12752. (cond
  12753. ((eq exitcmd 'return)
  12754. ;; sublevel of current
  12755. (setq org-remember-previous-location
  12756. (cons (abbreviate-file-name file)
  12757. (org-get-heading 'notags)))
  12758. (if reversed
  12759. (outline-next-heading)
  12760. (org-end-of-subtree t)
  12761. (if (not (bolp))
  12762. (if (looking-at "[ \t]*\n")
  12763. (beginning-of-line 2)
  12764. (end-of-line 1)
  12765. (insert "\n"))))
  12766. (bookmark-set "org-remember-last-stored")
  12767. (org-paste-subtree (org-get-legal-level level 1) txt))
  12768. ((eq exitcmd 'left)
  12769. ;; before current
  12770. (bookmark-set "org-remember-last-stored")
  12771. (org-paste-subtree level txt))
  12772. ((eq exitcmd 'right)
  12773. ;; after current
  12774. (org-end-of-subtree t)
  12775. (bookmark-set "org-remember-last-stored")
  12776. (org-paste-subtree level txt))
  12777. (t (error "This should not happen"))))
  12778. ((and (bobp) (not reversed))
  12779. ;; Put it at the end, one level below level 1
  12780. (save-restriction
  12781. (widen)
  12782. (goto-char (point-max))
  12783. (if (not (bolp)) (newline))
  12784. (bookmark-set "org-remember-last-stored")
  12785. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12786. ((and (bobp) reversed)
  12787. ;; Put it at the start, as level 1
  12788. (save-restriction
  12789. (widen)
  12790. (goto-char (point-min))
  12791. (re-search-forward "^\\*+ " nil t)
  12792. (beginning-of-line 1)
  12793. (bookmark-set "org-remember-last-stored")
  12794. (org-paste-subtree 1 txt)))
  12795. (t
  12796. ;; Put it right there, with automatic level determined by
  12797. ;; org-paste-subtree or from prefix arg
  12798. (bookmark-set "org-remember-last-stored")
  12799. (org-paste-subtree
  12800. (if (numberp current-prefix-arg) current-prefix-arg)
  12801. txt)))
  12802. (when remember-save-after-remembering
  12803. (save-buffer)
  12804. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12805. t) ;; return t to indicate that we took care of this note.
  12806. (defun org-get-org-file ()
  12807. "Read a filename, with default directory `org-directory'."
  12808. (let ((default (or org-default-notes-file remember-data-file)))
  12809. (read-file-name (format "File name [%s]: " default)
  12810. (file-name-as-directory org-directory)
  12811. default)))
  12812. (defun org-notes-order-reversed-p ()
  12813. "Check if the current file should receive notes in reversed order."
  12814. (cond
  12815. ((not org-reverse-note-order) nil)
  12816. ((eq t org-reverse-note-order) t)
  12817. ((not (listp org-reverse-note-order)) nil)
  12818. (t (catch 'exit
  12819. (let ((all org-reverse-note-order)
  12820. entry)
  12821. (while (setq entry (pop all))
  12822. (if (string-match (car entry) buffer-file-name)
  12823. (throw 'exit (cdr entry))))
  12824. nil)))))
  12825. ;;; Refiling
  12826. (defvar org-refile-target-table nil
  12827. "The list of refile targets, created by `org-refile'.")
  12828. (defvar org-agenda-new-buffers nil
  12829. "Buffers created to visit agenda files.")
  12830. (defun org-get-refile-targets (&optional default-buffer)
  12831. "Produce a table with refile targets."
  12832. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12833. targets txt re files f desc descre)
  12834. (with-current-buffer (or default-buffer (current-buffer))
  12835. (while (setq entry (pop entries))
  12836. (setq files (car entry) desc (cdr entry))
  12837. (cond
  12838. ((null files) (setq files (list (current-buffer))))
  12839. ((eq files 'org-agenda-files)
  12840. (setq files (org-agenda-files 'unrestricted)))
  12841. ((and (symbolp files) (fboundp files))
  12842. (setq files (funcall files)))
  12843. ((and (symbolp files) (boundp files))
  12844. (setq files (symbol-value files))))
  12845. (if (stringp files) (setq files (list files)))
  12846. (cond
  12847. ((eq (car desc) :tag)
  12848. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12849. ((eq (car desc) :todo)
  12850. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12851. ((eq (car desc) :regexp)
  12852. (setq descre (cdr desc)))
  12853. ((eq (car desc) :level)
  12854. (setq descre (concat "^\\*\\{" (number-to-string
  12855. (if org-odd-levels-only
  12856. (1- (* 2 (cdr desc)))
  12857. (cdr desc)))
  12858. "\\}[ \t]")))
  12859. ((eq (car desc) :maxlevel)
  12860. (setq descre (concat "^\\*\\{1," (number-to-string
  12861. (if org-odd-levels-only
  12862. (1- (* 2 (cdr desc)))
  12863. (cdr desc)))
  12864. "\\}[ \t]")))
  12865. (t (error "Bad refiling target description %s" desc)))
  12866. (while (setq f (pop files))
  12867. (save-excursion
  12868. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12869. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12870. (save-excursion
  12871. (save-restriction
  12872. (widen)
  12873. (goto-char (point-min))
  12874. (while (re-search-forward descre nil t)
  12875. (goto-char (point-at-bol))
  12876. (when (looking-at org-complex-heading-regexp)
  12877. (setq txt (match-string 4)
  12878. re (concat "^" (regexp-quote
  12879. (buffer-substring (match-beginning 1)
  12880. (match-end 4)))))
  12881. (if (match-end 5) (setq re (concat re "[ \t]+"
  12882. (regexp-quote
  12883. (match-string 5)))))
  12884. (setq re (concat re "[ \t]*$"))
  12885. (when org-refile-use-outline-path
  12886. (setq txt (mapconcat 'identity
  12887. (append
  12888. (if (eq org-refile-use-outline-path 'file)
  12889. (list (file-name-nondirectory
  12890. (buffer-file-name (buffer-base-buffer))))
  12891. (if (eq org-refile-use-outline-path 'full-file-path)
  12892. (list (buffer-file-name (buffer-base-buffer)))))
  12893. (org-get-outline-path)
  12894. (list txt))
  12895. "/")))
  12896. (push (list txt f re (point)) targets))
  12897. (goto-char (point-at-eol))))))))
  12898. (nreverse targets))))
  12899. (defun org-get-outline-path ()
  12900. "Return the outline path to the current entry, as a list."
  12901. (let (rtn)
  12902. (save-excursion
  12903. (while (org-up-heading-safe)
  12904. (when (looking-at org-complex-heading-regexp)
  12905. (push (org-match-string-no-properties 4) rtn)))
  12906. rtn)))
  12907. (defvar org-refile-history nil
  12908. "History for refiling operations.")
  12909. (defun org-refile (&optional goto default-buffer)
  12910. "Move the entry at point to another heading.
  12911. The list of target headings is compiled using the information in
  12912. `org-refile-targets', which see. This list is created upon first use, and
  12913. you can update it by calling this command with a double prefix (`C-u C-u').
  12914. FIXME: Can we find a better way of updating?
  12915. At the target location, the entry is filed as a subitem of the target heading.
  12916. Depending on `org-reverse-note-order', the new subitem will either be the
  12917. first of the last subitem.
  12918. With prefix arg GOTO, the command will only visit the target location,
  12919. not actually move anything.
  12920. With a double prefix `C-c C-c', go to the location where the last refiling
  12921. operation has put the subtree.
  12922. With a double prefix argument, the command can be used to jump to any
  12923. heading in the current buffer."
  12924. (interactive "P")
  12925. (let* ((cbuf (current-buffer))
  12926. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12927. pos it nbuf file re level reversed)
  12928. (if (equal goto '(16))
  12929. (org-refile-goto-last-stored)
  12930. (when (setq it (org-refile-get-location
  12931. (if goto "Goto: " "Refile to: ") default-buffer))
  12932. (setq file (nth 1 it)
  12933. re (nth 2 it)
  12934. pos (nth 3 it))
  12935. (setq nbuf (or (find-buffer-visiting file)
  12936. (find-file-noselect file)))
  12937. (if goto
  12938. (progn
  12939. (switch-to-buffer nbuf)
  12940. (goto-char pos)
  12941. (org-show-context 'org-goto))
  12942. (org-copy-special)
  12943. (save-excursion
  12944. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12945. (find-file-noselect file))))
  12946. (setq reversed (org-notes-order-reversed-p))
  12947. (save-excursion
  12948. (save-restriction
  12949. (widen)
  12950. (goto-char pos)
  12951. (looking-at outline-regexp)
  12952. (setq level (org-get-legal-level (funcall outline-level) 1))
  12953. (goto-char
  12954. (if reversed
  12955. (outline-next-heading)
  12956. (or (save-excursion (outline-get-next-sibling))
  12957. (org-end-of-subtree t t)
  12958. (point-max))))
  12959. (bookmark-set "org-refile-last-stored")
  12960. (org-paste-subtree level))))
  12961. (org-cut-special)
  12962. (message "Entry refiled to \"%s\"" (car it)))))))
  12963. (defun org-refile-goto-last-stored ()
  12964. "Go to the location where the last refile was stored."
  12965. (interactive)
  12966. (bookmark-jump "org-refile-last-stored")
  12967. (message "This is the location of the last refile"))
  12968. (defun org-refile-get-location (&optional prompt default-buffer)
  12969. "Prompt the user for a refile location, using PROMPT."
  12970. (let ((org-refile-targets org-refile-targets)
  12971. (org-refile-use-outline-path org-refile-use-outline-path))
  12972. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12973. (unless org-refile-target-table
  12974. (error "No refile targets"))
  12975. (let* ((cbuf (current-buffer))
  12976. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12977. (fname (and filename (file-truename filename)))
  12978. (tbl (mapcar
  12979. (lambda (x)
  12980. (if (not (equal fname (file-truename (nth 1 x))))
  12981. (cons (concat (car x) " (" (file-name-nondirectory
  12982. (nth 1 x)) ")")
  12983. (cdr x))
  12984. x))
  12985. org-refile-target-table))
  12986. (completion-ignore-case t))
  12987. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12988. tbl)))
  12989. ;;;; Dynamic blocks
  12990. (defun org-find-dblock (name)
  12991. "Find the first dynamic block with name NAME in the buffer.
  12992. If not found, stay at current position and return nil."
  12993. (let (pos)
  12994. (save-excursion
  12995. (goto-char (point-min))
  12996. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12997. nil t)
  12998. (match-beginning 0))))
  12999. (if pos (goto-char pos))
  13000. pos))
  13001. (defconst org-dblock-start-re
  13002. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  13003. "Matches the startline of a dynamic block, with parameters.")
  13004. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  13005. "Matches the end of a dyhamic block.")
  13006. (defun org-create-dblock (plist)
  13007. "Create a dynamic block section, with parameters taken from PLIST.
  13008. PLIST must containe a :name entry which is used as name of the block."
  13009. (unless (bolp) (newline))
  13010. (let ((name (plist-get plist :name)))
  13011. (insert "#+BEGIN: " name)
  13012. (while plist
  13013. (if (eq (car plist) :name)
  13014. (setq plist (cddr plist))
  13015. (insert " " (prin1-to-string (pop plist)))))
  13016. (insert "\n\n#+END:\n")
  13017. (beginning-of-line -2)))
  13018. (defun org-prepare-dblock ()
  13019. "Prepare dynamic block for refresh.
  13020. This empties the block, puts the cursor at the insert position and returns
  13021. the property list including an extra property :name with the block name."
  13022. (unless (looking-at org-dblock-start-re)
  13023. (error "Not at a dynamic block"))
  13024. (let* ((begdel (1+ (match-end 0)))
  13025. (name (org-no-properties (match-string 1)))
  13026. (params (append (list :name name)
  13027. (read (concat "(" (match-string 3) ")")))))
  13028. (unless (re-search-forward org-dblock-end-re nil t)
  13029. (error "Dynamic block not terminated"))
  13030. (delete-region begdel (match-beginning 0))
  13031. (goto-char begdel)
  13032. (open-line 1)
  13033. params))
  13034. (defun org-map-dblocks (&optional command)
  13035. "Apply COMMAND to all dynamic blocks in the current buffer.
  13036. If COMMAND is not given, use `org-update-dblock'."
  13037. (let ((cmd (or command 'org-update-dblock))
  13038. pos)
  13039. (save-excursion
  13040. (goto-char (point-min))
  13041. (while (re-search-forward org-dblock-start-re nil t)
  13042. (goto-char (setq pos (match-beginning 0)))
  13043. (condition-case nil
  13044. (funcall cmd)
  13045. (error (message "Error during update of dynamic block")))
  13046. (goto-char pos)
  13047. (unless (re-search-forward org-dblock-end-re nil t)
  13048. (error "Dynamic block not terminated"))))))
  13049. (defun org-dblock-update (&optional arg)
  13050. "User command for updating dynamic blocks.
  13051. Update the dynamic block at point. With prefix ARG, update all dynamic
  13052. blocks in the buffer."
  13053. (interactive "P")
  13054. (if arg
  13055. (org-update-all-dblocks)
  13056. (or (looking-at org-dblock-start-re)
  13057. (org-beginning-of-dblock))
  13058. (org-update-dblock)))
  13059. (defun org-update-dblock ()
  13060. "Update the dynamic block at point
  13061. This means to empty the block, parse for parameters and then call
  13062. the correct writing function."
  13063. (save-window-excursion
  13064. (let* ((pos (point))
  13065. (line (org-current-line))
  13066. (params (org-prepare-dblock))
  13067. (name (plist-get params :name))
  13068. (cmd (intern (concat "org-dblock-write:" name))))
  13069. (message "Updating dynamic block `%s' at line %d..." name line)
  13070. (funcall cmd params)
  13071. (message "Updating dynamic block `%s' at line %d...done" name line)
  13072. (goto-char pos))))
  13073. (defun org-beginning-of-dblock ()
  13074. "Find the beginning of the dynamic block at point.
  13075. Error if there is no scuh block at point."
  13076. (let ((pos (point))
  13077. beg)
  13078. (end-of-line 1)
  13079. (if (and (re-search-backward org-dblock-start-re nil t)
  13080. (setq beg (match-beginning 0))
  13081. (re-search-forward org-dblock-end-re nil t)
  13082. (> (match-end 0) pos))
  13083. (goto-char beg)
  13084. (goto-char pos)
  13085. (error "Not in a dynamic block"))))
  13086. (defun org-update-all-dblocks ()
  13087. "Update all dynamic blocks in the buffer.
  13088. This function can be used in a hook."
  13089. (when (org-mode-p)
  13090. (org-map-dblocks 'org-update-dblock)))
  13091. ;;;; Completion
  13092. (defconst org-additional-option-like-keywords
  13093. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13094. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13095. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13096. (defun org-complete (&optional arg)
  13097. "Perform completion on word at point.
  13098. At the beginning of a headline, this completes TODO keywords as given in
  13099. `org-todo-keywords'.
  13100. If the current word is preceded by a backslash, completes the TeX symbols
  13101. that are supported for HTML support.
  13102. If the current word is preceded by \"#+\", completes special words for
  13103. setting file options.
  13104. In the line after \"#+STARTUP:, complete valid keywords.\"
  13105. At all other locations, this simply calls the value of
  13106. `org-completion-fallback-command'."
  13107. (interactive "P")
  13108. (org-without-partial-completion
  13109. (catch 'exit
  13110. (let* ((end (point))
  13111. (beg1 (save-excursion
  13112. (skip-chars-backward (org-re "[:alnum:]_@"))
  13113. (point)))
  13114. (beg (save-excursion
  13115. (skip-chars-backward "a-zA-Z0-9_:$")
  13116. (point)))
  13117. (confirm (lambda (x) (stringp (car x))))
  13118. (searchhead (equal (char-before beg) ?*))
  13119. (tag (and (equal (char-before beg1) ?:)
  13120. (equal (char-after (point-at-bol)) ?*)))
  13121. (prop (and (equal (char-before beg1) ?:)
  13122. (not (equal (char-after (point-at-bol)) ?*))))
  13123. (texp (equal (char-before beg) ?\\))
  13124. (link (equal (char-before beg) ?\[))
  13125. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13126. beg)
  13127. "#+"))
  13128. (startup (string-match "^#\\+STARTUP:.*"
  13129. (buffer-substring (point-at-bol) (point))))
  13130. (completion-ignore-case opt)
  13131. (type nil)
  13132. (tbl nil)
  13133. (table (cond
  13134. (opt
  13135. (setq type :opt)
  13136. (append
  13137. (mapcar
  13138. (lambda (x)
  13139. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13140. (cons (match-string 2 x) (match-string 1 x)))
  13141. (org-split-string (org-get-current-options) "\n"))
  13142. (mapcar 'list org-additional-option-like-keywords)))
  13143. (startup
  13144. (setq type :startup)
  13145. org-startup-options)
  13146. (link (append org-link-abbrev-alist-local
  13147. org-link-abbrev-alist))
  13148. (texp
  13149. (setq type :tex)
  13150. org-html-entities)
  13151. ((string-match "\\`\\*+[ \t]+\\'"
  13152. (buffer-substring (point-at-bol) beg))
  13153. (setq type :todo)
  13154. (mapcar 'list org-todo-keywords-1))
  13155. (searchhead
  13156. (setq type :searchhead)
  13157. (save-excursion
  13158. (goto-char (point-min))
  13159. (while (re-search-forward org-todo-line-regexp nil t)
  13160. (push (list
  13161. (org-make-org-heading-search-string
  13162. (match-string 3) t))
  13163. tbl)))
  13164. tbl)
  13165. (tag (setq type :tag beg beg1)
  13166. (or org-tag-alist (org-get-buffer-tags)))
  13167. (prop (setq type :prop beg beg1)
  13168. (mapcar 'list (org-buffer-property-keys nil t t)))
  13169. (t (progn
  13170. (call-interactively org-completion-fallback-command)
  13171. (throw 'exit nil)))))
  13172. (pattern (buffer-substring-no-properties beg end))
  13173. (completion (try-completion pattern table confirm)))
  13174. (cond ((eq completion t)
  13175. (if (not (assoc (upcase pattern) table))
  13176. (message "Already complete")
  13177. (if (equal type :opt)
  13178. (insert (substring (cdr (assoc (upcase pattern) table))
  13179. (length pattern)))
  13180. (if (memq type '(:tag :prop)) (insert ":")))))
  13181. ((null completion)
  13182. (message "Can't find completion for \"%s\"" pattern)
  13183. (ding))
  13184. ((not (string= pattern completion))
  13185. (delete-region beg end)
  13186. (if (string-match " +$" completion)
  13187. (setq completion (replace-match "" t t completion)))
  13188. (insert completion)
  13189. (if (get-buffer-window "*Completions*")
  13190. (delete-window (get-buffer-window "*Completions*")))
  13191. (if (assoc completion table)
  13192. (if (eq type :todo) (insert " ")
  13193. (if (memq type '(:tag :prop)) (insert ":"))))
  13194. (if (and (equal type :opt) (assoc completion table))
  13195. (message "%s" (substitute-command-keys
  13196. "Press \\[org-complete] again to insert example settings"))))
  13197. (t
  13198. (message "Making completion list...")
  13199. (let ((list (sort (all-completions pattern table confirm)
  13200. 'string<)))
  13201. (with-output-to-temp-buffer "*Completions*"
  13202. (condition-case nil
  13203. ;; Protection needed for XEmacs and emacs 21
  13204. (display-completion-list list pattern)
  13205. (error (display-completion-list list)))))
  13206. (message "Making completion list...%s" "done")))))))
  13207. ;;;; TODO, DEADLINE, Comments
  13208. (defun org-toggle-comment ()
  13209. "Change the COMMENT state of an entry."
  13210. (interactive)
  13211. (save-excursion
  13212. (org-back-to-heading)
  13213. (let (case-fold-search)
  13214. (if (looking-at (concat outline-regexp
  13215. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13216. (replace-match "" t t nil 1)
  13217. (if (looking-at outline-regexp)
  13218. (progn
  13219. (goto-char (match-end 0))
  13220. (insert org-comment-string " ")))))))
  13221. (defvar org-last-todo-state-is-todo nil
  13222. "This is non-nil when the last TODO state change led to a TODO state.
  13223. If the last change removed the TODO tag or switched to DONE, then
  13224. this is nil.")
  13225. (defvar org-setting-tags nil) ; dynamically skiped
  13226. ;; FIXME: better place
  13227. (defun org-property-or-variable-value (var &optional inherit)
  13228. "Check if there is a property fixing the value of VAR.
  13229. If yes, return this value. If not, return the current value of the variable."
  13230. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13231. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13232. (read prop)
  13233. (symbol-value var))))
  13234. (defun org-parse-local-options (string var)
  13235. "Parse STRING for startup setting relevant for variable VAR."
  13236. (let ((rtn (symbol-value var))
  13237. e opts)
  13238. (save-match-data
  13239. (if (or (not string) (not (string-match "\\S-" string)))
  13240. rtn
  13241. (setq opts (delq nil (mapcar (lambda (x)
  13242. (setq e (assoc x org-startup-options))
  13243. (if (eq (nth 1 e) var) e nil))
  13244. (org-split-string string "[ \t]+"))))
  13245. (if (not opts)
  13246. rtn
  13247. (setq rtn nil)
  13248. (while (setq e (pop opts))
  13249. (if (not (nth 3 e))
  13250. (setq rtn (nth 2 e))
  13251. (if (not (listp rtn)) (setq rtn nil))
  13252. (push (nth 2 e) rtn)))
  13253. rtn)))))
  13254. (defvar org-blocker-hook nil
  13255. "Hook for functions that are allowed to block a state change.
  13256. Each function gets as its single argument a property list, see
  13257. `org-trigger-hook' for more information about this list.
  13258. If any of the functions in this hook returns nil, the state change
  13259. is blocked.")
  13260. (defvar org-trigger-hook nil
  13261. "Hook for functions that are triggered by a state change.
  13262. Each function gets as its single argument a property list with at least
  13263. the following elements:
  13264. (:type type-of-change :position pos-at-entry-start
  13265. :from old-state :to new-state)
  13266. Depending on the type, more properties may be present.
  13267. This mechanism is currently implemented for:
  13268. TODO state changes
  13269. ------------------
  13270. :type todo-state-change
  13271. :from previous state (keyword as a string), or nil
  13272. :to new state (keyword as a string), or nil")
  13273. (defun org-todo (&optional arg)
  13274. "Change the TODO state of an item.
  13275. The state of an item is given by a keyword at the start of the heading,
  13276. like
  13277. *** TODO Write paper
  13278. *** DONE Call mom
  13279. The different keywords are specified in the variable `org-todo-keywords'.
  13280. By default the available states are \"TODO\" and \"DONE\".
  13281. So for this example: when the item starts with TODO, it is changed to DONE.
  13282. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13283. DONE are present, add TODO at the beginning of the heading.
  13284. With C-u prefix arg, use completion to determine the new state.
  13285. With numeric prefix arg, switch to that state.
  13286. For calling through lisp, arg is also interpreted in the following way:
  13287. 'none -> empty state
  13288. \"\"(empty string) -> switch to empty state
  13289. 'done -> switch to DONE
  13290. 'nextset -> switch to the next set of keywords
  13291. 'previousset -> switch to the previous set of keywords
  13292. \"WAITING\" -> switch to the specified keyword, but only if it
  13293. really is a member of `org-todo-keywords'."
  13294. (interactive "P")
  13295. (save-excursion
  13296. (catch 'exit
  13297. (org-back-to-heading)
  13298. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13299. (or (looking-at (concat " +" org-todo-regexp " *"))
  13300. (looking-at " *"))
  13301. (let* ((match-data (match-data))
  13302. (startpos (point-at-bol))
  13303. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13304. (org-log-done org-log-done)
  13305. (org-log-repeat org-log-repeat)
  13306. (org-todo-log-states org-todo-log-states)
  13307. (this (match-string 1))
  13308. (hl-pos (match-beginning 0))
  13309. (head (org-get-todo-sequence-head this))
  13310. (ass (assoc head org-todo-kwd-alist))
  13311. (interpret (nth 1 ass))
  13312. (done-word (nth 3 ass))
  13313. (final-done-word (nth 4 ass))
  13314. (last-state (or this ""))
  13315. (completion-ignore-case t)
  13316. (member (member this org-todo-keywords-1))
  13317. (tail (cdr member))
  13318. (state (cond
  13319. ((and org-todo-key-trigger
  13320. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13321. (and (not arg) org-use-fast-todo-selection
  13322. (not (eq org-use-fast-todo-selection 'prefix)))))
  13323. ;; Use fast selection
  13324. (org-fast-todo-selection))
  13325. ((and (equal arg '(4))
  13326. (or (not org-use-fast-todo-selection)
  13327. (not org-todo-key-trigger)))
  13328. ;; Read a state with completion
  13329. (completing-read "State: " (mapcar (lambda(x) (list x))
  13330. org-todo-keywords-1)
  13331. nil t))
  13332. ((eq arg 'right)
  13333. (if this
  13334. (if tail (car tail) nil)
  13335. (car org-todo-keywords-1)))
  13336. ((eq arg 'left)
  13337. (if (equal member org-todo-keywords-1)
  13338. nil
  13339. (if this
  13340. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13341. org-todo-keywords-1)
  13342. (org-last org-todo-keywords-1))))
  13343. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13344. (setq arg nil))) ; hack to fall back to cycling
  13345. (arg
  13346. ;; user or caller requests a specific state
  13347. (cond
  13348. ((equal arg "") nil)
  13349. ((eq arg 'none) nil)
  13350. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13351. ((eq arg 'nextset)
  13352. (or (car (cdr (member head org-todo-heads)))
  13353. (car org-todo-heads)))
  13354. ((eq arg 'previousset)
  13355. (let ((org-todo-heads (reverse org-todo-heads)))
  13356. (or (car (cdr (member head org-todo-heads)))
  13357. (car org-todo-heads))))
  13358. ((car (member arg org-todo-keywords-1)))
  13359. ((nth (1- (prefix-numeric-value arg))
  13360. org-todo-keywords-1))))
  13361. ((null member) (or head (car org-todo-keywords-1)))
  13362. ((equal this final-done-word) nil) ;; -> make empty
  13363. ((null tail) nil) ;; -> first entry
  13364. ((eq interpret 'sequence)
  13365. (car tail))
  13366. ((memq interpret '(type priority))
  13367. (if (eq this-command last-command)
  13368. (car tail)
  13369. (if (> (length tail) 0)
  13370. (or done-word (car org-done-keywords))
  13371. nil)))
  13372. (t nil)))
  13373. (next (if state (concat " " state " ") " "))
  13374. (change-plist (list :type 'todo-state-change :from this :to state
  13375. :position startpos))
  13376. dolog now-done-p)
  13377. (when org-blocker-hook
  13378. (unless (save-excursion
  13379. (save-match-data
  13380. (run-hook-with-args-until-failure
  13381. 'org-blocker-hook change-plist)))
  13382. (if (interactive-p)
  13383. (error "TODO state change from %s to %s blocked" this state)
  13384. ;; fail silently
  13385. (message "TODO state change from %s to %s blocked" this state)
  13386. (throw 'exit nil))))
  13387. (store-match-data match-data)
  13388. (replace-match next t t)
  13389. (unless (pos-visible-in-window-p hl-pos)
  13390. (message "TODO state changed to %s" (org-trim next)))
  13391. (unless head
  13392. (setq head (org-get-todo-sequence-head state)
  13393. ass (assoc head org-todo-kwd-alist)
  13394. interpret (nth 1 ass)
  13395. done-word (nth 3 ass)
  13396. final-done-word (nth 4 ass)))
  13397. (when (memq arg '(nextset previousset))
  13398. (message "Keyword-Set %d/%d: %s"
  13399. (- (length org-todo-sets) -1
  13400. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13401. (length org-todo-sets)
  13402. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13403. (setq org-last-todo-state-is-todo
  13404. (not (member state org-done-keywords)))
  13405. (setq now-done-p (and (member state org-done-keywords)
  13406. (not (member this org-done-keywords))))
  13407. (and logging (org-local-logging logging))
  13408. (when (and (or org-todo-log-states org-log-done)
  13409. (not (memq arg '(nextset previousset))))
  13410. ;; we need to look at recording a time and note
  13411. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13412. (nth 2 (assoc this org-todo-log-states))))
  13413. (when (and state
  13414. (member state org-not-done-keywords)
  13415. (not (member this org-not-done-keywords)))
  13416. ;; This is now a todo state and was not one before
  13417. ;; If there was a CLOSED time stamp, get rid of it.
  13418. (org-add-planning-info nil nil 'closed))
  13419. (when (and now-done-p org-log-done)
  13420. ;; It is now done, and it was not done before
  13421. (org-add-planning-info 'closed (org-current-time))
  13422. (if (and (not dolog) (eq 'note org-log-done))
  13423. (org-add-log-maybe 'done state 'findpos 'note)))
  13424. (when (and state dolog)
  13425. ;; This is a non-nil state, and we need to log it
  13426. (org-add-log-maybe 'state state 'findpos dolog)))
  13427. ;; Fixup tag positioning
  13428. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13429. (run-hooks 'org-after-todo-state-change-hook)
  13430. (if (and arg (not (member state org-done-keywords)))
  13431. (setq head (org-get-todo-sequence-head state)))
  13432. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13433. ;; Do we need to trigger a repeat?
  13434. (when now-done-p (org-auto-repeat-maybe state))
  13435. ;; Fixup cursor location if close to the keyword
  13436. (if (and (outline-on-heading-p)
  13437. (not (bolp))
  13438. (save-excursion (beginning-of-line 1)
  13439. (looking-at org-todo-line-regexp))
  13440. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13441. (progn
  13442. (goto-char (or (match-end 2) (match-end 1)))
  13443. (just-one-space)))
  13444. (when org-trigger-hook
  13445. (save-excursion
  13446. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13447. (defun org-local-logging (value)
  13448. "Get logging settings from a property VALUE."
  13449. (let* (words w a)
  13450. ;; directly set the variables, they are already local.
  13451. (setq org-log-done nil
  13452. org-log-repeat nil
  13453. org-todo-log-states nil)
  13454. (setq words (org-split-string value))
  13455. (while (setq w (pop words))
  13456. (cond
  13457. ((setq a (assoc w org-startup-options))
  13458. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13459. (set (nth 1 a) (nth 2 a))))
  13460. ((setq a (org-extract-log-state-settings w))
  13461. (and (member (car a) org-todo-keywords-1)
  13462. (push a org-todo-log-states)))))))
  13463. (defun org-get-todo-sequence-head (kwd)
  13464. "Return the head of the TODO sequence to which KWD belongs.
  13465. If KWD is not set, check if there is a text property remembering the
  13466. right sequence."
  13467. (let (p)
  13468. (cond
  13469. ((not kwd)
  13470. (or (get-text-property (point-at-bol) 'org-todo-head)
  13471. (progn
  13472. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13473. nil (point-at-eol)))
  13474. (get-text-property p 'org-todo-head))))
  13475. ((not (member kwd org-todo-keywords-1))
  13476. (car org-todo-keywords-1))
  13477. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13478. (defun org-fast-todo-selection ()
  13479. "Fast TODO keyword selection with single keys.
  13480. Returns the new TODO keyword, or nil if no state change should occur."
  13481. (let* ((fulltable org-todo-key-alist)
  13482. (done-keywords org-done-keywords) ;; needed for the faces.
  13483. (maxlen (apply 'max (mapcar
  13484. (lambda (x)
  13485. (if (stringp (car x)) (string-width (car x)) 0))
  13486. fulltable)))
  13487. (expert nil)
  13488. (fwidth (+ maxlen 3 1 3))
  13489. (ncol (/ (- (window-width) 4) fwidth))
  13490. tg cnt e c tbl
  13491. groups ingroup)
  13492. (save-window-excursion
  13493. (if expert
  13494. (set-buffer (get-buffer-create " *Org todo*"))
  13495. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13496. (erase-buffer)
  13497. (org-set-local 'org-done-keywords done-keywords)
  13498. (setq tbl fulltable cnt 0)
  13499. (while (setq e (pop tbl))
  13500. (cond
  13501. ((equal e '(:startgroup))
  13502. (push '() groups) (setq ingroup t)
  13503. (when (not (= cnt 0))
  13504. (setq cnt 0)
  13505. (insert "\n"))
  13506. (insert "{ "))
  13507. ((equal e '(:endgroup))
  13508. (setq ingroup nil cnt 0)
  13509. (insert "}\n"))
  13510. (t
  13511. (setq tg (car e) c (cdr e))
  13512. (if ingroup (push tg (car groups)))
  13513. (setq tg (org-add-props tg nil 'face
  13514. (org-get-todo-face tg)))
  13515. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13516. (insert "[" c "] " tg (make-string
  13517. (- fwidth 4 (length tg)) ?\ ))
  13518. (when (= (setq cnt (1+ cnt)) ncol)
  13519. (insert "\n")
  13520. (if ingroup (insert " "))
  13521. (setq cnt 0)))))
  13522. (insert "\n")
  13523. (goto-char (point-min))
  13524. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13525. (fit-window-to-buffer))
  13526. (message "[a-z..]:Set [SPC]:clear")
  13527. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13528. (cond
  13529. ((or (= c ?\C-g)
  13530. (and (= c ?q) (not (rassoc c fulltable))))
  13531. (setq quit-flag t))
  13532. ((= c ?\ ) nil)
  13533. ((setq e (rassoc c fulltable) tg (car e))
  13534. tg)
  13535. (t (setq quit-flag t))))))
  13536. (defun org-get-repeat ()
  13537. "Check if tere is a deadline/schedule with repeater in this entry."
  13538. (save-match-data
  13539. (save-excursion
  13540. (org-back-to-heading t)
  13541. (if (re-search-forward
  13542. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13543. (match-string 1)))))
  13544. (defvar org-last-changed-timestamp)
  13545. (defvar org-log-post-message)
  13546. (defvar org-log-note-purpose)
  13547. (defun org-auto-repeat-maybe (done-word)
  13548. "Check if the current headline contains a repeated deadline/schedule.
  13549. If yes, set TODO state back to what it was and change the base date
  13550. of repeating deadline/scheduled time stamps to new date.
  13551. This function is run automatically after each state change to a DONE state."
  13552. ;; last-state is dynamically scoped into this function
  13553. (let* ((repeat (org-get-repeat))
  13554. (aa (assoc last-state org-todo-kwd-alist))
  13555. (interpret (nth 1 aa))
  13556. (head (nth 2 aa))
  13557. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13558. (msg "Entry repeats: ")
  13559. (org-log-done nil)
  13560. (org-todo-log-states nil)
  13561. re type n what ts)
  13562. (when repeat
  13563. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13564. (org-todo (if (eq interpret 'type) last-state head))
  13565. (when (and org-log-repeat
  13566. (or (not (memq 'org-add-log-note
  13567. (default-value 'post-command-hook)))
  13568. (eq org-log-note-purpose 'done)))
  13569. ;; Make sure a note is taken;
  13570. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13571. 'findpos org-log-repeat))
  13572. (org-back-to-heading t)
  13573. (org-add-planning-info nil nil 'closed)
  13574. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13575. org-deadline-time-regexp "\\)\\|\\("
  13576. org-ts-regexp "\\)"))
  13577. (while (re-search-forward
  13578. re (save-excursion (outline-next-heading) (point)) t)
  13579. (setq type (if (match-end 1) org-scheduled-string
  13580. (if (match-end 3) org-deadline-string "Plain:"))
  13581. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13582. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13583. (setq n (string-to-number (match-string 1 ts))
  13584. what (match-string 2 ts))
  13585. (if (equal what "w") (setq n (* n 7) what "d"))
  13586. (org-timestamp-change n (cdr (assoc what whata)))
  13587. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13588. (setq org-log-post-message msg)
  13589. (message "%s" msg))))
  13590. (defun org-show-todo-tree (arg)
  13591. "Make a compact tree which shows all headlines marked with TODO.
  13592. The tree will show the lines where the regexp matches, and all higher
  13593. headlines above the match.
  13594. With a \\[universal-argument] prefix, also show the DONE entries.
  13595. With a numeric prefix N, construct a sparse tree for the Nth element
  13596. of `org-todo-keywords-1'."
  13597. (interactive "P")
  13598. (let ((case-fold-search nil)
  13599. (kwd-re
  13600. (cond ((null arg) org-not-done-regexp)
  13601. ((equal arg '(4))
  13602. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13603. (mapcar 'list org-todo-keywords-1))))
  13604. (concat "\\("
  13605. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13606. "\\)\\>")))
  13607. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13608. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13609. org-todo-keywords-1)))
  13610. (t (error "Invalid prefix argument: %s" arg)))))
  13611. (message "%d TODO entries found"
  13612. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13613. (defun org-deadline (&optional remove)
  13614. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13615. With argument REMOVE, remove any deadline from the item."
  13616. (interactive "P")
  13617. (if remove
  13618. (progn
  13619. (org-remove-timestamp-with-keyword org-deadline-string)
  13620. (message "Item no longer has a deadline."))
  13621. (org-add-planning-info 'deadline nil 'closed)))
  13622. (defun org-schedule (&optional remove)
  13623. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13624. With argument REMOVE, remove any scheduling date from the item."
  13625. (interactive "P")
  13626. (if remove
  13627. (progn
  13628. (org-remove-timestamp-with-keyword org-scheduled-string)
  13629. (message "Item is no longer scheduled."))
  13630. (org-add-planning-info 'scheduled nil 'closed)))
  13631. (defun org-remove-timestamp-with-keyword (keyword)
  13632. "Remove all time stamps with KEYWORD in the current entry."
  13633. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13634. beg)
  13635. (save-excursion
  13636. (org-back-to-heading t)
  13637. (setq beg (point))
  13638. (org-end-of-subtree t t)
  13639. (while (re-search-backward re beg t)
  13640. (replace-match "")
  13641. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13642. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13643. (defun org-add-planning-info (what &optional time &rest remove)
  13644. "Insert new timestamp with keyword in the line directly after the headline.
  13645. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13646. If non is given, the user is prompted for a date.
  13647. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13648. be removed."
  13649. (interactive)
  13650. (let (org-time-was-given org-end-time-was-given)
  13651. (when what (setq time (or time (org-read-date nil 'to-time))))
  13652. (when (and org-insert-labeled-timestamps-at-point
  13653. (member what '(scheduled deadline)))
  13654. (insert
  13655. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13656. (org-insert-time-stamp time org-time-was-given
  13657. nil nil nil (list org-end-time-was-given))
  13658. (setq what nil))
  13659. (save-excursion
  13660. (save-restriction
  13661. (let (col list elt ts buffer-invisibility-spec)
  13662. (org-back-to-heading t)
  13663. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13664. (goto-char (match-end 1))
  13665. (setq col (current-column))
  13666. (goto-char (match-end 0))
  13667. (if (eobp) (insert "\n") (forward-char 1))
  13668. (if (and (not (looking-at outline-regexp))
  13669. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13670. "[^\r\n]*"))
  13671. (not (equal (match-string 1) org-clock-string)))
  13672. (narrow-to-region (match-beginning 0) (match-end 0))
  13673. (insert-before-markers "\n")
  13674. (backward-char 1)
  13675. (narrow-to-region (point) (point))
  13676. (indent-to-column col))
  13677. ;; Check if we have to remove something.
  13678. (setq list (cons what remove))
  13679. (while list
  13680. (setq elt (pop list))
  13681. (goto-char (point-min))
  13682. (when (or (and (eq elt 'scheduled)
  13683. (re-search-forward org-scheduled-time-regexp nil t))
  13684. (and (eq elt 'deadline)
  13685. (re-search-forward org-deadline-time-regexp nil t))
  13686. (and (eq elt 'closed)
  13687. (re-search-forward org-closed-time-regexp nil t)))
  13688. (replace-match "")
  13689. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13690. (if (looking-at " +") (replace-match ""))))
  13691. (goto-char (point-max))
  13692. (when what
  13693. (insert
  13694. (if (not (equal (char-before) ?\ )) " " "")
  13695. (cond ((eq what 'scheduled) org-scheduled-string)
  13696. ((eq what 'deadline) org-deadline-string)
  13697. ((eq what 'closed) org-closed-string))
  13698. " ")
  13699. (setq ts (org-insert-time-stamp
  13700. time
  13701. (or org-time-was-given
  13702. (and (eq what 'closed) org-log-done-with-time))
  13703. (eq what 'closed)
  13704. nil nil (list org-end-time-was-given)))
  13705. (end-of-line 1))
  13706. (goto-char (point-min))
  13707. (widen)
  13708. (if (looking-at "[ \t]+\r?\n")
  13709. (replace-match ""))
  13710. ts)))))
  13711. (defvar org-log-note-marker (make-marker))
  13712. (defvar org-log-note-purpose nil)
  13713. (defvar org-log-note-state nil)
  13714. (defvar org-log-note-how nil)
  13715. (defvar org-log-note-window-configuration nil)
  13716. (defvar org-log-note-return-to (make-marker))
  13717. (defvar org-log-post-message nil
  13718. "Message to be displayed after a log note has been stored.
  13719. The auto-repeater uses this.")
  13720. (defun org-add-log-maybe (&optional purpose state findpos how)
  13721. "Set up the post command hook to take a note.
  13722. If this is about to TODO state change, the new state is expected in STATE.
  13723. When FINDPOS is non-nil, find the correct position for the note in
  13724. the current entry. If not, assume that it can be inserted at point."
  13725. (save-excursion
  13726. (when findpos
  13727. (org-back-to-heading t)
  13728. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13729. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13730. "[^\r\n]*\\)?"))
  13731. (goto-char (match-end 0))
  13732. (unless org-log-states-order-reversed
  13733. (and (= (char-after) ?\n) (forward-char 1))
  13734. (org-skip-over-state-notes)
  13735. (skip-chars-backward " \t\n\r")))
  13736. (move-marker org-log-note-marker (point))
  13737. (setq org-log-note-purpose purpose
  13738. org-log-note-state state
  13739. org-log-note-how how)
  13740. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13741. (defun org-skip-over-state-notes ()
  13742. "Skip past the list of State notes in an entry."
  13743. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13744. (while (looking-at "[ \t]*- State")
  13745. (condition-case nil
  13746. (org-next-item)
  13747. (error (org-end-of-item)))))
  13748. (defun org-add-log-note (&optional purpose)
  13749. "Pop up a window for taking a note, and add this note later at point."
  13750. (remove-hook 'post-command-hook 'org-add-log-note)
  13751. (setq org-log-note-window-configuration (current-window-configuration))
  13752. (delete-other-windows)
  13753. (move-marker org-log-note-return-to (point))
  13754. (switch-to-buffer (marker-buffer org-log-note-marker))
  13755. (goto-char org-log-note-marker)
  13756. (org-switch-to-buffer-other-window "*Org Note*")
  13757. (erase-buffer)
  13758. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13759. (org-store-log-note)
  13760. (let ((org-inhibit-startup t)) (org-mode))
  13761. (insert (format "# Insert note for %s.
  13762. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13763. (cond
  13764. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13765. ((eq org-log-note-purpose 'done) "closed todo item")
  13766. ((eq org-log-note-purpose 'state)
  13767. (format "state change to \"%s\"" org-log-note-state))
  13768. (t (error "This should not happen")))))
  13769. (org-set-local 'org-finish-function 'org-store-log-note)))
  13770. (defun org-store-log-note ()
  13771. "Finish taking a log note, and insert it to where it belongs."
  13772. (let ((txt (buffer-string))
  13773. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13774. lines ind)
  13775. (kill-buffer (current-buffer))
  13776. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13777. (setq txt (replace-match "" t t txt)))
  13778. (if (string-match "\\s-+\\'" txt)
  13779. (setq txt (replace-match "" t t txt)))
  13780. (setq lines (org-split-string txt "\n"))
  13781. (when (and note (string-match "\\S-" note))
  13782. (setq note
  13783. (org-replace-escapes
  13784. note
  13785. (list (cons "%u" (user-login-name))
  13786. (cons "%U" user-full-name)
  13787. (cons "%t" (format-time-string
  13788. (org-time-stamp-format 'long 'inactive)
  13789. (current-time)))
  13790. (cons "%s" (if org-log-note-state
  13791. (concat "\"" org-log-note-state "\"")
  13792. "")))))
  13793. (if lines (setq note (concat note " \\\\")))
  13794. (push note lines))
  13795. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13796. (when lines
  13797. (save-excursion
  13798. (set-buffer (marker-buffer org-log-note-marker))
  13799. (save-excursion
  13800. (goto-char org-log-note-marker)
  13801. (move-marker org-log-note-marker nil)
  13802. (end-of-line 1)
  13803. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13804. (indent-relative nil)
  13805. (insert "- " (pop lines))
  13806. (org-indent-line-function)
  13807. (beginning-of-line 1)
  13808. (looking-at "[ \t]*")
  13809. (setq ind (concat (match-string 0) " "))
  13810. (end-of-line 1)
  13811. (while lines (insert "\n" ind (pop lines)))))))
  13812. (set-window-configuration org-log-note-window-configuration)
  13813. (with-current-buffer (marker-buffer org-log-note-return-to)
  13814. (goto-char org-log-note-return-to))
  13815. (move-marker org-log-note-return-to nil)
  13816. (and org-log-post-message (message "%s" org-log-post-message)))
  13817. ;; FIXME: what else would be useful?
  13818. ;; - priority
  13819. ;; - date
  13820. (defun org-sparse-tree (&optional arg)
  13821. "Create a sparse tree, prompt for the details.
  13822. This command can create sparse trees. You first need to select the type
  13823. of match used to create the tree:
  13824. t Show entries with a specific TODO keyword.
  13825. T Show entries selected by a tags match.
  13826. p Enter a property name and its value (both with completion on existing
  13827. names/values) and show entries with that property.
  13828. r Show entries matching a regular expression
  13829. d Show deadlines due within `org-deadline-warning-days'."
  13830. (interactive "P")
  13831. (let (ans kwd value)
  13832. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13833. (setq ans (read-char-exclusive))
  13834. (cond
  13835. ((equal ans ?d)
  13836. (call-interactively 'org-check-deadlines))
  13837. ((equal ans ?b)
  13838. (call-interactively 'org-check-before-date))
  13839. ((equal ans ?t)
  13840. (org-show-todo-tree '(4)))
  13841. ((equal ans ?T)
  13842. (call-interactively 'org-tags-sparse-tree))
  13843. ((member ans '(?p ?P))
  13844. (setq kwd (completing-read "Property: "
  13845. (mapcar 'list (org-buffer-property-keys))))
  13846. (setq value (completing-read "Value: "
  13847. (mapcar 'list (org-property-values kwd))))
  13848. (unless (string-match "\\`{.*}\\'" value)
  13849. (setq value (concat "\"" value "\"")))
  13850. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13851. ((member ans '(?r ?R ?/))
  13852. (call-interactively 'org-occur))
  13853. (t (error "No such sparse tree command \"%c\"" ans)))))
  13854. (defvar org-occur-highlights nil)
  13855. (make-variable-buffer-local 'org-occur-highlights)
  13856. (defun org-occur (regexp &optional keep-previous callback)
  13857. "Make a compact tree which shows all matches of REGEXP.
  13858. The tree will show the lines where the regexp matches, and all higher
  13859. headlines above the match. It will also show the heading after the match,
  13860. to make sure editing the matching entry is easy.
  13861. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13862. call to `org-occur' will be kept, to allow stacking of calls to this
  13863. command.
  13864. If CALLBACK is non-nil, it is a function which is called to confirm
  13865. that the match should indeed be shown."
  13866. (interactive "sRegexp: \nP")
  13867. (or keep-previous (org-remove-occur-highlights nil nil t))
  13868. (let ((cnt 0))
  13869. (save-excursion
  13870. (goto-char (point-min))
  13871. (if (or (not keep-previous) ; do not want to keep
  13872. (not org-occur-highlights)) ; no previous matches
  13873. ;; hide everything
  13874. (org-overview))
  13875. (while (re-search-forward regexp nil t)
  13876. (when (or (not callback)
  13877. (save-match-data (funcall callback)))
  13878. (setq cnt (1+ cnt))
  13879. (when org-highlight-sparse-tree-matches
  13880. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13881. (org-show-context 'occur-tree))))
  13882. (when org-remove-highlights-with-change
  13883. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13884. nil 'local))
  13885. (unless org-sparse-tree-open-archived-trees
  13886. (org-hide-archived-subtrees (point-min) (point-max)))
  13887. (run-hooks 'org-occur-hook)
  13888. (if (interactive-p)
  13889. (message "%d match(es) for regexp %s" cnt regexp))
  13890. cnt))
  13891. (defun org-show-context (&optional key)
  13892. "Make sure point and context and visible.
  13893. How much context is shown depends upon the variables
  13894. `org-show-hierarchy-above', `org-show-following-heading'. and
  13895. `org-show-siblings'."
  13896. (let ((heading-p (org-on-heading-p t))
  13897. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13898. (following-p (org-get-alist-option org-show-following-heading key))
  13899. (entry-p (org-get-alist-option org-show-entry-below key))
  13900. (siblings-p (org-get-alist-option org-show-siblings key)))
  13901. (catch 'exit
  13902. ;; Show heading or entry text
  13903. (if (and heading-p (not entry-p))
  13904. (org-flag-heading nil) ; only show the heading
  13905. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13906. (org-show-hidden-entry))) ; show entire entry
  13907. (when following-p
  13908. ;; Show next sibling, or heading below text
  13909. (save-excursion
  13910. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13911. (org-flag-heading nil))))
  13912. (when siblings-p (org-show-siblings))
  13913. (when hierarchy-p
  13914. ;; show all higher headings, possibly with siblings
  13915. (save-excursion
  13916. (while (and (condition-case nil
  13917. (progn (org-up-heading-all 1) t)
  13918. (error nil))
  13919. (not (bobp)))
  13920. (org-flag-heading nil)
  13921. (when siblings-p (org-show-siblings))))))))
  13922. (defun org-reveal (&optional siblings)
  13923. "Show current entry, hierarchy above it, and the following headline.
  13924. This can be used to show a consistent set of context around locations
  13925. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13926. not t for the search context.
  13927. With optional argument SIBLINGS, on each level of the hierarchy all
  13928. siblings are shown. This repairs the tree structure to what it would
  13929. look like when opened with hierarchical calls to `org-cycle'."
  13930. (interactive "P")
  13931. (let ((org-show-hierarchy-above t)
  13932. (org-show-following-heading t)
  13933. (org-show-siblings (if siblings t org-show-siblings)))
  13934. (org-show-context nil)))
  13935. (defun org-highlight-new-match (beg end)
  13936. "Highlight from BEG to END and mark the highlight is an occur headline."
  13937. (let ((ov (org-make-overlay beg end)))
  13938. (org-overlay-put ov 'face 'secondary-selection)
  13939. (push ov org-occur-highlights)))
  13940. (defun org-remove-occur-highlights (&optional beg end noremove)
  13941. "Remove the occur highlights from the buffer.
  13942. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13943. from the `before-change-functions' in the current buffer."
  13944. (interactive)
  13945. (unless org-inhibit-highlight-removal
  13946. (mapc 'org-delete-overlay org-occur-highlights)
  13947. (setq org-occur-highlights nil)
  13948. (unless noremove
  13949. (remove-hook 'before-change-functions
  13950. 'org-remove-occur-highlights 'local))))
  13951. ;;;; Priorities
  13952. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13953. "Regular expression matching the priority indicator.")
  13954. (defvar org-remove-priority-next-time nil)
  13955. (defun org-priority-up ()
  13956. "Increase the priority of the current item."
  13957. (interactive)
  13958. (org-priority 'up))
  13959. (defun org-priority-down ()
  13960. "Decrease the priority of the current item."
  13961. (interactive)
  13962. (org-priority 'down))
  13963. (defun org-priority (&optional action)
  13964. "Change the priority of an item by ARG.
  13965. ACTION can be `set', `up', `down', or a character."
  13966. (interactive)
  13967. (setq action (or action 'set))
  13968. (let (current new news have remove)
  13969. (save-excursion
  13970. (org-back-to-heading)
  13971. (if (looking-at org-priority-regexp)
  13972. (setq current (string-to-char (match-string 2))
  13973. have t)
  13974. (setq current org-default-priority))
  13975. (cond
  13976. ((or (eq action 'set) (integerp action))
  13977. (if (integerp action)
  13978. (setq new action)
  13979. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13980. (setq new (read-char-exclusive)))
  13981. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13982. (= (upcase org-lowest-priority) org-lowest-priority))
  13983. (setq new (upcase new)))
  13984. (cond ((equal new ?\ ) (setq remove t))
  13985. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13986. (error "Priority must be between `%c' and `%c'"
  13987. org-highest-priority org-lowest-priority))))
  13988. ((eq action 'up)
  13989. (if (and (not have) (eq last-command this-command))
  13990. (setq new org-lowest-priority)
  13991. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13992. org-default-priority (1- current)))))
  13993. ((eq action 'down)
  13994. (if (and (not have) (eq last-command this-command))
  13995. (setq new org-highest-priority)
  13996. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13997. org-default-priority (1+ current)))))
  13998. (t (error "Invalid action")))
  13999. (if (or (< (upcase new) org-highest-priority)
  14000. (> (upcase new) org-lowest-priority))
  14001. (setq remove t))
  14002. (setq news (format "%c" new))
  14003. (if have
  14004. (if remove
  14005. (replace-match "" t t nil 1)
  14006. (replace-match news t t nil 2))
  14007. (if remove
  14008. (error "No priority cookie found in line")
  14009. (looking-at org-todo-line-regexp)
  14010. (if (match-end 2)
  14011. (progn
  14012. (goto-char (match-end 2))
  14013. (insert " [#" news "]"))
  14014. (goto-char (match-beginning 3))
  14015. (insert "[#" news "] ")))))
  14016. (org-preserve-lc (org-set-tags nil 'align))
  14017. (if remove
  14018. (message "Priority removed")
  14019. (message "Priority of current item set to %s" news))))
  14020. (defun org-get-priority (s)
  14021. "Find priority cookie and return priority."
  14022. (save-match-data
  14023. (if (not (string-match org-priority-regexp s))
  14024. (* 1000 (- org-lowest-priority org-default-priority))
  14025. (* 1000 (- org-lowest-priority
  14026. (string-to-char (match-string 2 s)))))))
  14027. ;;;; Tags
  14028. (defun org-scan-tags (action matcher &optional todo-only)
  14029. "Scan headline tags with inheritance and produce output ACTION.
  14030. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  14031. evaluated, testing if a given set of tags qualifies a headline for
  14032. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  14033. are included in the output."
  14034. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  14035. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  14036. (org-re
  14037. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  14038. (props (list 'face nil
  14039. 'done-face 'org-done
  14040. 'undone-face nil
  14041. 'mouse-face 'highlight
  14042. 'org-not-done-regexp org-not-done-regexp
  14043. 'org-todo-regexp org-todo-regexp
  14044. 'keymap org-agenda-keymap
  14045. 'help-echo
  14046. (format "mouse-2 or RET jump to org file %s"
  14047. (abbreviate-file-name
  14048. (or (buffer-file-name (buffer-base-buffer))
  14049. (buffer-name (buffer-base-buffer)))))))
  14050. (case-fold-search nil)
  14051. lspos
  14052. tags tags-list tags-alist (llast 0) rtn level category i txt
  14053. todo marker entry priority)
  14054. (save-excursion
  14055. (goto-char (point-min))
  14056. (when (eq action 'sparse-tree)
  14057. (org-overview)
  14058. (org-remove-occur-highlights))
  14059. (while (re-search-forward re nil t)
  14060. (catch :skip
  14061. (setq todo (if (match-end 1) (match-string 2))
  14062. tags (if (match-end 4) (match-string 4)))
  14063. (goto-char (setq lspos (1+ (match-beginning 0))))
  14064. (setq level (org-reduced-level (funcall outline-level))
  14065. category (org-get-category))
  14066. (setq i llast llast level)
  14067. ;; remove tag lists from same and sublevels
  14068. (while (>= i level)
  14069. (when (setq entry (assoc i tags-alist))
  14070. (setq tags-alist (delete entry tags-alist)))
  14071. (setq i (1- i)))
  14072. ;; add the nex tags
  14073. (when tags
  14074. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  14075. tags-alist
  14076. (cons (cons level tags) tags-alist)))
  14077. ;; compile tags for current headline
  14078. (setq tags-list
  14079. (if org-use-tag-inheritance
  14080. (apply 'append (mapcar 'cdr tags-alist))
  14081. tags))
  14082. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14083. (eval matcher)
  14084. (or (not org-agenda-skip-archived-trees)
  14085. (not (member org-archive-tag tags-list))))
  14086. (and (eq action 'agenda) (org-agenda-skip))
  14087. ;; list this headline
  14088. (if (eq action 'sparse-tree)
  14089. (progn
  14090. (and org-highlight-sparse-tree-matches
  14091. (org-get-heading) (match-end 0)
  14092. (org-highlight-new-match
  14093. (match-beginning 0) (match-beginning 1)))
  14094. (org-show-context 'tags-tree))
  14095. (setq txt (org-format-agenda-item
  14096. ""
  14097. (concat
  14098. (if org-tags-match-list-sublevels
  14099. (make-string (1- level) ?.) "")
  14100. (org-get-heading))
  14101. category tags-list)
  14102. priority (org-get-priority txt))
  14103. (goto-char lspos)
  14104. (setq marker (org-agenda-new-marker))
  14105. (org-add-props txt props
  14106. 'org-marker marker 'org-hd-marker marker 'org-category category
  14107. 'priority priority 'type "tagsmatch")
  14108. (push txt rtn))
  14109. ;; if we are to skip sublevels, jump to end of subtree
  14110. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14111. (when (and (eq action 'sparse-tree)
  14112. (not org-sparse-tree-open-archived-trees))
  14113. (org-hide-archived-subtrees (point-min) (point-max)))
  14114. (nreverse rtn)))
  14115. (defvar todo-only) ;; dynamically scoped
  14116. (defun org-tags-sparse-tree (&optional todo-only match)
  14117. "Create a sparse tree according to tags string MATCH.
  14118. MATCH can contain positive and negative selection of tags, like
  14119. \"+WORK+URGENT-WITHBOSS\".
  14120. If optional argument TODO_ONLY is non-nil, only select lines that are
  14121. also TODO lines."
  14122. (interactive "P")
  14123. (org-prepare-agenda-buffers (list (current-buffer)))
  14124. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14125. (defvar org-cached-props nil)
  14126. (defun org-cached-entry-get (pom property)
  14127. (if (or (eq t org-use-property-inheritance)
  14128. (member property org-use-property-inheritance))
  14129. ;; Caching is not possible, check it directly
  14130. (org-entry-get pom property 'inherit)
  14131. ;; Get all properties, so that we can do complicated checks easily
  14132. (cdr (assoc property (or org-cached-props
  14133. (setq org-cached-props
  14134. (org-entry-properties pom)))))))
  14135. (defun org-global-tags-completion-table (&optional files)
  14136. "Return the list of all tags in all agenda buffer/files."
  14137. (save-excursion
  14138. (org-uniquify
  14139. (delq nil
  14140. (apply 'append
  14141. (mapcar
  14142. (lambda (file)
  14143. (set-buffer (find-file-noselect file))
  14144. (append (org-get-buffer-tags)
  14145. (mapcar (lambda (x) (if (stringp (car-safe x))
  14146. (list (car-safe x)) nil))
  14147. org-tag-alist)))
  14148. (if (and files (car files))
  14149. files
  14150. (org-agenda-files))))))))
  14151. (defun org-make-tags-matcher (match)
  14152. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14153. ;; todo-only is scoped dynamically into this function, and the function
  14154. ;; may change it it the matcher asksk for it.
  14155. (unless match
  14156. ;; Get a new match request, with completion
  14157. (let ((org-last-tags-completion-table
  14158. (org-global-tags-completion-table)))
  14159. (setq match (completing-read
  14160. "Match: " 'org-tags-completion-function nil nil nil
  14161. 'org-tags-history))))
  14162. ;; Parse the string and create a lisp form
  14163. (let ((match0 match)
  14164. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14165. minus tag mm
  14166. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14167. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14168. (if (string-match "/+" match)
  14169. ;; match contains also a todo-matching request
  14170. (progn
  14171. (setq tagsmatch (substring match 0 (match-beginning 0))
  14172. todomatch (substring match (match-end 0)))
  14173. (if (string-match "^!" todomatch)
  14174. (setq todo-only t todomatch (substring todomatch 1)))
  14175. (if (string-match "^\\s-*$" todomatch)
  14176. (setq todomatch nil)))
  14177. ;; only matching tags
  14178. (setq tagsmatch match todomatch nil))
  14179. ;; Make the tags matcher
  14180. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14181. (setq tagsmatcher t)
  14182. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14183. (while (setq term (pop orterms))
  14184. (while (and (equal (substring term -1) "\\") orterms)
  14185. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14186. (while (string-match re term)
  14187. (setq minus (and (match-end 1)
  14188. (equal (match-string 1 term) "-"))
  14189. tag (match-string 2 term)
  14190. re-p (equal (string-to-char tag) ?{)
  14191. level-p (match-end 3)
  14192. prop-p (match-end 4)
  14193. mm (cond
  14194. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14195. (level-p `(= level ,(string-to-number
  14196. (match-string 3 term))))
  14197. (prop-p
  14198. (setq pn (match-string 4 term)
  14199. pv (match-string 5 term)
  14200. cat-p (equal pn "CATEGORY")
  14201. re-p (equal (string-to-char pv) ?{)
  14202. pv (substring pv 1 -1))
  14203. (if (equal pn "CATEGORY")
  14204. (setq gv '(get-text-property (point) 'org-category))
  14205. (setq gv `(org-cached-entry-get nil ,pn)))
  14206. (if re-p
  14207. `(string-match ,pv (or ,gv ""))
  14208. `(equal ,pv (or ,gv ""))))
  14209. (t `(member ,(downcase tag) tags-list)))
  14210. mm (if minus (list 'not mm) mm)
  14211. term (substring term (match-end 0)))
  14212. (push mm tagsmatcher))
  14213. (push (if (> (length tagsmatcher) 1)
  14214. (cons 'and tagsmatcher)
  14215. (car tagsmatcher))
  14216. orlist)
  14217. (setq tagsmatcher nil))
  14218. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14219. (setq tagsmatcher
  14220. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14221. ;; Make the todo matcher
  14222. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14223. (setq todomatcher t)
  14224. (setq orterms (org-split-string todomatch "|") orlist nil)
  14225. (while (setq term (pop orterms))
  14226. (while (string-match re term)
  14227. (setq minus (and (match-end 1)
  14228. (equal (match-string 1 term) "-"))
  14229. kwd (match-string 2 term)
  14230. re-p (equal (string-to-char kwd) ?{)
  14231. term (substring term (match-end 0))
  14232. mm (if re-p
  14233. `(string-match ,(substring kwd 1 -1) todo)
  14234. (list 'equal 'todo kwd))
  14235. mm (if minus (list 'not mm) mm))
  14236. (push mm todomatcher))
  14237. (push (if (> (length todomatcher) 1)
  14238. (cons 'and todomatcher)
  14239. (car todomatcher))
  14240. orlist)
  14241. (setq todomatcher nil))
  14242. (setq todomatcher (if (> (length orlist) 1)
  14243. (cons 'or orlist) (car orlist))))
  14244. ;; Return the string and lisp forms of the matcher
  14245. (setq matcher (if todomatcher
  14246. (list 'and tagsmatcher todomatcher)
  14247. tagsmatcher))
  14248. (cons match0 matcher)))
  14249. (defun org-match-any-p (re list)
  14250. "Does re match any element of list?"
  14251. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14252. (delq nil list))
  14253. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14254. (defvar org-tags-overlay (org-make-overlay 1 1))
  14255. (org-detach-overlay org-tags-overlay)
  14256. (defun org-align-tags-here (to-col)
  14257. ;; Assumes that this is a headline
  14258. (let ((pos (point)) (col (current-column)) tags)
  14259. (beginning-of-line 1)
  14260. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14261. (< pos (match-beginning 2)))
  14262. (progn
  14263. (setq tags (match-string 2))
  14264. (goto-char (match-beginning 1))
  14265. (insert " ")
  14266. (delete-region (point) (1+ (match-end 0)))
  14267. (backward-char 1)
  14268. (move-to-column
  14269. (max (1+ (current-column))
  14270. (1+ col)
  14271. (if (> to-col 0)
  14272. to-col
  14273. (- (abs to-col) (length tags))))
  14274. t)
  14275. (insert tags)
  14276. (move-to-column (min (current-column) col) t))
  14277. (goto-char pos))))
  14278. (defun org-set-tags (&optional arg just-align)
  14279. "Set the tags for the current headline.
  14280. With prefix ARG, realign all tags in headings in the current buffer."
  14281. (interactive "P")
  14282. (let* ((re (concat "^" outline-regexp))
  14283. (current (org-get-tags-string))
  14284. (col (current-column))
  14285. (org-setting-tags t)
  14286. table current-tags inherited-tags ; computed below when needed
  14287. tags p0 c0 c1 rpl)
  14288. (if arg
  14289. (save-excursion
  14290. (goto-char (point-min))
  14291. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14292. (while (re-search-forward re nil t)
  14293. (org-set-tags nil t)
  14294. (end-of-line 1)))
  14295. (message "All tags realigned to column %d" org-tags-column))
  14296. (if just-align
  14297. (setq tags current)
  14298. ;; Get a new set of tags from the user
  14299. (save-excursion
  14300. (setq table (or org-tag-alist (org-get-buffer-tags))
  14301. org-last-tags-completion-table table
  14302. current-tags (org-split-string current ":")
  14303. inherited-tags (nreverse
  14304. (nthcdr (length current-tags)
  14305. (nreverse (org-get-tags-at))))
  14306. tags
  14307. (if (or (eq t org-use-fast-tag-selection)
  14308. (and org-use-fast-tag-selection
  14309. (delq nil (mapcar 'cdr table))))
  14310. (org-fast-tag-selection
  14311. current-tags inherited-tags table
  14312. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14313. (let ((org-add-colon-after-tag-completion t))
  14314. (org-trim
  14315. (org-without-partial-completion
  14316. (completing-read "Tags: " 'org-tags-completion-function
  14317. nil nil current 'org-tags-history)))))))
  14318. (while (string-match "[-+&]+" tags)
  14319. ;; No boolean logic, just a list
  14320. (setq tags (replace-match ":" t t tags))))
  14321. (if (string-match "\\`[\t ]*\\'" tags)
  14322. (setq tags "")
  14323. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14324. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14325. ;; Insert new tags at the correct column
  14326. (beginning-of-line 1)
  14327. (cond
  14328. ((and (equal current "") (equal tags "")))
  14329. ((re-search-forward
  14330. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14331. (point-at-eol) t)
  14332. (if (equal tags "")
  14333. (setq rpl "")
  14334. (goto-char (match-beginning 0))
  14335. (setq c0 (current-column) p0 (point)
  14336. c1 (max (1+ c0) (if (> org-tags-column 0)
  14337. org-tags-column
  14338. (- (- org-tags-column) (length tags))))
  14339. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14340. (replace-match rpl t t)
  14341. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14342. tags)
  14343. (t (error "Tags alignment failed")))
  14344. (move-to-column col)
  14345. (unless just-align
  14346. (run-hooks 'org-after-tags-change-hook)))))
  14347. (defun org-change-tag-in-region (beg end tag off)
  14348. "Add or remove TAG for each entry in the region.
  14349. This works in the agenda, and also in an org-mode buffer."
  14350. (interactive
  14351. (list (region-beginning) (region-end)
  14352. (let ((org-last-tags-completion-table
  14353. (if (org-mode-p)
  14354. (org-get-buffer-tags)
  14355. (org-global-tags-completion-table))))
  14356. (completing-read
  14357. "Tag: " 'org-tags-completion-function nil nil nil
  14358. 'org-tags-history))
  14359. (progn
  14360. (message "[s]et or [r]emove? ")
  14361. (equal (read-char-exclusive) ?r))))
  14362. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14363. (let ((agendap (equal major-mode 'org-agenda-mode))
  14364. l1 l2 m buf pos newhead (cnt 0))
  14365. (goto-char end)
  14366. (setq l2 (1- (org-current-line)))
  14367. (goto-char beg)
  14368. (setq l1 (org-current-line))
  14369. (loop for l from l1 to l2 do
  14370. (goto-line l)
  14371. (setq m (get-text-property (point) 'org-hd-marker))
  14372. (when (or (and (org-mode-p) (org-on-heading-p))
  14373. (and agendap m))
  14374. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14375. pos (if agendap m (point)))
  14376. (with-current-buffer buf
  14377. (save-excursion
  14378. (save-restriction
  14379. (goto-char pos)
  14380. (setq cnt (1+ cnt))
  14381. (org-toggle-tag tag (if off 'off 'on))
  14382. (setq newhead (org-get-heading)))))
  14383. (and agendap (org-agenda-change-all-lines newhead m))))
  14384. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14385. (defun org-tags-completion-function (string predicate &optional flag)
  14386. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14387. (confirm (lambda (x) (stringp (car x)))))
  14388. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14389. (setq s1 (match-string 1 string)
  14390. s2 (match-string 2 string))
  14391. (setq s1 "" s2 string))
  14392. (cond
  14393. ((eq flag nil)
  14394. ;; try completion
  14395. (setq rtn (try-completion s2 ctable confirm))
  14396. (if (stringp rtn)
  14397. (setq rtn
  14398. (concat s1 s2 (substring rtn (length s2))
  14399. (if (and org-add-colon-after-tag-completion
  14400. (assoc rtn ctable))
  14401. ":" ""))))
  14402. rtn)
  14403. ((eq flag t)
  14404. ;; all-completions
  14405. (all-completions s2 ctable confirm)
  14406. )
  14407. ((eq flag 'lambda)
  14408. ;; exact match?
  14409. (assoc s2 ctable)))
  14410. ))
  14411. (defun org-fast-tag-insert (kwd tags face &optional end)
  14412. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14413. (insert (format "%-12s" (concat kwd ":"))
  14414. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14415. (or end "")))
  14416. (defun org-fast-tag-show-exit (flag)
  14417. (save-excursion
  14418. (goto-line 3)
  14419. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14420. (replace-match ""))
  14421. (when flag
  14422. (end-of-line 1)
  14423. (move-to-column (- (window-width) 19) t)
  14424. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14425. (defun org-set-current-tags-overlay (current prefix)
  14426. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14427. (if (featurep 'xemacs)
  14428. (org-overlay-display org-tags-overlay (concat prefix s)
  14429. 'secondary-selection)
  14430. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14431. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14432. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14433. "Fast tag selection with single keys.
  14434. CURRENT is the current list of tags in the headline, INHERITED is the
  14435. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14436. possibly with grouping information. TODO-TABLE is a similar table with
  14437. TODO keywords, should these have keys assigned to them.
  14438. If the keys are nil, a-z are automatically assigned.
  14439. Returns the new tags string, or nil to not change the current settings."
  14440. (let* ((fulltable (append table todo-table))
  14441. (maxlen (apply 'max (mapcar
  14442. (lambda (x)
  14443. (if (stringp (car x)) (string-width (car x)) 0))
  14444. fulltable)))
  14445. (buf (current-buffer))
  14446. (expert (eq org-fast-tag-selection-single-key 'expert))
  14447. (buffer-tags nil)
  14448. (fwidth (+ maxlen 3 1 3))
  14449. (ncol (/ (- (window-width) 4) fwidth))
  14450. (i-face 'org-done)
  14451. (c-face 'org-todo)
  14452. tg cnt e c char c1 c2 ntable tbl rtn
  14453. ov-start ov-end ov-prefix
  14454. (exit-after-next org-fast-tag-selection-single-key)
  14455. (done-keywords org-done-keywords)
  14456. groups ingroup)
  14457. (save-excursion
  14458. (beginning-of-line 1)
  14459. (if (looking-at
  14460. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14461. (setq ov-start (match-beginning 1)
  14462. ov-end (match-end 1)
  14463. ov-prefix "")
  14464. (setq ov-start (1- (point-at-eol))
  14465. ov-end (1+ ov-start))
  14466. (skip-chars-forward "^\n\r")
  14467. (setq ov-prefix
  14468. (concat
  14469. (buffer-substring (1- (point)) (point))
  14470. (if (> (current-column) org-tags-column)
  14471. " "
  14472. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14473. (org-move-overlay org-tags-overlay ov-start ov-end)
  14474. (save-window-excursion
  14475. (if expert
  14476. (set-buffer (get-buffer-create " *Org tags*"))
  14477. (delete-other-windows)
  14478. (split-window-vertically)
  14479. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14480. (erase-buffer)
  14481. (org-set-local 'org-done-keywords done-keywords)
  14482. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14483. (org-fast-tag-insert "Current" current c-face "\n\n")
  14484. (org-fast-tag-show-exit exit-after-next)
  14485. (org-set-current-tags-overlay current ov-prefix)
  14486. (setq tbl fulltable char ?a cnt 0)
  14487. (while (setq e (pop tbl))
  14488. (cond
  14489. ((equal e '(:startgroup))
  14490. (push '() groups) (setq ingroup t)
  14491. (when (not (= cnt 0))
  14492. (setq cnt 0)
  14493. (insert "\n"))
  14494. (insert "{ "))
  14495. ((equal e '(:endgroup))
  14496. (setq ingroup nil cnt 0)
  14497. (insert "}\n"))
  14498. (t
  14499. (setq tg (car e) c2 nil)
  14500. (if (cdr e)
  14501. (setq c (cdr e))
  14502. ;; automatically assign a character.
  14503. (setq c1 (string-to-char
  14504. (downcase (substring
  14505. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14506. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14507. (while (or (rassoc char ntable) (rassoc char table))
  14508. (setq char (1+ char)))
  14509. (setq c2 c1))
  14510. (setq c (or c2 char)))
  14511. (if ingroup (push tg (car groups)))
  14512. (setq tg (org-add-props tg nil 'face
  14513. (cond
  14514. ((not (assoc tg table))
  14515. (org-get-todo-face tg))
  14516. ((member tg current) c-face)
  14517. ((member tg inherited) i-face)
  14518. (t nil))))
  14519. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14520. (insert "[" c "] " tg (make-string
  14521. (- fwidth 4 (length tg)) ?\ ))
  14522. (push (cons tg c) ntable)
  14523. (when (= (setq cnt (1+ cnt)) ncol)
  14524. (insert "\n")
  14525. (if ingroup (insert " "))
  14526. (setq cnt 0)))))
  14527. (setq ntable (nreverse ntable))
  14528. (insert "\n")
  14529. (goto-char (point-min))
  14530. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14531. (fit-window-to-buffer))
  14532. (setq rtn
  14533. (catch 'exit
  14534. (while t
  14535. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14536. (if groups " [!] no groups" " [!]groups")
  14537. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14538. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14539. (cond
  14540. ((= c ?\r) (throw 'exit t))
  14541. ((= c ?!)
  14542. (setq groups (not groups))
  14543. (goto-char (point-min))
  14544. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14545. ((= c ?\C-c)
  14546. (if (not expert)
  14547. (org-fast-tag-show-exit
  14548. (setq exit-after-next (not exit-after-next)))
  14549. (setq expert nil)
  14550. (delete-other-windows)
  14551. (split-window-vertically)
  14552. (org-switch-to-buffer-other-window " *Org tags*")
  14553. (and (fboundp 'fit-window-to-buffer)
  14554. (fit-window-to-buffer))))
  14555. ((or (= c ?\C-g)
  14556. (and (= c ?q) (not (rassoc c ntable))))
  14557. (org-detach-overlay org-tags-overlay)
  14558. (setq quit-flag t))
  14559. ((= c ?\ )
  14560. (setq current nil)
  14561. (if exit-after-next (setq exit-after-next 'now)))
  14562. ((= c ?\t)
  14563. (condition-case nil
  14564. (setq tg (completing-read
  14565. "Tag: "
  14566. (or buffer-tags
  14567. (with-current-buffer buf
  14568. (org-get-buffer-tags)))))
  14569. (quit (setq tg "")))
  14570. (when (string-match "\\S-" tg)
  14571. (add-to-list 'buffer-tags (list tg))
  14572. (if (member tg current)
  14573. (setq current (delete tg current))
  14574. (push tg current)))
  14575. (if exit-after-next (setq exit-after-next 'now)))
  14576. ((setq e (rassoc c todo-table) tg (car e))
  14577. (with-current-buffer buf
  14578. (save-excursion (org-todo tg)))
  14579. (if exit-after-next (setq exit-after-next 'now)))
  14580. ((setq e (rassoc c ntable) tg (car e))
  14581. (if (member tg current)
  14582. (setq current (delete tg current))
  14583. (loop for g in groups do
  14584. (if (member tg g)
  14585. (mapc (lambda (x)
  14586. (setq current (delete x current)))
  14587. g)))
  14588. (push tg current))
  14589. (if exit-after-next (setq exit-after-next 'now))))
  14590. ;; Create a sorted list
  14591. (setq current
  14592. (sort current
  14593. (lambda (a b)
  14594. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14595. (if (eq exit-after-next 'now) (throw 'exit t))
  14596. (goto-char (point-min))
  14597. (beginning-of-line 2)
  14598. (delete-region (point) (point-at-eol))
  14599. (org-fast-tag-insert "Current" current c-face)
  14600. (org-set-current-tags-overlay current ov-prefix)
  14601. (while (re-search-forward
  14602. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14603. (setq tg (match-string 1))
  14604. (add-text-properties
  14605. (match-beginning 1) (match-end 1)
  14606. (list 'face
  14607. (cond
  14608. ((member tg current) c-face)
  14609. ((member tg inherited) i-face)
  14610. (t (get-text-property (match-beginning 1) 'face))))))
  14611. (goto-char (point-min)))))
  14612. (org-detach-overlay org-tags-overlay)
  14613. (if rtn
  14614. (mapconcat 'identity current ":")
  14615. nil))))
  14616. (defun org-get-tags-string ()
  14617. "Get the TAGS string in the current headline."
  14618. (unless (org-on-heading-p t)
  14619. (error "Not on a heading"))
  14620. (save-excursion
  14621. (beginning-of-line 1)
  14622. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14623. (org-match-string-no-properties 1)
  14624. "")))
  14625. (defun org-get-tags ()
  14626. "Get the list of tags specified in the current headline."
  14627. (org-split-string (org-get-tags-string) ":"))
  14628. (defun org-get-buffer-tags ()
  14629. "Get a table of all tags used in the buffer, for completion."
  14630. (let (tags)
  14631. (save-excursion
  14632. (goto-char (point-min))
  14633. (while (re-search-forward
  14634. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14635. (when (equal (char-after (point-at-bol 0)) ?*)
  14636. (mapc (lambda (x) (add-to-list 'tags x))
  14637. (org-split-string (org-match-string-no-properties 1) ":")))))
  14638. (mapcar 'list tags)))
  14639. ;;;; Properties
  14640. ;;; Setting and retrieving properties
  14641. (defconst org-special-properties
  14642. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14643. "TIMESTAMP" "TIMESTAMP_IA")
  14644. "The special properties valid in Org-mode.
  14645. These are properties that are not defined in the property drawer,
  14646. but in some other way.")
  14647. (defconst org-default-properties
  14648. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14649. "LOCATION" "LOGGING" "COLUMNS")
  14650. "Some properties that are used by Org-mode for various purposes.
  14651. Being in this list makes sure that they are offered for completion.")
  14652. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14653. "Regular expression matching the first line of a property drawer.")
  14654. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14655. "Regular expression matching the first line of a property drawer.")
  14656. (defun org-property-action ()
  14657. "Do an action on properties."
  14658. (interactive)
  14659. (let (c)
  14660. (org-at-property-p)
  14661. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14662. (setq c (read-char-exclusive))
  14663. (cond
  14664. ((equal c ?s)
  14665. (call-interactively 'org-set-property))
  14666. ((equal c ?d)
  14667. (call-interactively 'org-delete-property))
  14668. ((equal c ?D)
  14669. (call-interactively 'org-delete-property-globally))
  14670. ((equal c ?c)
  14671. (call-interactively 'org-compute-property-at-point))
  14672. (t (error "No such property action %c" c)))))
  14673. (defun org-at-property-p ()
  14674. "Is the cursor in a property line?"
  14675. ;; FIXME: Does not check if we are actually in the drawer.
  14676. ;; FIXME: also returns true on any drawers.....
  14677. ;; This is used by C-c C-c for property action.
  14678. (save-excursion
  14679. (beginning-of-line 1)
  14680. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14681. (defmacro org-with-point-at (pom &rest body)
  14682. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14683. (declare (indent 1) (debug t))
  14684. `(save-excursion
  14685. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14686. (save-excursion
  14687. (goto-char (or pom (point)))
  14688. ,@body)))
  14689. (defun org-get-property-block (&optional beg end force)
  14690. "Return the (beg . end) range of the body of the property drawer.
  14691. BEG and END can be beginning and end of subtree, if not given
  14692. they will be found.
  14693. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14694. (catch 'exit
  14695. (save-excursion
  14696. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14697. (end (or end (progn (outline-next-heading) (point)))))
  14698. (goto-char beg)
  14699. (if (re-search-forward org-property-start-re end t)
  14700. (setq beg (1+ (match-end 0)))
  14701. (if force
  14702. (save-excursion
  14703. (org-insert-property-drawer)
  14704. (setq end (progn (outline-next-heading) (point))))
  14705. (throw 'exit nil))
  14706. (goto-char beg)
  14707. (if (re-search-forward org-property-start-re end t)
  14708. (setq beg (1+ (match-end 0)))))
  14709. (if (re-search-forward org-property-end-re end t)
  14710. (setq end (match-beginning 0))
  14711. (or force (throw 'exit nil))
  14712. (goto-char beg)
  14713. (setq end beg)
  14714. (org-indent-line-function)
  14715. (insert ":END:\n"))
  14716. (cons beg end)))))
  14717. (defun org-entry-properties (&optional pom which)
  14718. "Get all properties of the entry at point-or-marker POM.
  14719. This includes the TODO keyword, the tags, time strings for deadline,
  14720. scheduled, and clocking, and any additional properties defined in the
  14721. entry. The return value is an alist, keys may occur multiple times
  14722. if the property key was used several times.
  14723. POM may also be nil, in which case the current entry is used.
  14724. If WHICH is nil or `all', get all properties. If WHICH is
  14725. `special' or `standard', only get that subclass."
  14726. (setq which (or which 'all))
  14727. (org-with-point-at pom
  14728. (let ((clockstr (substring org-clock-string 0 -1))
  14729. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14730. beg end range props sum-props key value string clocksum)
  14731. (save-excursion
  14732. (when (condition-case nil (org-back-to-heading t) (error nil))
  14733. (setq beg (point))
  14734. (setq sum-props (get-text-property (point) 'org-summaries))
  14735. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14736. (outline-next-heading)
  14737. (setq end (point))
  14738. (when (memq which '(all special))
  14739. ;; Get the special properties, like TODO and tags
  14740. (goto-char beg)
  14741. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14742. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14743. (when (looking-at org-priority-regexp)
  14744. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14745. (when (and (setq value (org-get-tags-string))
  14746. (string-match "\\S-" value))
  14747. (push (cons "TAGS" value) props))
  14748. (when (setq value (org-get-tags-at))
  14749. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14750. props))
  14751. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14752. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14753. string (if (equal key clockstr)
  14754. (org-no-properties
  14755. (org-trim
  14756. (buffer-substring
  14757. (match-beginning 3) (goto-char (point-at-eol)))))
  14758. (substring (org-match-string-no-properties 3) 1 -1)))
  14759. (unless key
  14760. (if (= (char-after (match-beginning 3)) ?\[)
  14761. (setq key "TIMESTAMP_IA")
  14762. (setq key "TIMESTAMP")))
  14763. (when (or (equal key clockstr) (not (assoc key props)))
  14764. (push (cons key string) props)))
  14765. )
  14766. (when (memq which '(all standard))
  14767. ;; Get the standard properties, like :PORP: ...
  14768. (setq range (org-get-property-block beg end))
  14769. (when range
  14770. (goto-char (car range))
  14771. (while (re-search-forward
  14772. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14773. (cdr range) t)
  14774. (setq key (org-match-string-no-properties 1)
  14775. value (org-trim (or (org-match-string-no-properties 2) "")))
  14776. (unless (member key excluded)
  14777. (push (cons key (or value "")) props)))))
  14778. (if clocksum
  14779. (push (cons "CLOCKSUM"
  14780. (org-column-number-to-string (/ (float clocksum) 60.)
  14781. 'add_times))
  14782. props))
  14783. (append sum-props (nreverse props)))))))
  14784. (defun org-entry-get (pom property &optional inherit)
  14785. "Get value of PROPERTY for entry at point-or-marker POM.
  14786. If INHERIT is non-nil and the entry does not have the property,
  14787. then also check higher levels of the hierarchy.
  14788. If the property is present but empty, the return value is the empty string.
  14789. If the property is not present at all, nil is returned."
  14790. (org-with-point-at pom
  14791. (if inherit
  14792. (org-entry-get-with-inheritance property)
  14793. (if (member property org-special-properties)
  14794. ;; We need a special property. Use brute force, get all properties.
  14795. (cdr (assoc property (org-entry-properties nil 'special)))
  14796. (let ((range (org-get-property-block)))
  14797. (if (and range
  14798. (goto-char (car range))
  14799. (re-search-forward
  14800. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14801. (cdr range) t))
  14802. ;; Found the property, return it.
  14803. (if (match-end 1)
  14804. (org-match-string-no-properties 1)
  14805. "")))))))
  14806. (defun org-entry-delete (pom property)
  14807. "Delete the property PROPERTY from entry at point-or-marker POM."
  14808. (org-with-point-at pom
  14809. (if (member property org-special-properties)
  14810. nil ; cannot delete these properties.
  14811. (let ((range (org-get-property-block)))
  14812. (if (and range
  14813. (goto-char (car range))
  14814. (re-search-forward
  14815. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14816. (cdr range) t))
  14817. (progn
  14818. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14819. t)
  14820. nil)))))
  14821. ;; Multi-values properties are properties that contain multiple values
  14822. ;; These values are assumed to be single words, separated by whitespace.
  14823. (defun org-entry-add-to-multivalued-property (pom property value)
  14824. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14825. (let* ((old (org-entry-get pom property))
  14826. (values (and old (org-split-string old "[ \t]"))))
  14827. (unless (member value values)
  14828. (setq values (cons value values))
  14829. (org-entry-put pom property
  14830. (mapconcat 'identity values " ")))))
  14831. (defun org-entry-remove-from-multivalued-property (pom property value)
  14832. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14833. (let* ((old (org-entry-get pom property))
  14834. (values (and old (org-split-string old "[ \t]"))))
  14835. (when (member value values)
  14836. (setq values (delete value values))
  14837. (org-entry-put pom property
  14838. (mapconcat 'identity values " ")))))
  14839. (defun org-entry-member-in-multivalued-property (pom property value)
  14840. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14841. (let* ((old (org-entry-get pom property))
  14842. (values (and old (org-split-string old "[ \t]"))))
  14843. (member value values)))
  14844. (defvar org-entry-property-inherited-from (make-marker))
  14845. (defun org-entry-get-with-inheritance (property)
  14846. "Get entry property, and search higher levels if not present."
  14847. (let (tmp)
  14848. (save-excursion
  14849. (save-restriction
  14850. (widen)
  14851. (catch 'ex
  14852. (while t
  14853. (when (setq tmp (org-entry-get nil property))
  14854. (org-back-to-heading t)
  14855. (move-marker org-entry-property-inherited-from (point))
  14856. (throw 'ex tmp))
  14857. (or (org-up-heading-safe) (throw 'ex nil)))))
  14858. (or tmp (cdr (assoc property org-local-properties))
  14859. (cdr (assoc property org-global-properties))))))
  14860. (defun org-entry-put (pom property value)
  14861. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14862. (org-with-point-at pom
  14863. (org-back-to-heading t)
  14864. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14865. range)
  14866. (cond
  14867. ((equal property "TODO")
  14868. (when (and (stringp value) (string-match "\\S-" value)
  14869. (not (member value org-todo-keywords-1)))
  14870. (error "\"%s\" is not a valid TODO state" value))
  14871. (if (or (not value)
  14872. (not (string-match "\\S-" value)))
  14873. (setq value 'none))
  14874. (org-todo value)
  14875. (org-set-tags nil 'align))
  14876. ((equal property "PRIORITY")
  14877. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14878. (string-to-char value) ?\ ))
  14879. (org-set-tags nil 'align))
  14880. ((equal property "SCHEDULED")
  14881. (if (re-search-forward org-scheduled-time-regexp end t)
  14882. (cond
  14883. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14884. ((eq value 'later) (org-timestamp-change 1 'day))
  14885. (t (call-interactively 'org-schedule)))
  14886. (call-interactively 'org-schedule)))
  14887. ((equal property "DEADLINE")
  14888. (if (re-search-forward org-deadline-time-regexp end t)
  14889. (cond
  14890. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14891. ((eq value 'later) (org-timestamp-change 1 'day))
  14892. (t (call-interactively 'org-deadline)))
  14893. (call-interactively 'org-deadline)))
  14894. ((member property org-special-properties)
  14895. (error "The %s property can not yet be set with `org-entry-put'"
  14896. property))
  14897. (t ; a non-special property
  14898. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14899. (setq range (org-get-property-block beg end 'force))
  14900. (goto-char (car range))
  14901. (if (re-search-forward
  14902. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14903. (progn
  14904. (delete-region (match-beginning 1) (match-end 1))
  14905. (goto-char (match-beginning 1)))
  14906. (goto-char (cdr range))
  14907. (insert "\n")
  14908. (backward-char 1)
  14909. (org-indent-line-function)
  14910. (insert ":" property ":"))
  14911. (and value (insert " " value))
  14912. (org-indent-line-function)))))))
  14913. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14914. "Get all property keys in the current buffer.
  14915. With INCLUDE-SPECIALS, also list the special properties that relect things
  14916. like tags and TODO state.
  14917. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14918. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14919. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14920. formats in the current buffer."
  14921. (let (rtn range cfmt cols s p)
  14922. (save-excursion
  14923. (save-restriction
  14924. (widen)
  14925. (goto-char (point-min))
  14926. (while (re-search-forward org-property-start-re nil t)
  14927. (setq range (org-get-property-block))
  14928. (goto-char (car range))
  14929. (while (re-search-forward
  14930. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14931. (cdr range) t)
  14932. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14933. (outline-next-heading))))
  14934. (when include-specials
  14935. (setq rtn (append org-special-properties rtn)))
  14936. (when include-defaults
  14937. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14938. (when include-columns
  14939. (save-excursion
  14940. (save-restriction
  14941. (widen)
  14942. (goto-char (point-min))
  14943. (while (re-search-forward
  14944. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14945. nil t)
  14946. (setq cfmt (match-string 2) s 0)
  14947. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14948. cfmt s)
  14949. (setq s (match-end 0)
  14950. p (match-string 1 cfmt))
  14951. (unless (or (equal p "ITEM")
  14952. (member p org-special-properties))
  14953. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14954. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14955. (defun org-property-values (key)
  14956. "Return a list of all values of property KEY."
  14957. (save-excursion
  14958. (save-restriction
  14959. (widen)
  14960. (goto-char (point-min))
  14961. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14962. values)
  14963. (while (re-search-forward re nil t)
  14964. (add-to-list 'values (org-trim (match-string 1))))
  14965. (delete "" values)))))
  14966. (defun org-insert-property-drawer ()
  14967. "Insert a property drawer into the current entry."
  14968. (interactive)
  14969. (org-back-to-heading t)
  14970. (looking-at outline-regexp)
  14971. (let ((indent (- (match-end 0)(match-beginning 0)))
  14972. (beg (point))
  14973. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14974. end hiddenp)
  14975. (outline-next-heading)
  14976. (setq end (point))
  14977. (goto-char beg)
  14978. (while (re-search-forward re end t))
  14979. (setq hiddenp (org-invisible-p))
  14980. (end-of-line 1)
  14981. (and (equal (char-after) ?\n) (forward-char 1))
  14982. (org-skip-over-state-notes)
  14983. (skip-chars-backward " \t\n\r")
  14984. (if (eq (char-before) ?*) (forward-char 1))
  14985. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14986. (beginning-of-line 0)
  14987. (indent-to-column indent)
  14988. (beginning-of-line 2)
  14989. (indent-to-column indent)
  14990. (beginning-of-line 0)
  14991. (if hiddenp
  14992. (save-excursion
  14993. (org-back-to-heading t)
  14994. (hide-entry))
  14995. (org-flag-drawer t))))
  14996. (defun org-set-property (property value)
  14997. "In the current entry, set PROPERTY to VALUE.
  14998. When called interactively, this will prompt for a property name, offering
  14999. completion on existing and default properties. And then it will prompt
  15000. for a value, offering competion either on allowed values (via an inherited
  15001. xxx_ALL property) or on existing values in other instances of this property
  15002. in the current file."
  15003. (interactive
  15004. (let* ((prop (completing-read
  15005. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  15006. (cur (org-entry-get nil prop))
  15007. (allowed (org-property-get-allowed-values nil prop 'table))
  15008. (existing (mapcar 'list (org-property-values prop)))
  15009. (val (if allowed
  15010. (completing-read "Value: " allowed nil 'req-match)
  15011. (completing-read
  15012. (concat "Value" (if (and cur (string-match "\\S-" cur))
  15013. (concat "[" cur "]") "")
  15014. ": ")
  15015. existing nil nil "" nil cur))))
  15016. (list prop (if (equal val "") cur val))))
  15017. (unless (equal (org-entry-get nil property) value)
  15018. (org-entry-put nil property value)))
  15019. (defun org-delete-property (property)
  15020. "In the current entry, delete PROPERTY."
  15021. (interactive
  15022. (let* ((prop (completing-read
  15023. "Property: " (org-entry-properties nil 'standard))))
  15024. (list prop)))
  15025. (message "Property %s %s" property
  15026. (if (org-entry-delete nil property)
  15027. "deleted"
  15028. "was not present in the entry")))
  15029. (defun org-delete-property-globally (property)
  15030. "Remove PROPERTY globally, from all entries."
  15031. (interactive
  15032. (let* ((prop (completing-read
  15033. "Globally remove property: "
  15034. (mapcar 'list (org-buffer-property-keys)))))
  15035. (list prop)))
  15036. (save-excursion
  15037. (save-restriction
  15038. (widen)
  15039. (goto-char (point-min))
  15040. (let ((cnt 0))
  15041. (while (re-search-forward
  15042. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  15043. nil t)
  15044. (setq cnt (1+ cnt))
  15045. (replace-match ""))
  15046. (message "Property \"%s\" removed from %d entries" property cnt)))))
  15047. (defvar org-columns-current-fmt-compiled) ; defined below
  15048. (defun org-compute-property-at-point ()
  15049. "Compute the property at point.
  15050. This looks for an enclosing column format, extracts the operator and
  15051. then applies it to the proerty in the column format's scope."
  15052. (interactive)
  15053. (unless (org-at-property-p)
  15054. (error "Not at a property"))
  15055. (let ((prop (org-match-string-no-properties 2)))
  15056. (org-columns-get-format-and-top-level)
  15057. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15058. (error "No operator defined for property %s" prop))
  15059. (org-columns-compute prop)))
  15060. (defun org-property-get-allowed-values (pom property &optional table)
  15061. "Get allowed values for the property PROPERTY.
  15062. When TABLE is non-nil, return an alist that can directly be used for
  15063. completion."
  15064. (let (vals)
  15065. (cond
  15066. ((equal property "TODO")
  15067. (setq vals (org-with-point-at pom
  15068. (append org-todo-keywords-1 '("")))))
  15069. ((equal property "PRIORITY")
  15070. (let ((n org-lowest-priority))
  15071. (while (>= n org-highest-priority)
  15072. (push (char-to-string n) vals)
  15073. (setq n (1- n)))))
  15074. ((member property org-special-properties))
  15075. (t
  15076. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15077. (when (and vals (string-match "\\S-" vals))
  15078. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15079. (setq vals (mapcar (lambda (x)
  15080. (cond ((stringp x) x)
  15081. ((numberp x) (number-to-string x))
  15082. ((symbolp x) (symbol-name x))
  15083. (t "???")))
  15084. vals)))))
  15085. (if table (mapcar 'list vals) vals)))
  15086. (defun org-property-previous-allowed-value (&optional previous)
  15087. "Switch to the next allowed value for this property."
  15088. (interactive)
  15089. (org-property-next-allowed-value t))
  15090. (defun org-property-next-allowed-value (&optional previous)
  15091. "Switch to the next allowed value for this property."
  15092. (interactive)
  15093. (unless (org-at-property-p)
  15094. (error "Not at a property"))
  15095. (let* ((key (match-string 2))
  15096. (value (match-string 3))
  15097. (allowed (or (org-property-get-allowed-values (point) key)
  15098. (and (member value '("[ ]" "[-]" "[X]"))
  15099. '("[ ]" "[X]"))))
  15100. nval)
  15101. (unless allowed
  15102. (error "Allowed values for this property have not been defined"))
  15103. (if previous (setq allowed (reverse allowed)))
  15104. (if (member value allowed)
  15105. (setq nval (car (cdr (member value allowed)))))
  15106. (setq nval (or nval (car allowed)))
  15107. (if (equal nval value)
  15108. (error "Only one allowed value for this property"))
  15109. (org-at-property-p)
  15110. (replace-match (concat " :" key ": " nval) t t)
  15111. (org-indent-line-function)
  15112. (beginning-of-line 1)
  15113. (skip-chars-forward " \t")))
  15114. (defun org-find-entry-with-id (ident)
  15115. "Locate the entry that contains the ID property with exact value IDENT.
  15116. IDENT can be a string, a symbol or a number, this function will search for
  15117. the string representation of it.
  15118. Return the position where this entry starts, or nil if there is no such entry."
  15119. (let ((id (cond
  15120. ((stringp ident) ident)
  15121. ((symbol-name ident) (symbol-name ident))
  15122. ((numberp ident) (number-to-string ident))
  15123. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15124. (case-fold-search nil))
  15125. (save-excursion
  15126. (save-restriction
  15127. (widen)
  15128. (goto-char (point-min))
  15129. (when (re-search-forward
  15130. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15131. nil t)
  15132. (org-back-to-heading)
  15133. (point))))))
  15134. ;;; Column View
  15135. (defvar org-columns-overlays nil
  15136. "Holds the list of current column overlays.")
  15137. (defvar org-columns-current-fmt nil
  15138. "Local variable, holds the currently active column format.")
  15139. (defvar org-columns-current-fmt-compiled nil
  15140. "Local variable, holds the currently active column format.
  15141. This is the compiled version of the format.")
  15142. (defvar org-columns-current-widths nil
  15143. "Loval variable, holds the currently widths of fields.")
  15144. (defvar org-columns-current-maxwidths nil
  15145. "Loval variable, holds the currently active maximum column widths.")
  15146. (defvar org-columns-begin-marker (make-marker)
  15147. "Points to the position where last a column creation command was called.")
  15148. (defvar org-columns-top-level-marker (make-marker)
  15149. "Points to the position where current columns region starts.")
  15150. (defvar org-columns-map (make-sparse-keymap)
  15151. "The keymap valid in column display.")
  15152. (defun org-columns-content ()
  15153. "Switch to contents view while in columns view."
  15154. (interactive)
  15155. (org-overview)
  15156. (org-content))
  15157. (org-defkey org-columns-map "c" 'org-columns-content)
  15158. (org-defkey org-columns-map "o" 'org-overview)
  15159. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15160. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15161. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15162. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15163. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15164. (org-defkey org-columns-map "q" 'org-columns-quit)
  15165. (org-defkey org-columns-map "r" 'org-columns-redo)
  15166. (org-defkey org-columns-map "g" 'org-columns-redo)
  15167. (org-defkey org-columns-map [left] 'backward-char)
  15168. (org-defkey org-columns-map "\M-b" 'backward-char)
  15169. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15170. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15171. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15172. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15173. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15174. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15175. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15176. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15177. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15178. (org-defkey org-columns-map ">" 'org-columns-widen)
  15179. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15180. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15181. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15182. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15183. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15184. '("Column"
  15185. ["Edit property" org-columns-edit-value t]
  15186. ["Next allowed value" org-columns-next-allowed-value t]
  15187. ["Previous allowed value" org-columns-previous-allowed-value t]
  15188. ["Show full value" org-columns-show-value t]
  15189. ["Edit allowed values" org-columns-edit-allowed t]
  15190. "--"
  15191. ["Edit column attributes" org-columns-edit-attributes t]
  15192. ["Increase column width" org-columns-widen t]
  15193. ["Decrease column width" org-columns-narrow t]
  15194. "--"
  15195. ["Move column right" org-columns-move-right t]
  15196. ["Move column left" org-columns-move-left t]
  15197. ["Add column" org-columns-new t]
  15198. ["Delete column" org-columns-delete t]
  15199. "--"
  15200. ["CONTENTS" org-columns-content t]
  15201. ["OVERVIEW" org-overview t]
  15202. ["Refresh columns display" org-columns-redo t]
  15203. "--"
  15204. ["Open link" org-columns-open-link t]
  15205. "--"
  15206. ["Quit" org-columns-quit t]))
  15207. (defun org-columns-new-overlay (beg end &optional string face)
  15208. "Create a new column overlay and add it to the list."
  15209. (let ((ov (org-make-overlay beg end)))
  15210. (org-overlay-put ov 'face (or face 'secondary-selection))
  15211. (org-overlay-display ov string face)
  15212. (push ov org-columns-overlays)
  15213. ov))
  15214. (defun org-columns-display-here (&optional props)
  15215. "Overlay the current line with column display."
  15216. (interactive)
  15217. (let* ((fmt org-columns-current-fmt-compiled)
  15218. (beg (point-at-bol))
  15219. (level-face (save-excursion
  15220. (beginning-of-line 1)
  15221. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15222. (org-get-level-face 2))))
  15223. (color (list :foreground
  15224. (face-attribute (or level-face 'default) :foreground)))
  15225. props pom property ass width f string ov column val modval)
  15226. ;; Check if the entry is in another buffer.
  15227. (unless props
  15228. (if (eq major-mode 'org-agenda-mode)
  15229. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15230. (get-text-property (point) 'org-marker))
  15231. props (if pom (org-entry-properties pom) nil))
  15232. (setq props (org-entry-properties nil))))
  15233. ;; Walk the format
  15234. (while (setq column (pop fmt))
  15235. (setq property (car column)
  15236. ass (if (equal property "ITEM")
  15237. (cons "ITEM"
  15238. (save-match-data
  15239. (org-no-properties
  15240. (org-remove-tabs
  15241. (buffer-substring-no-properties
  15242. (point-at-bol) (point-at-eol))))))
  15243. (assoc property props))
  15244. width (or (cdr (assoc property org-columns-current-maxwidths))
  15245. (nth 2 column)
  15246. (length property))
  15247. f (format "%%-%d.%ds | " width width)
  15248. val (or (cdr ass) "")
  15249. modval (if (equal property "ITEM")
  15250. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15251. string (format f (or modval val)))
  15252. ;; Create the overlay
  15253. (org-unmodified
  15254. (setq ov (org-columns-new-overlay
  15255. beg (setq beg (1+ beg)) string
  15256. (list color 'org-column)))
  15257. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15258. (org-overlay-put ov 'keymap org-columns-map)
  15259. (org-overlay-put ov 'org-columns-key property)
  15260. (org-overlay-put ov 'org-columns-value (cdr ass))
  15261. (org-overlay-put ov 'org-columns-value-modified modval)
  15262. (org-overlay-put ov 'org-columns-pom pom)
  15263. (org-overlay-put ov 'org-columns-format f))
  15264. (if (or (not (char-after beg))
  15265. (equal (char-after beg) ?\n))
  15266. (let ((inhibit-read-only t))
  15267. (save-excursion
  15268. (goto-char beg)
  15269. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15270. ;; Make the rest of the line disappear.
  15271. (org-unmodified
  15272. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15273. (org-overlay-put ov 'invisible t)
  15274. (org-overlay-put ov 'keymap org-columns-map)
  15275. (org-overlay-put ov 'intangible t)
  15276. (push ov org-columns-overlays)
  15277. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15278. (org-overlay-put ov 'keymap org-columns-map)
  15279. (push ov org-columns-overlays)
  15280. (let ((inhibit-read-only t))
  15281. (put-text-property (max (point-min) (1- (point-at-bol)))
  15282. (min (point-max) (1+ (point-at-eol)))
  15283. 'read-only "Type `e' to edit property")))))
  15284. (defvar org-previous-header-line-format nil
  15285. "The header line format before column view was turned on.")
  15286. (defvar org-columns-inhibit-recalculation nil
  15287. "Inhibit recomputing of columns on column view startup.")
  15288. (defvar header-line-format)
  15289. (defun org-columns-display-here-title ()
  15290. "Overlay the newline before the current line with the table title."
  15291. (interactive)
  15292. (let ((fmt org-columns-current-fmt-compiled)
  15293. string (title "")
  15294. property width f column str widths)
  15295. (while (setq column (pop fmt))
  15296. (setq property (car column)
  15297. str (or (nth 1 column) property)
  15298. width (or (cdr (assoc property org-columns-current-maxwidths))
  15299. (nth 2 column)
  15300. (length str))
  15301. widths (push width widths)
  15302. f (format "%%-%d.%ds | " width width)
  15303. string (format f str)
  15304. title (concat title string)))
  15305. (setq title (concat
  15306. (org-add-props " " nil 'display '(space :align-to 0))
  15307. (org-add-props title nil 'face '(:weight bold :underline t))))
  15308. (org-set-local 'org-previous-header-line-format header-line-format)
  15309. (org-set-local 'org-columns-current-widths (nreverse widths))
  15310. (setq header-line-format title)))
  15311. (defun org-columns-remove-overlays ()
  15312. "Remove all currently active column overlays."
  15313. (interactive)
  15314. (when (marker-buffer org-columns-begin-marker)
  15315. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15316. (when (local-variable-p 'org-previous-header-line-format)
  15317. (setq header-line-format org-previous-header-line-format)
  15318. (kill-local-variable 'org-previous-header-line-format))
  15319. (move-marker org-columns-begin-marker nil)
  15320. (move-marker org-columns-top-level-marker nil)
  15321. (org-unmodified
  15322. (mapc 'org-delete-overlay org-columns-overlays)
  15323. (setq org-columns-overlays nil)
  15324. (let ((inhibit-read-only t))
  15325. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15326. (defun org-columns-cleanup-item (item fmt)
  15327. "Remove from ITEM what is a column in the format FMT."
  15328. (if (not org-complex-heading-regexp)
  15329. item
  15330. (when (string-match org-complex-heading-regexp item)
  15331. (concat
  15332. (org-add-props (concat (match-string 1 item) " ") nil
  15333. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15334. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15335. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15336. " " (match-string 4 item)
  15337. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15338. (defun org-columns-show-value ()
  15339. "Show the full value of the property."
  15340. (interactive)
  15341. (let ((value (get-char-property (point) 'org-columns-value)))
  15342. (message "Value is: %s" (or value ""))))
  15343. (defun org-columns-quit ()
  15344. "Remove the column overlays and in this way exit column editing."
  15345. (interactive)
  15346. (org-unmodified
  15347. (org-columns-remove-overlays)
  15348. (let ((inhibit-read-only t))
  15349. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15350. (when (eq major-mode 'org-agenda-mode)
  15351. (message
  15352. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15353. (defun org-columns-check-computed ()
  15354. "Check if this column value is computed.
  15355. If yes, throw an error indicating that changing it does not make sense."
  15356. (let ((val (get-char-property (point) 'org-columns-value)))
  15357. (when (and (stringp val)
  15358. (get-char-property 0 'org-computed val))
  15359. (error "This value is computed from the entry's children"))))
  15360. (defun org-columns-todo (&optional arg)
  15361. "Change the TODO state during column view."
  15362. (interactive "P")
  15363. (org-columns-edit-value "TODO"))
  15364. (defun org-columns-set-tags-or-toggle (&optional arg)
  15365. "Toggle checkbox at point, or set tags for current headline."
  15366. (interactive "P")
  15367. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15368. (get-char-property (point) 'org-columns-value))
  15369. (org-columns-next-allowed-value)
  15370. (org-columns-edit-value "TAGS")))
  15371. (defun org-columns-edit-value (&optional key)
  15372. "Edit the value of the property at point in column view.
  15373. Where possible, use the standard interface for changing this line."
  15374. (interactive)
  15375. (org-columns-check-computed)
  15376. (let* ((external-key key)
  15377. (col (current-column))
  15378. (key (or key (get-char-property (point) 'org-columns-key)))
  15379. (value (get-char-property (point) 'org-columns-value))
  15380. (bol (point-at-bol)) (eol (point-at-eol))
  15381. (pom (or (get-text-property bol 'org-hd-marker)
  15382. (point))) ; keep despite of compiler waring
  15383. (line-overlays
  15384. (delq nil (mapcar (lambda (x)
  15385. (and (eq (overlay-buffer x) (current-buffer))
  15386. (>= (overlay-start x) bol)
  15387. (<= (overlay-start x) eol)
  15388. x))
  15389. org-columns-overlays)))
  15390. nval eval allowed)
  15391. (cond
  15392. ((equal key "CLOCKSUM")
  15393. (error "This special column cannot be edited"))
  15394. ((equal key "ITEM")
  15395. (setq eval '(org-with-point-at pom
  15396. (org-edit-headline))))
  15397. ((equal key "TODO")
  15398. (setq eval '(org-with-point-at pom
  15399. (let ((current-prefix-arg
  15400. (if external-key current-prefix-arg '(4))))
  15401. (call-interactively 'org-todo)))))
  15402. ((equal key "PRIORITY")
  15403. (setq eval '(org-with-point-at pom
  15404. (call-interactively 'org-priority))))
  15405. ((equal key "TAGS")
  15406. (setq eval '(org-with-point-at pom
  15407. (let ((org-fast-tag-selection-single-key
  15408. (if (eq org-fast-tag-selection-single-key 'expert)
  15409. t org-fast-tag-selection-single-key)))
  15410. (call-interactively 'org-set-tags)))))
  15411. ((equal key "DEADLINE")
  15412. (setq eval '(org-with-point-at pom
  15413. (call-interactively 'org-deadline))))
  15414. ((equal key "SCHEDULED")
  15415. (setq eval '(org-with-point-at pom
  15416. (call-interactively 'org-schedule))))
  15417. (t
  15418. (setq allowed (org-property-get-allowed-values pom key 'table))
  15419. (if allowed
  15420. (setq nval (completing-read "Value: " allowed nil t))
  15421. (setq nval (read-string "Edit: " value)))
  15422. (setq nval (org-trim nval))
  15423. (when (not (equal nval value))
  15424. (setq eval '(org-entry-put pom key nval)))))
  15425. (when eval
  15426. (let ((inhibit-read-only t))
  15427. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15428. (unwind-protect
  15429. (progn
  15430. (setq org-columns-overlays
  15431. (org-delete-all line-overlays org-columns-overlays))
  15432. (mapc 'org-delete-overlay line-overlays)
  15433. (org-columns-eval eval))
  15434. (org-columns-display-here))))
  15435. (move-to-column col)
  15436. (if (and (org-mode-p)
  15437. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15438. (org-columns-update key))))
  15439. (defun org-edit-headline () ; FIXME: this is not columns specific
  15440. "Edit the current headline, the part without TODO keyword, TAGS."
  15441. (org-back-to-heading)
  15442. (when (looking-at org-todo-line-regexp)
  15443. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15444. (txt (match-string 3))
  15445. (post "")
  15446. txt2)
  15447. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15448. (setq post (match-string 0 txt)
  15449. txt (substring txt 0 (match-beginning 0))))
  15450. (setq txt2 (read-string "Edit: " txt))
  15451. (when (not (equal txt txt2))
  15452. (beginning-of-line 1)
  15453. (insert pre txt2 post)
  15454. (delete-region (point) (point-at-eol))
  15455. (org-set-tags nil t)))))
  15456. (defun org-columns-edit-allowed ()
  15457. "Edit the list of allowed values for the current property."
  15458. (interactive)
  15459. (let* ((key (get-char-property (point) 'org-columns-key))
  15460. (key1 (concat key "_ALL"))
  15461. (allowed (org-entry-get (point) key1 t))
  15462. nval)
  15463. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15464. (setq nval (read-string "Allowed: " allowed))
  15465. (org-entry-put
  15466. (cond ((marker-position org-entry-property-inherited-from)
  15467. org-entry-property-inherited-from)
  15468. ((marker-position org-columns-top-level-marker)
  15469. org-columns-top-level-marker))
  15470. key1 nval)))
  15471. (defmacro org-no-warnings (&rest body)
  15472. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15473. (defun org-columns-eval (form)
  15474. (let (hidep)
  15475. (save-excursion
  15476. (beginning-of-line 1)
  15477. ;; `next-line' is needed here, because it skips invisible line.
  15478. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15479. (setq hidep (org-on-heading-p 1)))
  15480. (eval form)
  15481. (and hidep (hide-entry))))
  15482. (defun org-columns-previous-allowed-value ()
  15483. "Switch to the previous allowed value for this column."
  15484. (interactive)
  15485. (org-columns-next-allowed-value t))
  15486. (defun org-columns-next-allowed-value (&optional previous)
  15487. "Switch to the next allowed value for this column."
  15488. (interactive)
  15489. (org-columns-check-computed)
  15490. (let* ((col (current-column))
  15491. (key (get-char-property (point) 'org-columns-key))
  15492. (value (get-char-property (point) 'org-columns-value))
  15493. (bol (point-at-bol)) (eol (point-at-eol))
  15494. (pom (or (get-text-property bol 'org-hd-marker)
  15495. (point))) ; keep despite of compiler waring
  15496. (line-overlays
  15497. (delq nil (mapcar (lambda (x)
  15498. (and (eq (overlay-buffer x) (current-buffer))
  15499. (>= (overlay-start x) bol)
  15500. (<= (overlay-start x) eol)
  15501. x))
  15502. org-columns-overlays)))
  15503. (allowed (or (org-property-get-allowed-values pom key)
  15504. (and (memq
  15505. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15506. '(checkbox checkbox-n-of-m checkbox-percent))
  15507. '("[ ]" "[X]"))))
  15508. nval)
  15509. (when (equal key "ITEM")
  15510. (error "Cannot edit item headline from here"))
  15511. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15512. (error "Allowed values for this property have not been defined"))
  15513. (if (member key '("SCHEDULED" "DEADLINE"))
  15514. (setq nval (if previous 'earlier 'later))
  15515. (if previous (setq allowed (reverse allowed)))
  15516. (if (member value allowed)
  15517. (setq nval (car (cdr (member value allowed)))))
  15518. (setq nval (or nval (car allowed)))
  15519. (if (equal nval value)
  15520. (error "Only one allowed value for this property")))
  15521. (let ((inhibit-read-only t))
  15522. (remove-text-properties (1- bol) eol '(read-only t))
  15523. (unwind-protect
  15524. (progn
  15525. (setq org-columns-overlays
  15526. (org-delete-all line-overlays org-columns-overlays))
  15527. (mapc 'org-delete-overlay line-overlays)
  15528. (org-columns-eval '(org-entry-put pom key nval)))
  15529. (org-columns-display-here)))
  15530. (move-to-column col)
  15531. (if (and (org-mode-p)
  15532. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15533. (org-columns-update key))))
  15534. (defun org-verify-version (task)
  15535. (cond
  15536. ((eq task 'columns)
  15537. (if (or (featurep 'xemacs)
  15538. (< emacs-major-version 22))
  15539. (error "Emacs 22 is required for the columns feature")))))
  15540. (defun org-columns-open-link (&optional arg)
  15541. (interactive "P")
  15542. (let ((value (get-char-property (point) 'org-columns-value)))
  15543. (org-open-link-from-string value arg)))
  15544. (defun org-open-link-from-string (s &optional arg)
  15545. "Open a link in the string S, as if it was in Org-mode."
  15546. (interactive)
  15547. (with-temp-buffer
  15548. (let ((org-inhibit-startup t))
  15549. (org-mode)
  15550. (insert s)
  15551. (goto-char (point-min))
  15552. (org-open-at-point arg))))
  15553. (defun org-columns-get-format-and-top-level ()
  15554. (let (fmt)
  15555. (when (condition-case nil (org-back-to-heading) (error nil))
  15556. (move-marker org-entry-property-inherited-from nil)
  15557. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15558. (setq fmt (or fmt org-columns-default-format))
  15559. (org-set-local 'org-columns-current-fmt fmt)
  15560. (org-columns-compile-format fmt)
  15561. (if (marker-position org-entry-property-inherited-from)
  15562. (move-marker org-columns-top-level-marker
  15563. org-entry-property-inherited-from)
  15564. (move-marker org-columns-top-level-marker (point)))
  15565. fmt))
  15566. (defun org-columns ()
  15567. "Turn on column view on an org-mode file."
  15568. (interactive)
  15569. (org-verify-version 'columns)
  15570. (org-columns-remove-overlays)
  15571. (move-marker org-columns-begin-marker (point))
  15572. (let (beg end fmt cache maxwidths)
  15573. (setq fmt (org-columns-get-format-and-top-level))
  15574. (save-excursion
  15575. (goto-char org-columns-top-level-marker)
  15576. (setq beg (point))
  15577. (unless org-columns-inhibit-recalculation
  15578. (org-columns-compute-all))
  15579. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15580. (point-max)))
  15581. ;; Get and cache the properties
  15582. (goto-char beg)
  15583. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15584. (save-excursion
  15585. (save-restriction
  15586. (narrow-to-region beg end)
  15587. (org-clock-sum))))
  15588. (while (re-search-forward (concat "^" outline-regexp) end t)
  15589. (push (cons (org-current-line) (org-entry-properties)) cache))
  15590. (when cache
  15591. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15592. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15593. (org-columns-display-here-title)
  15594. (mapc (lambda (x)
  15595. (goto-line (car x))
  15596. (org-columns-display-here (cdr x)))
  15597. cache)))))
  15598. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15599. "Insert a new column, to the leeft o the current column."
  15600. (interactive)
  15601. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15602. cell)
  15603. (setq prop (completing-read
  15604. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15605. nil nil prop))
  15606. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15607. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15608. (if (string-match "\\S-" width)
  15609. (setq width (string-to-number width))
  15610. (setq width nil))
  15611. (setq fmt (completing-read "Summary [none]: "
  15612. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15613. nil t))
  15614. (if (string-match "\\S-" fmt)
  15615. (setq fmt (intern fmt))
  15616. (setq fmt nil))
  15617. (if (eq fmt 'none) (setq fmt nil))
  15618. (if editp
  15619. (progn
  15620. (setcar editp prop)
  15621. (setcdr editp (list title width nil fmt)))
  15622. (setq cell (nthcdr (1- (current-column))
  15623. org-columns-current-fmt-compiled))
  15624. (setcdr cell (cons (list prop title width nil fmt)
  15625. (cdr cell))))
  15626. (org-columns-store-format)
  15627. (org-columns-redo)))
  15628. (defun org-columns-delete ()
  15629. "Delete the column at point from columns view."
  15630. (interactive)
  15631. (let* ((n (current-column))
  15632. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15633. (when (y-or-n-p
  15634. (format "Are you sure you want to remove column \"%s\"? " title))
  15635. (setq org-columns-current-fmt-compiled
  15636. (delq (nth n org-columns-current-fmt-compiled)
  15637. org-columns-current-fmt-compiled))
  15638. (org-columns-store-format)
  15639. (org-columns-redo)
  15640. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15641. (backward-char 1)))))
  15642. (defun org-columns-edit-attributes ()
  15643. "Edit the attributes of the current column."
  15644. (interactive)
  15645. (let* ((n (current-column))
  15646. (info (nth n org-columns-current-fmt-compiled)))
  15647. (apply 'org-columns-new info)))
  15648. (defun org-columns-widen (arg)
  15649. "Make the column wider by ARG characters."
  15650. (interactive "p")
  15651. (let* ((n (current-column))
  15652. (entry (nth n org-columns-current-fmt-compiled))
  15653. (width (or (nth 2 entry)
  15654. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15655. (setq width (max 1 (+ width arg)))
  15656. (setcar (nthcdr 2 entry) width)
  15657. (org-columns-store-format)
  15658. (org-columns-redo)))
  15659. (defun org-columns-narrow (arg)
  15660. "Make the column nrrower by ARG characters."
  15661. (interactive "p")
  15662. (org-columns-widen (- arg)))
  15663. (defun org-columns-move-right ()
  15664. "Swap this column with the one to the right."
  15665. (interactive)
  15666. (let* ((n (current-column))
  15667. (cell (nthcdr n org-columns-current-fmt-compiled))
  15668. e)
  15669. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15670. (error "Cannot shift this column further to the right"))
  15671. (setq e (car cell))
  15672. (setcar cell (car (cdr cell)))
  15673. (setcdr cell (cons e (cdr (cdr cell))))
  15674. (org-columns-store-format)
  15675. (org-columns-redo)
  15676. (forward-char 1)))
  15677. (defun org-columns-move-left ()
  15678. "Swap this column with the one to the left."
  15679. (interactive)
  15680. (let* ((n (current-column)))
  15681. (when (= n 0)
  15682. (error "Cannot shift this column further to the left"))
  15683. (backward-char 1)
  15684. (org-columns-move-right)
  15685. (backward-char 1)))
  15686. (defun org-columns-store-format ()
  15687. "Store the text version of the current columns format in appropriate place.
  15688. This is either in the COLUMNS property of the node starting the current column
  15689. display, or in the #+COLUMNS line of the current buffer."
  15690. (let (fmt (cnt 0))
  15691. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15692. (org-set-local 'org-columns-current-fmt fmt)
  15693. (if (marker-position org-columns-top-level-marker)
  15694. (save-excursion
  15695. (goto-char org-columns-top-level-marker)
  15696. (if (and (org-at-heading-p)
  15697. (org-entry-get nil "COLUMNS"))
  15698. (org-entry-put nil "COLUMNS" fmt)
  15699. (goto-char (point-min))
  15700. ;; Overwrite all #+COLUMNS lines....
  15701. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15702. (setq cnt (1+ cnt))
  15703. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15704. (unless (> cnt 0)
  15705. (goto-char (point-min))
  15706. (or (org-on-heading-p t) (outline-next-heading))
  15707. (let ((inhibit-read-only t))
  15708. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15709. (org-set-local 'org-columns-default-format fmt))))))
  15710. (defvar org-overriding-columns-format nil
  15711. "When set, overrides any other definition.")
  15712. (defvar org-agenda-view-columns-initially nil
  15713. "When set, switch to columns view immediately after creating the agenda.")
  15714. (defun org-agenda-columns ()
  15715. "Turn on column view in the agenda."
  15716. (interactive)
  15717. (org-verify-version 'columns)
  15718. (org-columns-remove-overlays)
  15719. (move-marker org-columns-begin-marker (point))
  15720. (let (fmt cache maxwidths m)
  15721. (cond
  15722. ((and (local-variable-p 'org-overriding-columns-format)
  15723. org-overriding-columns-format)
  15724. (setq fmt org-overriding-columns-format))
  15725. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15726. (setq fmt (org-entry-get m "COLUMNS" t)))
  15727. ((and (boundp 'org-columns-current-fmt)
  15728. (local-variable-p 'org-columns-current-fmt)
  15729. org-columns-current-fmt)
  15730. (setq fmt org-columns-current-fmt))
  15731. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15732. (setq m (get-text-property m 'org-hd-marker))
  15733. (setq fmt (org-entry-get m "COLUMNS" t))))
  15734. (setq fmt (or fmt org-columns-default-format))
  15735. (org-set-local 'org-columns-current-fmt fmt)
  15736. (org-columns-compile-format fmt)
  15737. (save-excursion
  15738. ;; Get and cache the properties
  15739. (goto-char (point-min))
  15740. (while (not (eobp))
  15741. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15742. (get-text-property (point) 'org-marker)))
  15743. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15744. (beginning-of-line 2))
  15745. (when cache
  15746. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15747. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15748. (org-columns-display-here-title)
  15749. (mapc (lambda (x)
  15750. (goto-line (car x))
  15751. (org-columns-display-here (cdr x)))
  15752. cache)))))
  15753. (defun org-columns-get-autowidth-alist (s cache)
  15754. "Derive the maximum column widths from the format and the cache."
  15755. (let ((start 0) rtn)
  15756. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15757. (push (cons (match-string 1 s) 1) rtn)
  15758. (setq start (match-end 0)))
  15759. (mapc (lambda (x)
  15760. (setcdr x (apply 'max
  15761. (mapcar
  15762. (lambda (y)
  15763. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15764. cache))))
  15765. rtn)
  15766. rtn))
  15767. (defun org-columns-compute-all ()
  15768. "Compute all columns that have operators defined."
  15769. (org-unmodified
  15770. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15771. (let ((columns org-columns-current-fmt-compiled) col)
  15772. (while (setq col (pop columns))
  15773. (when (nth 3 col)
  15774. (save-excursion
  15775. (org-columns-compute (car col)))))))
  15776. (defun org-columns-update (property)
  15777. "Recompute PROPERTY, and update the columns display for it."
  15778. (org-columns-compute property)
  15779. (let (fmt val pos)
  15780. (save-excursion
  15781. (mapc (lambda (ov)
  15782. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15783. (setq pos (org-overlay-start ov))
  15784. (goto-char pos)
  15785. (when (setq val (cdr (assoc property
  15786. (get-text-property
  15787. (point-at-bol) 'org-summaries))))
  15788. (setq fmt (org-overlay-get ov 'org-columns-format))
  15789. (org-overlay-put ov 'org-columns-value val)
  15790. (org-overlay-put ov 'display (format fmt val)))))
  15791. org-columns-overlays))))
  15792. (defun org-columns-compute (property)
  15793. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15794. (interactive)
  15795. (let* ((re (concat "^" outline-regexp))
  15796. (lmax 30) ; Does anyone use deeper levels???
  15797. (lsum (make-vector lmax 0))
  15798. (lflag (make-vector lmax nil))
  15799. (level 0)
  15800. (ass (assoc property org-columns-current-fmt-compiled))
  15801. (format (nth 4 ass))
  15802. (printf (nth 5 ass))
  15803. (beg org-columns-top-level-marker)
  15804. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15805. (save-excursion
  15806. ;; Find the region to compute
  15807. (goto-char beg)
  15808. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15809. (goto-char end)
  15810. ;; Walk the tree from the back and do the computations
  15811. (while (re-search-backward re beg t)
  15812. (setq sumpos (match-beginning 0)
  15813. last-level level
  15814. level (org-outline-level)
  15815. val (org-entry-get nil property)
  15816. valflag (and val (string-match "\\S-" val)))
  15817. (cond
  15818. ((< level last-level)
  15819. ;; put the sum of lower levels here as a property
  15820. (setq sum (aref lsum last-level) ; current sum
  15821. flag (aref lflag last-level) ; any valid entries from children?
  15822. str (org-column-number-to-string sum format printf)
  15823. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15824. useval (if flag str1 (if valflag val ""))
  15825. sum-alist (get-text-property sumpos 'org-summaries))
  15826. (if (assoc property sum-alist)
  15827. (setcdr (assoc property sum-alist) useval)
  15828. (push (cons property useval) sum-alist)
  15829. (org-unmodified
  15830. (add-text-properties sumpos (1+ sumpos)
  15831. (list 'org-summaries sum-alist))))
  15832. (when val
  15833. (org-entry-put nil property (if flag str val)))
  15834. ;; add current to current level accumulator
  15835. (when (or flag valflag)
  15836. (aset lsum level (+ (aref lsum level)
  15837. (if flag sum (org-column-string-to-number
  15838. (if flag str val) format))))
  15839. (aset lflag level t))
  15840. ;; clear accumulators for deeper levels
  15841. (loop for l from (1+ level) to (1- lmax) do
  15842. (aset lsum l 0)
  15843. (aset lflag l nil)))
  15844. ((>= level last-level)
  15845. ;; add what we have here to the accumulator for this level
  15846. (aset lsum level (+ (aref lsum level)
  15847. (org-column-string-to-number (or val "0") format)))
  15848. (and valflag (aset lflag level t)))
  15849. (t (error "This should not happen")))))))
  15850. (defun org-columns-redo ()
  15851. "Construct the column display again."
  15852. (interactive)
  15853. (message "Recomputing columns...")
  15854. (save-excursion
  15855. (if (marker-position org-columns-begin-marker)
  15856. (goto-char org-columns-begin-marker))
  15857. (org-columns-remove-overlays)
  15858. (if (org-mode-p)
  15859. (call-interactively 'org-columns)
  15860. (call-interactively 'org-agenda-columns)))
  15861. (message "Recomputing columns...done"))
  15862. (defun org-columns-not-in-agenda ()
  15863. (if (eq major-mode 'org-agenda-mode)
  15864. (error "This command is only allowed in Org-mode buffers")))
  15865. (defun org-string-to-number (s)
  15866. "Convert string to number, and interpret hh:mm:ss."
  15867. (if (not (string-match ":" s))
  15868. (string-to-number s)
  15869. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15870. (while l
  15871. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15872. sum)))
  15873. (defun org-column-number-to-string (n fmt &optional printf)
  15874. "Convert a computed column number to a string value, according to FMT."
  15875. (cond
  15876. ((eq fmt 'add_times)
  15877. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15878. (format "%d:%02d" h m)))
  15879. ((eq fmt 'checkbox)
  15880. (cond ((= n (floor n)) "[X]")
  15881. ((> n 1.) "[-]")
  15882. (t "[ ]")))
  15883. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15884. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15885. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15886. (printf (format printf n))
  15887. ((eq fmt 'currency)
  15888. (format "%.2f" n))
  15889. (t (number-to-string n))))
  15890. (defun org-nofm-to-completion (n m &optional percent)
  15891. (if (not percent)
  15892. (format "[%d/%d]" n m)
  15893. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15894. (defun org-column-string-to-number (s fmt)
  15895. "Convert a column value to a number that can be used for column computing."
  15896. (cond
  15897. ((string-match ":" s)
  15898. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15899. (while l
  15900. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15901. sum))
  15902. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15903. (if (equal s "[X]") 1. 0.000001))
  15904. (t (string-to-number s))))
  15905. (defun org-columns-uncompile-format (cfmt)
  15906. "Turn the compiled columns format back into a string representation."
  15907. (let ((rtn "") e s prop title op width fmt printf)
  15908. (while (setq e (pop cfmt))
  15909. (setq prop (car e)
  15910. title (nth 1 e)
  15911. width (nth 2 e)
  15912. op (nth 3 e)
  15913. fmt (nth 4 e)
  15914. printf (nth 5 e))
  15915. (cond
  15916. ((eq fmt 'add_times) (setq op ":"))
  15917. ((eq fmt 'checkbox) (setq op "X"))
  15918. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15919. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15920. ((eq fmt 'add_numbers) (setq op "+"))
  15921. ((eq fmt 'currency) (setq op "$")))
  15922. (if (and op printf) (setq op (concat op ";" printf)))
  15923. (if (equal title prop) (setq title nil))
  15924. (setq s (concat "%" (if width (number-to-string width))
  15925. prop
  15926. (if title (concat "(" title ")"))
  15927. (if op (concat "{" op "}"))))
  15928. (setq rtn (concat rtn " " s)))
  15929. (org-trim rtn)))
  15930. (defun org-columns-compile-format (fmt)
  15931. "Turn a column format string into an alist of specifications.
  15932. The alist has one entry for each column in the format. The elements of
  15933. that list are:
  15934. property the property
  15935. title the title field for the columns
  15936. width the column width in characters, can be nil for automatic
  15937. operator the operator if any
  15938. format the output format for computed results, derived from operator
  15939. printf a printf format for computed values"
  15940. (let ((start 0) width prop title op f printf)
  15941. (setq org-columns-current-fmt-compiled nil)
  15942. (while (string-match
  15943. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15944. fmt start)
  15945. (setq start (match-end 0)
  15946. width (match-string 1 fmt)
  15947. prop (match-string 2 fmt)
  15948. title (or (match-string 3 fmt) prop)
  15949. op (match-string 4 fmt)
  15950. f nil
  15951. printf nil)
  15952. (if width (setq width (string-to-number width)))
  15953. (when (and op (string-match ";" op))
  15954. (setq printf (substring op (match-end 0))
  15955. op (substring op 0 (match-beginning 0))))
  15956. (cond
  15957. ((equal op "+") (setq f 'add_numbers))
  15958. ((equal op "$") (setq f 'currency))
  15959. ((equal op ":") (setq f 'add_times))
  15960. ((equal op "X") (setq f 'checkbox))
  15961. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15962. ((equal op "X%") (setq f 'checkbox-percent))
  15963. )
  15964. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15965. (setq org-columns-current-fmt-compiled
  15966. (nreverse org-columns-current-fmt-compiled))))
  15967. ;;; Dynamic block for Column view
  15968. (defun org-columns-capture-view ()
  15969. "Get the column view of the current buffer and return it as a list.
  15970. The list will contains the title row and all other rows. Each row is
  15971. a list of fields."
  15972. (save-excursion
  15973. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15974. (n (length title)) row tbl)
  15975. (goto-char (point-min))
  15976. (while (re-search-forward "^\\*+ " nil t)
  15977. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15978. (setq row nil)
  15979. (loop for i from 0 to (1- n) do
  15980. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15981. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15982. "")
  15983. row))
  15984. (setq row (nreverse row))
  15985. (push row tbl)))
  15986. (append (list title 'hline) (nreverse tbl)))))
  15987. (defun org-dblock-write:columnview (params)
  15988. "Write the column view table.
  15989. PARAMS is a property list of parameters:
  15990. :width enforce same column widths with <N> specifiers.
  15991. :id the :ID: property of the entry where the columns view
  15992. should be built, as a string. When `local', call locally.
  15993. When `global' call column view with the cursor at the beginning
  15994. of the buffer (usually this means that the whole buffer switches
  15995. to column view).
  15996. :hlines When t, insert a hline before each item. When a number, insert
  15997. a hline before each level <= that number.
  15998. :vlines When t, make each column a colgroup to enforce vertical lines."
  15999. (let ((pos (move-marker (make-marker) (point)))
  16000. (hlines (plist-get params :hlines))
  16001. (vlines (plist-get params :vlines))
  16002. tbl id idpos nfields tmp)
  16003. (save-excursion
  16004. (save-restriction
  16005. (when (setq id (plist-get params :id))
  16006. (cond ((not id) nil)
  16007. ((eq id 'global) (goto-char (point-min)))
  16008. ((eq id 'local) nil)
  16009. ((setq idpos (org-find-entry-with-id id))
  16010. (goto-char idpos))
  16011. (t (error "Cannot find entry with :ID: %s" id))))
  16012. (org-columns)
  16013. (setq tbl (org-columns-capture-view))
  16014. (setq nfields (length (car tbl)))
  16015. (org-columns-quit)))
  16016. (goto-char pos)
  16017. (move-marker pos nil)
  16018. (when tbl
  16019. (when (plist-get params :hlines)
  16020. (setq tmp nil)
  16021. (while tbl
  16022. (if (eq (car tbl) 'hline)
  16023. (push (pop tbl) tmp)
  16024. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  16025. (if (and (not (eq (car tmp) 'hline))
  16026. (or (eq hlines t)
  16027. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  16028. (push 'hline tmp)))
  16029. (push (pop tbl) tmp)))
  16030. (setq tbl (nreverse tmp)))
  16031. (when vlines
  16032. (setq tbl (mapcar (lambda (x)
  16033. (if (eq 'hline x) x (cons "" x)))
  16034. tbl))
  16035. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  16036. (setq pos (point))
  16037. (insert (org-listtable-to-string tbl))
  16038. (when (plist-get params :width)
  16039. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  16040. org-columns-current-widths "|")))
  16041. (goto-char pos)
  16042. (org-table-align))))
  16043. (defun org-listtable-to-string (tbl)
  16044. "Convert a listtable TBL to a string that contains the Org-mode table.
  16045. The table still need to be alligned. The resulting string has no leading
  16046. and tailing newline characters."
  16047. (mapconcat
  16048. (lambda (x)
  16049. (cond
  16050. ((listp x)
  16051. (concat "|" (mapconcat 'identity x "|") "|"))
  16052. ((eq x 'hline) "|-|")
  16053. (t (error "Garbage in listtable: %s" x))))
  16054. tbl "\n"))
  16055. (defun org-insert-columns-dblock ()
  16056. "Create a dynamic block capturing a column view table."
  16057. (interactive)
  16058. (let ((defaults '(:name "columnview" :hlines 1))
  16059. (id (completing-read
  16060. "Capture columns (local, global, entry with :ID: property) [local]: "
  16061. (append '(("global") ("local"))
  16062. (mapcar 'list (org-property-values "ID"))))))
  16063. (if (equal id "") (setq id 'local))
  16064. (if (equal id "global") (setq id 'global))
  16065. (setq defaults (append defaults (list :id id)))
  16066. (org-create-dblock defaults)
  16067. (org-update-dblock)))
  16068. ;;;; Timestamps
  16069. (defvar org-last-changed-timestamp nil)
  16070. (defvar org-time-was-given) ; dynamically scoped parameter
  16071. (defvar org-end-time-was-given) ; dynamically scoped parameter
  16072. (defvar org-ts-what) ; dynamically scoped parameter
  16073. (defun org-time-stamp (arg)
  16074. "Prompt for a date/time and insert a time stamp.
  16075. If the user specifies a time like HH:MM, or if this command is called
  16076. with a prefix argument, the time stamp will contain date and time.
  16077. Otherwise, only the date will be included. All parts of a date not
  16078. specified by the user will be filled in from the current date/time.
  16079. So if you press just return without typing anything, the time stamp
  16080. will represent the current date/time. If there is already a timestamp
  16081. at the cursor, it will be modified."
  16082. (interactive "P")
  16083. (let* ((ts nil)
  16084. (default-time
  16085. ;; Default time is either today, or, when entering a range,
  16086. ;; the range start.
  16087. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16088. (save-excursion
  16089. (re-search-backward
  16090. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16091. (- (point) 20) t)))
  16092. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16093. (current-time)))
  16094. (default-input (and ts (org-get-compact-tod ts)))
  16095. org-time-was-given org-end-time-was-given time)
  16096. (cond
  16097. ((and (org-at-timestamp-p)
  16098. (eq last-command 'org-time-stamp)
  16099. (eq this-command 'org-time-stamp))
  16100. (insert "--")
  16101. (setq time (let ((this-command this-command))
  16102. (org-read-date arg 'totime nil nil default-time default-input)))
  16103. (org-insert-time-stamp time (or org-time-was-given arg)))
  16104. ((org-at-timestamp-p)
  16105. (setq time (let ((this-command this-command))
  16106. (org-read-date arg 'totime nil nil default-time default-input)))
  16107. (when (org-at-timestamp-p) ; just to get the match data
  16108. (replace-match "")
  16109. (setq org-last-changed-timestamp
  16110. (org-insert-time-stamp
  16111. time (or org-time-was-given arg)
  16112. nil nil nil (list org-end-time-was-given))))
  16113. (message "Timestamp updated"))
  16114. (t
  16115. (setq time (let ((this-command this-command))
  16116. (org-read-date arg 'totime nil nil default-time default-input)))
  16117. (org-insert-time-stamp time (or org-time-was-given arg)
  16118. nil nil nil (list org-end-time-was-given))))))
  16119. ;; FIXME: can we use this for something else????
  16120. ;; like computing time differences?????
  16121. (defun org-get-compact-tod (s)
  16122. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16123. (let* ((t1 (match-string 1 s))
  16124. (h1 (string-to-number (match-string 2 s)))
  16125. (m1 (string-to-number (match-string 3 s)))
  16126. (t2 (and (match-end 4) (match-string 5 s)))
  16127. (h2 (and t2 (string-to-number (match-string 6 s))))
  16128. (m2 (and t2 (string-to-number (match-string 7 s))))
  16129. dh dm)
  16130. (if (not t2)
  16131. t1
  16132. (setq dh (- h2 h1) dm (- m2 m1))
  16133. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16134. (concat t1 "+" (number-to-string dh)
  16135. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16136. (defun org-time-stamp-inactive (&optional arg)
  16137. "Insert an inactive time stamp.
  16138. An inactive time stamp is enclosed in square brackets instead of angle
  16139. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16140. does not link to the calendar and cannot be changed with the S-cursor keys.
  16141. So these are more for recording a certain time/date."
  16142. (interactive "P")
  16143. (let (org-time-was-given org-end-time-was-given time)
  16144. (setq time (org-read-date arg 'totime))
  16145. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16146. nil nil (list org-end-time-was-given))))
  16147. (defvar org-date-ovl (org-make-overlay 1 1))
  16148. (org-overlay-put org-date-ovl 'face 'org-warning)
  16149. (org-detach-overlay org-date-ovl)
  16150. (defvar org-ans1) ; dynamically scoped parameter
  16151. (defvar org-ans2) ; dynamically scoped parameter
  16152. (defvar org-plain-time-of-day-regexp) ; defined below
  16153. (defvar org-read-date-overlay nil)
  16154. (defvar org-dcst nil) ; dynamically scoped
  16155. (defun org-read-date (&optional with-time to-time from-string prompt
  16156. default-time default-input)
  16157. "Read a date, possibly a time, and make things smooth for the user.
  16158. The prompt will suggest to enter an ISO date, but you can also enter anything
  16159. which will at least partially be understood by `parse-time-string'.
  16160. Unrecognized parts of the date will default to the current day, month, year,
  16161. hour and minute. If this command is called to replace a timestamp at point,
  16162. of to enter the second timestamp of a range, the default time is taken from the
  16163. existing stamp. For example,
  16164. 3-2-5 --> 2003-02-05
  16165. feb 15 --> currentyear-02-15
  16166. sep 12 9 --> 2009-09-12
  16167. 12:45 --> today 12:45
  16168. 22 sept 0:34 --> currentyear-09-22 0:34
  16169. 12 --> currentyear-currentmonth-12
  16170. Fri --> nearest Friday (today or later)
  16171. etc.
  16172. Furthermore you can specify a relative date by giving, as the *first* thing
  16173. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16174. change in days weeks, months, years.
  16175. With a single plus or minus, the date is relative to today. With a double
  16176. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16177. +4d --> four days from today
  16178. +4 --> same as above
  16179. +2w --> two weeks from today
  16180. ++5 --> five days from default date
  16181. The function understands only English month and weekday abbreviations,
  16182. but this can be configured with the variables `parse-time-months' and
  16183. `parse-time-weekdays'.
  16184. While prompting, a calendar is popped up - you can also select the
  16185. date with the mouse (button 1). The calendar shows a period of three
  16186. months. To scroll it to other months, use the keys `>' and `<'.
  16187. If you don't like the calendar, turn it off with
  16188. \(setq org-read-date-popup-calendar nil)
  16189. With optional argument TO-TIME, the date will immediately be converted
  16190. to an internal time.
  16191. With an optional argument WITH-TIME, the prompt will suggest to also
  16192. insert a time. Note that when WITH-TIME is not set, you can still
  16193. enter a time, and this function will inform the calling routine about
  16194. this change. The calling routine may then choose to change the format
  16195. used to insert the time stamp into the buffer to include the time.
  16196. With optional argument FROM-STRING, read from this string instead from
  16197. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16198. the time/date that is used for everything that is not specified by the
  16199. user."
  16200. (require 'parse-time)
  16201. (let* ((org-time-stamp-rounding-minutes
  16202. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16203. (org-dcst org-display-custom-times)
  16204. (ct (org-current-time))
  16205. (def (or default-time ct))
  16206. (defdecode (decode-time def))
  16207. (dummy (progn
  16208. (when (< (nth 2 defdecode) org-extend-today-until)
  16209. (setcar (nthcdr 2 defdecode) -1)
  16210. (setcar (nthcdr 1 defdecode) 59)
  16211. (setq def (apply 'encode-time defdecode)
  16212. defdecode (decode-time def)))))
  16213. (calendar-move-hook nil)
  16214. (view-diary-entries-initially nil)
  16215. (view-calendar-holidays-initially nil)
  16216. (timestr (format-time-string
  16217. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16218. (prompt (concat (if prompt (concat prompt " ") "")
  16219. (format "Date+time [%s]: " timestr)))
  16220. ans (org-ans0 "") org-ans1 org-ans2 final)
  16221. (cond
  16222. (from-string (setq ans from-string))
  16223. (org-read-date-popup-calendar
  16224. (save-excursion
  16225. (save-window-excursion
  16226. (calendar)
  16227. (calendar-forward-day (- (time-to-days def)
  16228. (calendar-absolute-from-gregorian
  16229. (calendar-current-date))))
  16230. (org-eval-in-calendar nil t)
  16231. (let* ((old-map (current-local-map))
  16232. (map (copy-keymap calendar-mode-map))
  16233. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16234. (org-defkey map (kbd "RET") 'org-calendar-select)
  16235. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16236. 'org-calendar-select-mouse)
  16237. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16238. 'org-calendar-select-mouse)
  16239. (org-defkey minibuffer-local-map [(meta shift left)]
  16240. (lambda () (interactive)
  16241. (org-eval-in-calendar '(calendar-backward-month 1))))
  16242. (org-defkey minibuffer-local-map [(meta shift right)]
  16243. (lambda () (interactive)
  16244. (org-eval-in-calendar '(calendar-forward-month 1))))
  16245. (org-defkey minibuffer-local-map [(meta shift up)]
  16246. (lambda () (interactive)
  16247. (org-eval-in-calendar '(calendar-backward-year 1))))
  16248. (org-defkey minibuffer-local-map [(meta shift down)]
  16249. (lambda () (interactive)
  16250. (org-eval-in-calendar '(calendar-forward-year 1))))
  16251. (org-defkey minibuffer-local-map [(shift up)]
  16252. (lambda () (interactive)
  16253. (org-eval-in-calendar '(calendar-backward-week 1))))
  16254. (org-defkey minibuffer-local-map [(shift down)]
  16255. (lambda () (interactive)
  16256. (org-eval-in-calendar '(calendar-forward-week 1))))
  16257. (org-defkey minibuffer-local-map [(shift left)]
  16258. (lambda () (interactive)
  16259. (org-eval-in-calendar '(calendar-backward-day 1))))
  16260. (org-defkey minibuffer-local-map [(shift right)]
  16261. (lambda () (interactive)
  16262. (org-eval-in-calendar '(calendar-forward-day 1))))
  16263. (org-defkey minibuffer-local-map ">"
  16264. (lambda () (interactive)
  16265. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16266. (org-defkey minibuffer-local-map "<"
  16267. (lambda () (interactive)
  16268. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16269. (unwind-protect
  16270. (progn
  16271. (use-local-map map)
  16272. (add-hook 'post-command-hook 'org-read-date-display)
  16273. (setq org-ans0 (read-string prompt default-input nil nil))
  16274. ;; org-ans0: from prompt
  16275. ;; org-ans1: from mouse click
  16276. ;; org-ans2: from calendar motion
  16277. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16278. (remove-hook 'post-command-hook 'org-read-date-display)
  16279. (use-local-map old-map)
  16280. (when org-read-date-overlay
  16281. (org-delete-overlay org-read-date-overlay)
  16282. (setq org-read-date-overlay nil)))))))
  16283. (t ; Naked prompt only
  16284. (unwind-protect
  16285. (setq ans (read-string prompt default-input nil timestr))
  16286. (when org-read-date-overlay
  16287. (org-delete-overlay org-read-date-overlay)
  16288. (setq org-read-date-overlay nil)))))
  16289. (setq final (org-read-date-analyze ans def defdecode))
  16290. (if to-time
  16291. (apply 'encode-time final)
  16292. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16293. (format "%04d-%02d-%02d %02d:%02d"
  16294. (nth 5 final) (nth 4 final) (nth 3 final)
  16295. (nth 2 final) (nth 1 final))
  16296. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16297. (defvar def)
  16298. (defvar defdecode)
  16299. (defvar with-time)
  16300. (defun org-read-date-display ()
  16301. "Display the currrent date prompt interpretation in the minibuffer."
  16302. (when org-read-date-display-live
  16303. (when org-read-date-overlay
  16304. (org-delete-overlay org-read-date-overlay))
  16305. (let ((p (point)))
  16306. (end-of-line 1)
  16307. (while (not (equal (buffer-substring
  16308. (max (point-min) (- (point) 4)) (point))
  16309. " "))
  16310. (insert " "))
  16311. (goto-char p))
  16312. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16313. " " (or org-ans1 org-ans2)))
  16314. (org-end-time-was-given nil)
  16315. (f (org-read-date-analyze ans def defdecode))
  16316. (fmts (if org-dcst
  16317. org-time-stamp-custom-formats
  16318. org-time-stamp-formats))
  16319. (fmt (if (or with-time
  16320. (and (boundp 'org-time-was-given) org-time-was-given))
  16321. (cdr fmts)
  16322. (car fmts)))
  16323. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16324. (when (and org-end-time-was-given
  16325. (string-match org-plain-time-of-day-regexp txt))
  16326. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16327. org-end-time-was-given
  16328. (substring txt (match-end 0)))))
  16329. (setq org-read-date-overlay
  16330. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16331. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16332. (defun org-read-date-analyze (ans def defdecode)
  16333. "Analyze the combined answer of the date prompt."
  16334. ;; FIXME: cleanup and comment
  16335. (let (delta deltan deltaw deltadef year month day
  16336. hour minute second wday pm h2 m2 tl wday1)
  16337. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16338. (setq ans (replace-match "" t t ans)
  16339. deltan (car delta)
  16340. deltaw (nth 1 delta)
  16341. deltadef (nth 2 delta)))
  16342. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16343. (when (string-match
  16344. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16345. (setq year (if (match-end 2)
  16346. (string-to-number (match-string 2 ans))
  16347. (string-to-number (format-time-string "%Y")))
  16348. month (string-to-number (match-string 3 ans))
  16349. day (string-to-number (match-string 4 ans)))
  16350. (if (< year 100) (setq year (+ 2000 year)))
  16351. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16352. t nil ans)))
  16353. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16354. ;; If there is a time with am/pm, and *no* time without it, we convert
  16355. ;; so that matching will be successful.
  16356. (loop for i from 1 to 2 do ; twice, for end time as well
  16357. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16358. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16359. (setq hour (string-to-number (match-string 1 ans))
  16360. minute (if (match-end 3)
  16361. (string-to-number (match-string 3 ans))
  16362. 0)
  16363. pm (equal ?p
  16364. (string-to-char (downcase (match-string 4 ans)))))
  16365. (if (and (= hour 12) (not pm))
  16366. (setq hour 0)
  16367. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16368. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16369. t t ans))))
  16370. ;; Check if a time range is given as a duration
  16371. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16372. (setq hour (string-to-number (match-string 1 ans))
  16373. h2 (+ hour (string-to-number (match-string 3 ans)))
  16374. minute (string-to-number (match-string 2 ans))
  16375. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16376. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16377. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16378. ;; Check if there is a time range
  16379. (when (boundp 'org-end-time-was-given)
  16380. (setq org-time-was-given nil)
  16381. (when (and (string-match org-plain-time-of-day-regexp ans)
  16382. (match-end 8))
  16383. (setq org-end-time-was-given (match-string 8 ans))
  16384. (setq ans (concat (substring ans 0 (match-beginning 7))
  16385. (substring ans (match-end 7))))))
  16386. (setq tl (parse-time-string ans)
  16387. day (or (nth 3 tl) (nth 3 defdecode))
  16388. month (or (nth 4 tl)
  16389. (if (and org-read-date-prefer-future
  16390. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16391. (1+ (nth 4 defdecode))
  16392. (nth 4 defdecode)))
  16393. year (or (nth 5 tl)
  16394. (if (and org-read-date-prefer-future
  16395. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16396. (1+ (nth 5 defdecode))
  16397. (nth 5 defdecode)))
  16398. hour (or (nth 2 tl) (nth 2 defdecode))
  16399. minute (or (nth 1 tl) (nth 1 defdecode))
  16400. second (or (nth 0 tl) 0)
  16401. wday (nth 6 tl))
  16402. (when deltan
  16403. (unless deltadef
  16404. (let ((now (decode-time (current-time))))
  16405. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16406. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16407. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16408. ((equal deltaw "m") (setq month (+ month deltan)))
  16409. ((equal deltaw "y") (setq year (+ year deltan)))))
  16410. (when (and wday (not (nth 3 tl)))
  16411. ;; Weekday was given, but no day, so pick that day in the week
  16412. ;; on or after the derived date.
  16413. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16414. (unless (equal wday wday1)
  16415. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16416. (if (and (boundp 'org-time-was-given)
  16417. (nth 2 tl))
  16418. (setq org-time-was-given t))
  16419. (if (< year 100) (setq year (+ 2000 year)))
  16420. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16421. (list second minute hour day month year)))
  16422. (defvar parse-time-weekdays)
  16423. (defun org-read-date-get-relative (s today default)
  16424. "Check string S for special relative date string.
  16425. TODAY and DEFAULT are internal times, for today and for a default.
  16426. Return shift list (N what def-flag)
  16427. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16428. N is the number of WHATs to shift.
  16429. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16430. the DEFAULT date rather than TODAY."
  16431. (when (string-match
  16432. (concat
  16433. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16434. "\\([0-9]+\\)?"
  16435. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16436. "\\([ \t]\\|$\\)") s)
  16437. (let* ((dir (if (match-end 1)
  16438. (string-to-char (substring (match-string 1 s) -1))
  16439. ?+))
  16440. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16441. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16442. (what (if (match-end 3) (match-string 3 s) "d"))
  16443. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16444. (date (if rel default today))
  16445. (wday (nth 6 (decode-time date)))
  16446. delta)
  16447. (if wday1
  16448. (progn
  16449. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16450. (if (= dir ?-) (setq delta (- delta 7)))
  16451. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16452. (list delta "d" rel))
  16453. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16454. (defun org-eval-in-calendar (form &optional keepdate)
  16455. "Eval FORM in the calendar window and return to current window.
  16456. Also, store the cursor date in variable org-ans2."
  16457. (let ((sw (selected-window)))
  16458. (select-window (get-buffer-window "*Calendar*"))
  16459. (eval form)
  16460. (when (and (not keepdate) (calendar-cursor-to-date))
  16461. (let* ((date (calendar-cursor-to-date))
  16462. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16463. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16464. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16465. (select-window sw)))
  16466. ; ;; Update the prompt to show new default date
  16467. ; (save-excursion
  16468. ; (goto-char (point-min))
  16469. ; (when (and org-ans2
  16470. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16471. ; (get-text-property (match-end 0) 'field))
  16472. ; (let ((inhibit-read-only t))
  16473. ; (replace-match (concat "[" org-ans2 "]") t t)
  16474. ; (add-text-properties (point-min) (1+ (match-end 0))
  16475. ; (text-properties-at (1+ (point-min)))))))))
  16476. (defun org-calendar-select ()
  16477. "Return to `org-read-date' with the date currently selected.
  16478. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16479. (interactive)
  16480. (when (calendar-cursor-to-date)
  16481. (let* ((date (calendar-cursor-to-date))
  16482. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16483. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16484. (if (active-minibuffer-window) (exit-minibuffer))))
  16485. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16486. "Insert a date stamp for the date given by the internal TIME.
  16487. WITH-HM means, use the stamp format that includes the time of the day.
  16488. INACTIVE means use square brackets instead of angular ones, so that the
  16489. stamp will not contribute to the agenda.
  16490. PRE and POST are optional strings to be inserted before and after the
  16491. stamp.
  16492. The command returns the inserted time stamp."
  16493. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16494. stamp)
  16495. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16496. (insert-before-markers (or pre ""))
  16497. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16498. (when (listp extra)
  16499. (setq extra (car extra))
  16500. (if (and (stringp extra)
  16501. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16502. (setq extra (format "-%02d:%02d"
  16503. (string-to-number (match-string 1 extra))
  16504. (string-to-number (match-string 2 extra))))
  16505. (setq extra nil)))
  16506. (when extra
  16507. (backward-char 1)
  16508. (insert-before-markers extra)
  16509. (forward-char 1))
  16510. (insert-before-markers (or post ""))
  16511. stamp))
  16512. (defun org-toggle-time-stamp-overlays ()
  16513. "Toggle the use of custom time stamp formats."
  16514. (interactive)
  16515. (setq org-display-custom-times (not org-display-custom-times))
  16516. (unless org-display-custom-times
  16517. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16518. (while (setq p (next-single-property-change p 'display))
  16519. (if (and (get-text-property p 'display)
  16520. (eq (get-text-property p 'face) 'org-date))
  16521. (remove-text-properties
  16522. p (setq p (next-single-property-change p 'display))
  16523. '(display t))))
  16524. (set-buffer-modified-p bmp)))
  16525. (if (featurep 'xemacs)
  16526. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16527. (org-restart-font-lock)
  16528. (setq org-table-may-need-update t)
  16529. (if org-display-custom-times
  16530. (message "Time stamps are overlayed with custom format")
  16531. (message "Time stamp overlays removed")))
  16532. (defun org-display-custom-time (beg end)
  16533. "Overlay modified time stamp format over timestamp between BED and END."
  16534. (let* ((ts (buffer-substring beg end))
  16535. t1 w1 with-hm tf time str w2 (off 0))
  16536. (save-match-data
  16537. (setq t1 (org-parse-time-string ts t))
  16538. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16539. (setq off (- (match-end 0) (match-beginning 0)))))
  16540. (setq end (- end off))
  16541. (setq w1 (- end beg)
  16542. with-hm (and (nth 1 t1) (nth 2 t1))
  16543. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16544. time (org-fix-decoded-time t1)
  16545. str (org-add-props
  16546. (format-time-string
  16547. (substring tf 1 -1) (apply 'encode-time time))
  16548. nil 'mouse-face 'highlight)
  16549. w2 (length str))
  16550. (if (not (= w2 w1))
  16551. (add-text-properties (1+ beg) (+ 2 beg)
  16552. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16553. (if (featurep 'xemacs)
  16554. (progn
  16555. (put-text-property beg end 'invisible t)
  16556. (put-text-property beg end 'end-glyph (make-glyph str)))
  16557. (put-text-property beg end 'display str))))
  16558. (defun org-translate-time (string)
  16559. "Translate all timestamps in STRING to custom format.
  16560. But do this only if the variable `org-display-custom-times' is set."
  16561. (when org-display-custom-times
  16562. (save-match-data
  16563. (let* ((start 0)
  16564. (re org-ts-regexp-both)
  16565. t1 with-hm inactive tf time str beg end)
  16566. (while (setq start (string-match re string start))
  16567. (setq beg (match-beginning 0)
  16568. end (match-end 0)
  16569. t1 (save-match-data
  16570. (org-parse-time-string (substring string beg end) t))
  16571. with-hm (and (nth 1 t1) (nth 2 t1))
  16572. inactive (equal (substring string beg (1+ beg)) "[")
  16573. tf (funcall (if with-hm 'cdr 'car)
  16574. org-time-stamp-custom-formats)
  16575. time (org-fix-decoded-time t1)
  16576. str (format-time-string
  16577. (concat
  16578. (if inactive "[" "<") (substring tf 1 -1)
  16579. (if inactive "]" ">"))
  16580. (apply 'encode-time time))
  16581. string (replace-match str t t string)
  16582. start (+ start (length str)))))))
  16583. string)
  16584. (defun org-fix-decoded-time (time)
  16585. "Set 0 instead of nil for the first 6 elements of time.
  16586. Don't touch the rest."
  16587. (let ((n 0))
  16588. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16589. (defun org-days-to-time (timestamp-string)
  16590. "Difference between TIMESTAMP-STRING and now in days."
  16591. (- (time-to-days (org-time-string-to-time timestamp-string))
  16592. (time-to-days (current-time))))
  16593. (defun org-deadline-close (timestamp-string &optional ndays)
  16594. "Is the time in TIMESTAMP-STRING close to the current date?"
  16595. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16596. (and (< (org-days-to-time timestamp-string) ndays)
  16597. (not (org-entry-is-done-p))))
  16598. (defun org-get-wdays (ts)
  16599. "Get the deadline lead time appropriate for timestring TS."
  16600. (cond
  16601. ((<= org-deadline-warning-days 0)
  16602. ;; 0 or negative, enforce this value no matter what
  16603. (- org-deadline-warning-days))
  16604. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16605. ;; lead time is specified.
  16606. (floor (* (string-to-number (match-string 1 ts))
  16607. (cdr (assoc (match-string 2 ts)
  16608. '(("d" . 1) ("w" . 7)
  16609. ("m" . 30.4) ("y" . 365.25)))))))
  16610. ;; go for the default.
  16611. (t org-deadline-warning-days)))
  16612. (defun org-calendar-select-mouse (ev)
  16613. "Return to `org-read-date' with the date currently selected.
  16614. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16615. (interactive "e")
  16616. (mouse-set-point ev)
  16617. (when (calendar-cursor-to-date)
  16618. (let* ((date (calendar-cursor-to-date))
  16619. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16620. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16621. (if (active-minibuffer-window) (exit-minibuffer))))
  16622. (defun org-check-deadlines (ndays)
  16623. "Check if there are any deadlines due or past due.
  16624. A deadline is considered due if it happens within `org-deadline-warning-days'
  16625. days from today's date. If the deadline appears in an entry marked DONE,
  16626. it is not shown. The prefix arg NDAYS can be used to test that many
  16627. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16628. (interactive "P")
  16629. (let* ((org-warn-days
  16630. (cond
  16631. ((equal ndays '(4)) 100000)
  16632. (ndays (prefix-numeric-value ndays))
  16633. (t (abs org-deadline-warning-days))))
  16634. (case-fold-search nil)
  16635. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16636. (callback
  16637. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16638. (message "%d deadlines past-due or due within %d days"
  16639. (org-occur regexp nil callback)
  16640. org-warn-days)))
  16641. (defun org-check-before-date (date)
  16642. "Check if there are deadlines or scheduled entries before DATE."
  16643. (interactive (list (org-read-date)))
  16644. (let ((case-fold-search nil)
  16645. (regexp (concat "\\<\\(" org-deadline-string
  16646. "\\|" org-scheduled-string
  16647. "\\) *<\\([^>]+\\)>"))
  16648. (callback
  16649. (lambda () (time-less-p
  16650. (org-time-string-to-time (match-string 2))
  16651. (org-time-string-to-time date)))))
  16652. (message "%d entries before %s"
  16653. (org-occur regexp nil callback) date)))
  16654. (defun org-evaluate-time-range (&optional to-buffer)
  16655. "Evaluate a time range by computing the difference between start and end.
  16656. Normally the result is just printed in the echo area, but with prefix arg
  16657. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16658. If the time range is actually in a table, the result is inserted into the
  16659. next column.
  16660. For time difference computation, a year is assumed to be exactly 365
  16661. days in order to avoid rounding problems."
  16662. (interactive "P")
  16663. (or
  16664. (org-clock-update-time-maybe)
  16665. (save-excursion
  16666. (unless (org-at-date-range-p t)
  16667. (goto-char (point-at-bol))
  16668. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16669. (if (not (org-at-date-range-p t))
  16670. (error "Not at a time-stamp range, and none found in current line")))
  16671. (let* ((ts1 (match-string 1))
  16672. (ts2 (match-string 2))
  16673. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16674. (match-end (match-end 0))
  16675. (time1 (org-time-string-to-time ts1))
  16676. (time2 (org-time-string-to-time ts2))
  16677. (t1 (time-to-seconds time1))
  16678. (t2 (time-to-seconds time2))
  16679. (diff (abs (- t2 t1)))
  16680. (negative (< (- t2 t1) 0))
  16681. ;; (ys (floor (* 365 24 60 60)))
  16682. (ds (* 24 60 60))
  16683. (hs (* 60 60))
  16684. (fy "%dy %dd %02d:%02d")
  16685. (fy1 "%dy %dd")
  16686. (fd "%dd %02d:%02d")
  16687. (fd1 "%dd")
  16688. (fh "%02d:%02d")
  16689. y d h m align)
  16690. (if havetime
  16691. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16692. y 0
  16693. d (floor (/ diff ds)) diff (mod diff ds)
  16694. h (floor (/ diff hs)) diff (mod diff hs)
  16695. m (floor (/ diff 60)))
  16696. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16697. y 0
  16698. d (floor (+ (/ diff ds) 0.5))
  16699. h 0 m 0))
  16700. (if (not to-buffer)
  16701. (message "%s" (org-make-tdiff-string y d h m))
  16702. (if (org-at-table-p)
  16703. (progn
  16704. (goto-char match-end)
  16705. (setq align t)
  16706. (and (looking-at " *|") (goto-char (match-end 0))))
  16707. (goto-char match-end))
  16708. (if (looking-at
  16709. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16710. (replace-match ""))
  16711. (if negative (insert " -"))
  16712. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16713. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16714. (insert " " (format fh h m))))
  16715. (if align (org-table-align))
  16716. (message "Time difference inserted")))))
  16717. (defun org-make-tdiff-string (y d h m)
  16718. (let ((fmt "")
  16719. (l nil))
  16720. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16721. l (push y l)))
  16722. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16723. l (push d l)))
  16724. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16725. l (push h l)))
  16726. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16727. l (push m l)))
  16728. (apply 'format fmt (nreverse l))))
  16729. (defun org-time-string-to-time (s)
  16730. (apply 'encode-time (org-parse-time-string s)))
  16731. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16732. "Convert a time stamp to an absolute day number.
  16733. If there is a specifyer for a cyclic time stamp, get the closest date to
  16734. DAYNR."
  16735. (cond
  16736. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16737. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16738. daynr
  16739. (+ daynr 1000)))
  16740. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16741. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16742. (time-to-days (current-time))) (match-string 0 s)
  16743. prefer))
  16744. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16745. (defun org-time-from-absolute (d)
  16746. "Return the time corresponding to date D.
  16747. D may be an absolute day number, or a calendar-type list (month day year)."
  16748. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16749. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16750. (defun org-calendar-holiday ()
  16751. "List of holidays, for Diary display in Org-mode."
  16752. (require 'holidays)
  16753. (let ((hl (funcall
  16754. (if (fboundp 'calendar-check-holidays)
  16755. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16756. (if hl (mapconcat 'identity hl "; "))))
  16757. (defun org-diary-sexp-entry (sexp entry date)
  16758. "Process a SEXP diary ENTRY for DATE."
  16759. (require 'diary-lib)
  16760. (let ((result (if calendar-debug-sexp
  16761. (let ((stack-trace-on-error t))
  16762. (eval (car (read-from-string sexp))))
  16763. (condition-case nil
  16764. (eval (car (read-from-string sexp)))
  16765. (error
  16766. (beep)
  16767. (message "Bad sexp at line %d in %s: %s"
  16768. (org-current-line)
  16769. (buffer-file-name) sexp)
  16770. (sleep-for 2))))))
  16771. (cond ((stringp result) result)
  16772. ((and (consp result)
  16773. (stringp (cdr result))) (cdr result))
  16774. (result entry)
  16775. (t nil))))
  16776. (defun org-diary-to-ical-string (frombuf)
  16777. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16778. This uses the icalendar.el library."
  16779. (let* ((tmpdir (if (featurep 'xemacs)
  16780. (temp-directory)
  16781. temporary-file-directory))
  16782. (tmpfile (make-temp-name
  16783. (expand-file-name "orgics" tmpdir)))
  16784. buf rtn b e)
  16785. (save-excursion
  16786. (set-buffer frombuf)
  16787. (icalendar-export-region (point-min) (point-max) tmpfile)
  16788. (setq buf (find-buffer-visiting tmpfile))
  16789. (set-buffer buf)
  16790. (goto-char (point-min))
  16791. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16792. (setq b (match-beginning 0)))
  16793. (goto-char (point-max))
  16794. (if (re-search-backward "^END:VEVENT" nil t)
  16795. (setq e (match-end 0)))
  16796. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16797. (kill-buffer buf)
  16798. (kill-buffer frombuf)
  16799. (delete-file tmpfile)
  16800. rtn))
  16801. (defun org-closest-date (start current change prefer)
  16802. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16803. When PREFER is `past' return a date that is either CURRENT or past.
  16804. When PREFER is `future', return a date that is either CURRENT or future."
  16805. ;; Make the proper lists from the dates
  16806. (catch 'exit
  16807. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16808. dn dw sday cday n1 n2
  16809. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16810. (setq start (org-date-to-gregorian start)
  16811. current (org-date-to-gregorian
  16812. (if org-agenda-repeating-timestamp-show-all
  16813. current
  16814. (time-to-days (current-time))))
  16815. sday (calendar-absolute-from-gregorian start)
  16816. cday (calendar-absolute-from-gregorian current))
  16817. (if (<= cday sday) (throw 'exit sday))
  16818. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16819. (setq dn (string-to-number (match-string 1 change))
  16820. dw (cdr (assoc (match-string 2 change) a1)))
  16821. (error "Invalid change specifyer: %s" change))
  16822. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16823. (cond
  16824. ((eq dw 'day)
  16825. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16826. n2 (+ n1 dn)))
  16827. ((eq dw 'year)
  16828. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16829. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16830. (setq date1 (list m d y1)
  16831. n1 (calendar-absolute-from-gregorian date1)
  16832. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16833. n2 (calendar-absolute-from-gregorian date2)))
  16834. ((eq dw 'month)
  16835. ;; approx number of month between the tow dates
  16836. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16837. ;; How often does dn fit in there?
  16838. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16839. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16840. m (+ m nm)
  16841. ny (floor (/ m 12))
  16842. y (+ y ny)
  16843. m (- m (* ny 12)))
  16844. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16845. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16846. (setq m2 (+ m dn) y2 y)
  16847. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16848. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16849. (while (< n2 cday)
  16850. (setq n1 n2 m m2 y y2)
  16851. (setq m2 (+ m dn) y2 y)
  16852. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16853. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16854. (if org-agenda-repeating-timestamp-show-all
  16855. (cond
  16856. ((eq prefer 'past) n1)
  16857. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16858. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16859. (cond
  16860. ((eq prefer 'past) n1)
  16861. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16862. (t (if (= cday n1) n1 n2)))))))
  16863. (defun org-date-to-gregorian (date)
  16864. "Turn any specification of DATE into a gregorian date for the calendar."
  16865. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16866. ((and (listp date) (= (length date) 3)) date)
  16867. ((stringp date)
  16868. (setq date (org-parse-time-string date))
  16869. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16870. ((listp date)
  16871. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16872. (defun org-parse-time-string (s &optional nodefault)
  16873. "Parse the standard Org-mode time string.
  16874. This should be a lot faster than the normal `parse-time-string'.
  16875. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16876. hour and minute fields will be nil if not given."
  16877. (if (string-match org-ts-regexp0 s)
  16878. (list 0
  16879. (if (or (match-beginning 8) (not nodefault))
  16880. (string-to-number (or (match-string 8 s) "0")))
  16881. (if (or (match-beginning 7) (not nodefault))
  16882. (string-to-number (or (match-string 7 s) "0")))
  16883. (string-to-number (match-string 4 s))
  16884. (string-to-number (match-string 3 s))
  16885. (string-to-number (match-string 2 s))
  16886. nil nil nil)
  16887. (make-list 9 0)))
  16888. (defun org-timestamp-up (&optional arg)
  16889. "Increase the date item at the cursor by one.
  16890. If the cursor is on the year, change the year. If it is on the month or
  16891. the day, change that.
  16892. With prefix ARG, change by that many units."
  16893. (interactive "p")
  16894. (org-timestamp-change (prefix-numeric-value arg)))
  16895. (defun org-timestamp-down (&optional arg)
  16896. "Decrease the date item at the cursor by one.
  16897. If the cursor is on the year, change the year. If it is on the month or
  16898. the day, change that.
  16899. With prefix ARG, change by that many units."
  16900. (interactive "p")
  16901. (org-timestamp-change (- (prefix-numeric-value arg))))
  16902. (defun org-timestamp-up-day (&optional arg)
  16903. "Increase the date in the time stamp by one day.
  16904. With prefix ARG, change that many days."
  16905. (interactive "p")
  16906. (if (and (not (org-at-timestamp-p t))
  16907. (org-on-heading-p))
  16908. (org-todo 'up)
  16909. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16910. (defun org-timestamp-down-day (&optional arg)
  16911. "Decrease the date in the time stamp by one day.
  16912. With prefix ARG, change that many days."
  16913. (interactive "p")
  16914. (if (and (not (org-at-timestamp-p t))
  16915. (org-on-heading-p))
  16916. (org-todo 'down)
  16917. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16918. (defsubst org-pos-in-match-range (pos n)
  16919. (and (match-beginning n)
  16920. (<= (match-beginning n) pos)
  16921. (>= (match-end n) pos)))
  16922. (defun org-at-timestamp-p (&optional inactive-ok)
  16923. "Determine if the cursor is in or at a timestamp."
  16924. (interactive)
  16925. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16926. (pos (point))
  16927. (ans (or (looking-at tsr)
  16928. (save-excursion
  16929. (skip-chars-backward "^[<\n\r\t")
  16930. (if (> (point) (point-min)) (backward-char 1))
  16931. (and (looking-at tsr)
  16932. (> (- (match-end 0) pos) -1))))))
  16933. (and ans
  16934. (boundp 'org-ts-what)
  16935. (setq org-ts-what
  16936. (cond
  16937. ((= pos (match-beginning 0)) 'bracket)
  16938. ((= pos (1- (match-end 0))) 'bracket)
  16939. ((org-pos-in-match-range pos 2) 'year)
  16940. ((org-pos-in-match-range pos 3) 'month)
  16941. ((org-pos-in-match-range pos 7) 'hour)
  16942. ((org-pos-in-match-range pos 8) 'minute)
  16943. ((or (org-pos-in-match-range pos 4)
  16944. (org-pos-in-match-range pos 5)) 'day)
  16945. ((and (> pos (or (match-end 8) (match-end 5)))
  16946. (< pos (match-end 0)))
  16947. (- pos (or (match-end 8) (match-end 5))))
  16948. (t 'day))))
  16949. ans))
  16950. (defun org-toggle-timestamp-type ()
  16951. ""
  16952. (interactive)
  16953. (when (org-at-timestamp-p t)
  16954. (save-excursion
  16955. (goto-char (match-beginning 0))
  16956. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16957. (goto-char (1- (match-end 0)))
  16958. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16959. (message "Timestamp is now %sactive"
  16960. (if (equal (char-before) ?>) "in" ""))))
  16961. (defun org-timestamp-change (n &optional what)
  16962. "Change the date in the time stamp at point.
  16963. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16964. `year', `minute', `second'. If WHAT is not given, the cursor position
  16965. in the timestamp determines what will be changed."
  16966. (let ((pos (point))
  16967. with-hm inactive
  16968. org-ts-what
  16969. extra
  16970. ts time time0)
  16971. (if (not (org-at-timestamp-p t))
  16972. (error "Not at a timestamp"))
  16973. (if (and (not what) (eq org-ts-what 'bracket))
  16974. (org-toggle-timestamp-type)
  16975. (if (and (not what) (not (eq org-ts-what 'day))
  16976. org-display-custom-times
  16977. (get-text-property (point) 'display)
  16978. (not (get-text-property (1- (point)) 'display)))
  16979. (setq org-ts-what 'day))
  16980. (setq org-ts-what (or what org-ts-what)
  16981. inactive (= (char-after (match-beginning 0)) ?\[)
  16982. ts (match-string 0))
  16983. (replace-match "")
  16984. (if (string-match
  16985. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16986. ts)
  16987. (setq extra (match-string 1 ts)))
  16988. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16989. (setq with-hm t))
  16990. (setq time0 (org-parse-time-string ts))
  16991. (setq time
  16992. (encode-time (or (car time0) 0)
  16993. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16994. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16995. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16996. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16997. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16998. (nthcdr 6 time0)))
  16999. (when (integerp org-ts-what)
  17000. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  17001. (if (eq what 'calendar)
  17002. (let ((cal-date (org-get-date-from-calendar)))
  17003. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  17004. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  17005. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  17006. (setcar time0 (or (car time0) 0))
  17007. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  17008. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  17009. (setq time (apply 'encode-time time0))))
  17010. (setq org-last-changed-timestamp
  17011. (org-insert-time-stamp time with-hm inactive nil nil extra))
  17012. (org-clock-update-time-maybe)
  17013. (goto-char pos)
  17014. ;; Try to recenter the calendar window, if any
  17015. (if (and org-calendar-follow-timestamp-change
  17016. (get-buffer-window "*Calendar*" t)
  17017. (memq org-ts-what '(day month year)))
  17018. (org-recenter-calendar (time-to-days time))))))
  17019. ;; FIXME: does not yet work for lead times
  17020. (defun org-modify-ts-extra (s pos n)
  17021. "Change the different parts of the lead-time and repeat fields in timestamp."
  17022. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  17023. ng h m new)
  17024. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  17025. (cond
  17026. ((or (org-pos-in-match-range pos 2)
  17027. (org-pos-in-match-range pos 3))
  17028. (setq m (string-to-number (match-string 3 s))
  17029. h (string-to-number (match-string 2 s)))
  17030. (if (org-pos-in-match-range pos 2)
  17031. (setq h (+ h n))
  17032. (setq m (+ m n)))
  17033. (if (< m 0) (setq m (+ m 60) h (1- h)))
  17034. (if (> m 59) (setq m (- m 60) h (1+ h)))
  17035. (setq h (min 24 (max 0 h)))
  17036. (setq ng 1 new (format "-%02d:%02d" h m)))
  17037. ((org-pos-in-match-range pos 6)
  17038. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  17039. ((org-pos-in-match-range pos 5)
  17040. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  17041. (when ng
  17042. (setq s (concat
  17043. (substring s 0 (match-beginning ng))
  17044. new
  17045. (substring s (match-end ng))))))
  17046. s))
  17047. (defun org-recenter-calendar (date)
  17048. "If the calendar is visible, recenter it to DATE."
  17049. (let* ((win (selected-window))
  17050. (cwin (get-buffer-window "*Calendar*" t))
  17051. (calendar-move-hook nil))
  17052. (when cwin
  17053. (select-window cwin)
  17054. (calendar-goto-date (if (listp date) date
  17055. (calendar-gregorian-from-absolute date)))
  17056. (select-window win))))
  17057. (defun org-goto-calendar (&optional arg)
  17058. "Go to the Emacs calendar at the current date.
  17059. If there is a time stamp in the current line, go to that date.
  17060. A prefix ARG can be used to force the current date."
  17061. (interactive "P")
  17062. (let ((tsr org-ts-regexp) diff
  17063. (calendar-move-hook nil)
  17064. (view-calendar-holidays-initially nil)
  17065. (view-diary-entries-initially nil))
  17066. (if (or (org-at-timestamp-p)
  17067. (save-excursion
  17068. (beginning-of-line 1)
  17069. (looking-at (concat ".*" tsr))))
  17070. (let ((d1 (time-to-days (current-time)))
  17071. (d2 (time-to-days
  17072. (org-time-string-to-time (match-string 1)))))
  17073. (setq diff (- d2 d1))))
  17074. (calendar)
  17075. (calendar-goto-today)
  17076. (if (and diff (not arg)) (calendar-forward-day diff))))
  17077. (defun org-get-date-from-calendar ()
  17078. "Return a list (month day year) of date at point in calendar."
  17079. (with-current-buffer "*Calendar*"
  17080. (save-match-data
  17081. (calendar-cursor-to-date))))
  17082. (defun org-date-from-calendar ()
  17083. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17084. If there is already a time stamp at the cursor position, update it."
  17085. (interactive)
  17086. (if (org-at-timestamp-p t)
  17087. (org-timestamp-change 0 'calendar)
  17088. (let ((cal-date (org-get-date-from-calendar)))
  17089. (org-insert-time-stamp
  17090. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17091. (defvar appt-time-msg-list)
  17092. ;;;###autoload
  17093. (defun org-agenda-to-appt (&optional refresh filter)
  17094. "Activate appointments found in `org-agenda-files'.
  17095. With a \\[universal-argument] prefix, refresh the list of
  17096. appointements.
  17097. If FILTER is t, interactively prompt the user for a regular
  17098. expression, and filter out entries that don't match it.
  17099. If FILTER is a string, use this string as a regular expression
  17100. for filtering entries out.
  17101. FILTER can also be an alist with the car of each cell being
  17102. either 'headline or 'category. For example:
  17103. '((headline \"IMPORTANT\")
  17104. (category \"Work\"))
  17105. will only add headlines containing IMPORTANT or headlines
  17106. belonging to the \"Work\" category."
  17107. (interactive "P")
  17108. (require 'calendar)
  17109. (if refresh (setq appt-time-msg-list nil))
  17110. (if (eq filter t)
  17111. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17112. (let* ((cnt 0) ; count added events
  17113. (org-agenda-new-buffers nil)
  17114. (org-deadline-warning-days 0)
  17115. (today (org-date-to-gregorian
  17116. (time-to-days (current-time))))
  17117. (files (org-agenda-files)) entries file)
  17118. ;; Get all entries which may contain an appt
  17119. (while (setq file (pop files))
  17120. (setq entries
  17121. (append entries
  17122. (org-agenda-get-day-entries
  17123. file today :timestamp :scheduled :deadline))))
  17124. (setq entries (delq nil entries))
  17125. ;; Map thru entries and find if we should filter them out
  17126. (mapc
  17127. (lambda(x)
  17128. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17129. (cat (get-text-property 1 'org-category x))
  17130. (tod (get-text-property 1 'time-of-day x))
  17131. (ok (or (null filter)
  17132. (and (stringp filter) (string-match filter evt))
  17133. (and (listp filter)
  17134. (or (string-match
  17135. (cadr (assoc 'category filter)) cat)
  17136. (string-match
  17137. (cadr (assoc 'headline filter)) evt))))))
  17138. ;; FIXME: Shall we remove text-properties for the appt text?
  17139. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17140. (when (and ok tod)
  17141. (setq tod (number-to-string tod)
  17142. tod (when (string-match
  17143. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17144. (concat (match-string 1 tod) ":"
  17145. (match-string 2 tod))))
  17146. (appt-add tod evt)
  17147. (setq cnt (1+ cnt))))) entries)
  17148. (org-release-buffers org-agenda-new-buffers)
  17149. (if (eq cnt 0)
  17150. (message "No event to add")
  17151. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17152. ;;; The clock for measuring work time.
  17153. (defvar org-mode-line-string "")
  17154. (put 'org-mode-line-string 'risky-local-variable t)
  17155. (defvar org-mode-line-timer nil)
  17156. (defvar org-clock-heading "")
  17157. (defvar org-clock-start-time "")
  17158. (defun org-update-mode-line ()
  17159. (let* ((delta (- (time-to-seconds (current-time))
  17160. (time-to-seconds org-clock-start-time)))
  17161. (h (floor delta 3600))
  17162. (m (floor (- delta (* 3600 h)) 60)))
  17163. (setq org-mode-line-string
  17164. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17165. 'help-echo "Org-mode clock is running"))
  17166. (force-mode-line-update)))
  17167. (defvar org-clock-marker (make-marker)
  17168. "Marker recording the last clock-in.")
  17169. (defvar org-clock-mode-line-entry nil
  17170. "Information for the modeline about the running clock.")
  17171. (defun org-clock-in ()
  17172. "Start the clock on the current item.
  17173. If necessary, clock-out of the currently active clock."
  17174. (interactive)
  17175. (org-clock-out t)
  17176. (let (ts)
  17177. (save-excursion
  17178. (org-back-to-heading t)
  17179. (when (and org-clock-in-switch-to-state
  17180. (not (looking-at (concat outline-regexp "[ \t]*"
  17181. org-clock-in-switch-to-state
  17182. "\\>"))))
  17183. (org-todo org-clock-in-switch-to-state))
  17184. (if (and org-clock-heading-function
  17185. (functionp org-clock-heading-function))
  17186. (setq org-clock-heading (funcall org-clock-heading-function))
  17187. (if (looking-at org-complex-heading-regexp)
  17188. (setq org-clock-heading (match-string 4))
  17189. (setq org-clock-heading "???")))
  17190. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17191. (org-clock-find-position)
  17192. (insert "\n") (backward-char 1)
  17193. (indent-relative)
  17194. (insert org-clock-string " ")
  17195. (setq org-clock-start-time (current-time))
  17196. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17197. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17198. (or global-mode-string (setq global-mode-string '("")))
  17199. (or (memq 'org-mode-line-string global-mode-string)
  17200. (setq global-mode-string
  17201. (append global-mode-string '(org-mode-line-string))))
  17202. (org-update-mode-line)
  17203. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17204. (message "Clock started at %s" ts))))
  17205. (defun org-clock-find-position ()
  17206. "Find the location where the next clock line should be inserted."
  17207. (org-back-to-heading t)
  17208. (catch 'exit
  17209. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17210. (re (concat "^[ \t]*" org-clock-string))
  17211. (cnt 0)
  17212. first last)
  17213. (goto-char beg)
  17214. (when (eobp) (newline) (setq end (max (point) end)))
  17215. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17216. ;; we seem to have a CLOCK drawer, so go there.
  17217. (beginning-of-line 2)
  17218. (throw 'exit t))
  17219. ;; Lets count the CLOCK lines
  17220. (goto-char beg)
  17221. (while (re-search-forward re end t)
  17222. (setq first (or first (match-beginning 0))
  17223. last (match-beginning 0)
  17224. cnt (1+ cnt)))
  17225. (when (and (integerp org-clock-into-drawer)
  17226. (>= (1+ cnt) org-clock-into-drawer))
  17227. ;; Wrap current entries into a new drawer
  17228. (goto-char last)
  17229. (beginning-of-line 2)
  17230. (if (org-at-item-p) (org-end-of-item))
  17231. (insert ":END:\n")
  17232. (beginning-of-line 0)
  17233. (org-indent-line-function)
  17234. (goto-char first)
  17235. (insert ":CLOCK:\n")
  17236. (beginning-of-line 0)
  17237. (org-indent-line-function)
  17238. (org-flag-drawer t)
  17239. (beginning-of-line 2)
  17240. (throw 'exit nil))
  17241. (goto-char beg)
  17242. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17243. (not (equal (match-string 1) org-clock-string)))
  17244. ;; Planning info, skip to after it
  17245. (beginning-of-line 2)
  17246. (or (bolp) (newline)))
  17247. (when (eq t org-clock-into-drawer)
  17248. (insert ":CLOCK:\n:END:\n")
  17249. (beginning-of-line -1)
  17250. (org-indent-line-function)
  17251. (org-flag-drawer t)
  17252. (beginning-of-line 2)
  17253. (org-indent-line-function)))))
  17254. (defun org-clock-out (&optional fail-quietly)
  17255. "Stop the currently running clock.
  17256. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17257. (interactive)
  17258. (catch 'exit
  17259. (if (not (marker-buffer org-clock-marker))
  17260. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17261. (let (ts te s h m)
  17262. (save-excursion
  17263. (set-buffer (marker-buffer org-clock-marker))
  17264. (goto-char org-clock-marker)
  17265. (beginning-of-line 1)
  17266. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17267. (equal (match-string 1) org-clock-string))
  17268. (setq ts (match-string 2))
  17269. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17270. (goto-char (match-end 0))
  17271. (delete-region (point) (point-at-eol))
  17272. (insert "--")
  17273. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17274. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17275. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17276. h (floor (/ s 3600))
  17277. s (- s (* 3600 h))
  17278. m (floor (/ s 60))
  17279. s (- s (* 60 s)))
  17280. (insert " => " (format "%2d:%02d" h m))
  17281. (move-marker org-clock-marker nil)
  17282. (when org-log-note-clock-out
  17283. (org-add-log-maybe 'clock-out))
  17284. (when org-mode-line-timer
  17285. (cancel-timer org-mode-line-timer)
  17286. (setq org-mode-line-timer nil))
  17287. (setq global-mode-string
  17288. (delq 'org-mode-line-string global-mode-string))
  17289. (force-mode-line-update)
  17290. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17291. (defun org-clock-cancel ()
  17292. "Cancel the running clock be removing the start timestamp."
  17293. (interactive)
  17294. (if (not (marker-buffer org-clock-marker))
  17295. (error "No active clock"))
  17296. (save-excursion
  17297. (set-buffer (marker-buffer org-clock-marker))
  17298. (goto-char org-clock-marker)
  17299. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17300. (setq global-mode-string
  17301. (delq 'org-mode-line-string global-mode-string))
  17302. (force-mode-line-update)
  17303. (message "Clock canceled"))
  17304. (defun org-clock-goto (&optional delete-windows)
  17305. "Go to the currently clocked-in entry."
  17306. (interactive "P")
  17307. (if (not (marker-buffer org-clock-marker))
  17308. (error "No active clock"))
  17309. (switch-to-buffer-other-window
  17310. (marker-buffer org-clock-marker))
  17311. (if delete-windows (delete-other-windows))
  17312. (goto-char org-clock-marker)
  17313. (org-show-entry)
  17314. (org-back-to-heading)
  17315. (recenter))
  17316. (defvar org-clock-file-total-minutes nil
  17317. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17318. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17319. (defun org-clock-sum (&optional tstart tend)
  17320. "Sum the times for each subtree.
  17321. Puts the resulting times in minutes as a text property on each headline."
  17322. (interactive)
  17323. (let* ((bmp (buffer-modified-p))
  17324. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17325. org-clock-string
  17326. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17327. (lmax 30)
  17328. (ltimes (make-vector lmax 0))
  17329. (t1 0)
  17330. (level 0)
  17331. ts te dt
  17332. time)
  17333. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17334. (save-excursion
  17335. (goto-char (point-max))
  17336. (while (re-search-backward re nil t)
  17337. (cond
  17338. ((match-end 2)
  17339. ;; Two time stamps
  17340. (setq ts (match-string 2)
  17341. te (match-string 3)
  17342. ts (time-to-seconds
  17343. (apply 'encode-time (org-parse-time-string ts)))
  17344. te (time-to-seconds
  17345. (apply 'encode-time (org-parse-time-string te)))
  17346. ts (if tstart (max ts tstart) ts)
  17347. te (if tend (min te tend) te)
  17348. dt (- te ts)
  17349. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17350. ((match-end 4)
  17351. ;; A naket time
  17352. (setq t1 (+ t1 (string-to-number (match-string 5))
  17353. (* 60 (string-to-number (match-string 4))))))
  17354. (t ;; A headline
  17355. (setq level (- (match-end 1) (match-beginning 1)))
  17356. (when (or (> t1 0) (> (aref ltimes level) 0))
  17357. (loop for l from 0 to level do
  17358. (aset ltimes l (+ (aref ltimes l) t1)))
  17359. (setq t1 0 time (aref ltimes level))
  17360. (loop for l from level to (1- lmax) do
  17361. (aset ltimes l 0))
  17362. (goto-char (match-beginning 0))
  17363. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17364. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17365. (set-buffer-modified-p bmp)))
  17366. (defun org-clock-display (&optional total-only)
  17367. "Show subtree times in the entire buffer.
  17368. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17369. in the echo area."
  17370. (interactive)
  17371. (org-remove-clock-overlays)
  17372. (let (time h m p)
  17373. (org-clock-sum)
  17374. (unless total-only
  17375. (save-excursion
  17376. (goto-char (point-min))
  17377. (while (or (and (equal (setq p (point)) (point-min))
  17378. (get-text-property p :org-clock-minutes))
  17379. (setq p (next-single-property-change
  17380. (point) :org-clock-minutes)))
  17381. (goto-char p)
  17382. (when (setq time (get-text-property p :org-clock-minutes))
  17383. (org-put-clock-overlay time (funcall outline-level))))
  17384. (setq h (/ org-clock-file-total-minutes 60)
  17385. m (- org-clock-file-total-minutes (* 60 h)))
  17386. ;; Arrange to remove the overlays upon next change.
  17387. (when org-remove-highlights-with-change
  17388. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17389. nil 'local))))
  17390. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17391. (defvar org-clock-overlays nil)
  17392. (make-variable-buffer-local 'org-clock-overlays)
  17393. (defun org-put-clock-overlay (time &optional level)
  17394. "Put an overlays on the current line, displaying TIME.
  17395. If LEVEL is given, prefix time with a corresponding number of stars.
  17396. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17397. will be easy to remove."
  17398. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17399. (l (if level (org-get-legal-level level 0) 0))
  17400. (off 0)
  17401. ov tx)
  17402. (move-to-column c)
  17403. (unless (eolp) (skip-chars-backward "^ \t"))
  17404. (skip-chars-backward " \t")
  17405. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17406. tx (concat (buffer-substring (1- (point)) (point))
  17407. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17408. (org-add-props (format "%s %2d:%02d%s"
  17409. (make-string l ?*) h m
  17410. (make-string (- 16 l) ?\ ))
  17411. '(face secondary-selection))
  17412. ""))
  17413. (if (not (featurep 'xemacs))
  17414. (org-overlay-put ov 'display tx)
  17415. (org-overlay-put ov 'invisible t)
  17416. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17417. (push ov org-clock-overlays)))
  17418. (defun org-remove-clock-overlays (&optional beg end noremove)
  17419. "Remove the occur highlights from the buffer.
  17420. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17421. from the `before-change-functions' in the current buffer."
  17422. (interactive)
  17423. (unless org-inhibit-highlight-removal
  17424. (mapc 'org-delete-overlay org-clock-overlays)
  17425. (setq org-clock-overlays nil)
  17426. (unless noremove
  17427. (remove-hook 'before-change-functions
  17428. 'org-remove-clock-overlays 'local))))
  17429. (defun org-clock-out-if-current ()
  17430. "Clock out if the current entry contains the running clock.
  17431. This is used to stop the clock after a TODO entry is marked DONE,
  17432. and is only done if the variable `org-clock-out-when-done' is not nil."
  17433. (when (and org-clock-out-when-done
  17434. (member state org-done-keywords)
  17435. (equal (marker-buffer org-clock-marker) (current-buffer))
  17436. (< (point) org-clock-marker)
  17437. (> (save-excursion (outline-next-heading) (point))
  17438. org-clock-marker))
  17439. ;; Clock out, but don't accept a logging message for this.
  17440. (let ((org-log-note-clock-out nil))
  17441. (org-clock-out))))
  17442. (add-hook 'org-after-todo-state-change-hook
  17443. 'org-clock-out-if-current)
  17444. (defun org-check-running-clock ()
  17445. "Check if the current buffer contains the running clock.
  17446. If yes, offer to stop it and to save the buffer with the changes."
  17447. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17448. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17449. (buffer-name))))
  17450. (org-clock-out)
  17451. (when (y-or-n-p "Save changed buffer?")
  17452. (save-buffer))))
  17453. (defun org-clock-report (&optional arg)
  17454. "Create a table containing a report about clocked time.
  17455. If the cursor is inside an existing clocktable block, then the table
  17456. will be updated. If not, a new clocktable will be inserted.
  17457. When called with a prefix argument, move to the first clock table in the
  17458. buffer and update it."
  17459. (interactive "P")
  17460. (org-remove-clock-overlays)
  17461. (when arg
  17462. (org-find-dblock "clocktable")
  17463. (org-show-entry))
  17464. (if (org-in-clocktable-p)
  17465. (goto-char (org-in-clocktable-p))
  17466. (org-create-dblock (list :name "clocktable"
  17467. :maxlevel 2 :scope 'file)))
  17468. (org-update-dblock))
  17469. (defun org-in-clocktable-p ()
  17470. "Check if the cursor is in a clocktable."
  17471. (let ((pos (point)) start)
  17472. (save-excursion
  17473. (end-of-line 1)
  17474. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17475. (setq start (match-beginning 0))
  17476. (re-search-forward "^#\\+END:.*" nil t)
  17477. (>= (match-end 0) pos)
  17478. start))))
  17479. (defun org-clock-update-time-maybe ()
  17480. "If this is a CLOCK line, update it and return t.
  17481. Otherwise, return nil."
  17482. (interactive)
  17483. (save-excursion
  17484. (beginning-of-line 1)
  17485. (skip-chars-forward " \t")
  17486. (when (looking-at org-clock-string)
  17487. (let ((re (concat "[ \t]*" org-clock-string
  17488. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17489. "\\([ \t]*=>.*\\)?"))
  17490. ts te h m s)
  17491. (if (not (looking-at re))
  17492. nil
  17493. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17494. (end-of-line 1)
  17495. (setq ts (match-string 1)
  17496. te (match-string 2))
  17497. (setq s (- (time-to-seconds
  17498. (apply 'encode-time (org-parse-time-string te)))
  17499. (time-to-seconds
  17500. (apply 'encode-time (org-parse-time-string ts))))
  17501. h (floor (/ s 3600))
  17502. s (- s (* 3600 h))
  17503. m (floor (/ s 60))
  17504. s (- s (* 60 s)))
  17505. (insert " => " (format "%2d:%02d" h m))
  17506. t)))))
  17507. (defun org-clock-special-range (key &optional time as-strings)
  17508. "Return two times bordering a special time range.
  17509. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17510. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17511. A week starts Monday 0:00 and ends Sunday 24:00.
  17512. The range is determined relative to TIME. TIME defaults to the current time.
  17513. The return value is a cons cell with two internal times like the ones
  17514. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17515. the returned times will be formatted strings."
  17516. (let* ((tm (decode-time (or time (current-time))))
  17517. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17518. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17519. (dow (nth 6 tm))
  17520. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17521. (cond
  17522. ((eq key 'today)
  17523. (setq h 0 m 0 h1 24 m1 0))
  17524. ((eq key 'yesterday)
  17525. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17526. ((eq key 'thisweek)
  17527. (setq diff (if (= dow 0) 6 (1- dow))
  17528. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17529. ((eq key 'lastweek)
  17530. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17531. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17532. ((eq key 'thismonth)
  17533. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17534. ((eq key 'lastmonth)
  17535. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17536. ((eq key 'thisyear)
  17537. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17538. ((eq key 'lastyear)
  17539. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17540. (t (error "No such time block %s" key)))
  17541. (setq ts (encode-time s m h d month y)
  17542. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17543. (or d1 d) (or month1 month) (or y1 y)))
  17544. (setq fm (cdr org-time-stamp-formats))
  17545. (if as-strings
  17546. (cons (format-time-string fm ts) (format-time-string fm te))
  17547. (cons ts te))))
  17548. (defun org-dblock-write:clocktable (params)
  17549. "Write the standard clocktable."
  17550. (catch 'exit
  17551. (let* ((hlchars '((1 . "*") (2 . "/")))
  17552. (ins (make-marker))
  17553. (total-time nil)
  17554. (scope (plist-get params :scope))
  17555. (tostring (plist-get params :tostring))
  17556. (multifile (plist-get params :multifile))
  17557. (header (plist-get params :header))
  17558. (maxlevel (or (plist-get params :maxlevel) 3))
  17559. (step (plist-get params :step))
  17560. (emph (plist-get params :emphasize))
  17561. (ts (plist-get params :tstart))
  17562. (te (plist-get params :tend))
  17563. (block (plist-get params :block))
  17564. ipos time h m p level hlc hdl
  17565. cc beg end pos tbl)
  17566. (when step
  17567. (org-clocktable-steps params)
  17568. (throw 'exit nil))
  17569. (when block
  17570. (setq cc (org-clock-special-range block nil t)
  17571. ts (car cc) te (cdr cc)))
  17572. (if ts (setq ts (time-to-seconds
  17573. (apply 'encode-time (org-parse-time-string ts)))))
  17574. (if te (setq te (time-to-seconds
  17575. (apply 'encode-time (org-parse-time-string te)))))
  17576. (move-marker ins (point))
  17577. (setq ipos (point))
  17578. ;; Get the right scope
  17579. (setq pos (point))
  17580. (save-restriction
  17581. (cond
  17582. ((not scope))
  17583. ((eq scope 'file) (widen))
  17584. ((eq scope 'subtree) (org-narrow-to-subtree))
  17585. ((eq scope 'tree)
  17586. (while (org-up-heading-safe))
  17587. (org-narrow-to-subtree))
  17588. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17589. (symbol-name scope)))
  17590. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17591. (catch 'exit
  17592. (while (org-up-heading-safe)
  17593. (looking-at outline-regexp)
  17594. (if (<= (org-reduced-level (funcall outline-level)) level)
  17595. (throw 'exit nil))))
  17596. (org-narrow-to-subtree))
  17597. ((or (listp scope) (eq scope 'agenda))
  17598. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17599. (scope 'agenda)
  17600. (p1 (copy-sequence params))
  17601. file)
  17602. (plist-put p1 :tostring t)
  17603. (plist-put p1 :multifile t)
  17604. (plist-put p1 :scope 'file)
  17605. (org-prepare-agenda-buffers files)
  17606. (while (setq file (pop files))
  17607. (with-current-buffer (find-buffer-visiting file)
  17608. (push (org-clocktable-add-file
  17609. file (org-dblock-write:clocktable p1)) tbl)
  17610. (setq total-time (+ (or total-time 0)
  17611. org-clock-file-total-minutes)))))))
  17612. (goto-char pos)
  17613. (unless (eq scope 'agenda)
  17614. (org-clock-sum ts te)
  17615. (goto-char (point-min))
  17616. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17617. (goto-char p)
  17618. (when (setq time (get-text-property p :org-clock-minutes))
  17619. (save-excursion
  17620. (beginning-of-line 1)
  17621. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17622. (setq level (org-reduced-level
  17623. (- (match-end 1) (match-beginning 1))))
  17624. (<= level maxlevel))
  17625. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17626. hdl (match-string 2)
  17627. h (/ time 60)
  17628. m (- time (* 60 h)))
  17629. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17630. (push (concat
  17631. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17632. (make-string (1- level) ?|)
  17633. hlc (format "%d:%02d" h m) hlc
  17634. " |") tbl))))))
  17635. (setq tbl (nreverse tbl))
  17636. (if tostring
  17637. (if tbl (mapconcat 'identity tbl "\n") nil)
  17638. (goto-char ins)
  17639. (insert-before-markers
  17640. (or header
  17641. (concat
  17642. "Clock summary at ["
  17643. (substring
  17644. (format-time-string (cdr org-time-stamp-formats))
  17645. 1 -1)
  17646. "]."
  17647. (if block
  17648. (format " Considered range is /%s/." block)
  17649. "")
  17650. "\n\n"))
  17651. (if (eq scope 'agenda) "|File" "")
  17652. "|L|Headline|Time|\n")
  17653. (setq total-time (or total-time org-clock-file-total-minutes)
  17654. h (/ total-time 60)
  17655. m (- total-time (* 60 h)))
  17656. (insert-before-markers
  17657. "|-\n|"
  17658. (if (eq scope 'agenda) "|" "")
  17659. "|"
  17660. "*Total time*| "
  17661. (format "*%d:%02d*" h m)
  17662. "|\n|-\n")
  17663. (setq tbl (delq nil tbl))
  17664. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17665. (equal (substring (car tbl) 0 2) "|-"))
  17666. (pop tbl))
  17667. (insert-before-markers (mapconcat
  17668. 'identity (delq nil tbl)
  17669. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17670. (backward-delete-char 1)
  17671. (goto-char ipos)
  17672. (skip-chars-forward "^|")
  17673. (org-table-align))))))
  17674. (defun org-clocktable-steps (params)
  17675. (let* ((p1 (copy-sequence params))
  17676. (ts (plist-get p1 :tstart))
  17677. (te (plist-get p1 :tend))
  17678. (step0 (plist-get p1 :step))
  17679. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17680. (block (plist-get p1 :block))
  17681. cc)
  17682. (when block
  17683. (setq cc (org-clock-special-range block nil t)
  17684. ts (car cc) te (cdr cc)))
  17685. (if ts (setq ts (time-to-seconds
  17686. (apply 'encode-time (org-parse-time-string ts)))))
  17687. (if te (setq te (time-to-seconds
  17688. (apply 'encode-time (org-parse-time-string te)))))
  17689. (plist-put p1 :header "")
  17690. (plist-put p1 :step nil)
  17691. (plist-put p1 :block nil)
  17692. (while (< ts te)
  17693. (or (bolp) (insert "\n"))
  17694. (plist-put p1 :tstart (format-time-string
  17695. (car org-time-stamp-formats)
  17696. (seconds-to-time ts)))
  17697. (plist-put p1 :tend (format-time-string
  17698. (car org-time-stamp-formats)
  17699. (seconds-to-time (setq ts (+ ts step)))))
  17700. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17701. (plist-get p1 :tstart) "\n")
  17702. (org-dblock-write:clocktable p1)
  17703. (re-search-forward "#\\+END:")
  17704. (end-of-line 0))))
  17705. (defun org-clocktable-add-file (file table)
  17706. (if table
  17707. (let ((lines (org-split-string table "\n"))
  17708. (ff (file-name-nondirectory file)))
  17709. (mapconcat 'identity
  17710. (mapcar (lambda (x)
  17711. (if (string-match org-table-dataline-regexp x)
  17712. (concat "|" ff x)
  17713. x))
  17714. lines)
  17715. "\n"))))
  17716. ;; FIXME: I don't think anybody uses this, ask David
  17717. (defun org-collect-clock-time-entries ()
  17718. "Return an internal list with clocking information.
  17719. This list has one entry for each CLOCK interval.
  17720. FIXME: describe the elements."
  17721. (interactive)
  17722. (let ((re (concat "^[ \t]*" org-clock-string
  17723. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17724. rtn beg end next cont level title total closedp leafp
  17725. clockpos titlepos h m donep)
  17726. (save-excursion
  17727. (org-clock-sum)
  17728. (goto-char (point-min))
  17729. (while (re-search-forward re nil t)
  17730. (setq clockpos (match-beginning 0)
  17731. beg (match-string 1) end (match-string 2)
  17732. cont (match-end 0))
  17733. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17734. end (apply 'encode-time (org-parse-time-string end)))
  17735. (org-back-to-heading t)
  17736. (setq donep (org-entry-is-done-p))
  17737. (setq titlepos (point)
  17738. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17739. h (/ total 60) m (- total (* 60 h))
  17740. total (cons h m))
  17741. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17742. (setq level (- (match-end 1) (match-beginning 1))
  17743. title (org-match-string-no-properties 2))
  17744. (save-excursion (outline-next-heading) (setq next (point)))
  17745. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17746. (goto-char next)
  17747. (setq leafp (and (looking-at "^\\*+ ")
  17748. (<= (- (match-end 0) (point)) level)))
  17749. (push (list beg end clockpos closedp donep
  17750. total title titlepos level leafp)
  17751. rtn)
  17752. (goto-char cont)))
  17753. (nreverse rtn)))
  17754. ;;;; Agenda, and Diary Integration
  17755. ;;; Define the Org-agenda-mode
  17756. (defvar org-agenda-mode-map (make-sparse-keymap)
  17757. "Keymap for `org-agenda-mode'.")
  17758. (defvar org-agenda-menu) ; defined later in this file.
  17759. (defvar org-agenda-follow-mode nil)
  17760. (defvar org-agenda-show-log nil)
  17761. (defvar org-agenda-redo-command nil)
  17762. (defvar org-agenda-mode-hook nil)
  17763. (defvar org-agenda-type nil)
  17764. (defvar org-agenda-force-single-file nil)
  17765. (defun org-agenda-mode ()
  17766. "Mode for time-sorted view on action items in Org-mode files.
  17767. The following commands are available:
  17768. \\{org-agenda-mode-map}"
  17769. (interactive)
  17770. (kill-all-local-variables)
  17771. (setq org-agenda-undo-list nil
  17772. org-agenda-pending-undo-list nil)
  17773. (setq major-mode 'org-agenda-mode)
  17774. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17775. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17776. (setq mode-name "Org-Agenda")
  17777. (use-local-map org-agenda-mode-map)
  17778. (easy-menu-add org-agenda-menu)
  17779. (if org-startup-truncated (setq truncate-lines t))
  17780. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17781. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17782. ;; Make sure properties are removed when copying text
  17783. (when (boundp 'buffer-substring-filters)
  17784. (org-set-local 'buffer-substring-filters
  17785. (cons (lambda (x)
  17786. (set-text-properties 0 (length x) nil x) x)
  17787. buffer-substring-filters)))
  17788. (unless org-agenda-keep-modes
  17789. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17790. org-agenda-show-log nil))
  17791. (easy-menu-change
  17792. '("Agenda") "Agenda Files"
  17793. (append
  17794. (list
  17795. (vector
  17796. (if (get 'org-agenda-files 'org-restrict)
  17797. "Restricted to single file"
  17798. "Edit File List")
  17799. '(org-edit-agenda-file-list)
  17800. (not (get 'org-agenda-files 'org-restrict)))
  17801. "--")
  17802. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17803. (org-agenda-set-mode-name)
  17804. (apply
  17805. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17806. (list 'org-agenda-mode-hook)))
  17807. (substitute-key-definition 'undo 'org-agenda-undo
  17808. org-agenda-mode-map global-map)
  17809. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17810. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17811. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17812. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17813. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17814. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17815. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17816. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17817. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17818. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17819. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17820. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17821. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17822. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17823. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17824. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17825. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17826. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17827. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17828. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17829. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17830. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17831. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17832. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17833. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17834. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17835. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17836. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17837. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17838. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17839. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17840. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17841. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17842. (while l (org-defkey org-agenda-mode-map
  17843. (int-to-string (pop l)) 'digit-argument)))
  17844. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17845. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17846. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17847. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17848. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17849. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17850. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17851. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17852. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17853. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17854. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17855. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17856. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17857. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17858. (org-defkey org-agenda-mode-map "n" 'next-line)
  17859. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17860. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17861. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17862. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17863. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17864. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17865. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17866. (eval-after-load "calendar"
  17867. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17868. 'org-calendar-goto-agenda))
  17869. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17870. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17871. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17872. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17873. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17874. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17875. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17876. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17877. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17878. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17879. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17880. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17881. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17882. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17883. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17884. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17885. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17886. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17887. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17888. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17889. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17890. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17891. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17892. "Local keymap for agenda entries from Org-mode.")
  17893. (org-defkey org-agenda-keymap
  17894. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17895. (org-defkey org-agenda-keymap
  17896. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17897. (when org-agenda-mouse-1-follows-link
  17898. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17899. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17900. '("Agenda"
  17901. ("Agenda Files")
  17902. "--"
  17903. ["Show" org-agenda-show t]
  17904. ["Go To (other window)" org-agenda-goto t]
  17905. ["Go To (this window)" org-agenda-switch-to t]
  17906. ["Follow Mode" org-agenda-follow-mode
  17907. :style toggle :selected org-agenda-follow-mode :active t]
  17908. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17909. "--"
  17910. ["Cycle TODO" org-agenda-todo t]
  17911. ["Archive subtree" org-agenda-archive t]
  17912. ["Delete subtree" org-agenda-kill t]
  17913. "--"
  17914. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17915. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17916. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17917. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17918. "--"
  17919. ("Tags and Properties"
  17920. ["Show all Tags" org-agenda-show-tags t]
  17921. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17922. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17923. "--"
  17924. ["Column View" org-columns t])
  17925. ("Date/Schedule"
  17926. ["Schedule" org-agenda-schedule t]
  17927. ["Set Deadline" org-agenda-deadline t]
  17928. "--"
  17929. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17930. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17931. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17932. ("Clock"
  17933. ["Clock in" org-agenda-clock-in t]
  17934. ["Clock out" org-agenda-clock-out t]
  17935. ["Clock cancel" org-agenda-clock-cancel t]
  17936. ["Goto running clock" org-clock-goto t])
  17937. ("Priority"
  17938. ["Set Priority" org-agenda-priority t]
  17939. ["Increase Priority" org-agenda-priority-up t]
  17940. ["Decrease Priority" org-agenda-priority-down t]
  17941. ["Show Priority" org-agenda-show-priority t])
  17942. ("Calendar/Diary"
  17943. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17944. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17945. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17946. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17947. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17948. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17949. "--"
  17950. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17951. "--"
  17952. ("View"
  17953. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17954. :style radio :selected (equal org-agenda-ndays 1)]
  17955. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17956. :style radio :selected (equal org-agenda-ndays 7)]
  17957. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17958. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17959. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17960. :style radio :selected (member org-agenda-ndays '(365 366))]
  17961. "--"
  17962. ["Show Logbook entries" org-agenda-log-mode
  17963. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17964. ["Include Diary" org-agenda-toggle-diary
  17965. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17966. ["Use Time Grid" org-agenda-toggle-time-grid
  17967. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17968. ["Write view to file" org-write-agenda t]
  17969. ["Rebuild buffer" org-agenda-redo t]
  17970. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17971. "--"
  17972. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17973. "--"
  17974. ["Quit" org-agenda-quit t]
  17975. ["Exit and Release Buffers" org-agenda-exit t]
  17976. ))
  17977. ;;; Agenda undo
  17978. (defvar org-agenda-allow-remote-undo t
  17979. "Non-nil means, allow remote undo from the agenda buffer.")
  17980. (defvar org-agenda-undo-list nil
  17981. "List of undoable operations in the agenda since last refresh.")
  17982. (defvar org-agenda-undo-has-started-in nil
  17983. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17984. (defvar org-agenda-pending-undo-list nil
  17985. "In a series of undo commands, this is the list of remaning undo items.")
  17986. (defmacro org-if-unprotected (&rest body)
  17987. "Execute BODY if there is no `org-protected' text property at point."
  17988. (declare (debug t))
  17989. `(unless (get-text-property (point) 'org-protected)
  17990. ,@body))
  17991. (defmacro org-with-remote-undo (_buffer &rest _body)
  17992. "Execute BODY while recording undo information in two buffers."
  17993. (declare (indent 1) (debug t))
  17994. `(let ((_cline (org-current-line))
  17995. (_cmd this-command)
  17996. (_buf1 (current-buffer))
  17997. (_buf2 ,_buffer)
  17998. (_undo1 buffer-undo-list)
  17999. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  18000. _c1 _c2)
  18001. ,@_body
  18002. (when org-agenda-allow-remote-undo
  18003. (setq _c1 (org-verify-change-for-undo
  18004. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  18005. _c2 (org-verify-change-for-undo
  18006. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  18007. (when (or _c1 _c2)
  18008. ;; make sure there are undo boundaries
  18009. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  18010. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  18011. ;; remember which buffer to undo
  18012. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  18013. org-agenda-undo-list)))))
  18014. (defun org-agenda-undo ()
  18015. "Undo a remote editing step in the agenda.
  18016. This undoes changes both in the agenda buffer and in the remote buffer
  18017. that have been changed along."
  18018. (interactive)
  18019. (or org-agenda-allow-remote-undo
  18020. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  18021. (if (not (eq this-command last-command))
  18022. (setq org-agenda-undo-has-started-in nil
  18023. org-agenda-pending-undo-list org-agenda-undo-list))
  18024. (if (not org-agenda-pending-undo-list)
  18025. (error "No further undo information"))
  18026. (let* ((entry (pop org-agenda-pending-undo-list))
  18027. buf line cmd rembuf)
  18028. (setq cmd (pop entry) line (pop entry))
  18029. (setq rembuf (nth 2 entry))
  18030. (org-with-remote-undo rembuf
  18031. (while (bufferp (setq buf (pop entry)))
  18032. (if (pop entry)
  18033. (with-current-buffer buf
  18034. (let ((last-undo-buffer buf)
  18035. (inhibit-read-only t))
  18036. (unless (memq buf org-agenda-undo-has-started-in)
  18037. (push buf org-agenda-undo-has-started-in)
  18038. (make-local-variable 'pending-undo-list)
  18039. (undo-start))
  18040. (while (and pending-undo-list
  18041. (listp pending-undo-list)
  18042. (not (car pending-undo-list)))
  18043. (pop pending-undo-list))
  18044. (undo-more 1))))))
  18045. (goto-line line)
  18046. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  18047. (defun org-verify-change-for-undo (l1 l2)
  18048. "Verify that a real change occurred between the undo lists L1 and L2."
  18049. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  18050. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18051. (not (eq l1 l2)))
  18052. ;;; Agenda dispatch
  18053. (defvar org-agenda-restrict nil)
  18054. (defvar org-agenda-restrict-begin (make-marker))
  18055. (defvar org-agenda-restrict-end (make-marker))
  18056. (defvar org-agenda-last-dispatch-buffer nil)
  18057. (defvar org-agenda-overriding-restriction nil)
  18058. ;;;###autoload
  18059. (defun org-agenda (arg &optional keys restriction)
  18060. "Dispatch agenda commands to collect entries to the agenda buffer.
  18061. Prompts for a command to execute. Any prefix arg will be passed
  18062. on to the selected command. The default selections are:
  18063. a Call `org-agenda-list' to display the agenda for current day or week.
  18064. t Call `org-todo-list' to display the global todo list.
  18065. T Call `org-todo-list' to display the global todo list, select only
  18066. entries with a specific TODO keyword (the user gets a prompt).
  18067. m Call `org-tags-view' to display headlines with tags matching
  18068. a condition (the user is prompted for the condition).
  18069. M Like `m', but select only TODO entries, no ordinary headlines.
  18070. L Create a timeline for the current buffer.
  18071. e Export views to associated files.
  18072. More commands can be added by configuring the variable
  18073. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18074. searches can be pre-defined in this way.
  18075. If the current buffer is in Org-mode and visiting a file, you can also
  18076. first press `<' once to indicate that the agenda should be temporarily
  18077. \(until the next use of \\[org-agenda]) restricted to the current file.
  18078. Pressing `<' twice means to restrict to the current subtree or region
  18079. \(if active)."
  18080. (interactive "P")
  18081. (catch 'exit
  18082. (let* ((prefix-descriptions nil)
  18083. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18084. (org-agenda-custom-commands
  18085. ;; normalize different versions
  18086. (delq nil
  18087. (mapcar
  18088. (lambda (x)
  18089. (cond ((stringp (cdr x))
  18090. (push x prefix-descriptions)
  18091. nil)
  18092. ((stringp (nth 1 x)) x)
  18093. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18094. (t (cons (car x) (cons "" (cdr x))))))
  18095. org-agenda-custom-commands)))
  18096. (buf (current-buffer))
  18097. (bfn (buffer-file-name (buffer-base-buffer)))
  18098. entry key type match lprops ans)
  18099. ;; Turn off restriction unless there is an overriding one
  18100. (unless org-agenda-overriding-restriction
  18101. (put 'org-agenda-files 'org-restrict nil)
  18102. (setq org-agenda-restrict nil)
  18103. (move-marker org-agenda-restrict-begin nil)
  18104. (move-marker org-agenda-restrict-end nil))
  18105. ;; Delete old local properties
  18106. (put 'org-agenda-redo-command 'org-lprops nil)
  18107. ;; Remember where this call originated
  18108. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18109. (unless keys
  18110. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18111. keys (car ans)
  18112. restriction (cdr ans)))
  18113. ;; Estabish the restriction, if any
  18114. (when (and (not org-agenda-overriding-restriction) restriction)
  18115. (put 'org-agenda-files 'org-restrict (list bfn))
  18116. (cond
  18117. ((eq restriction 'region)
  18118. (setq org-agenda-restrict t)
  18119. (move-marker org-agenda-restrict-begin (region-beginning))
  18120. (move-marker org-agenda-restrict-end (region-end)))
  18121. ((eq restriction 'subtree)
  18122. (save-excursion
  18123. (setq org-agenda-restrict t)
  18124. (org-back-to-heading t)
  18125. (move-marker org-agenda-restrict-begin (point))
  18126. (move-marker org-agenda-restrict-end
  18127. (progn (org-end-of-subtree t)))))))
  18128. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18129. ;; For example the todo list should not need it (but does...)
  18130. (cond
  18131. ((setq entry (assoc keys org-agenda-custom-commands))
  18132. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18133. (progn
  18134. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18135. (put 'org-agenda-redo-command 'org-lprops lprops)
  18136. (cond
  18137. ((eq type 'agenda)
  18138. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18139. ((eq type 'alltodo)
  18140. (org-let lprops '(org-todo-list current-prefix-arg)))
  18141. ((eq type 'stuck)
  18142. (org-let lprops '(org-agenda-list-stuck-projects
  18143. current-prefix-arg)))
  18144. ((eq type 'tags)
  18145. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18146. ((eq type 'tags-todo)
  18147. (org-let lprops '(org-tags-view '(4) match)))
  18148. ((eq type 'todo)
  18149. (org-let lprops '(org-todo-list match)))
  18150. ((eq type 'tags-tree)
  18151. (org-check-for-org-mode)
  18152. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18153. ((eq type 'todo-tree)
  18154. (org-check-for-org-mode)
  18155. (org-let lprops
  18156. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18157. (regexp-quote match) "\\>"))))
  18158. ((eq type 'occur-tree)
  18159. (org-check-for-org-mode)
  18160. (org-let lprops '(org-occur match)))
  18161. ((functionp type)
  18162. (org-let lprops '(funcall type match)))
  18163. ((fboundp type)
  18164. (org-let lprops '(funcall type match)))
  18165. (t (error "Invalid custom agenda command type %s" type))))
  18166. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18167. ((equal keys "C")
  18168. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18169. (customize-variable 'org-agenda-custom-commands))
  18170. ((equal keys "a") (call-interactively 'org-agenda-list))
  18171. ((equal keys "t") (call-interactively 'org-todo-list))
  18172. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18173. ((equal keys "m") (call-interactively 'org-tags-view))
  18174. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18175. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18176. ((equal keys "L")
  18177. (unless (org-mode-p)
  18178. (error "This is not an Org-mode file"))
  18179. (unless restriction
  18180. (put 'org-agenda-files 'org-restrict (list bfn))
  18181. (org-call-with-arg 'org-timeline arg)))
  18182. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18183. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18184. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18185. (t (error "Invalid agenda key"))))))
  18186. (defun org-agenda-normalize-custom-commands (cmds)
  18187. (delq nil
  18188. (mapcar
  18189. (lambda (x)
  18190. (cond ((stringp (cdr x)) nil)
  18191. ((stringp (nth 1 x)) x)
  18192. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18193. (t (cons (car x) (cons "" (cdr x))))))
  18194. cmds)))
  18195. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18196. "The user interface for selecting an agenda command."
  18197. (catch 'exit
  18198. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18199. (restrict-ok (and bfn (org-mode-p)))
  18200. (region-p (org-region-active-p))
  18201. (custom org-agenda-custom-commands)
  18202. (selstring "")
  18203. restriction second-time
  18204. c entry key type match prefixes rmheader header-end custom1 desc)
  18205. (save-window-excursion
  18206. (delete-other-windows)
  18207. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18208. (erase-buffer)
  18209. (insert (eval-when-compile
  18210. (let ((header
  18211. "
  18212. Press key for an agenda command: < Buffer,subtree/region restriction
  18213. -------------------------------- > Remove restriction
  18214. a Agenda for current week or day e Export agenda views
  18215. t List of all TODO entries T Entries with special TODO kwd
  18216. m Match a TAGS query M Like m, but only TODO entries
  18217. L Timeline for current buffer # List stuck projects (!=configure)
  18218. / Multi-occur C Configure custom agenda commands
  18219. ")
  18220. (start 0))
  18221. (while (string-match
  18222. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18223. header start)
  18224. (setq start (match-end 0))
  18225. (add-text-properties (match-beginning 2) (match-end 2)
  18226. '(face bold) header))
  18227. header)))
  18228. (setq header-end (move-marker (make-marker) (point)))
  18229. (while t
  18230. (setq custom1 custom)
  18231. (when (eq rmheader t)
  18232. (goto-line 1)
  18233. (re-search-forward ":" nil t)
  18234. (delete-region (match-end 0) (point-at-eol))
  18235. (forward-char 1)
  18236. (looking-at "-+")
  18237. (delete-region (match-end 0) (point-at-eol))
  18238. (move-marker header-end (match-end 0)))
  18239. (goto-char header-end)
  18240. (delete-region (point) (point-max))
  18241. (while (setq entry (pop custom1))
  18242. (setq key (car entry) desc (nth 1 entry)
  18243. type (nth 2 entry) match (nth 3 entry))
  18244. (if (> (length key) 1)
  18245. (add-to-list 'prefixes (string-to-char key))
  18246. (insert
  18247. (format
  18248. "\n%-4s%-14s: %s"
  18249. (org-add-props (copy-sequence key)
  18250. '(face bold))
  18251. (cond
  18252. ((string-match "\\S-" desc) desc)
  18253. ((eq type 'agenda) "Agenda for current week or day")
  18254. ((eq type 'alltodo) "List of all TODO entries")
  18255. ((eq type 'stuck) "List of stuck projects")
  18256. ((eq type 'todo) "TODO keyword")
  18257. ((eq type 'tags) "Tags query")
  18258. ((eq type 'tags-todo) "Tags (TODO)")
  18259. ((eq type 'tags-tree) "Tags tree")
  18260. ((eq type 'todo-tree) "TODO kwd tree")
  18261. ((eq type 'occur-tree) "Occur tree")
  18262. ((functionp type) (if (symbolp type)
  18263. (symbol-name type)
  18264. "Lambda expression"))
  18265. (t "???"))
  18266. (cond
  18267. ((stringp match)
  18268. (org-add-props match nil 'face 'org-warning))
  18269. (match
  18270. (format "set of %d commands" (length match)))
  18271. (t ""))))))
  18272. (when prefixes
  18273. (mapc (lambda (x)
  18274. (insert
  18275. (format "\n%s %s"
  18276. (org-add-props (char-to-string x)
  18277. nil 'face 'bold)
  18278. (or (cdr (assoc (concat selstring (char-to-string x))
  18279. prefix-descriptions))
  18280. "Prefix key"))))
  18281. prefixes))
  18282. (goto-char (point-min))
  18283. (when (fboundp 'fit-window-to-buffer)
  18284. (if second-time
  18285. (if (not (pos-visible-in-window-p (point-max)))
  18286. (fit-window-to-buffer))
  18287. (setq second-time t)
  18288. (fit-window-to-buffer)))
  18289. (message "Press key for agenda command%s:"
  18290. (if (or restrict-ok org-agenda-overriding-restriction)
  18291. (if org-agenda-overriding-restriction
  18292. " (restriction lock active)"
  18293. (if restriction
  18294. (format " (restricted to %s)" restriction)
  18295. " (unrestricted)"))
  18296. ""))
  18297. (setq c (read-char-exclusive))
  18298. (message "")
  18299. (cond
  18300. ((assoc (char-to-string c) custom)
  18301. (setq selstring (concat selstring (char-to-string c)))
  18302. (throw 'exit (cons selstring restriction)))
  18303. ((memq c prefixes)
  18304. (setq selstring (concat selstring (char-to-string c))
  18305. prefixes nil
  18306. rmheader (or rmheader t)
  18307. custom (delq nil (mapcar
  18308. (lambda (x)
  18309. (if (or (= (length (car x)) 1)
  18310. (/= (string-to-char (car x)) c))
  18311. nil
  18312. (cons (substring (car x) 1) (cdr x))))
  18313. custom))))
  18314. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18315. (message "Restriction is only possible in Org-mode buffers")
  18316. (ding) (sit-for 1))
  18317. ((eq c ?1)
  18318. (org-agenda-remove-restriction-lock 'noupdate)
  18319. (setq restriction 'buffer))
  18320. ((eq c ?0)
  18321. (org-agenda-remove-restriction-lock 'noupdate)
  18322. (setq restriction (if region-p 'region 'subtree)))
  18323. ((eq c ?<)
  18324. (org-agenda-remove-restriction-lock 'noupdate)
  18325. (setq restriction
  18326. (cond
  18327. ((eq restriction 'buffer)
  18328. (if region-p 'region 'subtree))
  18329. ((memq restriction '(subtree region))
  18330. nil)
  18331. (t 'buffer))))
  18332. ((eq c ?>)
  18333. (org-agenda-remove-restriction-lock 'noupdate)
  18334. (setq restriction nil))
  18335. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18336. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18337. ((and (> (length selstring) 0) (eq c ?\d))
  18338. (delete-window)
  18339. (org-agenda-get-restriction-and-command prefix-descriptions))
  18340. ((equal c ?q) (error "Abort"))
  18341. (t (error "Invalid key %c" c))))))))
  18342. (defun org-run-agenda-series (name series)
  18343. (org-prepare-agenda name)
  18344. (let* ((org-agenda-multi t)
  18345. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18346. (cmds (car series))
  18347. (gprops (nth 1 series))
  18348. match ;; The byte compiler incorrectly complains about this. Keep it!
  18349. cmd type lprops)
  18350. (while (setq cmd (pop cmds))
  18351. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18352. (cond
  18353. ((eq type 'agenda)
  18354. (org-let2 gprops lprops
  18355. '(call-interactively 'org-agenda-list)))
  18356. ((eq type 'alltodo)
  18357. (org-let2 gprops lprops
  18358. '(call-interactively 'org-todo-list)))
  18359. ((eq type 'stuck)
  18360. (org-let2 gprops lprops
  18361. '(call-interactively 'org-agenda-list-stuck-projects)))
  18362. ((eq type 'tags)
  18363. (org-let2 gprops lprops
  18364. '(org-tags-view current-prefix-arg match)))
  18365. ((eq type 'tags-todo)
  18366. (org-let2 gprops lprops
  18367. '(org-tags-view '(4) match)))
  18368. ((eq type 'todo)
  18369. (org-let2 gprops lprops
  18370. '(org-todo-list match)))
  18371. ((fboundp type)
  18372. (org-let2 gprops lprops
  18373. '(funcall type match)))
  18374. (t (error "Invalid type in command series"))))
  18375. (widen)
  18376. (setq org-agenda-redo-command redo)
  18377. (goto-char (point-min)))
  18378. (org-finalize-agenda))
  18379. ;;;###autoload
  18380. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18381. "Run an agenda command in batch mode and send the result to STDOUT.
  18382. If CMD-KEY is a string of length 1, it is used as a key in
  18383. `org-agenda-custom-commands' and triggers this command. If it is a
  18384. longer string it is used as a tags/todo match string.
  18385. Paramters are alternating variable names and values that will be bound
  18386. before running the agenda command."
  18387. (let (pars)
  18388. (while parameters
  18389. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18390. (if (> (length cmd-key) 2)
  18391. (eval (list 'let (nreverse pars)
  18392. (list 'org-tags-view nil cmd-key)))
  18393. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18394. (set-buffer org-agenda-buffer-name)
  18395. (princ (org-encode-for-stdout (buffer-string)))))
  18396. (defun org-encode-for-stdout (string)
  18397. (if (fboundp 'encode-coding-string)
  18398. (encode-coding-string string buffer-file-coding-system)
  18399. string))
  18400. (defvar org-agenda-info nil)
  18401. ;;;###autoload
  18402. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18403. "Run an agenda command in batch mode and send the result to STDOUT.
  18404. If CMD-KEY is a string of length 1, it is used as a key in
  18405. `org-agenda-custom-commands' and triggers this command. If it is a
  18406. longer string it is used as a tags/todo match string.
  18407. Paramters are alternating variable names and values that will be bound
  18408. before running the agenda command.
  18409. The output gives a line for each selected agenda item. Each
  18410. item is a list of comma-separated values, like this:
  18411. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18412. category The category of the item
  18413. head The headline, without TODO kwd, TAGS and PRIORITY
  18414. type The type of the agenda entry, can be
  18415. todo selected in TODO match
  18416. tagsmatch selected in tags match
  18417. diary imported from diary
  18418. deadline a deadline on given date
  18419. scheduled scheduled on given date
  18420. timestamp entry has timestamp on given date
  18421. closed entry was closed on given date
  18422. upcoming-deadline warning about deadline
  18423. past-scheduled forwarded scheduled item
  18424. block entry has date block including g. date
  18425. todo The todo keyword, if any
  18426. tags All tags including inherited ones, separated by colons
  18427. date The relevant date, like 2007-2-14
  18428. time The time, like 15:00-16:50
  18429. extra Sting with extra planning info
  18430. priority-l The priority letter if any was given
  18431. priority-n The computed numerical priority
  18432. agenda-day The day in the agenda where this is listed"
  18433. (let (pars)
  18434. (while parameters
  18435. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18436. (push (list 'org-agenda-remove-tags t) pars)
  18437. (if (> (length cmd-key) 2)
  18438. (eval (list 'let (nreverse pars)
  18439. (list 'org-tags-view nil cmd-key)))
  18440. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18441. (set-buffer org-agenda-buffer-name)
  18442. (let* ((lines (org-split-string (buffer-string) "\n"))
  18443. line)
  18444. (while (setq line (pop lines))
  18445. (catch 'next
  18446. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18447. (setq org-agenda-info
  18448. (org-fix-agenda-info (text-properties-at 0 line)))
  18449. (princ
  18450. (org-encode-for-stdout
  18451. (mapconcat 'org-agenda-export-csv-mapper
  18452. '(org-category txt type todo tags date time-of-day extra
  18453. priority-letter priority agenda-day)
  18454. ",")))
  18455. (princ "\n"))))))
  18456. (defun org-fix-agenda-info (props)
  18457. "Make sure all properties on an agenda item have a canonical form,
  18458. so the export commands can easily use it."
  18459. (let (tmp re)
  18460. (when (setq tmp (plist-get props 'tags))
  18461. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18462. (when (setq tmp (plist-get props 'date))
  18463. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18464. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18465. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18466. (setq tmp (calendar-date-string tmp)))
  18467. (setq props (plist-put props 'date tmp)))
  18468. (when (setq tmp (plist-get props 'day))
  18469. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18470. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18471. (setq tmp (calendar-date-string tmp)))
  18472. (setq props (plist-put props 'day tmp))
  18473. (setq props (plist-put props 'agenda-day tmp)))
  18474. (when (setq tmp (plist-get props 'txt))
  18475. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18476. (plist-put props 'priority-letter (match-string 1 tmp))
  18477. (setq tmp (replace-match "" t t tmp)))
  18478. (when (and (setq re (plist-get props 'org-todo-regexp))
  18479. (setq re (concat "\\`\\.*" re " ?"))
  18480. (string-match re tmp))
  18481. (plist-put props 'todo (match-string 1 tmp))
  18482. (setq tmp (replace-match "" t t tmp)))
  18483. (plist-put props 'txt tmp)))
  18484. props)
  18485. (defun org-agenda-export-csv-mapper (prop)
  18486. (let ((res (plist-get org-agenda-info prop)))
  18487. (setq res
  18488. (cond
  18489. ((not res) "")
  18490. ((stringp res) res)
  18491. (t (prin1-to-string res))))
  18492. (while (string-match "," res)
  18493. (setq res (replace-match ";" t t res)))
  18494. (org-trim res)))
  18495. ;;;###autoload
  18496. (defun org-store-agenda-views (&rest parameters)
  18497. (interactive)
  18498. (eval (list 'org-batch-store-agenda-views)))
  18499. ;; FIXME, why is this a macro?????
  18500. ;;;###autoload
  18501. (defmacro org-batch-store-agenda-views (&rest parameters)
  18502. "Run all custom agenda commands that have a file argument."
  18503. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18504. (pop-up-frames nil)
  18505. (dir default-directory)
  18506. pars cmd thiscmdkey files opts)
  18507. (while parameters
  18508. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18509. (setq pars (reverse pars))
  18510. (save-window-excursion
  18511. (while cmds
  18512. (setq cmd (pop cmds)
  18513. thiscmdkey (car cmd)
  18514. opts (nth 4 cmd)
  18515. files (nth 5 cmd))
  18516. (if (stringp files) (setq files (list files)))
  18517. (when files
  18518. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18519. (list 'org-agenda nil thiscmdkey)))
  18520. (set-buffer org-agenda-buffer-name)
  18521. (while files
  18522. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18523. (list 'org-write-agenda
  18524. (expand-file-name (pop files) dir) t))))
  18525. (and (get-buffer org-agenda-buffer-name)
  18526. (kill-buffer org-agenda-buffer-name)))))))
  18527. (defun org-write-agenda (file &optional nosettings)
  18528. "Write the current buffer (an agenda view) as a file.
  18529. Depending on the extension of the file name, plain text (.txt),
  18530. HTML (.html or .htm) or Postscript (.ps) is produced.
  18531. If NOSETTINGS is given, do not scope the settings of
  18532. `org-agenda-exporter-settings' into the export commands. This is used when
  18533. the settings have already been scoped and we do not wish to overrule other,
  18534. higher priority settings."
  18535. (interactive "FWrite agenda to file: ")
  18536. (if (not (file-writable-p file))
  18537. (error "Cannot write agenda to file %s" file))
  18538. (cond
  18539. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18540. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18541. (org-let (if nosettings nil org-agenda-exporter-settings)
  18542. '(save-excursion
  18543. (save-window-excursion
  18544. (cond
  18545. ((string-match "\\.html?\\'" file)
  18546. (set-buffer (htmlize-buffer (current-buffer)))
  18547. (when (and org-agenda-export-html-style
  18548. (string-match "<style>" org-agenda-export-html-style))
  18549. ;; replace <style> section with org-agenda-export-html-style
  18550. (goto-char (point-min))
  18551. (kill-region (- (search-forward "<style") 6)
  18552. (search-forward "</style>"))
  18553. (insert org-agenda-export-html-style))
  18554. (write-file file)
  18555. (kill-buffer (current-buffer))
  18556. (message "HTML written to %s" file))
  18557. ((string-match "\\.ps\\'" file)
  18558. (ps-print-buffer-with-faces file)
  18559. (message "Postscript written to %s" file))
  18560. (t
  18561. (let ((bs (buffer-string)))
  18562. (find-file file)
  18563. (insert bs)
  18564. (save-buffer 0)
  18565. (kill-buffer (current-buffer))
  18566. (message "Plain text written to %s" file))))))
  18567. (set-buffer org-agenda-buffer-name)))
  18568. (defmacro org-no-read-only (&rest body)
  18569. "Inhibit read-only for BODY."
  18570. `(let ((inhibit-read-only t)) ,@body))
  18571. (defun org-check-for-org-mode ()
  18572. "Make sure current buffer is in org-mode. Error if not."
  18573. (or (org-mode-p)
  18574. (error "Cannot execute org-mode agenda command on buffer in %s."
  18575. major-mode)))
  18576. (defun org-fit-agenda-window ()
  18577. "Fit the window to the buffer size."
  18578. (and (memq org-agenda-window-setup '(reorganize-frame))
  18579. (fboundp 'fit-window-to-buffer)
  18580. (fit-window-to-buffer
  18581. nil
  18582. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18583. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18584. ;;; Agenda file list
  18585. (defun org-agenda-files (&optional unrestricted)
  18586. "Get the list of agenda files.
  18587. Optional UNRESTRICTED means return the full list even if a restriction
  18588. is currently in place."
  18589. (let ((files
  18590. (cond
  18591. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18592. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18593. ((listp org-agenda-files) org-agenda-files)
  18594. (t (error "Invalid value of `org-agenda-files'")))))
  18595. (setq files (apply 'append
  18596. (mapcar (lambda (f)
  18597. (if (file-directory-p f)
  18598. (directory-files f t
  18599. org-agenda-file-regexp)
  18600. (list f)))
  18601. files)))
  18602. (if org-agenda-skip-unavailable-files
  18603. (delq nil
  18604. (mapcar (function
  18605. (lambda (file)
  18606. (and (file-readable-p file) file)))
  18607. files))
  18608. files))) ; `org-check-agenda-file' will remove them from the list
  18609. (defun org-edit-agenda-file-list ()
  18610. "Edit the list of agenda files.
  18611. Depending on setup, this either uses customize to edit the variable
  18612. `org-agenda-files', or it visits the file that is holding the list. In the
  18613. latter case, the buffer is set up in a way that saving it automatically kills
  18614. the buffer and restores the previous window configuration."
  18615. (interactive)
  18616. (if (stringp org-agenda-files)
  18617. (let ((cw (current-window-configuration)))
  18618. (find-file org-agenda-files)
  18619. (org-set-local 'org-window-configuration cw)
  18620. (org-add-hook 'after-save-hook
  18621. (lambda ()
  18622. (set-window-configuration
  18623. (prog1 org-window-configuration
  18624. (kill-buffer (current-buffer))))
  18625. (org-install-agenda-files-menu)
  18626. (message "New agenda file list installed"))
  18627. nil 'local)
  18628. (message "%s" (substitute-command-keys
  18629. "Edit list and finish with \\[save-buffer]")))
  18630. (customize-variable 'org-agenda-files)))
  18631. (defun org-store-new-agenda-file-list (list)
  18632. "Set new value for the agenda file list and save it correcly."
  18633. (if (stringp org-agenda-files)
  18634. (let ((f org-agenda-files) b)
  18635. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18636. (with-temp-file f
  18637. (insert (mapconcat 'identity list "\n") "\n")))
  18638. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18639. (setq org-agenda-files list)
  18640. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18641. (defun org-read-agenda-file-list ()
  18642. "Read the list of agenda files from a file."
  18643. (when (stringp org-agenda-files)
  18644. (with-temp-buffer
  18645. (insert-file-contents org-agenda-files)
  18646. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18647. ;;;###autoload
  18648. (defun org-cycle-agenda-files ()
  18649. "Cycle through the files in `org-agenda-files'.
  18650. If the current buffer visits an agenda file, find the next one in the list.
  18651. If the current buffer does not, find the first agenda file."
  18652. (interactive)
  18653. (let* ((fs (org-agenda-files t))
  18654. (files (append fs (list (car fs))))
  18655. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18656. file)
  18657. (unless files (error "No agenda files"))
  18658. (catch 'exit
  18659. (while (setq file (pop files))
  18660. (if (equal (file-truename file) tcf)
  18661. (when (car files)
  18662. (find-file (car files))
  18663. (throw 'exit t))))
  18664. (find-file (car fs)))
  18665. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18666. (defun org-agenda-file-to-front (&optional to-end)
  18667. "Move/add the current file to the top of the agenda file list.
  18668. If the file is not present in the list, it is added to the front. If it is
  18669. present, it is moved there. With optional argument TO-END, add/move to the
  18670. end of the list."
  18671. (interactive "P")
  18672. (let ((org-agenda-skip-unavailable-files nil)
  18673. (file-alist (mapcar (lambda (x)
  18674. (cons (file-truename x) x))
  18675. (org-agenda-files t)))
  18676. (ctf (file-truename buffer-file-name))
  18677. x had)
  18678. (setq x (assoc ctf file-alist) had x)
  18679. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18680. (if to-end
  18681. (setq file-alist (append (delq x file-alist) (list x)))
  18682. (setq file-alist (cons x (delq x file-alist))))
  18683. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18684. (org-install-agenda-files-menu)
  18685. (message "File %s to %s of agenda file list"
  18686. (if had "moved" "added") (if to-end "end" "front"))))
  18687. (defun org-remove-file (&optional file)
  18688. "Remove current file from the list of files in variable `org-agenda-files'.
  18689. These are the files which are being checked for agenda entries.
  18690. Optional argument FILE means, use this file instead of the current."
  18691. (interactive)
  18692. (let* ((org-agenda-skip-unavailable-files nil)
  18693. (file (or file buffer-file-name))
  18694. (true-file (file-truename file))
  18695. (afile (abbreviate-file-name file))
  18696. (files (delq nil (mapcar
  18697. (lambda (x)
  18698. (if (equal true-file
  18699. (file-truename x))
  18700. nil x))
  18701. (org-agenda-files t)))))
  18702. (if (not (= (length files) (length (org-agenda-files t))))
  18703. (progn
  18704. (org-store-new-agenda-file-list files)
  18705. (org-install-agenda-files-menu)
  18706. (message "Removed file: %s" afile))
  18707. (message "File was not in list: %s (not removed)" afile))))
  18708. (defun org-file-menu-entry (file)
  18709. (vector file (list 'find-file file) t))
  18710. (defun org-check-agenda-file (file)
  18711. "Make sure FILE exists. If not, ask user what to do."
  18712. (when (not (file-exists-p file))
  18713. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18714. (abbreviate-file-name file))
  18715. (let ((r (downcase (read-char-exclusive))))
  18716. (cond
  18717. ((equal r ?r)
  18718. (org-remove-file file)
  18719. (throw 'nextfile t))
  18720. (t (error "Abort"))))))
  18721. ;;; Agenda prepare and finalize
  18722. (defvar org-agenda-multi nil) ; dynammically scoped
  18723. (defvar org-agenda-buffer-name "*Org Agenda*")
  18724. (defvar org-pre-agenda-window-conf nil)
  18725. (defvar org-agenda-name nil)
  18726. (defun org-prepare-agenda (&optional name)
  18727. (setq org-todo-keywords-for-agenda nil)
  18728. (setq org-done-keywords-for-agenda nil)
  18729. (if org-agenda-multi
  18730. (progn
  18731. (setq buffer-read-only nil)
  18732. (goto-char (point-max))
  18733. (unless (or (bobp) org-agenda-compact-blocks)
  18734. (insert "\n" (make-string (window-width) ?=) "\n"))
  18735. (narrow-to-region (point) (point-max)))
  18736. (org-agenda-reset-markers)
  18737. (org-prepare-agenda-buffers (org-agenda-files))
  18738. (setq org-todo-keywords-for-agenda
  18739. (org-uniquify org-todo-keywords-for-agenda))
  18740. (setq org-done-keywords-for-agenda
  18741. (org-uniquify org-done-keywords-for-agenda))
  18742. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18743. (awin (get-buffer-window abuf)))
  18744. (cond
  18745. ((equal (current-buffer) abuf) nil)
  18746. (awin (select-window awin))
  18747. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18748. ((equal org-agenda-window-setup 'current-window)
  18749. (switch-to-buffer abuf))
  18750. ((equal org-agenda-window-setup 'other-window)
  18751. (org-switch-to-buffer-other-window abuf))
  18752. ((equal org-agenda-window-setup 'other-frame)
  18753. (switch-to-buffer-other-frame abuf))
  18754. ((equal org-agenda-window-setup 'reorganize-frame)
  18755. (delete-other-windows)
  18756. (org-switch-to-buffer-other-window abuf))))
  18757. (setq buffer-read-only nil)
  18758. (erase-buffer)
  18759. (org-agenda-mode)
  18760. (and name (not org-agenda-name)
  18761. (org-set-local 'org-agenda-name name)))
  18762. (setq buffer-read-only nil))
  18763. (defun org-finalize-agenda ()
  18764. "Finishing touch for the agenda buffer, called just before displaying it."
  18765. (unless org-agenda-multi
  18766. (save-excursion
  18767. (let ((inhibit-read-only t))
  18768. (goto-char (point-min))
  18769. (while (org-activate-bracket-links (point-max))
  18770. (add-text-properties (match-beginning 0) (match-end 0)
  18771. '(face org-link)))
  18772. (org-agenda-align-tags)
  18773. (unless org-agenda-with-colors
  18774. (remove-text-properties (point-min) (point-max) '(face nil))))
  18775. (if (and (boundp 'org-overriding-columns-format)
  18776. org-overriding-columns-format)
  18777. (org-set-local 'org-overriding-columns-format
  18778. org-overriding-columns-format))
  18779. (if (and (boundp 'org-agenda-view-columns-initially)
  18780. org-agenda-view-columns-initially)
  18781. (org-agenda-columns))
  18782. (when org-agenda-fontify-priorities
  18783. (org-fontify-priorities))
  18784. (run-hooks 'org-finalize-agenda-hook)
  18785. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18786. )))
  18787. (defun org-fontify-priorities ()
  18788. "Make highest priority lines bold, and lowest italic."
  18789. (interactive)
  18790. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18791. (org-delete-overlay o)))
  18792. (org-overlays-in (point-min) (point-max)))
  18793. (save-excursion
  18794. (let ((inhibit-read-only t)
  18795. b e p ov h l)
  18796. (goto-char (point-min))
  18797. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18798. (setq h (or (get-char-property (point) 'org-highest-priority)
  18799. org-highest-priority)
  18800. l (or (get-char-property (point) 'org-lowest-priority)
  18801. org-lowest-priority)
  18802. p (string-to-char (match-string 1))
  18803. b (match-beginning 0) e (point-at-eol)
  18804. ov (org-make-overlay b e))
  18805. (org-overlay-put
  18806. ov 'face
  18807. (cond ((listp org-agenda-fontify-priorities)
  18808. (cdr (assoc p org-agenda-fontify-priorities)))
  18809. ((equal p l) 'italic)
  18810. ((equal p h) 'bold)))
  18811. (org-overlay-put ov 'org-type 'org-priority)))))
  18812. (defun org-prepare-agenda-buffers (files)
  18813. "Create buffers for all agenda files, protect archived trees and comments."
  18814. (interactive)
  18815. (let ((pa '(:org-archived t))
  18816. (pc '(:org-comment t))
  18817. (pall '(:org-archived t :org-comment t))
  18818. (inhibit-read-only t)
  18819. (rea (concat ":" org-archive-tag ":"))
  18820. bmp file re)
  18821. (save-excursion
  18822. (save-restriction
  18823. (while (setq file (pop files))
  18824. (if (bufferp file)
  18825. (set-buffer file)
  18826. (org-check-agenda-file file)
  18827. (set-buffer (org-get-agenda-file-buffer file)))
  18828. (widen)
  18829. (setq bmp (buffer-modified-p))
  18830. (org-refresh-category-properties)
  18831. (setq org-todo-keywords-for-agenda
  18832. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18833. (setq org-done-keywords-for-agenda
  18834. (append org-done-keywords-for-agenda org-done-keywords))
  18835. (save-excursion
  18836. (remove-text-properties (point-min) (point-max) pall)
  18837. (when org-agenda-skip-archived-trees
  18838. (goto-char (point-min))
  18839. (while (re-search-forward rea nil t)
  18840. (if (org-on-heading-p t)
  18841. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18842. (goto-char (point-min))
  18843. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18844. (while (re-search-forward re nil t)
  18845. (add-text-properties
  18846. (match-beginning 0) (org-end-of-subtree t) pc)))
  18847. (set-buffer-modified-p bmp))))))
  18848. (defvar org-agenda-skip-function nil
  18849. "Function to be called at each match during agenda construction.
  18850. If this function returns nil, the current match should not be skipped.
  18851. Otherwise, the function must return a position from where the search
  18852. should be continued.
  18853. This may also be a Lisp form, it will be evaluated.
  18854. Never set this variable using `setq' or so, because then it will apply
  18855. to all future agenda commands. Instead, bind it with `let' to scope
  18856. it dynamically into the agenda-constructing command. A good way to set
  18857. it is through options in org-agenda-custom-commands.")
  18858. (defun org-agenda-skip ()
  18859. "Throw to `:skip' in places that should be skipped.
  18860. Also moves point to the end of the skipped region, so that search can
  18861. continue from there."
  18862. (let ((p (point-at-bol)) to fp)
  18863. (and org-agenda-skip-archived-trees
  18864. (get-text-property p :org-archived)
  18865. (org-end-of-subtree t)
  18866. (throw :skip t))
  18867. (and (get-text-property p :org-comment)
  18868. (org-end-of-subtree t)
  18869. (throw :skip t))
  18870. (if (equal (char-after p) ?#) (throw :skip t))
  18871. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18872. (consp org-agenda-skip-function))
  18873. (setq to (save-excursion
  18874. (save-match-data
  18875. (if fp
  18876. (funcall org-agenda-skip-function)
  18877. (eval org-agenda-skip-function))))))
  18878. (goto-char to)
  18879. (throw :skip t))))
  18880. (defvar org-agenda-markers nil
  18881. "List of all currently active markers created by `org-agenda'.")
  18882. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18883. "Creation time of the last agenda marker.")
  18884. (defun org-agenda-new-marker (&optional pos)
  18885. "Return a new agenda marker.
  18886. Org-mode keeps a list of these markers and resets them when they are
  18887. no longer in use."
  18888. (let ((m (copy-marker (or pos (point)))))
  18889. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18890. (push m org-agenda-markers)
  18891. m))
  18892. (defun org-agenda-reset-markers ()
  18893. "Reset markers created by `org-agenda'."
  18894. (while org-agenda-markers
  18895. (move-marker (pop org-agenda-markers) nil)))
  18896. (defun org-get-agenda-file-buffer (file)
  18897. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18898. it to the list of buffers which might be released later."
  18899. (let ((buf (org-find-base-buffer-visiting file)))
  18900. (if buf
  18901. buf ; just return it
  18902. ;; Make a new buffer and remember it
  18903. (setq buf (find-file-noselect file))
  18904. (if buf (push buf org-agenda-new-buffers))
  18905. buf)))
  18906. (defun org-release-buffers (blist)
  18907. "Release all buffers in list, asking the user for confirmation when needed.
  18908. When a buffer is unmodified, it is just killed. When modified, it is saved
  18909. \(if the user agrees) and then killed."
  18910. (let (buf file)
  18911. (while (setq buf (pop blist))
  18912. (setq file (buffer-file-name buf))
  18913. (when (and (buffer-modified-p buf)
  18914. file
  18915. (y-or-n-p (format "Save file %s? " file)))
  18916. (with-current-buffer buf (save-buffer)))
  18917. (kill-buffer buf))))
  18918. (defun org-get-category (&optional pos)
  18919. "Get the category applying to position POS."
  18920. (get-text-property (or pos (point)) 'org-category))
  18921. ;;; Agenda timeline
  18922. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18923. (defun org-timeline (&optional include-all)
  18924. "Show a time-sorted view of the entries in the current org file.
  18925. Only entries with a time stamp of today or later will be listed. With
  18926. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18927. under the current date.
  18928. If the buffer contains an active region, only check the region for
  18929. dates."
  18930. (interactive "P")
  18931. (require 'calendar)
  18932. (org-compile-prefix-format 'timeline)
  18933. (org-set-sorting-strategy 'timeline)
  18934. (let* ((dopast t)
  18935. (dotodo include-all)
  18936. (doclosed org-agenda-show-log)
  18937. (entry buffer-file-name)
  18938. (date (calendar-current-date))
  18939. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18940. (end (if (org-region-active-p) (region-end) (point-max)))
  18941. (day-numbers (org-get-all-dates beg end 'no-ranges
  18942. t doclosed ; always include today
  18943. org-timeline-show-empty-dates))
  18944. (org-deadline-warning-days 0)
  18945. (org-agenda-only-exact-dates t)
  18946. (today (time-to-days (current-time)))
  18947. (past t)
  18948. args
  18949. s e rtn d emptyp)
  18950. (setq org-agenda-redo-command
  18951. (list 'progn
  18952. (list 'org-switch-to-buffer-other-window (current-buffer))
  18953. (list 'org-timeline (list 'quote include-all))))
  18954. (if (not dopast)
  18955. ;; Remove past dates from the list of dates.
  18956. (setq day-numbers (delq nil (mapcar (lambda(x)
  18957. (if (>= x today) x nil))
  18958. day-numbers))))
  18959. (org-prepare-agenda (concat "Timeline "
  18960. (file-name-nondirectory buffer-file-name)))
  18961. (if doclosed (push :closed args))
  18962. (push :timestamp args)
  18963. (push :deadline args)
  18964. (push :scheduled args)
  18965. (push :sexp args)
  18966. (if dotodo (push :todo args))
  18967. (while (setq d (pop day-numbers))
  18968. (if (and (listp d) (eq (car d) :omitted))
  18969. (progn
  18970. (setq s (point))
  18971. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18972. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18973. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18974. (if (and (>= d today)
  18975. dopast
  18976. past)
  18977. (progn
  18978. (setq past nil)
  18979. (insert (make-string 79 ?-) "\n")))
  18980. (setq date (calendar-gregorian-from-absolute d))
  18981. (setq s (point))
  18982. (setq rtn (and (not emptyp)
  18983. (apply 'org-agenda-get-day-entries entry
  18984. date args)))
  18985. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18986. (progn
  18987. (insert
  18988. (if (stringp org-agenda-format-date)
  18989. (format-time-string org-agenda-format-date
  18990. (org-time-from-absolute date))
  18991. (funcall org-agenda-format-date date))
  18992. "\n")
  18993. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18994. (put-text-property s (1- (point)) 'org-date-line t)
  18995. (if (equal d today)
  18996. (put-text-property s (1- (point)) 'org-today t))
  18997. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18998. (put-text-property s (1- (point)) 'day d)))))
  18999. (goto-char (point-min))
  19000. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  19001. (point-min)))
  19002. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  19003. (org-finalize-agenda)
  19004. (setq buffer-read-only t)))
  19005. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  19006. "Return a list of all relevant day numbers from BEG to END buffer positions.
  19007. If NO-RANGES is non-nil, include only the start and end dates of a range,
  19008. not every single day in the range. If FORCE-TODAY is non-nil, make
  19009. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  19010. inactive time stamps (those in square brackets) are included.
  19011. When EMPTY is non-nil, also include days without any entries."
  19012. (let ((re (concat
  19013. (if pre-re pre-re "")
  19014. (if inactive org-ts-regexp-both org-ts-regexp)))
  19015. dates dates1 date day day1 day2 ts1 ts2)
  19016. (if force-today
  19017. (setq dates (list (time-to-days (current-time)))))
  19018. (save-excursion
  19019. (goto-char beg)
  19020. (while (re-search-forward re end t)
  19021. (setq day (time-to-days (org-time-string-to-time
  19022. (substring (match-string 1) 0 10))))
  19023. (or (memq day dates) (push day dates)))
  19024. (unless no-ranges
  19025. (goto-char beg)
  19026. (while (re-search-forward org-tr-regexp end t)
  19027. (setq ts1 (substring (match-string 1) 0 10)
  19028. ts2 (substring (match-string 2) 0 10)
  19029. day1 (time-to-days (org-time-string-to-time ts1))
  19030. day2 (time-to-days (org-time-string-to-time ts2)))
  19031. (while (< (setq day1 (1+ day1)) day2)
  19032. (or (memq day1 dates) (push day1 dates)))))
  19033. (setq dates (sort dates '<))
  19034. (when empty
  19035. (while (setq day (pop dates))
  19036. (setq day2 (car dates))
  19037. (push day dates1)
  19038. (when (and day2 empty)
  19039. (if (or (eq empty t)
  19040. (and (numberp empty) (<= (- day2 day) empty)))
  19041. (while (< (setq day (1+ day)) day2)
  19042. (push (list day) dates1))
  19043. (push (cons :omitted (- day2 day)) dates1))))
  19044. (setq dates (nreverse dates1)))
  19045. dates)))
  19046. ;;; Agenda Daily/Weekly
  19047. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  19048. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19049. (defvar org-agenda-last-arguments nil
  19050. "The arguments of the previous call to org-agenda")
  19051. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19052. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19053. (defvar org-include-all-loc nil) ; local variable
  19054. (defvar org-agenda-remove-date nil) ; dynamically scoped
  19055. ;;;###autoload
  19056. (defun org-agenda-list (&optional include-all start-day ndays)
  19057. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19058. The view will be for the current day or week, but from the overview buffer
  19059. you will be able to go to other days/weeks.
  19060. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19061. all unfinished TODO items will also be shown, before the agenda.
  19062. This feature is considered obsolete, please use the TODO list or a block
  19063. agenda instead.
  19064. With a numeric prefix argument in an interactive call, the agenda will
  19065. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19066. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19067. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19068. given in `org-agenda-start-on-weekday'."
  19069. (interactive "P")
  19070. (if (and (integerp include-all) (> include-all 0))
  19071. (setq ndays include-all include-all nil))
  19072. (setq ndays (or ndays org-agenda-ndays)
  19073. start-day (or start-day org-agenda-start-day))
  19074. (if org-agenda-overriding-arguments
  19075. (setq include-all (car org-agenda-overriding-arguments)
  19076. start-day (nth 1 org-agenda-overriding-arguments)
  19077. ndays (nth 2 org-agenda-overriding-arguments)))
  19078. (if (stringp start-day)
  19079. ;; Convert to an absolute day number
  19080. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19081. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19082. (org-compile-prefix-format 'agenda)
  19083. (org-set-sorting-strategy 'agenda)
  19084. (require 'calendar)
  19085. (let* ((org-agenda-start-on-weekday
  19086. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19087. org-agenda-start-on-weekday nil))
  19088. (thefiles (org-agenda-files))
  19089. (files thefiles)
  19090. (today (time-to-days
  19091. (time-subtract (current-time)
  19092. (list 0 (* 3600 org-extend-today-until) 0))))
  19093. (sd (or start-day today))
  19094. (start (if (or (null org-agenda-start-on-weekday)
  19095. (< org-agenda-ndays 7))
  19096. sd
  19097. (let* ((nt (calendar-day-of-week
  19098. (calendar-gregorian-from-absolute sd)))
  19099. (n1 org-agenda-start-on-weekday)
  19100. (d (- nt n1)))
  19101. (- sd (+ (if (< d 0) 7 0) d)))))
  19102. (day-numbers (list start))
  19103. (day-cnt 0)
  19104. (inhibit-redisplay (not debug-on-error))
  19105. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19106. (setq org-agenda-redo-command
  19107. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19108. ;; Make the list of days
  19109. (setq ndays (or ndays org-agenda-ndays)
  19110. nd ndays)
  19111. (while (> ndays 1)
  19112. (push (1+ (car day-numbers)) day-numbers)
  19113. (setq ndays (1- ndays)))
  19114. (setq day-numbers (nreverse day-numbers))
  19115. (org-prepare-agenda "Day/Week")
  19116. (org-set-local 'org-starting-day (car day-numbers))
  19117. (org-set-local 'org-include-all-loc include-all)
  19118. (org-set-local 'org-agenda-span
  19119. (org-agenda-ndays-to-span nd))
  19120. (when (and (or include-all org-agenda-include-all-todo)
  19121. (member today day-numbers))
  19122. (setq files thefiles
  19123. rtnall nil)
  19124. (while (setq file (pop files))
  19125. (catch 'nextfile
  19126. (org-check-agenda-file file)
  19127. (setq date (calendar-gregorian-from-absolute today)
  19128. rtn (org-agenda-get-day-entries
  19129. file date :todo))
  19130. (setq rtnall (append rtnall rtn))))
  19131. (when rtnall
  19132. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19133. (add-text-properties (point-min) (1- (point))
  19134. (list 'face 'org-agenda-structure))
  19135. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19136. (unless org-agenda-compact-blocks
  19137. (setq s (point))
  19138. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19139. "-agenda:\n")
  19140. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19141. 'org-date-line t)))
  19142. (while (setq d (pop day-numbers))
  19143. (setq date (calendar-gregorian-from-absolute d)
  19144. s (point))
  19145. (if (or (setq todayp (= d today))
  19146. (and (not start-pos) (= d sd)))
  19147. (setq start-pos (point))
  19148. (if (and start-pos (not end-pos))
  19149. (setq end-pos (point))))
  19150. (setq files thefiles
  19151. rtnall nil)
  19152. (while (setq file (pop files))
  19153. (catch 'nextfile
  19154. (org-check-agenda-file file)
  19155. (if org-agenda-show-log
  19156. (setq rtn (org-agenda-get-day-entries
  19157. file date
  19158. :deadline :scheduled :timestamp :sexp :closed))
  19159. (setq rtn (org-agenda-get-day-entries
  19160. file date
  19161. :deadline :scheduled :sexp :timestamp)))
  19162. (setq rtnall (append rtnall rtn))))
  19163. (if org-agenda-include-diary
  19164. (progn
  19165. (require 'diary-lib)
  19166. (setq rtn (org-get-entries-from-diary date))
  19167. (setq rtnall (append rtnall rtn))))
  19168. (if (or rtnall org-agenda-show-all-dates)
  19169. (progn
  19170. (setq day-cnt (1+ day-cnt))
  19171. (insert
  19172. (if (stringp org-agenda-format-date)
  19173. (format-time-string org-agenda-format-date
  19174. (org-time-from-absolute date))
  19175. (funcall org-agenda-format-date date))
  19176. "\n")
  19177. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19178. (put-text-property s (1- (point)) 'org-date-line t)
  19179. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19180. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19181. (if rtnall (insert
  19182. (org-finalize-agenda-entries
  19183. (org-agenda-add-time-grid-maybe
  19184. rtnall nd todayp))
  19185. "\n"))
  19186. (put-text-property s (1- (point)) 'day d)
  19187. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19188. (goto-char (point-min))
  19189. (org-fit-agenda-window)
  19190. (unless (and (pos-visible-in-window-p (point-min))
  19191. (pos-visible-in-window-p (point-max)))
  19192. (goto-char (1- (point-max)))
  19193. (recenter -1)
  19194. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19195. (progn
  19196. (goto-char (or start-pos 1))
  19197. (recenter 1))))
  19198. (goto-char (or start-pos 1))
  19199. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19200. (org-finalize-agenda)
  19201. (setq buffer-read-only t)
  19202. (message "")))
  19203. (defun org-agenda-ndays-to-span (n)
  19204. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19205. ;;; Agenda TODO list
  19206. (defvar org-select-this-todo-keyword nil)
  19207. (defvar org-last-arg nil)
  19208. ;;;###autoload
  19209. (defun org-todo-list (arg)
  19210. "Show all TODO entries from all agenda file in a single list.
  19211. The prefix arg can be used to select a specific TODO keyword and limit
  19212. the list to these. When using \\[universal-argument], you will be prompted
  19213. for a keyword. A numeric prefix directly selects the Nth keyword in
  19214. `org-todo-keywords-1'."
  19215. (interactive "P")
  19216. (require 'calendar)
  19217. (org-compile-prefix-format 'todo)
  19218. (org-set-sorting-strategy 'todo)
  19219. (org-prepare-agenda "TODO")
  19220. (let* ((today (time-to-days (current-time)))
  19221. (date (calendar-gregorian-from-absolute today))
  19222. (kwds org-todo-keywords-for-agenda)
  19223. (completion-ignore-case t)
  19224. (org-select-this-todo-keyword
  19225. (if (stringp arg) arg
  19226. (and arg (integerp arg) (> arg 0)
  19227. (nth (1- arg) kwds))))
  19228. rtn rtnall files file pos)
  19229. (when (equal arg '(4))
  19230. (setq org-select-this-todo-keyword
  19231. (completing-read "Keyword (or KWD1|K2D2|...): "
  19232. (mapcar 'list kwds) nil nil)))
  19233. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19234. (org-set-local 'org-last-arg arg)
  19235. (setq org-agenda-redo-command
  19236. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19237. (setq files (org-agenda-files)
  19238. rtnall nil)
  19239. (while (setq file (pop files))
  19240. (catch 'nextfile
  19241. (org-check-agenda-file file)
  19242. (setq rtn (org-agenda-get-day-entries file date :todo))
  19243. (setq rtnall (append rtnall rtn))))
  19244. (if org-agenda-overriding-header
  19245. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19246. nil 'face 'org-agenda-structure) "\n")
  19247. (insert "Global list of TODO items of type: ")
  19248. (add-text-properties (point-min) (1- (point))
  19249. (list 'face 'org-agenda-structure))
  19250. (setq pos (point))
  19251. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19252. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19253. (setq pos (point))
  19254. (unless org-agenda-multi
  19255. (insert "Available with `N r': (0)ALL")
  19256. (let ((n 0) s)
  19257. (mapc (lambda (x)
  19258. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19259. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19260. (insert "\n "))
  19261. (insert " " s))
  19262. kwds))
  19263. (insert "\n"))
  19264. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19265. (when rtnall
  19266. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19267. (goto-char (point-min))
  19268. (org-fit-agenda-window)
  19269. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19270. (org-finalize-agenda)
  19271. (setq buffer-read-only t)))
  19272. ;;; Agenda tags match
  19273. ;;;###autoload
  19274. (defun org-tags-view (&optional todo-only match)
  19275. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19276. The prefix arg TODO-ONLY limits the search to TODO entries."
  19277. (interactive "P")
  19278. (org-compile-prefix-format 'tags)
  19279. (org-set-sorting-strategy 'tags)
  19280. (let* ((org-tags-match-list-sublevels
  19281. (if todo-only t org-tags-match-list-sublevels))
  19282. (completion-ignore-case t)
  19283. rtn rtnall files file pos matcher
  19284. buffer)
  19285. (setq matcher (org-make-tags-matcher match)
  19286. match (car matcher) matcher (cdr matcher))
  19287. (org-prepare-agenda (concat "TAGS " match))
  19288. (setq org-agenda-redo-command
  19289. (list 'org-tags-view (list 'quote todo-only)
  19290. (list 'if 'current-prefix-arg nil match)))
  19291. (setq files (org-agenda-files)
  19292. rtnall nil)
  19293. (while (setq file (pop files))
  19294. (catch 'nextfile
  19295. (org-check-agenda-file file)
  19296. (setq buffer (if (file-exists-p file)
  19297. (org-get-agenda-file-buffer file)
  19298. (error "No such file %s" file)))
  19299. (if (not buffer)
  19300. ;; If file does not exist, merror message to agenda
  19301. (setq rtn (list
  19302. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19303. rtnall (append rtnall rtn))
  19304. (with-current-buffer buffer
  19305. (unless (org-mode-p)
  19306. (error "Agenda file %s is not in `org-mode'" file))
  19307. (save-excursion
  19308. (save-restriction
  19309. (if org-agenda-restrict
  19310. (narrow-to-region org-agenda-restrict-begin
  19311. org-agenda-restrict-end)
  19312. (widen))
  19313. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19314. (setq rtnall (append rtnall rtn))))))))
  19315. (if org-agenda-overriding-header
  19316. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19317. nil 'face 'org-agenda-structure) "\n")
  19318. (insert "Headlines with TAGS match: ")
  19319. (add-text-properties (point-min) (1- (point))
  19320. (list 'face 'org-agenda-structure))
  19321. (setq pos (point))
  19322. (insert match "\n")
  19323. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19324. (setq pos (point))
  19325. (unless org-agenda-multi
  19326. (insert "Press `C-u r' to search again with new search string\n"))
  19327. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19328. (when rtnall
  19329. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19330. (goto-char (point-min))
  19331. (org-fit-agenda-window)
  19332. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19333. (org-finalize-agenda)
  19334. (setq buffer-read-only t)))
  19335. ;;; Agenda Finding stuck projects
  19336. (defvar org-agenda-skip-regexp nil
  19337. "Regular expression used in skipping subtrees for the agenda.
  19338. This is basically a temporary global variable that can be set and then
  19339. used by user-defined selections using `org-agenda-skip-function'.")
  19340. (defvar org-agenda-overriding-header nil
  19341. "When this is set during todo and tags searches, will replace header.")
  19342. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19343. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19344. If yes, it returns the end position of this tree, causing agenda commands
  19345. to skip this subtree. This is a function that can be put into
  19346. `org-agenda-skip-function' for the duration of a command."
  19347. (let ((end (save-excursion (org-end-of-subtree t)))
  19348. skip)
  19349. (save-excursion
  19350. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19351. (and skip end)))
  19352. (defun org-agenda-skip-entry-if (&rest conditions)
  19353. "Skip entry if any of CONDITIONS is true.
  19354. See `org-agenda-skip-if' for details."
  19355. (org-agenda-skip-if nil conditions))
  19356. (defun org-agenda-skip-subtree-if (&rest conditions)
  19357. "Skip entry if any of CONDITIONS is true.
  19358. See `org-agenda-skip-if' for details."
  19359. (org-agenda-skip-if t conditions))
  19360. (defun org-agenda-skip-if (subtree conditions)
  19361. "Checks current entity for CONDITIONS.
  19362. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19363. the entry, i.e. the text before the next heading is checked.
  19364. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19365. from different tests. Valid conditions are:
  19366. scheduled Check if there is a scheduled cookie
  19367. notscheduled Check if there is no scheduled cookie
  19368. deadline Check if there is a deadline
  19369. notdeadline Check if there is no deadline
  19370. regexp Check if regexp matches
  19371. notregexp Check if regexp does not match.
  19372. The regexp is taken from the conditions list, it must come right after
  19373. the `regexp' or `notregexp' element.
  19374. If any of these conditions is met, this function returns the end point of
  19375. the entity, causing the search to continue from there. This is a function
  19376. that can be put into `org-agenda-skip-function' for the duration of a command."
  19377. (let (beg end m)
  19378. (org-back-to-heading t)
  19379. (setq beg (point)
  19380. end (if subtree
  19381. (progn (org-end-of-subtree t) (point))
  19382. (progn (outline-next-heading) (1- (point)))))
  19383. (goto-char beg)
  19384. (and
  19385. (or
  19386. (and (memq 'scheduled conditions)
  19387. (re-search-forward org-scheduled-time-regexp end t))
  19388. (and (memq 'notscheduled conditions)
  19389. (not (re-search-forward org-scheduled-time-regexp end t)))
  19390. (and (memq 'deadline conditions)
  19391. (re-search-forward org-deadline-time-regexp end t))
  19392. (and (memq 'notdeadline conditions)
  19393. (not (re-search-forward org-deadline-time-regexp end t)))
  19394. (and (setq m (memq 'regexp conditions))
  19395. (stringp (nth 1 m))
  19396. (re-search-forward (nth 1 m) end t))
  19397. (and (setq m (memq 'notregexp conditions))
  19398. (stringp (nth 1 m))
  19399. (not (re-search-forward (nth 1 m) end t))))
  19400. end)))
  19401. ;;;###autoload
  19402. (defun org-agenda-list-stuck-projects (&rest ignore)
  19403. "Create agenda view for projects that are stuck.
  19404. Stuck projects are project that have no next actions. For the definitions
  19405. of what a project is and how to check if it stuck, customize the variable
  19406. `org-stuck-projects'.
  19407. MATCH is being ignored."
  19408. (interactive)
  19409. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19410. ;; FIXME: we could have used org-agenda-skip-if here.
  19411. (org-agenda-overriding-header "List of stuck projects: ")
  19412. (matcher (nth 0 org-stuck-projects))
  19413. (todo (nth 1 org-stuck-projects))
  19414. (todo-wds (if (member "*" todo)
  19415. (progn
  19416. (org-prepare-agenda-buffers (org-agenda-files))
  19417. (org-delete-all
  19418. org-done-keywords-for-agenda
  19419. (copy-sequence org-todo-keywords-for-agenda)))
  19420. todo))
  19421. (todo-re (concat "^\\*+[ \t]+\\("
  19422. (mapconcat 'identity todo-wds "\\|")
  19423. "\\)\\>"))
  19424. (tags (nth 2 org-stuck-projects))
  19425. (tags-re (if (member "*" tags)
  19426. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19427. (concat "^\\*+ .*:\\("
  19428. (mapconcat 'identity tags "\\|")
  19429. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19430. (gen-re (nth 3 org-stuck-projects))
  19431. (re-list
  19432. (delq nil
  19433. (list
  19434. (if todo todo-re)
  19435. (if tags tags-re)
  19436. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19437. gen-re)))))
  19438. (setq org-agenda-skip-regexp
  19439. (if re-list
  19440. (mapconcat 'identity re-list "\\|")
  19441. (error "No information how to identify unstuck projects")))
  19442. (org-tags-view nil matcher)
  19443. (with-current-buffer org-agenda-buffer-name
  19444. (setq org-agenda-redo-command
  19445. '(org-agenda-list-stuck-projects
  19446. (or current-prefix-arg org-last-arg))))))
  19447. ;;; Diary integration
  19448. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19449. (defun org-get-entries-from-diary (date)
  19450. "Get the (Emacs Calendar) diary entries for DATE."
  19451. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19452. (diary-display-hook '(fancy-diary-display))
  19453. (pop-up-frames nil)
  19454. (list-diary-entries-hook
  19455. (cons 'org-diary-default-entry list-diary-entries-hook))
  19456. (diary-file-name-prefix-function nil) ; turn this feature off
  19457. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19458. entries
  19459. (org-disable-agenda-to-diary t))
  19460. (save-excursion
  19461. (save-window-excursion
  19462. (funcall (if (fboundp 'diary-list-entries)
  19463. 'diary-list-entries 'list-diary-entries)
  19464. date 1)))
  19465. (if (not (get-buffer fancy-diary-buffer))
  19466. (setq entries nil)
  19467. (with-current-buffer fancy-diary-buffer
  19468. (setq buffer-read-only nil)
  19469. (if (zerop (buffer-size))
  19470. ;; No entries
  19471. (setq entries nil)
  19472. ;; Omit the date and other unnecessary stuff
  19473. (org-agenda-cleanup-fancy-diary)
  19474. ;; Add prefix to each line and extend the text properties
  19475. (if (zerop (buffer-size))
  19476. (setq entries nil)
  19477. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19478. (set-buffer-modified-p nil)
  19479. (kill-buffer fancy-diary-buffer)))
  19480. (when entries
  19481. (setq entries (org-split-string entries "\n"))
  19482. (setq entries
  19483. (mapcar
  19484. (lambda (x)
  19485. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19486. ;; Extend the text properties to the beginning of the line
  19487. (org-add-props x (text-properties-at (1- (length x)) x)
  19488. 'type "diary" 'date date))
  19489. entries)))))
  19490. (defun org-agenda-cleanup-fancy-diary ()
  19491. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19492. This gets rid of the date, the underline under the date, and
  19493. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19494. date. It also removes lines that contain only whitespace."
  19495. (goto-char (point-min))
  19496. (if (looking-at ".*?:[ \t]*")
  19497. (progn
  19498. (replace-match "")
  19499. (re-search-forward "\n=+$" nil t)
  19500. (replace-match "")
  19501. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19502. (re-search-forward "\n=+$" nil t)
  19503. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19504. (goto-char (point-min))
  19505. (while (re-search-forward "^ +\n" nil t)
  19506. (replace-match ""))
  19507. (goto-char (point-min))
  19508. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19509. (replace-match "")))
  19510. ;; Make sure entries from the diary have the right text properties.
  19511. (eval-after-load "diary-lib"
  19512. '(if (boundp 'diary-modify-entry-list-string-function)
  19513. ;; We can rely on the hook, nothing to do
  19514. nil
  19515. ;; Hook not avaiable, must use advice to make this work
  19516. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19517. "Make the position visible."
  19518. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19519. (stringp string)
  19520. buffer-file-name)
  19521. (setq string (org-modify-diary-entry-string string))))))
  19522. (defun org-modify-diary-entry-string (string)
  19523. "Add text properties to string, allowing org-mode to act on it."
  19524. (org-add-props string nil
  19525. 'mouse-face 'highlight
  19526. 'keymap org-agenda-keymap
  19527. 'help-echo (if buffer-file-name
  19528. (format "mouse-2 or RET jump to diary file %s"
  19529. (abbreviate-file-name buffer-file-name))
  19530. "")
  19531. 'org-agenda-diary-link t
  19532. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19533. (defun org-diary-default-entry ()
  19534. "Add a dummy entry to the diary.
  19535. Needed to avoid empty dates which mess up holiday display."
  19536. ;; Catch the error if dealing with the new add-to-diary-alist
  19537. (when org-disable-agenda-to-diary
  19538. (condition-case nil
  19539. (add-to-diary-list original-date "Org-mode dummy" "")
  19540. (error
  19541. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19542. ;;;###autoload
  19543. (defun org-diary (&rest args)
  19544. "Return diary information from org-files.
  19545. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19546. It accesses org files and extracts information from those files to be
  19547. listed in the diary. The function accepts arguments specifying what
  19548. items should be listed. The following arguments are allowed:
  19549. :timestamp List the headlines of items containing a date stamp or
  19550. date range matching the selected date. Deadlines will
  19551. also be listed, on the expiration day.
  19552. :sexp List entries resulting from diary-like sexps.
  19553. :deadline List any deadlines past due, or due within
  19554. `org-deadline-warning-days'. The listing occurs only
  19555. in the diary for *today*, not at any other date. If
  19556. an entry is marked DONE, it is no longer listed.
  19557. :scheduled List all items which are scheduled for the given date.
  19558. The diary for *today* also contains items which were
  19559. scheduled earlier and are not yet marked DONE.
  19560. :todo List all TODO items from the org-file. This may be a
  19561. long list - so this is not turned on by default.
  19562. Like deadlines, these entries only show up in the
  19563. diary for *today*, not at any other date.
  19564. The call in the diary file should look like this:
  19565. &%%(org-diary) ~/path/to/some/orgfile.org
  19566. Use a separate line for each org file to check. Or, if you omit the file name,
  19567. all files listed in `org-agenda-files' will be checked automatically:
  19568. &%%(org-diary)
  19569. If you don't give any arguments (as in the example above), the default
  19570. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19571. So the example above may also be written as
  19572. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19573. The function expects the lisp variables `entry' and `date' to be provided
  19574. by the caller, because this is how the calendar works. Don't use this
  19575. function from a program - use `org-agenda-get-day-entries' instead."
  19576. (when (> (- (time-to-seconds (current-time))
  19577. org-agenda-last-marker-time)
  19578. 5)
  19579. (org-agenda-reset-markers))
  19580. (org-compile-prefix-format 'agenda)
  19581. (org-set-sorting-strategy 'agenda)
  19582. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19583. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19584. (list entry)
  19585. (org-agenda-files t)))
  19586. file rtn results)
  19587. (org-prepare-agenda-buffers files)
  19588. ;; If this is called during org-agenda, don't return any entries to
  19589. ;; the calendar. Org Agenda will list these entries itself.
  19590. (if org-disable-agenda-to-diary (setq files nil))
  19591. (while (setq file (pop files))
  19592. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19593. (setq results (append results rtn)))
  19594. (if results
  19595. (concat (org-finalize-agenda-entries results) "\n"))))
  19596. ;;; Agenda entry finders
  19597. (defun org-agenda-get-day-entries (file date &rest args)
  19598. "Does the work for `org-diary' and `org-agenda'.
  19599. FILE is the path to a file to be checked for entries. DATE is date like
  19600. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19601. which kind of entries should be extracted. For details about these, see
  19602. the documentation of `org-diary'."
  19603. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19604. (let* ((org-startup-folded nil)
  19605. (org-startup-align-all-tables nil)
  19606. (buffer (if (file-exists-p file)
  19607. (org-get-agenda-file-buffer file)
  19608. (error "No such file %s" file)))
  19609. arg results rtn)
  19610. (if (not buffer)
  19611. ;; If file does not exist, make sure an error message ends up in diary
  19612. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19613. (with-current-buffer buffer
  19614. (unless (org-mode-p)
  19615. (error "Agenda file %s is not in `org-mode'" file))
  19616. (let ((case-fold-search nil))
  19617. (save-excursion
  19618. (save-restriction
  19619. (if org-agenda-restrict
  19620. (narrow-to-region org-agenda-restrict-begin
  19621. org-agenda-restrict-end)
  19622. (widen))
  19623. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19624. (while (setq arg (pop args))
  19625. (cond
  19626. ((and (eq arg :todo)
  19627. (equal date (calendar-current-date)))
  19628. (setq rtn (org-agenda-get-todos))
  19629. (setq results (append results rtn)))
  19630. ((eq arg :timestamp)
  19631. (setq rtn (org-agenda-get-blocks))
  19632. (setq results (append results rtn))
  19633. (setq rtn (org-agenda-get-timestamps))
  19634. (setq results (append results rtn)))
  19635. ((eq arg :sexp)
  19636. (setq rtn (org-agenda-get-sexps))
  19637. (setq results (append results rtn)))
  19638. ((eq arg :scheduled)
  19639. (setq rtn (org-agenda-get-scheduled))
  19640. (setq results (append results rtn)))
  19641. ((eq arg :closed)
  19642. (setq rtn (org-agenda-get-closed))
  19643. (setq results (append results rtn)))
  19644. ((eq arg :deadline)
  19645. (setq rtn (org-agenda-get-deadlines))
  19646. (setq results (append results rtn))))))))
  19647. results))))
  19648. (defun org-entry-is-todo-p ()
  19649. (member (org-get-todo-state) org-not-done-keywords))
  19650. (defun org-entry-is-done-p ()
  19651. (member (org-get-todo-state) org-done-keywords))
  19652. (defun org-get-todo-state ()
  19653. (save-excursion
  19654. (org-back-to-heading t)
  19655. (and (looking-at org-todo-line-regexp)
  19656. (match-end 2)
  19657. (match-string 2))))
  19658. (defun org-at-date-range-p (&optional inactive-ok)
  19659. "Is the cursor inside a date range?"
  19660. (interactive)
  19661. (save-excursion
  19662. (catch 'exit
  19663. (let ((pos (point)))
  19664. (skip-chars-backward "^[<\r\n")
  19665. (skip-chars-backward "<[")
  19666. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19667. (>= (match-end 0) pos)
  19668. (throw 'exit t))
  19669. (skip-chars-backward "^<[\r\n")
  19670. (skip-chars-backward "<[")
  19671. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19672. (>= (match-end 0) pos)
  19673. (throw 'exit t)))
  19674. nil)))
  19675. (defun org-agenda-get-todos ()
  19676. "Return the TODO information for agenda display."
  19677. (let* ((props (list 'face nil
  19678. 'done-face 'org-done
  19679. 'org-not-done-regexp org-not-done-regexp
  19680. 'org-todo-regexp org-todo-regexp
  19681. 'mouse-face 'highlight
  19682. 'keymap org-agenda-keymap
  19683. 'help-echo
  19684. (format "mouse-2 or RET jump to org file %s"
  19685. (abbreviate-file-name buffer-file-name))))
  19686. ;; FIXME: get rid of the \n at some point but watch out
  19687. (regexp (concat "^\\*+[ \t]+\\("
  19688. (if org-select-this-todo-keyword
  19689. (if (equal org-select-this-todo-keyword "*")
  19690. org-todo-regexp
  19691. (concat "\\<\\("
  19692. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19693. "\\)\\>"))
  19694. org-not-done-regexp)
  19695. "[^\n\r]*\\)"))
  19696. marker priority category tags
  19697. ee txt beg end)
  19698. (goto-char (point-min))
  19699. (while (re-search-forward regexp nil t)
  19700. (catch :skip
  19701. (save-match-data
  19702. (beginning-of-line)
  19703. (setq beg (point) end (progn (outline-next-heading) (point)))
  19704. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19705. (re-search-forward org-ts-regexp end t))
  19706. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19707. (re-search-forward org-scheduled-time-regexp end t))
  19708. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19709. (re-search-forward org-deadline-time-regexp end t)
  19710. (org-deadline-close (match-string 1))))
  19711. (goto-char (1+ beg))
  19712. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19713. (throw :skip nil)))
  19714. (goto-char beg)
  19715. (org-agenda-skip)
  19716. (goto-char (match-beginning 1))
  19717. (setq marker (org-agenda-new-marker (match-beginning 0))
  19718. category (org-get-category)
  19719. tags (org-get-tags-at (point))
  19720. txt (org-format-agenda-item "" (match-string 1) category tags)
  19721. priority (1+ (org-get-priority txt)))
  19722. (org-add-props txt props
  19723. 'org-marker marker 'org-hd-marker marker
  19724. 'priority priority 'org-category category
  19725. 'type "todo")
  19726. (push txt ee)
  19727. (if org-agenda-todo-list-sublevels
  19728. (goto-char (match-end 1))
  19729. (org-end-of-subtree 'invisible))))
  19730. (nreverse ee)))
  19731. (defconst org-agenda-no-heading-message
  19732. "No heading for this item in buffer or region.")
  19733. (defun org-agenda-get-timestamps ()
  19734. "Return the date stamp information for agenda display."
  19735. (let* ((props (list 'face nil
  19736. 'org-not-done-regexp org-not-done-regexp
  19737. 'org-todo-regexp org-todo-regexp
  19738. 'mouse-face 'highlight
  19739. 'keymap org-agenda-keymap
  19740. 'help-echo
  19741. (format "mouse-2 or RET jump to org file %s"
  19742. (abbreviate-file-name buffer-file-name))))
  19743. (d1 (calendar-absolute-from-gregorian date))
  19744. (remove-re
  19745. (concat
  19746. (regexp-quote
  19747. (format-time-string
  19748. "<%Y-%m-%d"
  19749. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19750. ".*?>"))
  19751. (regexp
  19752. (concat
  19753. (regexp-quote
  19754. (substring
  19755. (format-time-string
  19756. (car org-time-stamp-formats)
  19757. (apply 'encode-time ; DATE bound by calendar
  19758. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19759. 0 11))
  19760. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19761. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19762. marker hdmarker deadlinep scheduledp donep tmp priority category
  19763. ee txt timestr tags b0 b3 e3 head)
  19764. (goto-char (point-min))
  19765. (while (re-search-forward regexp nil t)
  19766. (setq b0 (match-beginning 0)
  19767. b3 (match-beginning 3) e3 (match-end 3))
  19768. (catch :skip
  19769. (and (org-at-date-range-p) (throw :skip nil))
  19770. (org-agenda-skip)
  19771. (if (and (match-end 1)
  19772. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19773. (throw :skip nil))
  19774. (if (and e3
  19775. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19776. (throw :skip nil))
  19777. (setq marker (org-agenda-new-marker b0)
  19778. category (org-get-category b0)
  19779. tmp (buffer-substring (max (point-min)
  19780. (- b0 org-ds-keyword-length))
  19781. b0)
  19782. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19783. deadlinep (string-match org-deadline-regexp tmp)
  19784. scheduledp (string-match org-scheduled-regexp tmp)
  19785. donep (org-entry-is-done-p))
  19786. (if (or scheduledp deadlinep) (throw :skip t))
  19787. (if (string-match ">" timestr)
  19788. ;; substring should only run to end of time stamp
  19789. (setq timestr (substring timestr 0 (match-end 0))))
  19790. (save-excursion
  19791. (if (re-search-backward "^\\*+ " nil t)
  19792. (progn
  19793. (goto-char (match-beginning 0))
  19794. (setq hdmarker (org-agenda-new-marker)
  19795. tags (org-get-tags-at))
  19796. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19797. (setq head (match-string 1))
  19798. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19799. (setq txt (org-format-agenda-item
  19800. nil head category tags timestr nil
  19801. remove-re)))
  19802. (setq txt org-agenda-no-heading-message))
  19803. (setq priority (org-get-priority txt))
  19804. (org-add-props txt props
  19805. 'org-marker marker 'org-hd-marker hdmarker)
  19806. (org-add-props txt nil 'priority priority
  19807. 'org-category category 'date date
  19808. 'type "timestamp")
  19809. (push txt ee))
  19810. (outline-next-heading)))
  19811. (nreverse ee)))
  19812. (defun org-agenda-get-sexps ()
  19813. "Return the sexp information for agenda display."
  19814. (require 'diary-lib)
  19815. (let* ((props (list 'face nil
  19816. 'mouse-face 'highlight
  19817. 'keymap org-agenda-keymap
  19818. 'help-echo
  19819. (format "mouse-2 or RET jump to org file %s"
  19820. (abbreviate-file-name buffer-file-name))))
  19821. (regexp "^&?%%(")
  19822. marker category ee txt tags entry result beg b sexp sexp-entry)
  19823. (goto-char (point-min))
  19824. (while (re-search-forward regexp nil t)
  19825. (catch :skip
  19826. (org-agenda-skip)
  19827. (setq beg (match-beginning 0))
  19828. (goto-char (1- (match-end 0)))
  19829. (setq b (point))
  19830. (forward-sexp 1)
  19831. (setq sexp (buffer-substring b (point)))
  19832. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19833. (org-trim (match-string 1))
  19834. ""))
  19835. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19836. (when result
  19837. (setq marker (org-agenda-new-marker beg)
  19838. category (org-get-category beg))
  19839. (if (string-match "\\S-" result)
  19840. (setq txt result)
  19841. (setq txt "SEXP entry returned empty string"))
  19842. (setq txt (org-format-agenda-item
  19843. "" txt category tags 'time))
  19844. (org-add-props txt props 'org-marker marker)
  19845. (org-add-props txt nil
  19846. 'org-category category 'date date
  19847. 'type "sexp")
  19848. (push txt ee))))
  19849. (nreverse ee)))
  19850. (defun org-agenda-get-closed ()
  19851. "Return the logged TODO entries for agenda display."
  19852. (let* ((props (list 'mouse-face 'highlight
  19853. 'org-not-done-regexp org-not-done-regexp
  19854. 'org-todo-regexp org-todo-regexp
  19855. 'keymap org-agenda-keymap
  19856. 'help-echo
  19857. (format "mouse-2 or RET jump to org file %s"
  19858. (abbreviate-file-name buffer-file-name))))
  19859. (regexp (concat
  19860. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19861. (regexp-quote
  19862. (substring
  19863. (format-time-string
  19864. (car org-time-stamp-formats)
  19865. (apply 'encode-time ; DATE bound by calendar
  19866. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19867. 1 11))))
  19868. marker hdmarker priority category tags closedp
  19869. ee txt timestr)
  19870. (goto-char (point-min))
  19871. (while (re-search-forward regexp nil t)
  19872. (catch :skip
  19873. (org-agenda-skip)
  19874. (setq marker (org-agenda-new-marker (match-beginning 0))
  19875. closedp (equal (match-string 1) org-closed-string)
  19876. category (org-get-category (match-beginning 0))
  19877. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19878. ;; donep (org-entry-is-done-p)
  19879. )
  19880. (if (string-match "\\]" timestr)
  19881. ;; substring should only run to end of time stamp
  19882. (setq timestr (substring timestr 0 (match-end 0))))
  19883. (save-excursion
  19884. (if (re-search-backward "^\\*+ " nil t)
  19885. (progn
  19886. (goto-char (match-beginning 0))
  19887. (setq hdmarker (org-agenda-new-marker)
  19888. tags (org-get-tags-at))
  19889. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19890. (setq txt (org-format-agenda-item
  19891. (if closedp "Closed: " "Clocked: ")
  19892. (match-string 1) category tags timestr)))
  19893. (setq txt org-agenda-no-heading-message))
  19894. (setq priority 100000)
  19895. (org-add-props txt props
  19896. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19897. 'priority priority 'org-category category
  19898. 'type "closed" 'date date
  19899. 'undone-face 'org-warning 'done-face 'org-done)
  19900. (push txt ee))
  19901. (goto-char (point-at-eol))))
  19902. (nreverse ee)))
  19903. (defun org-agenda-get-deadlines ()
  19904. "Return the deadline information for agenda display."
  19905. (let* ((props (list 'mouse-face 'highlight
  19906. 'org-not-done-regexp org-not-done-regexp
  19907. 'org-todo-regexp org-todo-regexp
  19908. 'keymap org-agenda-keymap
  19909. 'help-echo
  19910. (format "mouse-2 or RET jump to org file %s"
  19911. (abbreviate-file-name buffer-file-name))))
  19912. (regexp org-deadline-time-regexp)
  19913. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19914. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19915. d2 diff dfrac wdays pos pos1 category tags
  19916. ee txt head face s upcomingp donep timestr)
  19917. (goto-char (point-min))
  19918. (while (re-search-forward regexp nil t)
  19919. (catch :skip
  19920. (org-agenda-skip)
  19921. (setq s (match-string 1)
  19922. pos (1- (match-beginning 1))
  19923. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19924. diff (- d2 d1)
  19925. wdays (org-get-wdays s)
  19926. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  19927. upcomingp (and todayp (> diff 0)))
  19928. ;; When to show a deadline in the calendar:
  19929. ;; If the expiration is within wdays warning time.
  19930. ;; Past-due deadlines are only shown on the current date
  19931. (if (or (and (<= diff wdays)
  19932. (and todayp (not org-agenda-only-exact-dates)))
  19933. (= diff 0))
  19934. (save-excursion
  19935. (setq category (org-get-category))
  19936. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19937. (progn
  19938. (goto-char (match-end 0))
  19939. (setq pos1 (match-beginning 0))
  19940. (setq tags (org-get-tags-at pos1))
  19941. (setq head (buffer-substring-no-properties
  19942. (point)
  19943. (progn (skip-chars-forward "^\r\n")
  19944. (point))))
  19945. (setq donep (string-match org-looking-at-done-regexp head))
  19946. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19947. (setq timestr
  19948. (concat (substring s (match-beginning 1)) " "))
  19949. (setq timestr 'time))
  19950. (if (and donep
  19951. (or org-agenda-skip-deadline-if-done
  19952. (not (= diff 0))))
  19953. (setq txt nil)
  19954. (setq txt (org-format-agenda-item
  19955. (if (= diff 0)
  19956. (car org-agenda-deadline-leaders)
  19957. (format (nth 1 org-agenda-deadline-leaders)
  19958. diff))
  19959. head category tags timestr))))
  19960. (setq txt org-agenda-no-heading-message))
  19961. (when txt
  19962. (setq face (org-agenda-deadline-face dfrac))
  19963. (org-add-props txt props
  19964. 'org-marker (org-agenda-new-marker pos)
  19965. 'org-hd-marker (org-agenda-new-marker pos1)
  19966. 'priority (+ (- diff)
  19967. (org-get-priority txt))
  19968. 'org-category category
  19969. 'type (if upcomingp "upcoming-deadline" "deadline")
  19970. 'date (if upcomingp date d2)
  19971. 'face (if donep 'org-done face)
  19972. 'undone-face face 'done-face 'org-done)
  19973. (push txt ee))))))
  19974. (nreverse ee)))
  19975. (defun org-agenda-deadline-face (fraction)
  19976. "Return the face to displaying a deadline item.
  19977. FRACTION is what fraction of the head-warning time has passed."
  19978. (let ((faces org-agenda-deadline-faces) f)
  19979. (catch 'exit
  19980. (while (setq f (pop faces))
  19981. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19982. (defun org-agenda-get-scheduled ()
  19983. "Return the scheduled information for agenda display."
  19984. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19985. 'org-todo-regexp org-todo-regexp
  19986. 'done-face 'org-done
  19987. 'mouse-face 'highlight
  19988. 'keymap org-agenda-keymap
  19989. 'help-echo
  19990. (format "mouse-2 or RET jump to org file %s"
  19991. (abbreviate-file-name buffer-file-name))))
  19992. (regexp org-scheduled-time-regexp)
  19993. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19994. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19995. d2 diff pos pos1 category tags
  19996. ee txt head pastschedp donep face timestr s)
  19997. (goto-char (point-min))
  19998. (while (re-search-forward regexp nil t)
  19999. (catch :skip
  20000. (org-agenda-skip)
  20001. (setq s (match-string 1)
  20002. pos (1- (match-beginning 1))
  20003. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20004. ;;; is this right?
  20005. ;;; do we need to do this for deadleine too????
  20006. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20007. diff (- d2 d1))
  20008. (setq pastschedp (and todayp (< diff 0)))
  20009. ;; When to show a scheduled item in the calendar:
  20010. ;; If it is on or past the date.
  20011. (if (or (and (< diff 0)
  20012. (and todayp (not org-agenda-only-exact-dates)))
  20013. (= diff 0))
  20014. (save-excursion
  20015. (setq category (org-get-category))
  20016. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20017. (progn
  20018. (goto-char (match-end 0))
  20019. (setq pos1 (match-beginning 0))
  20020. (setq tags (org-get-tags-at))
  20021. (setq head (buffer-substring-no-properties
  20022. (point)
  20023. (progn (skip-chars-forward "^\r\n") (point))))
  20024. (setq donep (string-match org-looking-at-done-regexp head))
  20025. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20026. (setq timestr
  20027. (concat (substring s (match-beginning 1)) " "))
  20028. (setq timestr 'time))
  20029. (if (and donep
  20030. (or org-agenda-skip-scheduled-if-done
  20031. (not (= diff 0))))
  20032. (setq txt nil)
  20033. (setq txt (org-format-agenda-item
  20034. (if (= diff 0)
  20035. (car org-agenda-scheduled-leaders)
  20036. (format (nth 1 org-agenda-scheduled-leaders)
  20037. (- 1 diff)))
  20038. head category tags timestr))))
  20039. (setq txt org-agenda-no-heading-message))
  20040. (when txt
  20041. (setq face (if pastschedp
  20042. 'org-scheduled-previously
  20043. 'org-scheduled-today))
  20044. (org-add-props txt props
  20045. 'undone-face face
  20046. 'face (if donep 'org-done face)
  20047. 'org-marker (org-agenda-new-marker pos)
  20048. 'org-hd-marker (org-agenda-new-marker pos1)
  20049. 'type (if pastschedp "past-scheduled" "scheduled")
  20050. 'date (if pastschedp d2 date)
  20051. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20052. 'org-category category)
  20053. (push txt ee))))))
  20054. (nreverse ee)))
  20055. (defun org-agenda-get-blocks ()
  20056. "Return the date-range information for agenda display."
  20057. (let* ((props (list 'face nil
  20058. 'org-not-done-regexp org-not-done-regexp
  20059. 'org-todo-regexp org-todo-regexp
  20060. 'mouse-face 'highlight
  20061. 'keymap org-agenda-keymap
  20062. 'help-echo
  20063. (format "mouse-2 or RET jump to org file %s"
  20064. (abbreviate-file-name buffer-file-name))))
  20065. (regexp org-tr-regexp)
  20066. (d0 (calendar-absolute-from-gregorian date))
  20067. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20068. donep head)
  20069. (goto-char (point-min))
  20070. (while (re-search-forward regexp nil t)
  20071. (catch :skip
  20072. (org-agenda-skip)
  20073. (setq pos (point))
  20074. (setq timestr (match-string 0)
  20075. s1 (match-string 1)
  20076. s2 (match-string 2)
  20077. d1 (time-to-days (org-time-string-to-time s1))
  20078. d2 (time-to-days (org-time-string-to-time s2)))
  20079. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20080. ;; Only allow days between the limits, because the normal
  20081. ;; date stamps will catch the limits.
  20082. (save-excursion
  20083. (setq marker (org-agenda-new-marker (point)))
  20084. (setq category (org-get-category))
  20085. (if (re-search-backward "^\\*+ " nil t)
  20086. (progn
  20087. (goto-char (match-beginning 0))
  20088. (setq hdmarker (org-agenda-new-marker (point)))
  20089. (setq tags (org-get-tags-at))
  20090. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20091. (setq head (match-string 1))
  20092. (and org-agenda-skip-timestamp-if-done
  20093. (org-entry-is-done-p)
  20094. (throw :skip t))
  20095. (setq txt (org-format-agenda-item
  20096. (format (if (= d1 d2) "" "(%d/%d): ")
  20097. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20098. head category tags
  20099. (if (= d0 d1) timestr))))
  20100. (setq txt org-agenda-no-heading-message))
  20101. (org-add-props txt props
  20102. 'org-marker marker 'org-hd-marker hdmarker
  20103. 'type "block" 'date date
  20104. 'priority (org-get-priority txt) 'org-category category)
  20105. (push txt ee)))
  20106. (goto-char pos)))
  20107. ;; Sort the entries by expiration date.
  20108. (nreverse ee)))
  20109. ;;; Agenda presentation and sorting
  20110. (defconst org-plain-time-of-day-regexp
  20111. (concat
  20112. "\\(\\<[012]?[0-9]"
  20113. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20114. "\\(--?"
  20115. "\\(\\<[012]?[0-9]"
  20116. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20117. "\\)?")
  20118. "Regular expression to match a plain time or time range.
  20119. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20120. groups carry important information:
  20121. 0 the full match
  20122. 1 the first time, range or not
  20123. 8 the second time, if it is a range.")
  20124. (defconst org-plain-time-extension-regexp
  20125. (concat
  20126. "\\(\\<[012]?[0-9]"
  20127. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20128. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20129. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20130. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20131. groups carry important information:
  20132. 0 the full match
  20133. 7 hours of duration
  20134. 9 minutes of duration")
  20135. (defconst org-stamp-time-of-day-regexp
  20136. (concat
  20137. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20138. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20139. "\\(--?"
  20140. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20141. "Regular expression to match a timestamp time or time range.
  20142. After a match, the following groups carry important information:
  20143. 0 the full match
  20144. 1 date plus weekday, for backreferencing to make sure both times on same day
  20145. 2 the first time, range or not
  20146. 4 the second time, if it is a range.")
  20147. (defvar org-prefix-has-time nil
  20148. "A flag, set by `org-compile-prefix-format'.
  20149. The flag is set if the currently compiled format contains a `%t'.")
  20150. (defvar org-prefix-has-tag nil
  20151. "A flag, set by `org-compile-prefix-format'.
  20152. The flag is set if the currently compiled format contains a `%T'.")
  20153. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20154. noprefix remove-re)
  20155. "Format TXT to be inserted into the agenda buffer.
  20156. In particular, it adds the prefix and corresponding text properties. EXTRA
  20157. must be a string and replaces the `%s' specifier in the prefix format.
  20158. CATEGORY (string, symbol or nil) may be used to overrule the default
  20159. category taken from local variable or file name. It will replace the `%c'
  20160. specifier in the format. DOTIME, when non-nil, indicates that a
  20161. time-of-day should be extracted from TXT for sorting of this entry, and for
  20162. the `%t' specifier in the format. When DOTIME is a string, this string is
  20163. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20164. only the correctly processes TXT should be returned - this is used by
  20165. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20166. Any match of REMOVE-RE will be removed from TXT."
  20167. (save-match-data
  20168. ;; Diary entries sometimes have extra whitespace at the beginning
  20169. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20170. (let* ((category (or category
  20171. org-category
  20172. (if buffer-file-name
  20173. (file-name-sans-extension
  20174. (file-name-nondirectory buffer-file-name))
  20175. "")))
  20176. (tag (if tags (nth (1- (length tags)) tags) ""))
  20177. time ; time and tag are needed for the eval of the prefix format
  20178. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20179. (time-of-day (and dotime (org-get-time-of-day ts)))
  20180. stamp plain s0 s1 s2 rtn srp)
  20181. (when (and dotime time-of-day org-prefix-has-time)
  20182. ;; Extract starting and ending time and move them to prefix
  20183. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20184. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20185. (setq s0 (match-string 0 ts)
  20186. srp (and stamp (match-end 3))
  20187. s1 (match-string (if plain 1 2) ts)
  20188. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20189. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20190. ;; them, we might want to remove them there to avoid duplication.
  20191. ;; The user can turn this off with a variable.
  20192. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20193. (string-match (concat (regexp-quote s0) " *") txt)
  20194. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20195. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20196. (= (match-beginning 0) 0)
  20197. t))
  20198. (setq txt (replace-match "" nil nil txt))))
  20199. ;; Normalize the time(s) to 24 hour
  20200. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20201. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20202. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20203. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20204. (let ((m (+ (string-to-number (match-string 2 s1))
  20205. (* 60 (string-to-number (match-string 1 s1)))
  20206. org-agenda-default-appointment-duration))
  20207. h)
  20208. (setq h (/ m 60) m (- m (* h 60)))
  20209. (setq s2 (format "%02d:%02d" h m))))
  20210. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20211. txt)
  20212. ;; Tags are in the string
  20213. (if (or (eq org-agenda-remove-tags t)
  20214. (and org-agenda-remove-tags
  20215. org-prefix-has-tag))
  20216. (setq txt (replace-match "" t t txt))
  20217. (setq txt (replace-match
  20218. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20219. (match-string 2 txt))
  20220. t t txt))))
  20221. (when remove-re
  20222. (while (string-match remove-re txt)
  20223. (setq txt (replace-match "" t t txt))))
  20224. ;; Create the final string
  20225. (if noprefix
  20226. (setq rtn txt)
  20227. ;; Prepare the variables needed in the eval of the compiled format
  20228. (setq time (cond (s2 (concat s1 "-" s2))
  20229. (s1 (concat s1 "......"))
  20230. (t ""))
  20231. extra (or extra "")
  20232. category (if (symbolp category) (symbol-name category) category))
  20233. ;; Evaluate the compiled format
  20234. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20235. ;; And finally add the text properties
  20236. (org-add-props rtn nil
  20237. 'org-category (downcase category) 'tags tags
  20238. 'org-highest-priority org-highest-priority
  20239. 'org-lowest-priority org-lowest-priority
  20240. 'prefix-length (- (length rtn) (length txt))
  20241. 'time-of-day time-of-day
  20242. 'txt txt
  20243. 'time time
  20244. 'extra extra
  20245. 'dotime dotime))))
  20246. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20247. (defvar org-agenda-sorting-strategy-selected nil)
  20248. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20249. (catch 'exit
  20250. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20251. ((and todayp (member 'today (car org-agenda-time-grid))))
  20252. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20253. ((member 'weekly (car org-agenda-time-grid)))
  20254. (t (throw 'exit list)))
  20255. (let* ((have (delq nil (mapcar
  20256. (lambda (x) (get-text-property 1 'time-of-day x))
  20257. list)))
  20258. (string (nth 1 org-agenda-time-grid))
  20259. (gridtimes (nth 2 org-agenda-time-grid))
  20260. (req (car org-agenda-time-grid))
  20261. (remove (member 'remove-match req))
  20262. new time)
  20263. (if (and (member 'require-timed req) (not have))
  20264. ;; don't show empty grid
  20265. (throw 'exit list))
  20266. (while (setq time (pop gridtimes))
  20267. (unless (and remove (member time have))
  20268. (setq time (int-to-string time))
  20269. (push (org-format-agenda-item
  20270. nil string "" nil
  20271. (concat (substring time 0 -2) ":" (substring time -2)))
  20272. new)
  20273. (put-text-property
  20274. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20275. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20276. (append new list)
  20277. (append list new)))))
  20278. (defun org-compile-prefix-format (key)
  20279. "Compile the prefix format into a Lisp form that can be evaluated.
  20280. The resulting form is returned and stored in the variable
  20281. `org-prefix-format-compiled'."
  20282. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20283. (let ((s (cond
  20284. ((stringp org-agenda-prefix-format)
  20285. org-agenda-prefix-format)
  20286. ((assq key org-agenda-prefix-format)
  20287. (cdr (assq key org-agenda-prefix-format)))
  20288. (t " %-12:c%?-12t% s")))
  20289. (start 0)
  20290. varform vars var e c f opt)
  20291. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20292. s start)
  20293. (setq var (cdr (assoc (match-string 4 s)
  20294. '(("c" . category) ("t" . time) ("s" . extra)
  20295. ("T" . tag))))
  20296. c (or (match-string 3 s) "")
  20297. opt (match-beginning 1)
  20298. start (1+ (match-beginning 0)))
  20299. (if (equal var 'time) (setq org-prefix-has-time t))
  20300. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20301. (setq f (concat "%" (match-string 2 s) "s"))
  20302. (if opt
  20303. (setq varform
  20304. `(if (equal "" ,var)
  20305. ""
  20306. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20307. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20308. (setq s (replace-match "%s" t nil s))
  20309. (push varform vars))
  20310. (setq vars (nreverse vars))
  20311. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20312. (defun org-set-sorting-strategy (key)
  20313. (if (symbolp (car org-agenda-sorting-strategy))
  20314. ;; the old format
  20315. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20316. (setq org-agenda-sorting-strategy-selected
  20317. (or (cdr (assq key org-agenda-sorting-strategy))
  20318. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20319. '(time-up category-keep priority-down)))))
  20320. (defun org-get-time-of-day (s &optional string mod24)
  20321. "Check string S for a time of day.
  20322. If found, return it as a military time number between 0 and 2400.
  20323. If not found, return nil.
  20324. The optional STRING argument forces conversion into a 5 character wide string
  20325. HH:MM."
  20326. (save-match-data
  20327. (when
  20328. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20329. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20330. (let* ((h (string-to-number (match-string 1 s)))
  20331. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20332. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20333. (am-p (equal ampm "am"))
  20334. (h1 (cond ((not ampm) h)
  20335. ((= h 12) (if am-p 0 12))
  20336. (t (+ h (if am-p 0 12)))))
  20337. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20338. (mod h1 24) h1))
  20339. (t0 (+ (* 100 h2) m))
  20340. (t1 (concat (if (>= h1 24) "+" " ")
  20341. (if (< t0 100) "0" "")
  20342. (if (< t0 10) "0" "")
  20343. (int-to-string t0))))
  20344. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20345. (defun org-finalize-agenda-entries (list &optional nosort)
  20346. "Sort and concatenate the agenda items."
  20347. (setq list (mapcar 'org-agenda-highlight-todo list))
  20348. (if nosort
  20349. list
  20350. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20351. (defun org-agenda-highlight-todo (x)
  20352. (let (re pl)
  20353. (if (eq x 'line)
  20354. (save-excursion
  20355. (beginning-of-line 1)
  20356. (setq re (get-text-property (point) 'org-todo-regexp))
  20357. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20358. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20359. (add-text-properties (match-beginning 0) (match-end 0)
  20360. (list 'face (org-get-todo-face 0)))
  20361. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20362. (delete-region (match-beginning 1) (1- (match-end 0)))
  20363. (goto-char (match-beginning 1))
  20364. (insert (format org-agenda-todo-keyword-format s)))))
  20365. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20366. pl (get-text-property 0 'prefix-length x))
  20367. (when (and re
  20368. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20369. x (or pl 0)) pl))
  20370. (add-text-properties
  20371. (or (match-end 1) (match-end 0)) (match-end 0)
  20372. (list 'face (org-get-todo-face (match-string 2 x)))
  20373. x)
  20374. (setq x (concat (substring x 0 (match-end 1))
  20375. (format org-agenda-todo-keyword-format
  20376. (match-string 2 x))
  20377. " "
  20378. (substring x (match-end 3)))))
  20379. x)))
  20380. (defsubst org-cmp-priority (a b)
  20381. "Compare the priorities of string A and B."
  20382. (let ((pa (or (get-text-property 1 'priority a) 0))
  20383. (pb (or (get-text-property 1 'priority b) 0)))
  20384. (cond ((> pa pb) +1)
  20385. ((< pa pb) -1)
  20386. (t nil))))
  20387. (defsubst org-cmp-category (a b)
  20388. "Compare the string values of categories of strings A and B."
  20389. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20390. (cb (or (get-text-property 1 'org-category b) "")))
  20391. (cond ((string-lessp ca cb) -1)
  20392. ((string-lessp cb ca) +1)
  20393. (t nil))))
  20394. (defsubst org-cmp-tag (a b)
  20395. "Compare the string values of categories of strings A and B."
  20396. (let ((ta (car (last (get-text-property 1 'tags a))))
  20397. (tb (car (last (get-text-property 1 'tags b)))))
  20398. (cond ((not ta) +1)
  20399. ((not tb) -1)
  20400. ((string-lessp ta tb) -1)
  20401. ((string-lessp tb ta) +1)
  20402. (t nil))))
  20403. (defsubst org-cmp-time (a b)
  20404. "Compare the time-of-day values of strings A and B."
  20405. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20406. (ta (or (get-text-property 1 'time-of-day a) def))
  20407. (tb (or (get-text-property 1 'time-of-day b) def)))
  20408. (cond ((< ta tb) -1)
  20409. ((< tb ta) +1)
  20410. (t nil))))
  20411. (defun org-entries-lessp (a b)
  20412. "Predicate for sorting agenda entries."
  20413. ;; The following variables will be used when the form is evaluated.
  20414. ;; So even though the compiler complains, keep them.
  20415. (let* ((time-up (org-cmp-time a b))
  20416. (time-down (if time-up (- time-up) nil))
  20417. (priority-up (org-cmp-priority a b))
  20418. (priority-down (if priority-up (- priority-up) nil))
  20419. (category-up (org-cmp-category a b))
  20420. (category-down (if category-up (- category-up) nil))
  20421. (category-keep (if category-up +1 nil))
  20422. (tag-up (org-cmp-tag a b))
  20423. (tag-down (if tag-up (- tag-up) nil)))
  20424. (cdr (assoc
  20425. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20426. '((-1 . t) (1 . nil) (nil . nil))))))
  20427. ;;; Agenda restriction lock
  20428. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20429. "Overlay to mark the headline to which arenda commands are restricted.")
  20430. (org-overlay-put org-agenda-restriction-lock-overlay
  20431. 'face 'org-agenda-restriction-lock)
  20432. (org-overlay-put org-agenda-restriction-lock-overlay
  20433. 'help-echo "Agendas are currently limited to this subtree.")
  20434. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20435. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20436. "Overlay marking the agenda restriction line in speedbar.")
  20437. (org-overlay-put org-speedbar-restriction-lock-overlay
  20438. 'face 'org-agenda-restriction-lock)
  20439. (org-overlay-put org-speedbar-restriction-lock-overlay
  20440. 'help-echo "Agendas are currently limited to this item.")
  20441. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20442. (defun org-agenda-set-restriction-lock (&optional type)
  20443. "Set restriction lock for agenda, to current subtree or file.
  20444. Restriction will be the file if TYPE is `file', or if type is the
  20445. universal prefix '(4), or if the cursor is before the first headline
  20446. in the file. Otherwise, restriction will be to the current subtree."
  20447. (interactive "P")
  20448. (and (equal type '(4)) (setq type 'file))
  20449. (setq type (cond
  20450. (type type)
  20451. ((org-at-heading-p) 'subtree)
  20452. ((condition-case nil (org-back-to-heading t) (error nil))
  20453. 'subtree)
  20454. (t 'file)))
  20455. (if (eq type 'subtree)
  20456. (progn
  20457. (setq org-agenda-restrict t)
  20458. (setq org-agenda-overriding-restriction 'subtree)
  20459. (put 'org-agenda-files 'org-restrict
  20460. (list (buffer-file-name (buffer-base-buffer))))
  20461. (org-back-to-heading t)
  20462. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20463. (move-marker org-agenda-restrict-begin (point))
  20464. (move-marker org-agenda-restrict-end
  20465. (save-excursion (org-end-of-subtree t)))
  20466. (message "Locking agenda restriction to subtree"))
  20467. (put 'org-agenda-files 'org-restrict
  20468. (list (buffer-file-name (buffer-base-buffer))))
  20469. (setq org-agenda-restrict nil)
  20470. (setq org-agenda-overriding-restriction 'file)
  20471. (move-marker org-agenda-restrict-begin nil)
  20472. (move-marker org-agenda-restrict-end nil)
  20473. (message "Locking agenda restriction to file"))
  20474. (setq current-prefix-arg nil)
  20475. (org-agenda-maybe-redo))
  20476. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20477. "Remove the agenda restriction lock."
  20478. (interactive "P")
  20479. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20480. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20481. (setq org-agenda-overriding-restriction nil)
  20482. (setq org-agenda-restrict nil)
  20483. (put 'org-agenda-files 'org-restrict nil)
  20484. (move-marker org-agenda-restrict-begin nil)
  20485. (move-marker org-agenda-restrict-end nil)
  20486. (setq current-prefix-arg nil)
  20487. (message "Agenda restriction lock removed")
  20488. (or noupdate (org-agenda-maybe-redo)))
  20489. (defun org-agenda-maybe-redo ()
  20490. "If there is any window showing the agenda view, update it."
  20491. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20492. (w0 (selected-window)))
  20493. (when w
  20494. (select-window w)
  20495. (org-agenda-redo)
  20496. (select-window w0)
  20497. (if org-agenda-overriding-restriction
  20498. (message "Agenda view shifted to new %s restriction"
  20499. org-agenda-overriding-restriction)
  20500. (message "Agenda restriction lock removed")))))
  20501. ;;; Agenda commands
  20502. (defun org-agenda-check-type (error &rest types)
  20503. "Check if agenda buffer is of allowed type.
  20504. If ERROR is non-nil, throw an error, otherwise just return nil."
  20505. (if (memq org-agenda-type types)
  20506. t
  20507. (if error
  20508. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20509. nil)))
  20510. (defun org-agenda-quit ()
  20511. "Exit agenda by removing the window or the buffer."
  20512. (interactive)
  20513. (let ((buf (current-buffer)))
  20514. (if (not (one-window-p)) (delete-window))
  20515. (kill-buffer buf)
  20516. (org-agenda-reset-markers)
  20517. (org-columns-remove-overlays))
  20518. ;; Maybe restore the pre-agenda window configuration.
  20519. (and org-agenda-restore-windows-after-quit
  20520. (not (eq org-agenda-window-setup 'other-frame))
  20521. org-pre-agenda-window-conf
  20522. (set-window-configuration org-pre-agenda-window-conf)))
  20523. (defun org-agenda-exit ()
  20524. "Exit agenda by removing the window or the buffer.
  20525. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20526. Org-mode buffers visited directly by the user will not be touched."
  20527. (interactive)
  20528. (org-release-buffers org-agenda-new-buffers)
  20529. (setq org-agenda-new-buffers nil)
  20530. (org-agenda-quit))
  20531. (defun org-agenda-execute (arg)
  20532. "Execute another agenda command, keeping same window.\\<global-map>
  20533. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20534. (interactive "P")
  20535. (let ((org-agenda-window-setup 'current-window))
  20536. (org-agenda arg)))
  20537. (defun org-save-all-org-buffers ()
  20538. "Save all Org-mode buffers without user confirmation."
  20539. (interactive)
  20540. (message "Saving all Org-mode buffers...")
  20541. (save-some-buffers t 'org-mode-p)
  20542. (message "Saving all Org-mode buffers... done"))
  20543. (defun org-agenda-redo ()
  20544. "Rebuild Agenda.
  20545. When this is the global TODO list, a prefix argument will be interpreted."
  20546. (interactive)
  20547. (let* ((org-agenda-keep-modes t)
  20548. (line (org-current-line))
  20549. (window-line (- line (org-current-line (window-start))))
  20550. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20551. (message "Rebuilding agenda buffer...")
  20552. (org-let lprops '(eval org-agenda-redo-command))
  20553. (setq org-agenda-undo-list nil
  20554. org-agenda-pending-undo-list nil)
  20555. (message "Rebuilding agenda buffer...done")
  20556. (goto-line line)
  20557. (recenter window-line)))
  20558. (defun org-agenda-goto-date (date)
  20559. "Jump to DATE in agenda."
  20560. (interactive (list (org-read-date)))
  20561. (org-agenda-list nil date))
  20562. (defun org-agenda-goto-today ()
  20563. "Go to today."
  20564. (interactive)
  20565. (org-agenda-check-type t 'timeline 'agenda)
  20566. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20567. (cond
  20568. (tdpos (goto-char tdpos))
  20569. ((eq org-agenda-type 'agenda)
  20570. (let* ((sd (time-to-days
  20571. (time-subtract (current-time)
  20572. (list 0 (* 3600 org-extend-today-until) 0))))
  20573. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20574. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20575. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20576. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20577. (org-agenda-redo)
  20578. (org-agenda-find-same-or-today-or-agenda)))
  20579. (t (error "Cannot find today")))))
  20580. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20581. (goto-char
  20582. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20583. (text-property-any (point-min) (point-max) 'org-today t)
  20584. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20585. (point-min))))
  20586. (defun org-agenda-later (arg)
  20587. "Go forward in time by thee current span.
  20588. With prefix ARG, go forward that many times the current span."
  20589. (interactive "p")
  20590. (org-agenda-check-type t 'agenda)
  20591. (let* ((span org-agenda-span)
  20592. (sd org-starting-day)
  20593. (greg (calendar-gregorian-from-absolute sd))
  20594. (cnt (get-text-property (point) 'org-day-cnt))
  20595. greg2 nd)
  20596. (cond
  20597. ((eq span 'day)
  20598. (setq sd (+ arg sd) nd 1))
  20599. ((eq span 'week)
  20600. (setq sd (+ (* 7 arg) sd) nd 7))
  20601. ((eq span 'month)
  20602. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20603. sd (calendar-absolute-from-gregorian greg2))
  20604. (setcar greg2 (1+ (car greg2)))
  20605. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20606. ((eq span 'year)
  20607. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20608. sd (calendar-absolute-from-gregorian greg2))
  20609. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20610. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20611. (let ((org-agenda-overriding-arguments
  20612. (list (car org-agenda-last-arguments) sd nd t)))
  20613. (org-agenda-redo)
  20614. (org-agenda-find-same-or-today-or-agenda cnt))))
  20615. (defun org-agenda-earlier (arg)
  20616. "Go backward in time by the current span.
  20617. With prefix ARG, go backward that many times the current span."
  20618. (interactive "p")
  20619. (org-agenda-later (- arg)))
  20620. (defun org-agenda-day-view ()
  20621. "Switch to daily view for agenda."
  20622. (interactive)
  20623. (setq org-agenda-ndays 1)
  20624. (org-agenda-change-time-span 'day))
  20625. (defun org-agenda-week-view ()
  20626. "Switch to daily view for agenda."
  20627. (interactive)
  20628. (setq org-agenda-ndays 7)
  20629. (org-agenda-change-time-span 'week))
  20630. (defun org-agenda-month-view ()
  20631. "Switch to daily view for agenda."
  20632. (interactive)
  20633. (org-agenda-change-time-span 'month))
  20634. (defun org-agenda-year-view ()
  20635. "Switch to daily view for agenda."
  20636. (interactive)
  20637. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20638. (org-agenda-change-time-span 'year)
  20639. (error "Abort")))
  20640. (defun org-agenda-change-time-span (span)
  20641. "Change the agenda view to SPAN.
  20642. SPAN may be `day', `week', `month', `year'."
  20643. (org-agenda-check-type t 'agenda)
  20644. (if (equal org-agenda-span span)
  20645. (error "Viewing span is already \"%s\"" span))
  20646. (let* ((sd (or (get-text-property (point) 'day)
  20647. org-starting-day))
  20648. (computed (org-agenda-compute-time-span sd span))
  20649. (org-agenda-overriding-arguments
  20650. (list (car org-agenda-last-arguments)
  20651. (car computed) (cdr computed) t)))
  20652. (org-agenda-redo)
  20653. (org-agenda-find-same-or-today-or-agenda))
  20654. (org-agenda-set-mode-name)
  20655. (message "Switched to %s view" span))
  20656. (defun org-agenda-compute-time-span (sd span)
  20657. "Compute starting date and number of days for agenda.
  20658. SPAN may be `day', `week', `month', `year'. The return value
  20659. is a cons cell with the starting date and the number of days,
  20660. so that the date SD will be in that range."
  20661. (let* ((greg (calendar-gregorian-from-absolute sd))
  20662. nd)
  20663. (cond
  20664. ((eq span 'day)
  20665. (setq nd 1))
  20666. ((eq span 'week)
  20667. (let* ((nt (calendar-day-of-week
  20668. (calendar-gregorian-from-absolute sd)))
  20669. (d (if org-agenda-start-on-weekday
  20670. (- nt org-agenda-start-on-weekday)
  20671. 0)))
  20672. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20673. (setq nd 7)))
  20674. ((eq span 'month)
  20675. (setq sd (calendar-absolute-from-gregorian
  20676. (list (car greg) 1 (nth 2 greg)))
  20677. nd (- (calendar-absolute-from-gregorian
  20678. (list (1+ (car greg)) 1 (nth 2 greg)))
  20679. sd)))
  20680. ((eq span 'year)
  20681. (setq sd (calendar-absolute-from-gregorian
  20682. (list 1 1 (nth 2 greg)))
  20683. nd (- (calendar-absolute-from-gregorian
  20684. (list 1 1 (1+ (nth 2 greg))))
  20685. sd))))
  20686. (cons sd nd)))
  20687. ;; FIXME: does not work if user makes date format that starts with a blank
  20688. (defun org-agenda-next-date-line (&optional arg)
  20689. "Jump to the next line indicating a date in agenda buffer."
  20690. (interactive "p")
  20691. (org-agenda-check-type t 'agenda 'timeline)
  20692. (beginning-of-line 1)
  20693. (if (looking-at "^\\S-") (forward-char 1))
  20694. (if (not (re-search-forward "^\\S-" nil t arg))
  20695. (progn
  20696. (backward-char 1)
  20697. (error "No next date after this line in this buffer")))
  20698. (goto-char (match-beginning 0)))
  20699. (defun org-agenda-previous-date-line (&optional arg)
  20700. "Jump to the previous line indicating a date in agenda buffer."
  20701. (interactive "p")
  20702. (org-agenda-check-type t 'agenda 'timeline)
  20703. (beginning-of-line 1)
  20704. (if (not (re-search-backward "^\\S-" nil t arg))
  20705. (error "No previous date before this line in this buffer")))
  20706. ;; Initialize the highlight
  20707. (defvar org-hl (org-make-overlay 1 1))
  20708. (org-overlay-put org-hl 'face 'highlight)
  20709. (defun org-highlight (begin end &optional buffer)
  20710. "Highlight a region with overlay."
  20711. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20712. org-hl begin end (or buffer (current-buffer))))
  20713. (defun org-unhighlight ()
  20714. "Detach overlay INDEX."
  20715. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20716. ;; FIXME this is currently not used.
  20717. (defun org-highlight-until-next-command (beg end &optional buffer)
  20718. (org-highlight beg end buffer)
  20719. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20720. (defun org-unhighlight-once ()
  20721. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20722. (org-unhighlight))
  20723. (defun org-agenda-follow-mode ()
  20724. "Toggle follow mode in an agenda buffer."
  20725. (interactive)
  20726. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20727. (org-agenda-set-mode-name)
  20728. (message "Follow mode is %s"
  20729. (if org-agenda-follow-mode "on" "off")))
  20730. (defun org-agenda-log-mode ()
  20731. "Toggle log mode in an agenda buffer."
  20732. (interactive)
  20733. (org-agenda-check-type t 'agenda 'timeline)
  20734. (setq org-agenda-show-log (not org-agenda-show-log))
  20735. (org-agenda-set-mode-name)
  20736. (org-agenda-redo)
  20737. (message "Log mode is %s"
  20738. (if org-agenda-show-log "on" "off")))
  20739. (defun org-agenda-toggle-diary ()
  20740. "Toggle diary inclusion in an agenda buffer."
  20741. (interactive)
  20742. (org-agenda-check-type t 'agenda)
  20743. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20744. (org-agenda-redo)
  20745. (org-agenda-set-mode-name)
  20746. (message "Diary inclusion turned %s"
  20747. (if org-agenda-include-diary "on" "off")))
  20748. (defun org-agenda-toggle-time-grid ()
  20749. "Toggle time grid in an agenda buffer."
  20750. (interactive)
  20751. (org-agenda-check-type t 'agenda)
  20752. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20753. (org-agenda-redo)
  20754. (org-agenda-set-mode-name)
  20755. (message "Time-grid turned %s"
  20756. (if org-agenda-use-time-grid "on" "off")))
  20757. (defun org-agenda-set-mode-name ()
  20758. "Set the mode name to indicate all the small mode settings."
  20759. (setq mode-name
  20760. (concat "Org-Agenda"
  20761. (if (equal org-agenda-ndays 1) " Day" "")
  20762. (if (equal org-agenda-ndays 7) " Week" "")
  20763. (if org-agenda-follow-mode " Follow" "")
  20764. (if org-agenda-include-diary " Diary" "")
  20765. (if org-agenda-use-time-grid " Grid" "")
  20766. (if org-agenda-show-log " Log" "")))
  20767. (force-mode-line-update))
  20768. (defun org-agenda-post-command-hook ()
  20769. (and (eolp) (not (bolp)) (backward-char 1))
  20770. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20771. (if (and org-agenda-follow-mode
  20772. (get-text-property (point) 'org-marker))
  20773. (org-agenda-show)))
  20774. (defun org-agenda-show-priority ()
  20775. "Show the priority of the current item.
  20776. This priority is composed of the main priority given with the [#A] cookies,
  20777. and by additional input from the age of a schedules or deadline entry."
  20778. (interactive)
  20779. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20780. (message "Priority is %d" (if pri pri -1000))))
  20781. (defun org-agenda-show-tags ()
  20782. "Show the tags applicable to the current item."
  20783. (interactive)
  20784. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20785. (if tags
  20786. (message "Tags are :%s:"
  20787. (org-no-properties (mapconcat 'identity tags ":")))
  20788. (message "No tags associated with this line"))))
  20789. (defun org-agenda-goto (&optional highlight)
  20790. "Go to the Org-mode file which contains the item at point."
  20791. (interactive)
  20792. (let* ((marker (or (get-text-property (point) 'org-marker)
  20793. (org-agenda-error)))
  20794. (buffer (marker-buffer marker))
  20795. (pos (marker-position marker)))
  20796. (switch-to-buffer-other-window buffer)
  20797. (widen)
  20798. (goto-char pos)
  20799. (when (org-mode-p)
  20800. (org-show-context 'agenda)
  20801. (save-excursion
  20802. (and (outline-next-heading)
  20803. (org-flag-heading nil)))) ; show the next heading
  20804. (recenter (/ (window-height) 2))
  20805. (run-hooks 'org-agenda-after-show-hook)
  20806. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20807. (defvar org-agenda-after-show-hook nil
  20808. "Normal hook run after an item has been shown from the agenda.
  20809. Point is in the buffer where the item originated.")
  20810. (defun org-agenda-kill ()
  20811. "Kill the entry or subtree belonging to the current agenda entry."
  20812. (interactive)
  20813. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20814. (let* ((marker (or (get-text-property (point) 'org-marker)
  20815. (org-agenda-error)))
  20816. (buffer (marker-buffer marker))
  20817. (pos (marker-position marker))
  20818. (type (get-text-property (point) 'type))
  20819. dbeg dend (n 0) conf)
  20820. (org-with-remote-undo buffer
  20821. (with-current-buffer buffer
  20822. (save-excursion
  20823. (goto-char pos)
  20824. (if (and (org-mode-p) (not (member type '("sexp"))))
  20825. (setq dbeg (progn (org-back-to-heading t) (point))
  20826. dend (org-end-of-subtree t t))
  20827. (setq dbeg (point-at-bol)
  20828. dend (min (point-max) (1+ (point-at-eol)))))
  20829. (goto-char dbeg)
  20830. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20831. (setq conf (or (eq t org-agenda-confirm-kill)
  20832. (and (numberp org-agenda-confirm-kill)
  20833. (> n org-agenda-confirm-kill))))
  20834. (and conf
  20835. (not (y-or-n-p
  20836. (format "Delete entry with %d lines in buffer \"%s\"? "
  20837. n (buffer-name buffer))))
  20838. (error "Abort"))
  20839. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20840. (with-current-buffer buffer (delete-region dbeg dend))
  20841. (message "Agenda item and source killed"))))
  20842. (defun org-agenda-archive ()
  20843. "Kill the entry or subtree belonging to the current agenda entry."
  20844. (interactive)
  20845. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20846. (let* ((marker (or (get-text-property (point) 'org-marker)
  20847. (org-agenda-error)))
  20848. (buffer (marker-buffer marker))
  20849. (pos (marker-position marker)))
  20850. (org-with-remote-undo buffer
  20851. (with-current-buffer buffer
  20852. (if (org-mode-p)
  20853. (save-excursion
  20854. (goto-char pos)
  20855. (org-remove-subtree-entries-from-agenda)
  20856. (org-back-to-heading t)
  20857. (org-archive-subtree))
  20858. (error "Archiving works only in Org-mode files"))))))
  20859. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20860. "Remove all lines in the agenda that correspond to a given subtree.
  20861. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20862. If this information is not given, the function uses the tree at point."
  20863. (let ((buf (or buf (current-buffer))) m p)
  20864. (save-excursion
  20865. (unless (and beg end)
  20866. (org-back-to-heading t)
  20867. (setq beg (point))
  20868. (org-end-of-subtree t)
  20869. (setq end (point)))
  20870. (set-buffer (get-buffer org-agenda-buffer-name))
  20871. (save-excursion
  20872. (goto-char (point-max))
  20873. (beginning-of-line 1)
  20874. (while (not (bobp))
  20875. (when (and (setq m (get-text-property (point) 'org-marker))
  20876. (equal buf (marker-buffer m))
  20877. (setq p (marker-position m))
  20878. (>= p beg)
  20879. (<= p end))
  20880. (let ((inhibit-read-only t))
  20881. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20882. (beginning-of-line 0))))))
  20883. (defun org-agenda-open-link ()
  20884. "Follow the link in the current line, if any."
  20885. (interactive)
  20886. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20887. (save-excursion
  20888. (save-restriction
  20889. (narrow-to-region (point-at-bol) (point-at-eol))
  20890. (org-open-at-point))))
  20891. (defun org-agenda-copy-local-variable (var)
  20892. "Get a variable from a referenced buffer and install it here."
  20893. (let ((m (get-text-property (point) 'org-marker)))
  20894. (when (and m (buffer-live-p (marker-buffer m)))
  20895. (org-set-local var (with-current-buffer (marker-buffer m)
  20896. (symbol-value var))))))
  20897. (defun org-agenda-switch-to (&optional delete-other-windows)
  20898. "Go to the Org-mode file which contains the item at point."
  20899. (interactive)
  20900. (let* ((marker (or (get-text-property (point) 'org-marker)
  20901. (org-agenda-error)))
  20902. (buffer (marker-buffer marker))
  20903. (pos (marker-position marker)))
  20904. (switch-to-buffer buffer)
  20905. (and delete-other-windows (delete-other-windows))
  20906. (widen)
  20907. (goto-char pos)
  20908. (when (org-mode-p)
  20909. (org-show-context 'agenda)
  20910. (save-excursion
  20911. (and (outline-next-heading)
  20912. (org-flag-heading nil)))))) ; show the next heading
  20913. (defun org-agenda-goto-mouse (ev)
  20914. "Go to the Org-mode file which contains the item at the mouse click."
  20915. (interactive "e")
  20916. (mouse-set-point ev)
  20917. (org-agenda-goto))
  20918. (defun org-agenda-show ()
  20919. "Display the Org-mode file which contains the item at point."
  20920. (interactive)
  20921. (let ((win (selected-window)))
  20922. (org-agenda-goto t)
  20923. (select-window win)))
  20924. (defun org-agenda-recenter (arg)
  20925. "Display the Org-mode file which contains the item at point and recenter."
  20926. (interactive "P")
  20927. (let ((win (selected-window)))
  20928. (org-agenda-goto t)
  20929. (recenter arg)
  20930. (select-window win)))
  20931. (defun org-agenda-show-mouse (ev)
  20932. "Display the Org-mode file which contains the item at the mouse click."
  20933. (interactive "e")
  20934. (mouse-set-point ev)
  20935. (org-agenda-show))
  20936. (defun org-agenda-check-no-diary ()
  20937. "Check if the entry is a diary link and abort if yes."
  20938. (if (get-text-property (point) 'org-agenda-diary-link)
  20939. (org-agenda-error)))
  20940. (defun org-agenda-error ()
  20941. (error "Command not allowed in this line"))
  20942. (defun org-agenda-tree-to-indirect-buffer ()
  20943. "Show the subtree corresponding to the current entry in an indirect buffer.
  20944. This calls the command `org-tree-to-indirect-buffer' from the original
  20945. Org-mode buffer.
  20946. With numerical prefix arg ARG, go up to this level and then take that tree.
  20947. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20948. dedicated frame)."
  20949. (interactive)
  20950. (org-agenda-check-no-diary)
  20951. (let* ((marker (or (get-text-property (point) 'org-marker)
  20952. (org-agenda-error)))
  20953. (buffer (marker-buffer marker))
  20954. (pos (marker-position marker)))
  20955. (with-current-buffer buffer
  20956. (save-excursion
  20957. (goto-char pos)
  20958. (call-interactively 'org-tree-to-indirect-buffer)))))
  20959. (defvar org-last-heading-marker (make-marker)
  20960. "Marker pointing to the headline that last changed its TODO state
  20961. by a remote command from the agenda.")
  20962. (defun org-agenda-todo-nextset ()
  20963. "Switch TODO entry to next sequence."
  20964. (interactive)
  20965. (org-agenda-todo 'nextset))
  20966. (defun org-agenda-todo-previousset ()
  20967. "Switch TODO entry to previous sequence."
  20968. (interactive)
  20969. (org-agenda-todo 'previousset))
  20970. (defun org-agenda-todo (&optional arg)
  20971. "Cycle TODO state of line at point, also in Org-mode file.
  20972. This changes the line at point, all other lines in the agenda referring to
  20973. the same tree node, and the headline of the tree node in the Org-mode file."
  20974. (interactive "P")
  20975. (org-agenda-check-no-diary)
  20976. (let* ((col (current-column))
  20977. (marker (or (get-text-property (point) 'org-marker)
  20978. (org-agenda-error)))
  20979. (buffer (marker-buffer marker))
  20980. (pos (marker-position marker))
  20981. (hdmarker (get-text-property (point) 'org-hd-marker))
  20982. (inhibit-read-only t)
  20983. newhead)
  20984. (org-with-remote-undo buffer
  20985. (with-current-buffer buffer
  20986. (widen)
  20987. (goto-char pos)
  20988. (org-show-context 'agenda)
  20989. (save-excursion
  20990. (and (outline-next-heading)
  20991. (org-flag-heading nil))) ; show the next heading
  20992. (org-todo arg)
  20993. (and (bolp) (forward-char 1))
  20994. (setq newhead (org-get-heading))
  20995. (save-excursion
  20996. (org-back-to-heading)
  20997. (move-marker org-last-heading-marker (point))))
  20998. (beginning-of-line 1)
  20999. (save-excursion
  21000. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21001. (move-to-column col))))
  21002. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21003. "Change all lines in the agenda buffer which match HDMARKER.
  21004. The new content of the line will be NEWHEAD (as modified by
  21005. `org-format-agenda-item'). HDMARKER is checked with
  21006. `equal' against all `org-hd-marker' text properties in the file.
  21007. If FIXFACE is non-nil, the face of each item is modified acording to
  21008. the new TODO state."
  21009. (let* ((inhibit-read-only t)
  21010. props m pl undone-face done-face finish new dotime cat tags)
  21011. (save-excursion
  21012. (goto-char (point-max))
  21013. (beginning-of-line 1)
  21014. (while (not finish)
  21015. (setq finish (bobp))
  21016. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21017. (equal m hdmarker))
  21018. (setq props (text-properties-at (point))
  21019. dotime (get-text-property (point) 'dotime)
  21020. cat (get-text-property (point) 'org-category)
  21021. tags (get-text-property (point) 'tags)
  21022. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21023. pl (get-text-property (point) 'prefix-length)
  21024. undone-face (get-text-property (point) 'undone-face)
  21025. done-face (get-text-property (point) 'done-face))
  21026. (move-to-column pl)
  21027. (cond
  21028. ((equal new "")
  21029. (beginning-of-line 1)
  21030. (and (looking-at ".*\n?") (replace-match "")))
  21031. ((looking-at ".*")
  21032. (replace-match new t t)
  21033. (beginning-of-line 1)
  21034. (add-text-properties (point-at-bol) (point-at-eol) props)
  21035. (when fixface
  21036. (add-text-properties
  21037. (point-at-bol) (point-at-eol)
  21038. (list 'face
  21039. (if org-last-todo-state-is-todo
  21040. undone-face done-face))))
  21041. (org-agenda-highlight-todo 'line)
  21042. (beginning-of-line 1))
  21043. (t (error "Line update did not work"))))
  21044. (beginning-of-line 0)))
  21045. (org-finalize-agenda)))
  21046. (defun org-agenda-align-tags (&optional line)
  21047. "Align all tags in agenda items to `org-agenda-tags-column'."
  21048. (let ((inhibit-read-only t) l c)
  21049. (save-excursion
  21050. (goto-char (if line (point-at-bol) (point-min)))
  21051. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21052. (if line (point-at-eol) nil) t)
  21053. (add-text-properties
  21054. (match-beginning 2) (match-end 2)
  21055. (list 'face (list 'org-tag (get-text-property
  21056. (match-beginning 2) 'face))))
  21057. (setq l (- (match-end 2) (match-beginning 2))
  21058. c (if (< org-agenda-tags-column 0)
  21059. (- (abs org-agenda-tags-column) l)
  21060. org-agenda-tags-column))
  21061. (delete-region (match-beginning 1) (match-end 1))
  21062. (goto-char (match-beginning 1))
  21063. (insert (org-add-props
  21064. (make-string (max 1 (- c (current-column))) ?\ )
  21065. (text-properties-at (point))))))))
  21066. (defun org-agenda-priority-up ()
  21067. "Increase the priority of line at point, also in Org-mode file."
  21068. (interactive)
  21069. (org-agenda-priority 'up))
  21070. (defun org-agenda-priority-down ()
  21071. "Decrease the priority of line at point, also in Org-mode file."
  21072. (interactive)
  21073. (org-agenda-priority 'down))
  21074. (defun org-agenda-priority (&optional force-direction)
  21075. "Set the priority of line at point, also in Org-mode file.
  21076. This changes the line at point, all other lines in the agenda referring to
  21077. the same tree node, and the headline of the tree node in the Org-mode file."
  21078. (interactive)
  21079. (org-agenda-check-no-diary)
  21080. (let* ((marker (or (get-text-property (point) 'org-marker)
  21081. (org-agenda-error)))
  21082. (hdmarker (get-text-property (point) 'org-hd-marker))
  21083. (buffer (marker-buffer hdmarker))
  21084. (pos (marker-position hdmarker))
  21085. (inhibit-read-only t)
  21086. newhead)
  21087. (org-with-remote-undo buffer
  21088. (with-current-buffer buffer
  21089. (widen)
  21090. (goto-char pos)
  21091. (org-show-context 'agenda)
  21092. (save-excursion
  21093. (and (outline-next-heading)
  21094. (org-flag-heading nil))) ; show the next heading
  21095. (funcall 'org-priority force-direction)
  21096. (end-of-line 1)
  21097. (setq newhead (org-get-heading)))
  21098. (org-agenda-change-all-lines newhead hdmarker)
  21099. (beginning-of-line 1))))
  21100. (defun org-get-tags-at (&optional pos)
  21101. "Get a list of all headline tags applicable at POS.
  21102. POS defaults to point. If tags are inherited, the list contains
  21103. the targets in the same sequence as the headlines appear, i.e.
  21104. the tags of the current headline come last."
  21105. (interactive)
  21106. (let (tags lastpos)
  21107. (save-excursion
  21108. (save-restriction
  21109. (widen)
  21110. (goto-char (or pos (point)))
  21111. (save-match-data
  21112. (org-back-to-heading t)
  21113. (condition-case nil
  21114. (while (not (equal lastpos (point)))
  21115. (setq lastpos (point))
  21116. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21117. (setq tags (append (org-split-string
  21118. (org-match-string-no-properties 1) ":")
  21119. tags)))
  21120. (or org-use-tag-inheritance (error ""))
  21121. (org-up-heading-all 1))
  21122. (error nil))))
  21123. tags)))
  21124. ;; FIXME: should fix the tags property of the agenda line.
  21125. (defun org-agenda-set-tags ()
  21126. "Set tags for the current headline."
  21127. (interactive)
  21128. (org-agenda-check-no-diary)
  21129. (if (and (org-region-active-p) (interactive-p))
  21130. (call-interactively 'org-change-tag-in-region)
  21131. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21132. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21133. (org-agenda-error)))
  21134. (buffer (marker-buffer hdmarker))
  21135. (pos (marker-position hdmarker))
  21136. (inhibit-read-only t)
  21137. newhead)
  21138. (org-with-remote-undo buffer
  21139. (with-current-buffer buffer
  21140. (widen)
  21141. (goto-char pos)
  21142. (save-excursion
  21143. (org-show-context 'agenda))
  21144. (save-excursion
  21145. (and (outline-next-heading)
  21146. (org-flag-heading nil))) ; show the next heading
  21147. (goto-char pos)
  21148. (call-interactively 'org-set-tags)
  21149. (end-of-line 1)
  21150. (setq newhead (org-get-heading)))
  21151. (org-agenda-change-all-lines newhead hdmarker)
  21152. (beginning-of-line 1)))))
  21153. (defun org-agenda-toggle-archive-tag ()
  21154. "Toggle the archive tag for the current entry."
  21155. (interactive)
  21156. (org-agenda-check-no-diary)
  21157. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21158. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21159. (org-agenda-error)))
  21160. (buffer (marker-buffer hdmarker))
  21161. (pos (marker-position hdmarker))
  21162. (inhibit-read-only t)
  21163. newhead)
  21164. (org-with-remote-undo buffer
  21165. (with-current-buffer buffer
  21166. (widen)
  21167. (goto-char pos)
  21168. (org-show-context 'agenda)
  21169. (save-excursion
  21170. (and (outline-next-heading)
  21171. (org-flag-heading nil))) ; show the next heading
  21172. (call-interactively 'org-toggle-archive-tag)
  21173. (end-of-line 1)
  21174. (setq newhead (org-get-heading)))
  21175. (org-agenda-change-all-lines newhead hdmarker)
  21176. (beginning-of-line 1))))
  21177. (defun org-agenda-date-later (arg &optional what)
  21178. "Change the date of this item to one day later."
  21179. (interactive "p")
  21180. (org-agenda-check-type t 'agenda 'timeline)
  21181. (org-agenda-check-no-diary)
  21182. (let* ((marker (or (get-text-property (point) 'org-marker)
  21183. (org-agenda-error)))
  21184. (buffer (marker-buffer marker))
  21185. (pos (marker-position marker)))
  21186. (org-with-remote-undo buffer
  21187. (with-current-buffer buffer
  21188. (widen)
  21189. (goto-char pos)
  21190. (if (not (org-at-timestamp-p))
  21191. (error "Cannot find time stamp"))
  21192. (org-timestamp-change arg (or what 'day)))
  21193. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21194. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21195. (defun org-agenda-date-earlier (arg &optional what)
  21196. "Change the date of this item to one day earlier."
  21197. (interactive "p")
  21198. (org-agenda-date-later (- arg) what))
  21199. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21200. "Show new date stamp via text properties."
  21201. ;; We use text properties to make this undoable
  21202. (let ((inhibit-read-only t))
  21203. (setq stamp (concat " " prefix " => " stamp))
  21204. (save-excursion
  21205. (goto-char (point-max))
  21206. (while (not (bobp))
  21207. (when (equal marker (get-text-property (point) 'org-marker))
  21208. (move-to-column (- (window-width) (length stamp)) t)
  21209. (if (featurep 'xemacs)
  21210. ;; Use `duplicable' property to trigger undo recording
  21211. (let ((ex (make-extent nil nil))
  21212. (gl (make-glyph stamp)))
  21213. (set-glyph-face gl 'secondary-selection)
  21214. (set-extent-properties
  21215. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21216. (insert-extent ex (1- (point)) (point-at-eol)))
  21217. (add-text-properties
  21218. (1- (point)) (point-at-eol)
  21219. (list 'display (org-add-props stamp nil
  21220. 'face 'secondary-selection))))
  21221. (beginning-of-line 1))
  21222. (beginning-of-line 0)))))
  21223. (defun org-agenda-date-prompt (arg)
  21224. "Change the date of this item. Date is prompted for, with default today.
  21225. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21226. be used to request time specification in the time stamp."
  21227. (interactive "P")
  21228. (org-agenda-check-type t 'agenda 'timeline)
  21229. (org-agenda-check-no-diary)
  21230. (let* ((marker (or (get-text-property (point) 'org-marker)
  21231. (org-agenda-error)))
  21232. (buffer (marker-buffer marker))
  21233. (pos (marker-position marker)))
  21234. (org-with-remote-undo buffer
  21235. (with-current-buffer buffer
  21236. (widen)
  21237. (goto-char pos)
  21238. (if (not (org-at-timestamp-p))
  21239. (error "Cannot find time stamp"))
  21240. (org-time-stamp arg)
  21241. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21242. (defun org-agenda-schedule (arg)
  21243. "Schedule the item at point."
  21244. (interactive "P")
  21245. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21246. (org-agenda-check-no-diary)
  21247. (let* ((marker (or (get-text-property (point) 'org-marker)
  21248. (org-agenda-error)))
  21249. (buffer (marker-buffer marker))
  21250. (pos (marker-position marker))
  21251. (org-insert-labeled-timestamps-at-point nil)
  21252. ts)
  21253. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21254. (set-marker-insertion-type marker t)
  21255. (org-with-remote-undo buffer
  21256. (with-current-buffer buffer
  21257. (widen)
  21258. (goto-char pos)
  21259. (setq ts (org-schedule arg)))
  21260. (org-agenda-show-new-time marker ts "S"))
  21261. (message "Item scheduled for %s" ts)))
  21262. (defun org-agenda-deadline (arg)
  21263. "Schedule the item at point."
  21264. (interactive "P")
  21265. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21266. (org-agenda-check-no-diary)
  21267. (let* ((marker (or (get-text-property (point) 'org-marker)
  21268. (org-agenda-error)))
  21269. (buffer (marker-buffer marker))
  21270. (pos (marker-position marker))
  21271. (org-insert-labeled-timestamps-at-point nil)
  21272. ts)
  21273. (org-with-remote-undo buffer
  21274. (with-current-buffer buffer
  21275. (widen)
  21276. (goto-char pos)
  21277. (setq ts (org-deadline arg)))
  21278. (org-agenda-show-new-time marker ts "S"))
  21279. (message "Deadline for this item set to %s" ts)))
  21280. (defun org-get-heading (&optional no-tags)
  21281. "Return the heading of the current entry, without the stars."
  21282. (save-excursion
  21283. (org-back-to-heading t)
  21284. (if (looking-at
  21285. (if no-tags
  21286. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21287. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21288. (match-string 1) "")))
  21289. (defun org-agenda-clock-in (&optional arg)
  21290. "Start the clock on the currently selected item."
  21291. (interactive "P")
  21292. (org-agenda-check-no-diary)
  21293. (let* ((marker (or (get-text-property (point) 'org-marker)
  21294. (org-agenda-error)))
  21295. (pos (marker-position marker)))
  21296. (org-with-remote-undo (marker-buffer marker)
  21297. (with-current-buffer (marker-buffer marker)
  21298. (widen)
  21299. (goto-char pos)
  21300. (org-clock-in)))))
  21301. (defun org-agenda-clock-out (&optional arg)
  21302. "Stop the currently running clock."
  21303. (interactive "P")
  21304. (unless (marker-buffer org-clock-marker)
  21305. (error "No running clock"))
  21306. (org-with-remote-undo (marker-buffer org-clock-marker)
  21307. (org-clock-out)))
  21308. (defun org-agenda-clock-cancel (&optional arg)
  21309. "Cancel the currently running clock."
  21310. (interactive "P")
  21311. (unless (marker-buffer org-clock-marker)
  21312. (error "No running clock"))
  21313. (org-with-remote-undo (marker-buffer org-clock-marker)
  21314. (org-clock-cancel)))
  21315. (defun org-agenda-diary-entry ()
  21316. "Make a diary entry, like the `i' command from the calendar.
  21317. All the standard commands work: block, weekly etc."
  21318. (interactive)
  21319. (org-agenda-check-type t 'agenda 'timeline)
  21320. (require 'diary-lib)
  21321. (let* ((char (progn
  21322. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21323. (read-char-exclusive)))
  21324. (cmd (cdr (assoc char
  21325. '((?d . insert-diary-entry)
  21326. (?w . insert-weekly-diary-entry)
  21327. (?m . insert-monthly-diary-entry)
  21328. (?y . insert-yearly-diary-entry)
  21329. (?a . insert-anniversary-diary-entry)
  21330. (?b . insert-block-diary-entry)
  21331. (?c . insert-cyclic-diary-entry)))))
  21332. (oldf (symbol-function 'calendar-cursor-to-date))
  21333. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21334. (point (point))
  21335. (mark (or (mark t) (point))))
  21336. (unless cmd
  21337. (error "No command associated with <%c>" char))
  21338. (unless (and (get-text-property point 'day)
  21339. (or (not (equal ?b char))
  21340. (get-text-property mark 'day)))
  21341. (error "Don't know which date to use for diary entry"))
  21342. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21343. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21344. (let ((calendar-mark-ring
  21345. (list (calendar-gregorian-from-absolute
  21346. (or (get-text-property mark 'day)
  21347. (get-text-property point 'day))))))
  21348. (unwind-protect
  21349. (progn
  21350. (fset 'calendar-cursor-to-date
  21351. (lambda (&optional error)
  21352. (calendar-gregorian-from-absolute
  21353. (get-text-property point 'day))))
  21354. (call-interactively cmd))
  21355. (fset 'calendar-cursor-to-date oldf)))))
  21356. (defun org-agenda-execute-calendar-command (cmd)
  21357. "Execute a calendar command from the agenda, with the date associated to
  21358. the cursor position."
  21359. (org-agenda-check-type t 'agenda 'timeline)
  21360. (require 'diary-lib)
  21361. (unless (get-text-property (point) 'day)
  21362. (error "Don't know which date to use for calendar command"))
  21363. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21364. (point (point))
  21365. (date (calendar-gregorian-from-absolute
  21366. (get-text-property point 'day)))
  21367. ;; the following 3 vars are needed in the calendar
  21368. (displayed-day (extract-calendar-day date))
  21369. (displayed-month (extract-calendar-month date))
  21370. (displayed-year (extract-calendar-year date)))
  21371. (unwind-protect
  21372. (progn
  21373. (fset 'calendar-cursor-to-date
  21374. (lambda (&optional error)
  21375. (calendar-gregorian-from-absolute
  21376. (get-text-property point 'day))))
  21377. (call-interactively cmd))
  21378. (fset 'calendar-cursor-to-date oldf))))
  21379. (defun org-agenda-phases-of-moon ()
  21380. "Display the phases of the moon for the 3 months around the cursor date."
  21381. (interactive)
  21382. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21383. (defun org-agenda-holidays ()
  21384. "Display the holidays for the 3 months around the cursor date."
  21385. (interactive)
  21386. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21387. (defun org-agenda-sunrise-sunset (arg)
  21388. "Display sunrise and sunset for the cursor date.
  21389. Latitude and longitude can be specified with the variables
  21390. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21391. argument, latitude and longitude will be prompted for."
  21392. (interactive "P")
  21393. (let ((calendar-longitude (if arg nil calendar-longitude))
  21394. (calendar-latitude (if arg nil calendar-latitude))
  21395. (calendar-location-name
  21396. (if arg "the given coordinates" calendar-location-name)))
  21397. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21398. (defun org-agenda-goto-calendar ()
  21399. "Open the Emacs calendar with the date at the cursor."
  21400. (interactive)
  21401. (org-agenda-check-type t 'agenda 'timeline)
  21402. (let* ((day (or (get-text-property (point) 'day)
  21403. (error "Don't know which date to open in calendar")))
  21404. (date (calendar-gregorian-from-absolute day))
  21405. (calendar-move-hook nil)
  21406. (view-calendar-holidays-initially nil)
  21407. (view-diary-entries-initially nil))
  21408. (calendar)
  21409. (calendar-goto-date date)))
  21410. (defun org-calendar-goto-agenda ()
  21411. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21412. This is a command that has to be installed in `calendar-mode-map'."
  21413. (interactive)
  21414. (org-agenda-list nil (calendar-absolute-from-gregorian
  21415. (calendar-cursor-to-date))
  21416. nil))
  21417. (defun org-agenda-convert-date ()
  21418. (interactive)
  21419. (org-agenda-check-type t 'agenda 'timeline)
  21420. (let ((day (get-text-property (point) 'day))
  21421. date s)
  21422. (unless day
  21423. (error "Don't know which date to convert"))
  21424. (setq date (calendar-gregorian-from-absolute day))
  21425. (setq s (concat
  21426. "Gregorian: " (calendar-date-string date) "\n"
  21427. "ISO: " (calendar-iso-date-string date) "\n"
  21428. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21429. "Julian: " (calendar-julian-date-string date) "\n"
  21430. "Astron. JD: " (calendar-astro-date-string date)
  21431. " (Julian date number at noon UTC)\n"
  21432. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21433. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21434. "French: " (calendar-french-date-string date) "\n"
  21435. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21436. "Mayan: " (calendar-mayan-date-string date) "\n"
  21437. "Coptic: " (calendar-coptic-date-string date) "\n"
  21438. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21439. "Persian: " (calendar-persian-date-string date) "\n"
  21440. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21441. (with-output-to-temp-buffer "*Dates*"
  21442. (princ s))
  21443. (if (fboundp 'fit-window-to-buffer)
  21444. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21445. ;;;; Embedded LaTeX
  21446. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21447. "Keymap for the minor `org-cdlatex-mode'.")
  21448. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21449. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21450. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21451. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21452. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21453. (defvar org-cdlatex-texmathp-advice-is-done nil
  21454. "Flag remembering if we have applied the advice to texmathp already.")
  21455. (define-minor-mode org-cdlatex-mode
  21456. "Toggle the minor `org-cdlatex-mode'.
  21457. This mode supports entering LaTeX environment and math in LaTeX fragments
  21458. in Org-mode.
  21459. \\{org-cdlatex-mode-map}"
  21460. nil " OCDL" nil
  21461. (when org-cdlatex-mode (require 'cdlatex))
  21462. (unless org-cdlatex-texmathp-advice-is-done
  21463. (setq org-cdlatex-texmathp-advice-is-done t)
  21464. (defadvice texmathp (around org-math-always-on activate)
  21465. "Always return t in org-mode buffers.
  21466. This is because we want to insert math symbols without dollars even outside
  21467. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21468. en embedded LaTeX fragement, let texmathp do its job.
  21469. \\[org-cdlatex-mode-map]"
  21470. (interactive)
  21471. (let (p)
  21472. (cond
  21473. ((not (org-mode-p)) ad-do-it)
  21474. ((eq this-command 'cdlatex-math-symbol)
  21475. (setq ad-return-value t
  21476. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21477. (t
  21478. (let ((p (org-inside-LaTeX-fragment-p)))
  21479. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21480. (setq ad-return-value t
  21481. texmathp-why '("Org-mode embedded math" . 0))
  21482. (if p ad-do-it)))))))))
  21483. (defun turn-on-org-cdlatex ()
  21484. "Unconditionally turn on `org-cdlatex-mode'."
  21485. (org-cdlatex-mode 1))
  21486. (defun org-inside-LaTeX-fragment-p ()
  21487. "Test if point is inside a LaTeX fragment.
  21488. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21489. sequence appearing also before point.
  21490. Even though the matchers for math are configurable, this function assumes
  21491. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21492. delimiters are skipped when they have been removed by customization.
  21493. The return value is nil, or a cons cell with the delimiter and
  21494. and the position of this delimiter.
  21495. This function does a reasonably good job, but can locally be fooled by
  21496. for example currency specifications. For example it will assume being in
  21497. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21498. fragments that are properly closed, but during editing, we have to live
  21499. with the uncertainty caused by missing closing delimiters. This function
  21500. looks only before point, not after."
  21501. (catch 'exit
  21502. (let ((pos (point))
  21503. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21504. (lim (progn
  21505. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21506. (point)))
  21507. dd-on str (start 0) m re)
  21508. (goto-char pos)
  21509. (when dodollar
  21510. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21511. re (nth 1 (assoc "$" org-latex-regexps)))
  21512. (while (string-match re str start)
  21513. (cond
  21514. ((= (match-end 0) (length str))
  21515. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21516. ((= (match-end 0) (- (length str) 5))
  21517. (throw 'exit nil))
  21518. (t (setq start (match-end 0))))))
  21519. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21520. (goto-char pos)
  21521. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21522. (and (match-beginning 2) (throw 'exit nil))
  21523. ;; count $$
  21524. (while (re-search-backward "\\$\\$" lim t)
  21525. (setq dd-on (not dd-on)))
  21526. (goto-char pos)
  21527. (if dd-on (cons "$$" m))))))
  21528. (defun org-try-cdlatex-tab ()
  21529. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21530. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21531. - inside a LaTeX fragment, or
  21532. - after the first word in a line, where an abbreviation expansion could
  21533. insert a LaTeX environment."
  21534. (when org-cdlatex-mode
  21535. (cond
  21536. ((save-excursion
  21537. (skip-chars-backward "a-zA-Z0-9*")
  21538. (skip-chars-backward " \t")
  21539. (bolp))
  21540. (cdlatex-tab) t)
  21541. ((org-inside-LaTeX-fragment-p)
  21542. (cdlatex-tab) t)
  21543. (t nil))))
  21544. (defun org-cdlatex-underscore-caret (&optional arg)
  21545. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21546. Revert to the normal definition outside of these fragments."
  21547. (interactive "P")
  21548. (if (org-inside-LaTeX-fragment-p)
  21549. (call-interactively 'cdlatex-sub-superscript)
  21550. (let (org-cdlatex-mode)
  21551. (call-interactively (key-binding (vector last-input-event))))))
  21552. (defun org-cdlatex-math-modify (&optional arg)
  21553. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21554. Revert to the normal definition outside of these fragments."
  21555. (interactive "P")
  21556. (if (org-inside-LaTeX-fragment-p)
  21557. (call-interactively 'cdlatex-math-modify)
  21558. (let (org-cdlatex-mode)
  21559. (call-interactively (key-binding (vector last-input-event))))))
  21560. (defvar org-latex-fragment-image-overlays nil
  21561. "List of overlays carrying the images of latex fragments.")
  21562. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21563. (defun org-remove-latex-fragment-image-overlays ()
  21564. "Remove all overlays with LaTeX fragment images in current buffer."
  21565. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21566. (setq org-latex-fragment-image-overlays nil))
  21567. (defun org-preview-latex-fragment (&optional subtree)
  21568. "Preview the LaTeX fragment at point, or all locally or globally.
  21569. If the cursor is in a LaTeX fragment, create the image and overlay
  21570. it over the source code. If there is no fragment at point, display
  21571. all fragments in the current text, from one headline to the next. With
  21572. prefix SUBTREE, display all fragments in the current subtree. With a
  21573. double prefix `C-u C-u', or when the cursor is before the first headline,
  21574. display all fragments in the buffer.
  21575. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21576. (interactive "P")
  21577. (org-remove-latex-fragment-image-overlays)
  21578. (save-excursion
  21579. (save-restriction
  21580. (let (beg end at msg)
  21581. (cond
  21582. ((or (equal subtree '(16))
  21583. (not (save-excursion
  21584. (re-search-backward (concat "^" outline-regexp) nil t))))
  21585. (setq beg (point-min) end (point-max)
  21586. msg "Creating images for buffer...%s"))
  21587. ((equal subtree '(4))
  21588. (org-back-to-heading)
  21589. (setq beg (point) end (org-end-of-subtree t)
  21590. msg "Creating images for subtree...%s"))
  21591. (t
  21592. (if (setq at (org-inside-LaTeX-fragment-p))
  21593. (goto-char (max (point-min) (- (cdr at) 2)))
  21594. (org-back-to-heading))
  21595. (setq beg (point) end (progn (outline-next-heading) (point))
  21596. msg (if at "Creating image...%s"
  21597. "Creating images for entry...%s"))))
  21598. (message msg "")
  21599. (narrow-to-region beg end)
  21600. (goto-char beg)
  21601. (org-format-latex
  21602. (concat "ltxpng/" (file-name-sans-extension
  21603. (file-name-nondirectory
  21604. buffer-file-name)))
  21605. default-directory 'overlays msg at 'forbuffer)
  21606. (message msg "done. Use `C-c C-c' to remove images.")))))
  21607. (defvar org-latex-regexps
  21608. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21609. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21610. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21611. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21612. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21613. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21614. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21615. "Regular expressions for matching embedded LaTeX.")
  21616. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21617. "Replace LaTeX fragments with links to an image, and produce images."
  21618. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21619. (let* ((prefixnodir (file-name-nondirectory prefix))
  21620. (absprefix (expand-file-name prefix dir))
  21621. (todir (file-name-directory absprefix))
  21622. (opt org-format-latex-options)
  21623. (matchers (plist-get opt :matchers))
  21624. (re-list org-latex-regexps)
  21625. (cnt 0) txt link beg end re e checkdir
  21626. m n block linkfile movefile ov)
  21627. ;; Check if there are old images files with this prefix, and remove them
  21628. (when (file-directory-p todir)
  21629. (mapc 'delete-file
  21630. (directory-files
  21631. todir 'full
  21632. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21633. ;; Check the different regular expressions
  21634. (while (setq e (pop re-list))
  21635. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21636. block (if (nth 3 e) "\n\n" ""))
  21637. (when (member m matchers)
  21638. (goto-char (point-min))
  21639. (while (re-search-forward re nil t)
  21640. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21641. (setq txt (match-string n)
  21642. beg (match-beginning n) end (match-end n)
  21643. cnt (1+ cnt)
  21644. linkfile (format "%s_%04d.png" prefix cnt)
  21645. movefile (format "%s_%04d.png" absprefix cnt)
  21646. link (concat block "[[file:" linkfile "]]" block))
  21647. (if msg (message msg cnt))
  21648. (goto-char beg)
  21649. (unless checkdir ; make sure the directory exists
  21650. (setq checkdir t)
  21651. (or (file-directory-p todir) (make-directory todir)))
  21652. (org-create-formula-image
  21653. txt movefile opt forbuffer)
  21654. (if overlays
  21655. (progn
  21656. (setq ov (org-make-overlay beg end))
  21657. (if (featurep 'xemacs)
  21658. (progn
  21659. (org-overlay-put ov 'invisible t)
  21660. (org-overlay-put
  21661. ov 'end-glyph
  21662. (make-glyph (vector 'png :file movefile))))
  21663. (org-overlay-put
  21664. ov 'display
  21665. (list 'image :type 'png :file movefile :ascent 'center)))
  21666. (push ov org-latex-fragment-image-overlays)
  21667. (goto-char end))
  21668. (delete-region beg end)
  21669. (insert link))))))))
  21670. ;; This function borrows from Ganesh Swami's latex2png.el
  21671. (defun org-create-formula-image (string tofile options buffer)
  21672. (let* ((tmpdir (if (featurep 'xemacs)
  21673. (temp-directory)
  21674. temporary-file-directory))
  21675. (texfilebase (make-temp-name
  21676. (expand-file-name "orgtex" tmpdir)))
  21677. (texfile (concat texfilebase ".tex"))
  21678. (dvifile (concat texfilebase ".dvi"))
  21679. (pngfile (concat texfilebase ".png"))
  21680. (fnh (face-attribute 'default :height nil))
  21681. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21682. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21683. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21684. "Black"))
  21685. (bg (or (plist-get options (if buffer :background :html-background))
  21686. "Transparent")))
  21687. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21688. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21689. (with-temp-file texfile
  21690. (insert org-format-latex-header
  21691. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21692. (let ((dir default-directory))
  21693. (condition-case nil
  21694. (progn
  21695. (cd tmpdir)
  21696. (call-process "latex" nil nil nil texfile))
  21697. (error nil))
  21698. (cd dir))
  21699. (if (not (file-exists-p dvifile))
  21700. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21701. (call-process "dvipng" nil nil nil
  21702. "-E" "-fg" fg "-bg" bg
  21703. "-D" dpi
  21704. ;;"-x" scale "-y" scale
  21705. "-T" "tight"
  21706. "-o" pngfile
  21707. dvifile)
  21708. (if (not (file-exists-p pngfile))
  21709. (progn (message "Failed to create png file from %s" texfile) nil)
  21710. ;; Use the requested file name and clean up
  21711. (copy-file pngfile tofile 'replace)
  21712. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21713. (delete-file (concat texfilebase e)))
  21714. pngfile))))
  21715. (defun org-dvipng-color (attr)
  21716. "Return an rgb color specification for dvipng."
  21717. (apply 'format "rgb %s %s %s"
  21718. (mapcar 'org-normalize-color
  21719. (color-values (face-attribute 'default attr nil)))))
  21720. (defun org-normalize-color (value)
  21721. "Return string to be used as color value for an RGB component."
  21722. (format "%g" (/ value 65535.0)))
  21723. ;;;; Exporting
  21724. ;;; Variables, constants, and parameter plists
  21725. (defconst org-level-max 20)
  21726. (defvar org-export-html-preamble nil
  21727. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21728. (defvar org-export-html-postamble nil
  21729. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21730. (defvar org-export-html-auto-preamble t
  21731. "Should default preamble be inserted? Set by publishing functions.")
  21732. (defvar org-export-html-auto-postamble t
  21733. "Should default postamble be inserted? Set by publishing functions.")
  21734. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21735. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21736. (defconst org-export-plist-vars
  21737. '((:language . org-export-default-language)
  21738. (:customtime . org-display-custom-times)
  21739. (:headline-levels . org-export-headline-levels)
  21740. (:section-numbers . org-export-with-section-numbers)
  21741. (:table-of-contents . org-export-with-toc)
  21742. (:preserve-breaks . org-export-preserve-breaks)
  21743. (:archived-trees . org-export-with-archived-trees)
  21744. (:emphasize . org-export-with-emphasize)
  21745. (:sub-superscript . org-export-with-sub-superscripts)
  21746. (:special-strings . org-export-with-special-strings)
  21747. (:footnotes . org-export-with-footnotes)
  21748. (:drawers . org-export-with-drawers)
  21749. (:tags . org-export-with-tags)
  21750. (:TeX-macros . org-export-with-TeX-macros)
  21751. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21752. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21753. (:fixed-width . org-export-with-fixed-width)
  21754. (:timestamps . org-export-with-timestamps)
  21755. (:author-info . org-export-author-info)
  21756. (:time-stamp-file . org-export-time-stamp-file)
  21757. (:tables . org-export-with-tables)
  21758. (:table-auto-headline . org-export-highlight-first-table-line)
  21759. (:style . org-export-html-style)
  21760. (:agenda-style . org-agenda-export-html-style)
  21761. (:convert-org-links . org-export-html-link-org-files-as-html)
  21762. (:inline-images . org-export-html-inline-images)
  21763. (:html-extension . org-export-html-extension)
  21764. (:html-table-tag . org-export-html-table-tag)
  21765. (:expand-quoted-html . org-export-html-expand)
  21766. (:timestamp . org-export-html-with-timestamp)
  21767. (:publishing-directory . org-export-publishing-directory)
  21768. (:preamble . org-export-html-preamble)
  21769. (:postamble . org-export-html-postamble)
  21770. (:auto-preamble . org-export-html-auto-preamble)
  21771. (:auto-postamble . org-export-html-auto-postamble)
  21772. (:author . user-full-name)
  21773. (:email . user-mail-address)))
  21774. (defun org-default-export-plist ()
  21775. "Return the property list with default settings for the export variables."
  21776. (let ((l org-export-plist-vars) rtn e)
  21777. (while (setq e (pop l))
  21778. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21779. rtn))
  21780. (defun org-infile-export-plist ()
  21781. "Return the property list with file-local settings for export."
  21782. (save-excursion
  21783. (save-restriction
  21784. (widen)
  21785. (goto-char 0)
  21786. (let ((re (org-make-options-regexp
  21787. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21788. p key val text options)
  21789. (while (re-search-forward re nil t)
  21790. (setq key (org-match-string-no-properties 1)
  21791. val (org-match-string-no-properties 2))
  21792. (cond
  21793. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21794. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21795. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21796. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21797. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21798. ((string-equal key "TEXT")
  21799. (setq text (if text (concat text "\n" val) val)))
  21800. ((string-equal key "OPTIONS") (setq options val))))
  21801. (setq p (plist-put p :text text))
  21802. (when options
  21803. (let ((op '(("H" . :headline-levels)
  21804. ("num" . :section-numbers)
  21805. ("toc" . :table-of-contents)
  21806. ("\\n" . :preserve-breaks)
  21807. ("@" . :expand-quoted-html)
  21808. (":" . :fixed-width)
  21809. ("|" . :tables)
  21810. ("^" . :sub-superscript)
  21811. ("-" . :special-strings)
  21812. ("f" . :footnotes)
  21813. ("d" . :drawers)
  21814. ("tags" . :tags)
  21815. ("*" . :emphasize)
  21816. ("TeX" . :TeX-macros)
  21817. ("LaTeX" . :LaTeX-fragments)
  21818. ("skip" . :skip-before-1st-heading)
  21819. ("author" . :author-info)
  21820. ("timestamp" . :time-stamp-file)))
  21821. o)
  21822. (while (setq o (pop op))
  21823. (if (string-match (concat (regexp-quote (car o))
  21824. ":\\([^ \t\n\r;,.]*\\)")
  21825. options)
  21826. (setq p (plist-put p (cdr o)
  21827. (car (read-from-string
  21828. (match-string 1 options)))))))))
  21829. p))))
  21830. (defun org-export-directory (type plist)
  21831. (let* ((val (plist-get plist :publishing-directory))
  21832. (dir (if (listp val)
  21833. (or (cdr (assoc type val)) ".")
  21834. val)))
  21835. dir))
  21836. (defun org-skip-comments (lines)
  21837. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21838. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21839. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21840. (case-fold-search nil)
  21841. rtn line level)
  21842. (while (setq line (pop lines))
  21843. (cond
  21844. ((and (string-match re1 line)
  21845. (setq level (- (match-end 1) (match-beginning 1))))
  21846. ;; Beginning of a COMMENT subtree. Skip it.
  21847. (while (and (setq line (pop lines))
  21848. (or (not (string-match re2 line))
  21849. (> (- (match-end 1) (match-beginning 1)) level))))
  21850. (setq lines (cons line lines)))
  21851. ((string-match "^#" line)
  21852. ;; an ordinary comment line
  21853. )
  21854. ((and org-export-table-remove-special-lines
  21855. (string-match "^[ \t]*|" line)
  21856. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21857. (and (string-match "| *<[0-9]+> *|" line)
  21858. (not (string-match "| *[^ <|]" line)))))
  21859. ;; a special table line that should be removed
  21860. )
  21861. (t (setq rtn (cons line rtn)))))
  21862. (nreverse rtn)))
  21863. (defun org-export (&optional arg)
  21864. (interactive)
  21865. (let ((help "[t] insert the export option template
  21866. \[v] limit export to visible part of outline tree
  21867. \[a] export as ASCII
  21868. \[h] export as HTML
  21869. \[H] export as HTML to temporary buffer
  21870. \[R] export region as HTML
  21871. \[b] export as HTML and browse immediately
  21872. \[x] export as XOXO
  21873. \[l] export as LaTeX
  21874. \[L] export as LaTeX to temporary buffer
  21875. \[i] export current file as iCalendar file
  21876. \[I] export all agenda files as iCalendar files
  21877. \[c] export agenda files into combined iCalendar file
  21878. \[F] publish current file
  21879. \[P] publish current project
  21880. \[X] publish... (project will be prompted for)
  21881. \[A] publish all projects")
  21882. (cmds
  21883. '((?t . org-insert-export-options-template)
  21884. (?v . org-export-visible)
  21885. (?a . org-export-as-ascii)
  21886. (?h . org-export-as-html)
  21887. (?b . org-export-as-html-and-open)
  21888. (?H . org-export-as-html-to-buffer)
  21889. (?R . org-export-region-as-html)
  21890. (?x . org-export-as-xoxo)
  21891. (?l . org-export-as-latex)
  21892. (?L . org-export-as-latex-to-buffer)
  21893. (?i . org-export-icalendar-this-file)
  21894. (?I . org-export-icalendar-all-agenda-files)
  21895. (?c . org-export-icalendar-combine-agenda-files)
  21896. (?F . org-publish-current-file)
  21897. (?P . org-publish-current-project)
  21898. (?X . org-publish)
  21899. (?A . org-publish-all)))
  21900. r1 r2 ass)
  21901. (save-window-excursion
  21902. (delete-other-windows)
  21903. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21904. (princ help))
  21905. (message "Select command: ")
  21906. (setq r1 (read-char-exclusive)))
  21907. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21908. (if (setq ass (assq r2 cmds))
  21909. (call-interactively (cdr ass))
  21910. (error "No command associated with key %c" r1))))
  21911. (defconst org-html-entities
  21912. '(("nbsp")
  21913. ("iexcl")
  21914. ("cent")
  21915. ("pound")
  21916. ("curren")
  21917. ("yen")
  21918. ("brvbar")
  21919. ("vert" . "&#124;")
  21920. ("sect")
  21921. ("uml")
  21922. ("copy")
  21923. ("ordf")
  21924. ("laquo")
  21925. ("not")
  21926. ("shy")
  21927. ("reg")
  21928. ("macr")
  21929. ("deg")
  21930. ("plusmn")
  21931. ("sup2")
  21932. ("sup3")
  21933. ("acute")
  21934. ("micro")
  21935. ("para")
  21936. ("middot")
  21937. ("odot"."o")
  21938. ("star"."*")
  21939. ("cedil")
  21940. ("sup1")
  21941. ("ordm")
  21942. ("raquo")
  21943. ("frac14")
  21944. ("frac12")
  21945. ("frac34")
  21946. ("iquest")
  21947. ("Agrave")
  21948. ("Aacute")
  21949. ("Acirc")
  21950. ("Atilde")
  21951. ("Auml")
  21952. ("Aring") ("AA"."&Aring;")
  21953. ("AElig")
  21954. ("Ccedil")
  21955. ("Egrave")
  21956. ("Eacute")
  21957. ("Ecirc")
  21958. ("Euml")
  21959. ("Igrave")
  21960. ("Iacute")
  21961. ("Icirc")
  21962. ("Iuml")
  21963. ("ETH")
  21964. ("Ntilde")
  21965. ("Ograve")
  21966. ("Oacute")
  21967. ("Ocirc")
  21968. ("Otilde")
  21969. ("Ouml")
  21970. ("times")
  21971. ("Oslash")
  21972. ("Ugrave")
  21973. ("Uacute")
  21974. ("Ucirc")
  21975. ("Uuml")
  21976. ("Yacute")
  21977. ("THORN")
  21978. ("szlig")
  21979. ("agrave")
  21980. ("aacute")
  21981. ("acirc")
  21982. ("atilde")
  21983. ("auml")
  21984. ("aring")
  21985. ("aelig")
  21986. ("ccedil")
  21987. ("egrave")
  21988. ("eacute")
  21989. ("ecirc")
  21990. ("euml")
  21991. ("igrave")
  21992. ("iacute")
  21993. ("icirc")
  21994. ("iuml")
  21995. ("eth")
  21996. ("ntilde")
  21997. ("ograve")
  21998. ("oacute")
  21999. ("ocirc")
  22000. ("otilde")
  22001. ("ouml")
  22002. ("divide")
  22003. ("oslash")
  22004. ("ugrave")
  22005. ("uacute")
  22006. ("ucirc")
  22007. ("uuml")
  22008. ("yacute")
  22009. ("thorn")
  22010. ("yuml")
  22011. ("fnof")
  22012. ("Alpha")
  22013. ("Beta")
  22014. ("Gamma")
  22015. ("Delta")
  22016. ("Epsilon")
  22017. ("Zeta")
  22018. ("Eta")
  22019. ("Theta")
  22020. ("Iota")
  22021. ("Kappa")
  22022. ("Lambda")
  22023. ("Mu")
  22024. ("Nu")
  22025. ("Xi")
  22026. ("Omicron")
  22027. ("Pi")
  22028. ("Rho")
  22029. ("Sigma")
  22030. ("Tau")
  22031. ("Upsilon")
  22032. ("Phi")
  22033. ("Chi")
  22034. ("Psi")
  22035. ("Omega")
  22036. ("alpha")
  22037. ("beta")
  22038. ("gamma")
  22039. ("delta")
  22040. ("epsilon")
  22041. ("varepsilon"."&epsilon;")
  22042. ("zeta")
  22043. ("eta")
  22044. ("theta")
  22045. ("iota")
  22046. ("kappa")
  22047. ("lambda")
  22048. ("mu")
  22049. ("nu")
  22050. ("xi")
  22051. ("omicron")
  22052. ("pi")
  22053. ("rho")
  22054. ("sigmaf") ("varsigma"."&sigmaf;")
  22055. ("sigma")
  22056. ("tau")
  22057. ("upsilon")
  22058. ("phi")
  22059. ("chi")
  22060. ("psi")
  22061. ("omega")
  22062. ("thetasym") ("vartheta"."&thetasym;")
  22063. ("upsih")
  22064. ("piv")
  22065. ("bull") ("bullet"."&bull;")
  22066. ("hellip") ("dots"."&hellip;")
  22067. ("prime")
  22068. ("Prime")
  22069. ("oline")
  22070. ("frasl")
  22071. ("weierp")
  22072. ("image")
  22073. ("real")
  22074. ("trade")
  22075. ("alefsym")
  22076. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22077. ("uarr") ("uparrow"."&uarr;")
  22078. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22079. ("darr")("downarrow"."&darr;")
  22080. ("harr") ("leftrightarrow"."&harr;")
  22081. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22082. ("lArr") ("Leftarrow"."&lArr;")
  22083. ("uArr") ("Uparrow"."&uArr;")
  22084. ("rArr") ("Rightarrow"."&rArr;")
  22085. ("dArr") ("Downarrow"."&dArr;")
  22086. ("hArr") ("Leftrightarrow"."&hArr;")
  22087. ("forall")
  22088. ("part") ("partial"."&part;")
  22089. ("exist") ("exists"."&exist;")
  22090. ("empty") ("emptyset"."&empty;")
  22091. ("nabla")
  22092. ("isin") ("in"."&isin;")
  22093. ("notin")
  22094. ("ni")
  22095. ("prod")
  22096. ("sum")
  22097. ("minus")
  22098. ("lowast") ("ast"."&lowast;")
  22099. ("radic")
  22100. ("prop") ("proptp"."&prop;")
  22101. ("infin") ("infty"."&infin;")
  22102. ("ang") ("angle"."&ang;")
  22103. ("and") ("wedge"."&and;")
  22104. ("or") ("vee"."&or;")
  22105. ("cap")
  22106. ("cup")
  22107. ("int")
  22108. ("there4")
  22109. ("sim")
  22110. ("cong") ("simeq"."&cong;")
  22111. ("asymp")("approx"."&asymp;")
  22112. ("ne") ("neq"."&ne;")
  22113. ("equiv")
  22114. ("le")
  22115. ("ge")
  22116. ("sub") ("subset"."&sub;")
  22117. ("sup") ("supset"."&sup;")
  22118. ("nsub")
  22119. ("sube")
  22120. ("supe")
  22121. ("oplus")
  22122. ("otimes")
  22123. ("perp")
  22124. ("sdot") ("cdot"."&sdot;")
  22125. ("lceil")
  22126. ("rceil")
  22127. ("lfloor")
  22128. ("rfloor")
  22129. ("lang")
  22130. ("rang")
  22131. ("loz") ("Diamond"."&loz;")
  22132. ("spades") ("spadesuit"."&spades;")
  22133. ("clubs") ("clubsuit"."&clubs;")
  22134. ("hearts") ("diamondsuit"."&hearts;")
  22135. ("diams") ("diamondsuit"."&diams;")
  22136. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22137. ("quot")
  22138. ("amp")
  22139. ("lt")
  22140. ("gt")
  22141. ("OElig")
  22142. ("oelig")
  22143. ("Scaron")
  22144. ("scaron")
  22145. ("Yuml")
  22146. ("circ")
  22147. ("tilde")
  22148. ("ensp")
  22149. ("emsp")
  22150. ("thinsp")
  22151. ("zwnj")
  22152. ("zwj")
  22153. ("lrm")
  22154. ("rlm")
  22155. ("ndash")
  22156. ("mdash")
  22157. ("lsquo")
  22158. ("rsquo")
  22159. ("sbquo")
  22160. ("ldquo")
  22161. ("rdquo")
  22162. ("bdquo")
  22163. ("dagger")
  22164. ("Dagger")
  22165. ("permil")
  22166. ("lsaquo")
  22167. ("rsaquo")
  22168. ("euro")
  22169. ("arccos"."arccos")
  22170. ("arcsin"."arcsin")
  22171. ("arctan"."arctan")
  22172. ("arg"."arg")
  22173. ("cos"."cos")
  22174. ("cosh"."cosh")
  22175. ("cot"."cot")
  22176. ("coth"."coth")
  22177. ("csc"."csc")
  22178. ("deg"."deg")
  22179. ("det"."det")
  22180. ("dim"."dim")
  22181. ("exp"."exp")
  22182. ("gcd"."gcd")
  22183. ("hom"."hom")
  22184. ("inf"."inf")
  22185. ("ker"."ker")
  22186. ("lg"."lg")
  22187. ("lim"."lim")
  22188. ("liminf"."liminf")
  22189. ("limsup"."limsup")
  22190. ("ln"."ln")
  22191. ("log"."log")
  22192. ("max"."max")
  22193. ("min"."min")
  22194. ("Pr"."Pr")
  22195. ("sec"."sec")
  22196. ("sin"."sin")
  22197. ("sinh"."sinh")
  22198. ("sup"."sup")
  22199. ("tan"."tan")
  22200. ("tanh"."tanh")
  22201. )
  22202. "Entities for TeX->HTML translation.
  22203. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22204. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22205. In that case, \"\\ent\" will be translated to \"&other;\".
  22206. The list contains HTML entities for Latin-1, Greek and other symbols.
  22207. It is supplemented by a number of commonly used TeX macros with appropriate
  22208. translations. There is currently no way for users to extend this.")
  22209. ;;; General functions for all backends
  22210. (defun org-cleaned-string-for-export (string &rest parameters)
  22211. "Cleanup a buffer STRING so that links can be created safely."
  22212. (interactive)
  22213. (let* ((re-radio (and org-target-link-regexp
  22214. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22215. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22216. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22217. (re-archive (concat ":" org-archive-tag ":"))
  22218. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22219. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22220. (htmlp (plist-get parameters :for-html))
  22221. (asciip (plist-get parameters :for-ascii))
  22222. (latexp (plist-get parameters :for-LaTeX))
  22223. (commentsp (plist-get parameters :comments))
  22224. (archived-trees (plist-get parameters :archived-trees))
  22225. (inhibit-read-only t)
  22226. (drawers org-drawers)
  22227. (exp-drawers (plist-get parameters :drawers))
  22228. (outline-regexp "\\*+ ")
  22229. a b xx
  22230. rtn p)
  22231. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22232. (erase-buffer)
  22233. (insert string)
  22234. ;; Remove license-to-kill stuff
  22235. (while (setq p (text-property-any (point-min) (point-max)
  22236. :org-license-to-kill t))
  22237. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22238. (let ((org-inhibit-startup t)) (org-mode))
  22239. (untabify (point-min) (point-max))
  22240. ;; Get rid of drawers
  22241. (unless (eq t exp-drawers)
  22242. (goto-char (point-min))
  22243. (let ((re (concat "^[ \t]*:\\("
  22244. (mapconcat
  22245. 'identity
  22246. (org-delete-all exp-drawers
  22247. (copy-sequence drawers))
  22248. "\\|")
  22249. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22250. (while (re-search-forward re nil t)
  22251. (replace-match ""))))
  22252. ;; Get the correct stuff before the first headline
  22253. (when (plist-get parameters :skip-before-1st-heading)
  22254. (goto-char (point-min))
  22255. (when (re-search-forward "^\\*+[ \t]" nil t)
  22256. (delete-region (point-min) (match-beginning 0))
  22257. (goto-char (point-min))
  22258. (insert "\n")))
  22259. (when (plist-get parameters :add-text)
  22260. (goto-char (point-min))
  22261. (insert (plist-get parameters :add-text) "\n"))
  22262. ;; Get rid of archived trees
  22263. (when (not (eq archived-trees t))
  22264. (goto-char (point-min))
  22265. (while (re-search-forward re-archive nil t)
  22266. (if (not (org-on-heading-p t))
  22267. (org-end-of-subtree t)
  22268. (beginning-of-line 1)
  22269. (setq a (if archived-trees
  22270. (1+ (point-at-eol)) (point))
  22271. b (org-end-of-subtree t))
  22272. (if (> b a) (delete-region a b)))))
  22273. ;; Find targets in comments and move them out of comments,
  22274. ;; but mark them as targets that should be invisible
  22275. (goto-char (point-min))
  22276. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22277. (replace-match "\\1(INVISIBLE)"))
  22278. ;; Protect backend specific stuff, throw away the others.
  22279. (let ((formatters
  22280. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22281. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22282. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22283. fmt)
  22284. (goto-char (point-min))
  22285. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22286. (goto-char (match-end 0))
  22287. (while (not (looking-at "#\\+END_EXAMPLE"))
  22288. (insert ": ")
  22289. (beginning-of-line 2)))
  22290. (goto-char (point-min))
  22291. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22292. (add-text-properties (match-beginning 0) (match-end 0)
  22293. '(org-protected t)))
  22294. (while formatters
  22295. (setq fmt (pop formatters))
  22296. (when (car fmt)
  22297. (goto-char (point-min))
  22298. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22299. ":[ \t]*\\(.*\\)") nil t)
  22300. (replace-match "\\1" t)
  22301. (add-text-properties
  22302. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22303. '(org-protected t))))
  22304. (goto-char (point-min))
  22305. (while (re-search-forward
  22306. (concat "^#\\+"
  22307. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22308. (cadddr fmt) "\\>.*\n?") nil t)
  22309. (if (car fmt)
  22310. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22311. '(org-protected t))
  22312. (delete-region (match-beginning 0) (match-end 0))))))
  22313. ;; Protect quoted subtrees
  22314. (goto-char (point-min))
  22315. (while (re-search-forward re-quote nil t)
  22316. (goto-char (match-beginning 0))
  22317. (end-of-line 1)
  22318. (add-text-properties (point) (org-end-of-subtree t)
  22319. '(org-protected t)))
  22320. ;; Protect verbatim elements
  22321. (goto-char (point-min))
  22322. (while (re-search-forward org-verbatim-re nil t)
  22323. (add-text-properties (match-beginning 4) (match-end 4)
  22324. '(org-protected t))
  22325. (goto-char (1+ (match-end 4))))
  22326. ;; Remove subtrees that are commented
  22327. (goto-char (point-min))
  22328. (while (re-search-forward re-commented nil t)
  22329. (goto-char (match-beginning 0))
  22330. (delete-region (point) (org-end-of-subtree t)))
  22331. ;; Remove special table lines
  22332. (when org-export-table-remove-special-lines
  22333. (goto-char (point-min))
  22334. (while (re-search-forward "^[ \t]*|" nil t)
  22335. (beginning-of-line 1)
  22336. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22337. (and (looking-at ".*?| *<[0-9]+> *|")
  22338. (not (looking-at ".*?| *[^ <|]"))))
  22339. (delete-region (max (point-min) (1- (point-at-bol)))
  22340. (point-at-eol))
  22341. (end-of-line 1))))
  22342. ;; Specific LaTeX stuff
  22343. (when latexp
  22344. (require 'org-export-latex nil)
  22345. (org-export-latex-cleaned-string))
  22346. (when asciip
  22347. (org-export-ascii-clean-string))
  22348. ;; Specific HTML stuff
  22349. (when htmlp
  22350. ;; Convert LaTeX fragments to images
  22351. (when (plist-get parameters :LaTeX-fragments)
  22352. (org-format-latex
  22353. (concat "ltxpng/" (file-name-sans-extension
  22354. (file-name-nondirectory
  22355. org-current-export-file)))
  22356. org-current-export-dir nil "Creating LaTeX image %s"))
  22357. (message "Exporting..."))
  22358. ;; Remove or replace comments
  22359. (goto-char (point-min))
  22360. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22361. (if commentsp
  22362. (progn (add-text-properties
  22363. (match-beginning 0) (match-end 0) '(org-protected t))
  22364. (replace-match (format commentsp (match-string 1)) t t))
  22365. (replace-match "")))
  22366. ;; Find matches for radio targets and turn them into internal links
  22367. (goto-char (point-min))
  22368. (when re-radio
  22369. (while (re-search-forward re-radio nil t)
  22370. (org-if-unprotected
  22371. (replace-match "\\1[[\\2]]"))))
  22372. ;; Find all links that contain a newline and put them into a single line
  22373. (goto-char (point-min))
  22374. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22375. (org-if-unprotected
  22376. (replace-match "\\1 \\3")
  22377. (goto-char (match-beginning 0))))
  22378. ;; Normalize links: Convert angle and plain links into bracket links
  22379. ;; Expand link abbreviations
  22380. (goto-char (point-min))
  22381. (while (re-search-forward re-plain-link nil t)
  22382. (goto-char (1- (match-end 0)))
  22383. (org-if-unprotected
  22384. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22385. ":" (match-string 3) "]]")))
  22386. ;; added 'org-link face to links
  22387. (put-text-property 0 (length s) 'face 'org-link s)
  22388. (replace-match s t t))))
  22389. (goto-char (point-min))
  22390. (while (re-search-forward re-angle-link nil t)
  22391. (goto-char (1- (match-end 0)))
  22392. (org-if-unprotected
  22393. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22394. ":" (match-string 3) "]]")))
  22395. (put-text-property 0 (length s) 'face 'org-link s)
  22396. (replace-match s t t))))
  22397. (goto-char (point-min))
  22398. (while (re-search-forward org-bracket-link-regexp nil t)
  22399. (org-if-unprotected
  22400. (let* ((s (concat "[[" (setq xx (save-match-data
  22401. (org-link-expand-abbrev (match-string 1))))
  22402. "]"
  22403. (if (match-end 3)
  22404. (match-string 2)
  22405. (concat "[" xx "]"))
  22406. "]")))
  22407. (put-text-property 0 (length s) 'face 'org-link s)
  22408. (replace-match s t t))))
  22409. ;; Find multiline emphasis and put them into single line
  22410. (when (plist-get parameters :emph-multiline)
  22411. (goto-char (point-min))
  22412. (while (re-search-forward org-emph-re nil t)
  22413. (if (not (= (char-after (match-beginning 3))
  22414. (char-after (match-beginning 4))))
  22415. (org-if-unprotected
  22416. (subst-char-in-region (match-beginning 0) (match-end 0)
  22417. ?\n ?\ t)
  22418. (goto-char (1- (match-end 0))))
  22419. (goto-char (1+ (match-beginning 0))))))
  22420. (setq rtn (buffer-string)))
  22421. (kill-buffer " org-mode-tmp")
  22422. rtn))
  22423. (defun org-export-grab-title-from-buffer ()
  22424. "Get a title for the current document, from looking at the buffer."
  22425. (let ((inhibit-read-only t))
  22426. (save-excursion
  22427. (goto-char (point-min))
  22428. (let ((end (save-excursion (outline-next-heading) (point))))
  22429. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22430. ;; Mark the line so that it will not be exported as normal text.
  22431. (org-unmodified
  22432. (add-text-properties (match-beginning 0) (match-end 0)
  22433. (list :org-license-to-kill t)))
  22434. ;; Return the title string
  22435. (org-trim (match-string 0)))))))
  22436. (defun org-export-get-title-from-subtree ()
  22437. "Return subtree title and exclude it from export."
  22438. (let (title (m (mark)))
  22439. (save-excursion
  22440. (goto-char (region-beginning))
  22441. (when (and (org-at-heading-p)
  22442. (>= (org-end-of-subtree t t) (region-end)))
  22443. ;; This is a subtree, we take the title from the first heading
  22444. (goto-char (region-beginning))
  22445. (looking-at org-todo-line-regexp)
  22446. (setq title (match-string 3))
  22447. (org-unmodified
  22448. (add-text-properties (point) (1+ (point-at-eol))
  22449. (list :org-license-to-kill t)))))
  22450. title))
  22451. (defun org-solidify-link-text (s &optional alist)
  22452. "Take link text and make a safe target out of it."
  22453. (save-match-data
  22454. (let* ((rtn
  22455. (mapconcat
  22456. 'identity
  22457. (org-split-string s "[ \t\r\n]+") "--"))
  22458. (a (assoc rtn alist)))
  22459. (or (cdr a) rtn))))
  22460. (defun org-get-min-level (lines)
  22461. "Get the minimum level in LINES."
  22462. (let ((re "^\\(\\*+\\) ") l min)
  22463. (catch 'exit
  22464. (while (setq l (pop lines))
  22465. (if (string-match re l)
  22466. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22467. 1)))
  22468. ;; Variable holding the vector with section numbers
  22469. (defvar org-section-numbers (make-vector org-level-max 0))
  22470. (defun org-init-section-numbers ()
  22471. "Initialize the vector for the section numbers."
  22472. (let* ((level -1)
  22473. (numbers (nreverse (org-split-string "" "\\.")))
  22474. (depth (1- (length org-section-numbers)))
  22475. (i depth) number-string)
  22476. (while (>= i 0)
  22477. (if (> i level)
  22478. (aset org-section-numbers i 0)
  22479. (setq number-string (or (car numbers) "0"))
  22480. (if (string-match "\\`[A-Z]\\'" number-string)
  22481. (aset org-section-numbers i
  22482. (- (string-to-char number-string) ?A -1))
  22483. (aset org-section-numbers i (string-to-number number-string)))
  22484. (pop numbers))
  22485. (setq i (1- i)))))
  22486. (defun org-section-number (&optional level)
  22487. "Return a string with the current section number.
  22488. When LEVEL is non-nil, increase section numbers on that level."
  22489. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22490. (when level
  22491. (when (> level -1)
  22492. (aset org-section-numbers
  22493. level (1+ (aref org-section-numbers level))))
  22494. (setq idx (1+ level))
  22495. (while (<= idx depth)
  22496. (if (not (= idx 1))
  22497. (aset org-section-numbers idx 0))
  22498. (setq idx (1+ idx))))
  22499. (setq idx 0)
  22500. (while (<= idx depth)
  22501. (setq n (aref org-section-numbers idx))
  22502. (setq string (concat string (if (not (string= string "")) "." "")
  22503. (int-to-string n)))
  22504. (setq idx (1+ idx)))
  22505. (save-match-data
  22506. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22507. (setq string (replace-match "" t nil string)))
  22508. (if (string-match "\\(\\.0\\)+\\'" string)
  22509. (setq string (replace-match "" t nil string))))
  22510. string))
  22511. ;;; ASCII export
  22512. (defvar org-last-level nil) ; dynamically scoped variable
  22513. (defvar org-min-level nil) ; dynamically scoped variable
  22514. (defvar org-levels-open nil) ; dynamically scoped parameter
  22515. (defvar org-ascii-current-indentation nil) ; For communication
  22516. (defun org-export-as-ascii (arg)
  22517. "Export the outline as a pretty ASCII file.
  22518. If there is an active region, export only the region.
  22519. The prefix ARG specifies how many levels of the outline should become
  22520. underlined headlines. The default is 3."
  22521. (interactive "P")
  22522. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22523. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22524. (org-infile-export-plist)))
  22525. (region-p (org-region-active-p))
  22526. (subtree-p
  22527. (when region-p
  22528. (save-excursion
  22529. (goto-char (region-beginning))
  22530. (and (org-at-heading-p)
  22531. (>= (org-end-of-subtree t t) (region-end))))))
  22532. (custom-times org-display-custom-times)
  22533. (org-ascii-current-indentation '(0 . 0))
  22534. (level 0) line txt
  22535. (umax nil)
  22536. (umax-toc nil)
  22537. (case-fold-search nil)
  22538. (filename (concat (file-name-as-directory
  22539. (org-export-directory :ascii opt-plist))
  22540. (file-name-sans-extension
  22541. (or (and subtree-p
  22542. (org-entry-get (region-beginning)
  22543. "EXPORT_FILE_NAME" t))
  22544. (file-name-nondirectory buffer-file-name)))
  22545. ".txt"))
  22546. (filename (if (equal (file-truename filename)
  22547. (file-truename buffer-file-name))
  22548. (concat filename ".txt")
  22549. filename))
  22550. (buffer (find-file-noselect filename))
  22551. (org-levels-open (make-vector org-level-max nil))
  22552. (odd org-odd-levels-only)
  22553. (date (plist-get opt-plist :date))
  22554. (author (plist-get opt-plist :author))
  22555. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22556. (plist-get opt-plist :title)
  22557. (and (not
  22558. (plist-get opt-plist :skip-before-1st-heading))
  22559. (org-export-grab-title-from-buffer))
  22560. (file-name-sans-extension
  22561. (file-name-nondirectory buffer-file-name))))
  22562. (email (plist-get opt-plist :email))
  22563. (language (plist-get opt-plist :language))
  22564. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22565. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22566. (todo nil)
  22567. (lang-words nil)
  22568. (region
  22569. (buffer-substring
  22570. (if (org-region-active-p) (region-beginning) (point-min))
  22571. (if (org-region-active-p) (region-end) (point-max))))
  22572. (lines (org-split-string
  22573. (org-cleaned-string-for-export
  22574. region
  22575. :for-ascii t
  22576. :skip-before-1st-heading
  22577. (plist-get opt-plist :skip-before-1st-heading)
  22578. :drawers (plist-get opt-plist :drawers)
  22579. :verbatim-multiline t
  22580. :archived-trees
  22581. (plist-get opt-plist :archived-trees)
  22582. :add-text (plist-get opt-plist :text))
  22583. "\n"))
  22584. thetoc have-headings first-heading-pos
  22585. table-open table-buffer)
  22586. (let ((inhibit-read-only t))
  22587. (org-unmodified
  22588. (remove-text-properties (point-min) (point-max)
  22589. '(:org-license-to-kill t))))
  22590. (setq org-min-level (org-get-min-level lines))
  22591. (setq org-last-level org-min-level)
  22592. (org-init-section-numbers)
  22593. (find-file-noselect filename)
  22594. (setq lang-words (or (assoc language org-export-language-setup)
  22595. (assoc "en" org-export-language-setup)))
  22596. (switch-to-buffer-other-window buffer)
  22597. (erase-buffer)
  22598. (fundamental-mode)
  22599. ;; create local variables for all options, to make sure all called
  22600. ;; functions get the correct information
  22601. (mapc (lambda (x)
  22602. (set (make-local-variable (cdr x))
  22603. (plist-get opt-plist (car x))))
  22604. org-export-plist-vars)
  22605. (org-set-local 'org-odd-levels-only odd)
  22606. (setq umax (if arg (prefix-numeric-value arg)
  22607. org-export-headline-levels))
  22608. (setq umax-toc (if (integerp org-export-with-toc)
  22609. (min org-export-with-toc umax)
  22610. umax))
  22611. ;; File header
  22612. (if title (org-insert-centered title ?=))
  22613. (insert "\n")
  22614. (if (and (or author email)
  22615. org-export-author-info)
  22616. (insert (concat (nth 1 lang-words) ": " (or author "")
  22617. (if email (concat " <" email ">") "")
  22618. "\n")))
  22619. (cond
  22620. ((and date (string-match "%" date))
  22621. (setq date (format-time-string date (current-time))))
  22622. (date)
  22623. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22624. (if (and date org-export-time-stamp-file)
  22625. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22626. (insert "\n\n")
  22627. (if org-export-with-toc
  22628. (progn
  22629. (push (concat (nth 3 lang-words) "\n") thetoc)
  22630. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22631. (mapc '(lambda (line)
  22632. (if (string-match org-todo-line-regexp
  22633. line)
  22634. ;; This is a headline
  22635. (progn
  22636. (setq have-headings t)
  22637. (setq level (- (match-end 1) (match-beginning 1))
  22638. level (org-tr-level level)
  22639. txt (match-string 3 line)
  22640. todo
  22641. (or (and org-export-mark-todo-in-toc
  22642. (match-beginning 2)
  22643. (not (member (match-string 2 line)
  22644. org-done-keywords)))
  22645. ; TODO, not DONE
  22646. (and org-export-mark-todo-in-toc
  22647. (= level umax-toc)
  22648. (org-search-todo-below
  22649. line lines level))))
  22650. (setq txt (org-html-expand-for-ascii txt))
  22651. (while (string-match org-bracket-link-regexp txt)
  22652. (setq txt
  22653. (replace-match
  22654. (match-string (if (match-end 2) 3 1) txt)
  22655. t t txt)))
  22656. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22657. (string-match
  22658. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22659. txt))
  22660. (setq txt (replace-match "" t t txt)))
  22661. (if (string-match quote-re0 txt)
  22662. (setq txt (replace-match "" t t txt)))
  22663. (if org-export-with-section-numbers
  22664. (setq txt (concat (org-section-number level)
  22665. " " txt)))
  22666. (if (<= level umax-toc)
  22667. (progn
  22668. (push
  22669. (concat
  22670. (make-string
  22671. (* (max 0 (- level org-min-level)) 4) ?\ )
  22672. (format (if todo "%s (*)\n" "%s\n") txt))
  22673. thetoc)
  22674. (setq org-last-level level))
  22675. ))))
  22676. lines)
  22677. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22678. (org-init-section-numbers)
  22679. (while (setq line (pop lines))
  22680. ;; Remove the quoted HTML tags.
  22681. (setq line (org-html-expand-for-ascii line))
  22682. ;; Remove targets
  22683. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22684. (setq line (replace-match "" t t line)))
  22685. ;; Replace internal links
  22686. (while (string-match org-bracket-link-regexp line)
  22687. (setq line (replace-match
  22688. (if (match-end 3) "[\\3]" "[\\1]")
  22689. t nil line)))
  22690. (when custom-times
  22691. (setq line (org-translate-time line)))
  22692. (cond
  22693. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22694. ;; a Headline
  22695. (setq first-heading-pos (or first-heading-pos (point)))
  22696. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22697. txt (match-string 2 line))
  22698. (org-ascii-level-start level txt umax lines))
  22699. ((and org-export-with-tables
  22700. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22701. (if (not table-open)
  22702. ;; New table starts
  22703. (setq table-open t table-buffer nil))
  22704. ;; Accumulate lines
  22705. (setq table-buffer (cons line table-buffer))
  22706. (when (or (not lines)
  22707. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22708. (car lines))))
  22709. (setq table-open nil
  22710. table-buffer (nreverse table-buffer))
  22711. (insert (mapconcat
  22712. (lambda (x)
  22713. (org-fix-indentation x org-ascii-current-indentation))
  22714. (org-format-table-ascii table-buffer)
  22715. "\n") "\n")))
  22716. (t
  22717. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22718. (if (and org-export-with-fixed-width
  22719. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22720. (setq line (replace-match "\\1" nil nil line)))
  22721. (insert line "\n"))))
  22722. (normal-mode)
  22723. ;; insert the table of contents
  22724. (when thetoc
  22725. (goto-char (point-min))
  22726. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22727. (progn
  22728. (goto-char (match-beginning 0))
  22729. (replace-match ""))
  22730. (goto-char first-heading-pos))
  22731. (mapc 'insert thetoc)
  22732. (or (looking-at "[ \t]*\n[ \t]*\n")
  22733. (insert "\n\n")))
  22734. ;; Convert whitespace place holders
  22735. (goto-char (point-min))
  22736. (let (beg end)
  22737. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22738. (setq end (next-single-property-change beg 'org-whitespace))
  22739. (goto-char beg)
  22740. (delete-region beg end)
  22741. (insert (make-string (- end beg) ?\ ))))
  22742. (save-buffer)
  22743. ;; remove display and invisible chars
  22744. (let (beg end)
  22745. (goto-char (point-min))
  22746. (while (setq beg (next-single-property-change (point) 'display))
  22747. (setq end (next-single-property-change beg 'display))
  22748. (delete-region beg end)
  22749. (goto-char beg)
  22750. (insert "=>"))
  22751. (goto-char (point-min))
  22752. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22753. (setq end (next-single-property-change beg 'org-cwidth))
  22754. (delete-region beg end)
  22755. (goto-char beg)))
  22756. (goto-char (point-min))))
  22757. (defun org-export-ascii-clean-string ()
  22758. "Do extra work for ASCII export"
  22759. (goto-char (point-min))
  22760. (while (re-search-forward org-verbatim-re nil t)
  22761. (goto-char (match-end 2))
  22762. (backward-delete-char 1) (insert "'")
  22763. (goto-char (match-beginning 2))
  22764. (delete-char 1) (insert "`")
  22765. (goto-char (match-end 2))))
  22766. (defun org-search-todo-below (line lines level)
  22767. "Search the subtree below LINE for any TODO entries."
  22768. (let ((rest (cdr (memq line lines)))
  22769. (re org-todo-line-regexp)
  22770. line lv todo)
  22771. (catch 'exit
  22772. (while (setq line (pop rest))
  22773. (if (string-match re line)
  22774. (progn
  22775. (setq lv (- (match-end 1) (match-beginning 1))
  22776. todo (and (match-beginning 2)
  22777. (not (member (match-string 2 line)
  22778. org-done-keywords))))
  22779. ; TODO, not DONE
  22780. (if (<= lv level) (throw 'exit nil))
  22781. (if todo (throw 'exit t))))))))
  22782. (defun org-html-expand-for-ascii (line)
  22783. "Handle quoted HTML for ASCII export."
  22784. (if org-export-html-expand
  22785. (while (string-match "@<[^<>\n]*>" line)
  22786. ;; We just remove the tags for now.
  22787. (setq line (replace-match "" nil nil line))))
  22788. line)
  22789. (defun org-insert-centered (s &optional underline)
  22790. "Insert the string S centered and underline it with character UNDERLINE."
  22791. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22792. (insert (make-string ind ?\ ) s "\n")
  22793. (if underline
  22794. (insert (make-string ind ?\ )
  22795. (make-string (string-width s) underline)
  22796. "\n"))))
  22797. (defun org-ascii-level-start (level title umax &optional lines)
  22798. "Insert a new level in ASCII export."
  22799. (let (char (n (- level umax 1)) (ind 0))
  22800. (if (> level umax)
  22801. (progn
  22802. (insert (make-string (* 2 n) ?\ )
  22803. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22804. org-export-ascii-bullets))
  22805. " " title "\n")
  22806. ;; find the indentation of the next non-empty line
  22807. (catch 'stop
  22808. (while lines
  22809. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22810. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22811. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22812. (pop lines)))
  22813. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22814. (if (or (not (equal (char-before) ?\n))
  22815. (not (equal (char-before (1- (point))) ?\n)))
  22816. (insert "\n"))
  22817. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22818. (unless org-export-with-tags
  22819. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22820. (setq title (replace-match "" t t title))))
  22821. (if org-export-with-section-numbers
  22822. (setq title (concat (org-section-number level) " " title)))
  22823. (insert title "\n" (make-string (string-width title) char) "\n")
  22824. (setq org-ascii-current-indentation '(0 . 0)))))
  22825. (defun org-export-visible (type arg)
  22826. "Create a copy of the visible part of the current buffer, and export it.
  22827. The copy is created in a temporary buffer and removed after use.
  22828. TYPE is the final key (as a string) that also select the export command in
  22829. the `C-c C-e' export dispatcher.
  22830. As a special case, if the you type SPC at the prompt, the temporary
  22831. org-mode file will not be removed but presented to you so that you can
  22832. continue to use it. The prefix arg ARG is passed through to the exporting
  22833. command."
  22834. (interactive
  22835. (list (progn
  22836. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22837. (read-char-exclusive))
  22838. current-prefix-arg))
  22839. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22840. (error "Invalid export key"))
  22841. (let* ((binding (cdr (assoc type
  22842. '((?a . org-export-as-ascii)
  22843. (?\C-a . org-export-as-ascii)
  22844. (?b . org-export-as-html-and-open)
  22845. (?\C-b . org-export-as-html-and-open)
  22846. (?h . org-export-as-html)
  22847. (?H . org-export-as-html-to-buffer)
  22848. (?R . org-export-region-as-html)
  22849. (?x . org-export-as-xoxo)))))
  22850. (keepp (equal type ?\ ))
  22851. (file buffer-file-name)
  22852. (buffer (get-buffer-create "*Org Export Visible*"))
  22853. s e)
  22854. ;; Need to hack the drawers here.
  22855. (save-excursion
  22856. (goto-char (point-min))
  22857. (while (re-search-forward org-drawer-regexp nil t)
  22858. (goto-char (match-beginning 1))
  22859. (or (org-invisible-p) (org-flag-drawer nil))))
  22860. (with-current-buffer buffer (erase-buffer))
  22861. (save-excursion
  22862. (setq s (goto-char (point-min)))
  22863. (while (not (= (point) (point-max)))
  22864. (goto-char (org-find-invisible))
  22865. (append-to-buffer buffer s (point))
  22866. (setq s (goto-char (org-find-visible))))
  22867. (org-cycle-hide-drawers 'all)
  22868. (goto-char (point-min))
  22869. (unless keepp
  22870. ;; Copy all comment lines to the end, to make sure #+ settings are
  22871. ;; still available for the second export step. Kind of a hack, but
  22872. ;; does do the trick.
  22873. (if (looking-at "#[^\r\n]*")
  22874. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22875. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22876. (append-to-buffer buffer (1+ (match-beginning 0))
  22877. (min (point-max) (1+ (match-end 0))))))
  22878. (set-buffer buffer)
  22879. (let ((buffer-file-name file)
  22880. (org-inhibit-startup t))
  22881. (org-mode)
  22882. (show-all)
  22883. (unless keepp (funcall binding arg))))
  22884. (if (not keepp)
  22885. (kill-buffer buffer)
  22886. (switch-to-buffer-other-window buffer)
  22887. (goto-char (point-min)))))
  22888. (defun org-find-visible ()
  22889. (let ((s (point)))
  22890. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22891. (get-char-property s 'invisible)))
  22892. s))
  22893. (defun org-find-invisible ()
  22894. (let ((s (point)))
  22895. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22896. (not (get-char-property s 'invisible))))
  22897. s))
  22898. ;;; HTML export
  22899. (defun org-get-current-options ()
  22900. "Return a string with current options as keyword options.
  22901. Does include HTML export options as well as TODO and CATEGORY stuff."
  22902. (format
  22903. "#+TITLE: %s
  22904. #+AUTHOR: %s
  22905. #+EMAIL: %s
  22906. #+LANGUAGE: %s
  22907. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22908. #+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
  22909. #+CATEGORY: %s
  22910. #+SEQ_TODO: %s
  22911. #+TYP_TODO: %s
  22912. #+PRIORITIES: %c %c %c
  22913. #+DRAWERS: %s
  22914. #+STARTUP: %s %s %s %s %s
  22915. #+TAGS: %s
  22916. #+ARCHIVE: %s
  22917. #+LINK: %s
  22918. "
  22919. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22920. org-export-headline-levels
  22921. org-export-with-section-numbers
  22922. org-export-with-toc
  22923. org-export-preserve-breaks
  22924. org-export-html-expand
  22925. org-export-with-fixed-width
  22926. org-export-with-tables
  22927. org-export-with-sub-superscripts
  22928. org-export-with-special-strings
  22929. org-export-with-footnotes
  22930. org-export-with-emphasize
  22931. org-export-with-TeX-macros
  22932. org-export-with-LaTeX-fragments
  22933. org-export-skip-text-before-1st-heading
  22934. org-export-with-drawers
  22935. org-export-with-tags
  22936. (file-name-nondirectory buffer-file-name)
  22937. "TODO FEEDBACK VERIFY DONE"
  22938. "Me Jason Marie DONE"
  22939. org-highest-priority org-lowest-priority org-default-priority
  22940. (mapconcat 'identity org-drawers " ")
  22941. (cdr (assoc org-startup-folded
  22942. '((nil . "showall") (t . "overview") (content . "content"))))
  22943. (if org-odd-levels-only "odd" "oddeven")
  22944. (if org-hide-leading-stars "hidestars" "showstars")
  22945. (if org-startup-align-all-tables "align" "noalign")
  22946. (cond ((eq org-log-done t) "logdone")
  22947. ((equal org-log-done 'note) "lognotedone")
  22948. ((not org-log-done) "nologdone"))
  22949. (or (mapconcat (lambda (x)
  22950. (cond
  22951. ((equal '(:startgroup) x) "{")
  22952. ((equal '(:endgroup) x) "}")
  22953. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22954. (t (car x))))
  22955. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22956. org-archive-location
  22957. "org file:~/org/%s.org"
  22958. ))
  22959. (defun org-insert-export-options-template ()
  22960. "Insert into the buffer a template with information for exporting."
  22961. (interactive)
  22962. (if (not (bolp)) (newline))
  22963. (let ((s (org-get-current-options)))
  22964. (and (string-match "#\\+CATEGORY" s)
  22965. (setq s (substring s 0 (match-beginning 0))))
  22966. (insert s)))
  22967. (defun org-toggle-fixed-width-section (arg)
  22968. "Toggle the fixed-width export.
  22969. If there is no active region, the QUOTE keyword at the current headline is
  22970. inserted or removed. When present, it causes the text between this headline
  22971. and the next to be exported as fixed-width text, and unmodified.
  22972. If there is an active region, this command adds or removes a colon as the
  22973. first character of this line. If the first character of a line is a colon,
  22974. this line is also exported in fixed-width font."
  22975. (interactive "P")
  22976. (let* ((cc 0)
  22977. (regionp (org-region-active-p))
  22978. (beg (if regionp (region-beginning) (point)))
  22979. (end (if regionp (region-end)))
  22980. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22981. (case-fold-search nil)
  22982. (re "[ \t]*\\(:\\)")
  22983. off)
  22984. (if regionp
  22985. (save-excursion
  22986. (goto-char beg)
  22987. (setq cc (current-column))
  22988. (beginning-of-line 1)
  22989. (setq off (looking-at re))
  22990. (while (> nlines 0)
  22991. (setq nlines (1- nlines))
  22992. (beginning-of-line 1)
  22993. (cond
  22994. (arg
  22995. (move-to-column cc t)
  22996. (insert ":\n")
  22997. (forward-line -1))
  22998. ((and off (looking-at re))
  22999. (replace-match "" t t nil 1))
  23000. ((not off) (move-to-column cc t) (insert ":")))
  23001. (forward-line 1)))
  23002. (save-excursion
  23003. (org-back-to-heading)
  23004. (if (looking-at (concat outline-regexp
  23005. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23006. (replace-match "" t t nil 1)
  23007. (if (looking-at outline-regexp)
  23008. (progn
  23009. (goto-char (match-end 0))
  23010. (insert org-quote-string " "))))))))
  23011. (defun org-export-as-html-and-open (arg)
  23012. "Export the outline as HTML and immediately open it with a browser.
  23013. If there is an active region, export only the region.
  23014. The prefix ARG specifies how many levels of the outline should become
  23015. headlines. The default is 3. Lower levels will become bulleted lists."
  23016. (interactive "P")
  23017. (org-export-as-html arg 'hidden)
  23018. (org-open-file buffer-file-name))
  23019. (defun org-export-as-html-batch ()
  23020. "Call `org-export-as-html', may be used in batch processing as
  23021. emacs --batch
  23022. --load=$HOME/lib/emacs/org.el
  23023. --eval \"(setq org-export-headline-levels 2)\"
  23024. --visit=MyFile --funcall org-export-as-html-batch"
  23025. (org-export-as-html org-export-headline-levels 'hidden))
  23026. (defun org-export-as-html-to-buffer (arg)
  23027. "Call `org-exort-as-html` with output to a temporary buffer.
  23028. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23029. (interactive "P")
  23030. (org-export-as-html arg nil nil "*Org HTML Export*")
  23031. (switch-to-buffer-other-window "*Org HTML Export*"))
  23032. (defun org-replace-region-by-html (beg end)
  23033. "Assume the current region has org-mode syntax, and convert it to HTML.
  23034. This can be used in any buffer. For example, you could write an
  23035. itemized list in org-mode syntax in an HTML buffer and then use this
  23036. command to convert it."
  23037. (interactive "r")
  23038. (let (reg html buf pop-up-frames)
  23039. (save-window-excursion
  23040. (if (org-mode-p)
  23041. (setq html (org-export-region-as-html
  23042. beg end t 'string))
  23043. (setq reg (buffer-substring beg end)
  23044. buf (get-buffer-create "*Org tmp*"))
  23045. (with-current-buffer buf
  23046. (erase-buffer)
  23047. (insert reg)
  23048. (org-mode)
  23049. (setq html (org-export-region-as-html
  23050. (point-min) (point-max) t 'string)))
  23051. (kill-buffer buf)))
  23052. (delete-region beg end)
  23053. (insert html)))
  23054. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23055. "Convert region from BEG to END in org-mode buffer to HTML.
  23056. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23057. contents, and only produce the region of converted text, useful for
  23058. cut-and-paste operations.
  23059. If BUFFER is a buffer or a string, use/create that buffer as a target
  23060. of the converted HTML. If BUFFER is the symbol `string', return the
  23061. produced HTML as a string and leave not buffer behind. For example,
  23062. a Lisp program could call this function in the following way:
  23063. (setq html (org-export-region-as-html beg end t 'string))
  23064. When called interactively, the output buffer is selected, and shown
  23065. in a window. A non-interactive call will only retunr the buffer."
  23066. (interactive "r\nP")
  23067. (when (interactive-p)
  23068. (setq buffer "*Org HTML Export*"))
  23069. (let ((transient-mark-mode t) (zmacs-regions t)
  23070. rtn)
  23071. (goto-char end)
  23072. (set-mark (point)) ;; to activate the region
  23073. (goto-char beg)
  23074. (setq rtn (org-export-as-html
  23075. nil nil nil
  23076. buffer body-only))
  23077. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23078. (if (and (interactive-p) (bufferp rtn))
  23079. (switch-to-buffer-other-window rtn)
  23080. rtn)))
  23081. (defvar html-table-tag nil) ; dynamically scoped into this.
  23082. (defun org-export-as-html (arg &optional hidden ext-plist
  23083. to-buffer body-only)
  23084. "Export the outline as a pretty HTML file.
  23085. If there is an active region, export only the region. The prefix
  23086. ARG specifies how many levels of the outline should become
  23087. headlines. The default is 3. Lower levels will become bulleted
  23088. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23089. EXT-PLIST is a property list with external parameters overriding
  23090. org-mode's default settings, but still inferior to file-local
  23091. settings. When TO-BUFFER is non-nil, create a buffer with that
  23092. name and export to that buffer. If TO-BUFFER is the symbol `string',
  23093. don't leave any buffer behind but just return the resulting HTML as
  23094. a string. When BODY-ONLY is set, don't produce the file header and footer,
  23095. simply return the content of <body>...</body>, without even
  23096. the body tags themselves."
  23097. (interactive "P")
  23098. ;; Make sure we have a file name when we need it.
  23099. (when (and (not (or to-buffer body-only))
  23100. (not buffer-file-name))
  23101. (if (buffer-base-buffer)
  23102. (org-set-local 'buffer-file-name
  23103. (with-current-buffer (buffer-base-buffer)
  23104. buffer-file-name))
  23105. (error "Need a file name to be able to export.")))
  23106. (message "Exporting...")
  23107. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23108. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23109. (setq-default org-done-keywords org-done-keywords)
  23110. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23111. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23112. ext-plist
  23113. (org-infile-export-plist)))
  23114. (style (plist-get opt-plist :style))
  23115. (html-extension (plist-get opt-plist :html-extension))
  23116. (link-validate (plist-get opt-plist :link-validation-function))
  23117. valid thetoc have-headings first-heading-pos
  23118. (odd org-odd-levels-only)
  23119. (region-p (org-region-active-p))
  23120. (subtree-p
  23121. (when region-p
  23122. (save-excursion
  23123. (goto-char (region-beginning))
  23124. (and (org-at-heading-p)
  23125. (>= (org-end-of-subtree t t) (region-end))))))
  23126. ;; The following two are dynamically scoped into other
  23127. ;; routines below.
  23128. (org-current-export-dir (org-export-directory :html opt-plist))
  23129. (org-current-export-file buffer-file-name)
  23130. (level 0) (line "") (origline "") txt todo
  23131. (umax nil)
  23132. (umax-toc nil)
  23133. (filename (if to-buffer nil
  23134. (expand-file-name
  23135. (concat
  23136. (file-name-sans-extension
  23137. (or (and subtree-p
  23138. (org-entry-get (region-beginning)
  23139. "EXPORT_FILE_NAME" t))
  23140. (file-name-nondirectory buffer-file-name)))
  23141. "." html-extension)
  23142. (file-name-as-directory
  23143. (org-export-directory :html opt-plist)))))
  23144. (current-dir (if buffer-file-name
  23145. (file-name-directory buffer-file-name)
  23146. default-directory))
  23147. (buffer (if to-buffer
  23148. (cond
  23149. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23150. (t (get-buffer-create to-buffer)))
  23151. (find-file-noselect filename)))
  23152. (org-levels-open (make-vector org-level-max nil))
  23153. (date (plist-get opt-plist :date))
  23154. (author (plist-get opt-plist :author))
  23155. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23156. (plist-get opt-plist :title)
  23157. (and (not
  23158. (plist-get opt-plist :skip-before-1st-heading))
  23159. (org-export-grab-title-from-buffer))
  23160. (and buffer-file-name
  23161. (file-name-sans-extension
  23162. (file-name-nondirectory buffer-file-name)))
  23163. "UNTITLED"))
  23164. (html-table-tag (plist-get opt-plist :html-table-tag))
  23165. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23166. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23167. (inquote nil)
  23168. (infixed nil)
  23169. (in-local-list nil)
  23170. (local-list-num nil)
  23171. (local-list-indent nil)
  23172. (llt org-plain-list-ordered-item-terminator)
  23173. (email (plist-get opt-plist :email))
  23174. (language (plist-get opt-plist :language))
  23175. (lang-words nil)
  23176. (target-alist nil) tg
  23177. (head-count 0) cnt
  23178. (start 0)
  23179. (coding-system (and (boundp 'buffer-file-coding-system)
  23180. buffer-file-coding-system))
  23181. (coding-system-for-write (or org-export-html-coding-system
  23182. coding-system))
  23183. (save-buffer-coding-system (or org-export-html-coding-system
  23184. coding-system))
  23185. (charset (and coding-system-for-write
  23186. (fboundp 'coding-system-get)
  23187. (coding-system-get coding-system-for-write
  23188. 'mime-charset)))
  23189. (region
  23190. (buffer-substring
  23191. (if region-p (region-beginning) (point-min))
  23192. (if region-p (region-end) (point-max))))
  23193. (lines
  23194. (org-split-string
  23195. (org-cleaned-string-for-export
  23196. region
  23197. :emph-multiline t
  23198. :for-html t
  23199. :skip-before-1st-heading
  23200. (plist-get opt-plist :skip-before-1st-heading)
  23201. :drawers (plist-get opt-plist :drawers)
  23202. :archived-trees
  23203. (plist-get opt-plist :archived-trees)
  23204. :add-text
  23205. (plist-get opt-plist :text)
  23206. :LaTeX-fragments
  23207. (plist-get opt-plist :LaTeX-fragments))
  23208. "[\r\n]"))
  23209. table-open type
  23210. table-buffer table-orig-buffer
  23211. ind start-is-num starter didclose
  23212. rpl path desc descp desc1 desc2 link
  23213. )
  23214. (let ((inhibit-read-only t))
  23215. (org-unmodified
  23216. (remove-text-properties (point-min) (point-max)
  23217. '(:org-license-to-kill t))))
  23218. (message "Exporting...")
  23219. (setq org-min-level (org-get-min-level lines))
  23220. (setq org-last-level org-min-level)
  23221. (org-init-section-numbers)
  23222. (cond
  23223. ((and date (string-match "%" date))
  23224. (setq date (format-time-string date (current-time))))
  23225. (date)
  23226. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23227. ;; Get the language-dependent settings
  23228. (setq lang-words (or (assoc language org-export-language-setup)
  23229. (assoc "en" org-export-language-setup)))
  23230. ;; Switch to the output buffer
  23231. (set-buffer buffer)
  23232. (let ((inhibit-read-only t)) (erase-buffer))
  23233. (fundamental-mode)
  23234. (and (fboundp 'set-buffer-file-coding-system)
  23235. (set-buffer-file-coding-system coding-system-for-write))
  23236. (let ((case-fold-search nil)
  23237. (org-odd-levels-only odd))
  23238. ;; create local variables for all options, to make sure all called
  23239. ;; functions get the correct information
  23240. (mapc (lambda (x)
  23241. (set (make-local-variable (cdr x))
  23242. (plist-get opt-plist (car x))))
  23243. org-export-plist-vars)
  23244. (setq umax (if arg (prefix-numeric-value arg)
  23245. org-export-headline-levels))
  23246. (setq umax-toc (if (integerp org-export-with-toc)
  23247. (min org-export-with-toc umax)
  23248. umax))
  23249. (unless body-only
  23250. ;; File header
  23251. (insert (format
  23252. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23253. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23254. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23255. lang=\"%s\" xml:lang=\"%s\">
  23256. <head>
  23257. <title>%s</title>
  23258. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23259. <meta name=\"generator\" content=\"Org-mode\"/>
  23260. <meta name=\"generated\" content=\"%s\"/>
  23261. <meta name=\"author\" content=\"%s\"/>
  23262. %s
  23263. </head><body>
  23264. "
  23265. language language (org-html-expand title)
  23266. (or charset "iso-8859-1") date author style))
  23267. (insert (or (plist-get opt-plist :preamble) ""))
  23268. (when (plist-get opt-plist :auto-preamble)
  23269. (if title (insert (format org-export-html-title-format
  23270. (org-html-expand title))))))
  23271. (if (and org-export-with-toc (not body-only))
  23272. (progn
  23273. (push (format "<h%d>%s</h%d>\n"
  23274. org-export-html-toplevel-hlevel
  23275. (nth 3 lang-words)
  23276. org-export-html-toplevel-hlevel)
  23277. thetoc)
  23278. (push "<ul>\n<li>" thetoc)
  23279. (setq lines
  23280. (mapcar '(lambda (line)
  23281. (if (string-match org-todo-line-regexp line)
  23282. ;; This is a headline
  23283. (progn
  23284. (setq have-headings t)
  23285. (setq level (- (match-end 1) (match-beginning 1))
  23286. level (org-tr-level level)
  23287. txt (save-match-data
  23288. (org-html-expand
  23289. (org-export-cleanup-toc-line
  23290. (match-string 3 line))))
  23291. todo
  23292. (or (and org-export-mark-todo-in-toc
  23293. (match-beginning 2)
  23294. (not (member (match-string 2 line)
  23295. org-done-keywords)))
  23296. ; TODO, not DONE
  23297. (and org-export-mark-todo-in-toc
  23298. (= level umax-toc)
  23299. (org-search-todo-below
  23300. line lines level))))
  23301. (if (string-match
  23302. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23303. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23304. (if (string-match quote-re0 txt)
  23305. (setq txt (replace-match "" t t txt)))
  23306. (if org-export-with-section-numbers
  23307. (setq txt (concat (org-section-number level)
  23308. " " txt)))
  23309. (if (<= level (max umax umax-toc))
  23310. (setq head-count (+ head-count 1)))
  23311. (if (<= level umax-toc)
  23312. (progn
  23313. (if (> level org-last-level)
  23314. (progn
  23315. (setq cnt (- level org-last-level))
  23316. (while (>= (setq cnt (1- cnt)) 0)
  23317. (push "\n<ul>\n<li>" thetoc))
  23318. (push "\n" thetoc)))
  23319. (if (< level org-last-level)
  23320. (progn
  23321. (setq cnt (- org-last-level level))
  23322. (while (>= (setq cnt (1- cnt)) 0)
  23323. (push "</li>\n</ul>" thetoc))
  23324. (push "\n" thetoc)))
  23325. ;; Check for targets
  23326. (while (string-match org-target-regexp line)
  23327. (setq tg (match-string 1 line)
  23328. line (replace-match
  23329. (concat "@<span class=\"target\">" tg "@</span> ")
  23330. t t line))
  23331. (push (cons (org-solidify-link-text tg)
  23332. (format "sec-%d" head-count))
  23333. target-alist))
  23334. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23335. (setq txt (replace-match "" t t txt)))
  23336. (push
  23337. (format
  23338. (if todo
  23339. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23340. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23341. head-count txt) thetoc)
  23342. (setq org-last-level level))
  23343. )))
  23344. line)
  23345. lines))
  23346. (while (> org-last-level (1- org-min-level))
  23347. (setq org-last-level (1- org-last-level))
  23348. (push "</li>\n</ul>\n" thetoc))
  23349. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23350. (setq head-count 0)
  23351. (org-init-section-numbers)
  23352. (while (setq line (pop lines) origline line)
  23353. (catch 'nextline
  23354. ;; end of quote section?
  23355. (when (and inquote (string-match "^\\*+ " line))
  23356. (insert "</pre>\n")
  23357. (setq inquote nil))
  23358. ;; inside a quote section?
  23359. (when inquote
  23360. (insert (org-html-protect line) "\n")
  23361. (throw 'nextline nil))
  23362. ;; verbatim lines
  23363. (when (and org-export-with-fixed-width
  23364. (string-match "^[ \t]*:\\(.*\\)" line))
  23365. (when (not infixed)
  23366. (setq infixed t)
  23367. (insert "<pre>\n"))
  23368. (insert (org-html-protect (match-string 1 line)) "\n")
  23369. (when (and lines
  23370. (not (string-match "^[ \t]*\\(:.*\\)"
  23371. (car lines))))
  23372. (setq infixed nil)
  23373. (insert "</pre>\n"))
  23374. (throw 'nextline nil))
  23375. ;; Protected HTML
  23376. (when (get-text-property 0 'org-protected line)
  23377. (let (par)
  23378. (when (re-search-backward
  23379. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23380. (setq par (match-string 1))
  23381. (replace-match "\\2\n"))
  23382. (insert line "\n")
  23383. (while (and lines
  23384. (or (= (length (car lines)) 0)
  23385. (get-text-property 0 'org-protected (car lines))))
  23386. (insert (pop lines) "\n"))
  23387. (and par (insert "<p>\n")))
  23388. (throw 'nextline nil))
  23389. ;; Horizontal line
  23390. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23391. (insert "\n<hr/>\n")
  23392. (throw 'nextline nil))
  23393. ;; make targets to anchors
  23394. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23395. (cond
  23396. ((match-end 2)
  23397. (setq line (replace-match
  23398. (concat "@<a name=\""
  23399. (org-solidify-link-text (match-string 1 line))
  23400. "\">\\nbsp@</a>")
  23401. t t line)))
  23402. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23403. (setq line (replace-match
  23404. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23405. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23406. t t line)))
  23407. (t
  23408. (setq line (replace-match
  23409. (concat "@<a name=\""
  23410. (org-solidify-link-text (match-string 1 line))
  23411. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23412. t t line)))))
  23413. (setq line (org-html-handle-time-stamps line))
  23414. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23415. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23416. ;; Also handle sub_superscripts and checkboxes
  23417. (or (string-match org-table-hline-regexp line)
  23418. (setq line (org-html-expand line)))
  23419. ;; Format the links
  23420. (setq start 0)
  23421. (while (string-match org-bracket-link-analytic-regexp line start)
  23422. (setq start (match-beginning 0))
  23423. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23424. (setq path (match-string 3 line))
  23425. (setq desc1 (if (match-end 5) (match-string 5 line))
  23426. desc2 (if (match-end 2) (concat type ":" path) path)
  23427. descp (and desc1 (not (equal desc1 desc2)))
  23428. desc (or desc1 desc2))
  23429. ;; Make an image out of the description if that is so wanted
  23430. (when (and descp (org-file-image-p desc))
  23431. (save-match-data
  23432. (if (string-match "^file:" desc)
  23433. (setq desc (substring desc (match-end 0)))))
  23434. (setq desc (concat "<img src=\"" desc "\"/>")))
  23435. ;; FIXME: do we need to unescape here somewhere?
  23436. (cond
  23437. ((equal type "internal")
  23438. (setq rpl
  23439. (concat
  23440. "<a href=\"#"
  23441. (org-solidify-link-text
  23442. (save-match-data (org-link-unescape path)) target-alist)
  23443. "\">" desc "</a>")))
  23444. ((member type '("http" "https"))
  23445. ;; standard URL, just check if we need to inline an image
  23446. (if (and (or (eq t org-export-html-inline-images)
  23447. (and org-export-html-inline-images (not descp)))
  23448. (org-file-image-p path))
  23449. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23450. (setq link (concat type ":" path))
  23451. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23452. ((member type '("ftp" "mailto" "news"))
  23453. ;; standard URL
  23454. (setq link (concat type ":" path))
  23455. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23456. ((string= type "file")
  23457. ;; FILE link
  23458. (let* ((filename path)
  23459. (abs-p (file-name-absolute-p filename))
  23460. thefile file-is-image-p search)
  23461. (save-match-data
  23462. (if (string-match "::\\(.*\\)" filename)
  23463. (setq search (match-string 1 filename)
  23464. filename (replace-match "" t nil filename)))
  23465. (setq valid
  23466. (if (functionp link-validate)
  23467. (funcall link-validate filename current-dir)
  23468. t))
  23469. (setq file-is-image-p (org-file-image-p filename))
  23470. (setq thefile (if abs-p (expand-file-name filename) filename))
  23471. (when (and org-export-html-link-org-files-as-html
  23472. (string-match "\\.org$" thefile))
  23473. (setq thefile (concat (substring thefile 0
  23474. (match-beginning 0))
  23475. "." html-extension))
  23476. (if (and search
  23477. ;; make sure this is can be used as target search
  23478. (not (string-match "^[0-9]*$" search))
  23479. (not (string-match "^\\*" search))
  23480. (not (string-match "^/.*/$" search)))
  23481. (setq thefile (concat thefile "#"
  23482. (org-solidify-link-text
  23483. (org-link-unescape search)))))
  23484. (when (string-match "^file:" desc)
  23485. (setq desc (replace-match "" t t desc))
  23486. (if (string-match "\\.org$" desc)
  23487. (setq desc (replace-match "" t t desc))))))
  23488. (setq rpl (if (and file-is-image-p
  23489. (or (eq t org-export-html-inline-images)
  23490. (and org-export-html-inline-images
  23491. (not descp))))
  23492. (concat "<img src=\"" thefile "\"/>")
  23493. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23494. (if (not valid) (setq rpl desc))))
  23495. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23496. (setq rpl (concat "<i>&lt;" type ":"
  23497. (save-match-data (org-link-unescape path))
  23498. "&gt;</i>"))))
  23499. (setq line (replace-match rpl t t line)
  23500. start (+ start (length rpl))))
  23501. ;; TODO items
  23502. (if (and (string-match org-todo-line-regexp line)
  23503. (match-beginning 2))
  23504. (setq line
  23505. (concat (substring line 0 (match-beginning 2))
  23506. "<span class=\""
  23507. (if (member (match-string 2 line)
  23508. org-done-keywords)
  23509. "done" "todo")
  23510. "\">" (match-string 2 line)
  23511. "</span>" (substring line (match-end 2)))))
  23512. ;; Does this contain a reference to a footnote?
  23513. (when org-export-with-footnotes
  23514. (setq start 0)
  23515. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23516. (if (get-text-property (match-beginning 2) 'org-protected line)
  23517. (setq start (match-end 2))
  23518. (let ((n (match-string 2 line)))
  23519. (setq line
  23520. (replace-match
  23521. (format
  23522. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23523. (match-string 1 line) n n n)
  23524. t t line))))))
  23525. (cond
  23526. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23527. ;; This is a headline
  23528. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23529. txt (match-string 2 line))
  23530. (if (string-match quote-re0 txt)
  23531. (setq txt (replace-match "" t t txt)))
  23532. (if (<= level (max umax umax-toc))
  23533. (setq head-count (+ head-count 1)))
  23534. (when in-local-list
  23535. ;; Close any local lists before inserting a new header line
  23536. (while local-list-num
  23537. (org-close-li)
  23538. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23539. (pop local-list-num))
  23540. (setq local-list-indent nil
  23541. in-local-list nil))
  23542. (setq first-heading-pos (or first-heading-pos (point)))
  23543. (org-html-level-start level txt umax
  23544. (and org-export-with-toc (<= level umax))
  23545. head-count)
  23546. ;; QUOTES
  23547. (when (string-match quote-re line)
  23548. (insert "<pre>")
  23549. (setq inquote t)))
  23550. ((and org-export-with-tables
  23551. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23552. (if (not table-open)
  23553. ;; New table starts
  23554. (setq table-open t table-buffer nil table-orig-buffer nil))
  23555. ;; Accumulate lines
  23556. (setq table-buffer (cons line table-buffer)
  23557. table-orig-buffer (cons origline table-orig-buffer))
  23558. (when (or (not lines)
  23559. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23560. (car lines))))
  23561. (setq table-open nil
  23562. table-buffer (nreverse table-buffer)
  23563. table-orig-buffer (nreverse table-orig-buffer))
  23564. (org-close-par-maybe)
  23565. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23566. (t
  23567. ;; Normal lines
  23568. (when (string-match
  23569. (cond
  23570. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23571. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23572. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23573. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23574. line)
  23575. (setq ind (org-get-string-indentation line)
  23576. start-is-num (match-beginning 4)
  23577. starter (if (match-beginning 2)
  23578. (substring (match-string 2 line) 0 -1))
  23579. line (substring line (match-beginning 5)))
  23580. (unless (string-match "[^ \t]" line)
  23581. ;; empty line. Pretend indentation is large.
  23582. (setq ind (if org-empty-line-terminates-plain-lists
  23583. 0
  23584. (1+ (or (car local-list-indent) 1)))))
  23585. (setq didclose nil)
  23586. (while (and in-local-list
  23587. (or (and (= ind (car local-list-indent))
  23588. (not starter))
  23589. (< ind (car local-list-indent))))
  23590. (setq didclose t)
  23591. (org-close-li)
  23592. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23593. (pop local-list-num) (pop local-list-indent)
  23594. (setq in-local-list local-list-indent))
  23595. (cond
  23596. ((and starter
  23597. (or (not in-local-list)
  23598. (> ind (car local-list-indent))))
  23599. ;; Start new (level of) list
  23600. (org-close-par-maybe)
  23601. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23602. (push start-is-num local-list-num)
  23603. (push ind local-list-indent)
  23604. (setq in-local-list t))
  23605. (starter
  23606. ;; continue current list
  23607. (org-close-li)
  23608. (insert "<li>\n"))
  23609. (didclose
  23610. ;; we did close a list, normal text follows: need <p>
  23611. (org-open-par)))
  23612. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23613. (setq line
  23614. (replace-match
  23615. (if (equal (match-string 1 line) "X")
  23616. "<b>[X]</b>"
  23617. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23618. t t line))))
  23619. ;; Empty lines start a new paragraph. If hand-formatted lists
  23620. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23621. ;; also start a new paragraph.
  23622. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23623. ;; Is this the start of a footnote?
  23624. (when org-export-with-footnotes
  23625. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23626. (org-close-par-maybe)
  23627. (let ((n (match-string 1 line)))
  23628. (setq line (replace-match
  23629. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23630. ;; Check if the line break needs to be conserved
  23631. (cond
  23632. ((string-match "\\\\\\\\[ \t]*$" line)
  23633. (setq line (replace-match "<br/>" t t line)))
  23634. (org-export-preserve-breaks
  23635. (setq line (concat line "<br/>"))))
  23636. (insert line "\n")))))
  23637. ;; Properly close all local lists and other lists
  23638. (when inquote (insert "</pre>\n"))
  23639. (when in-local-list
  23640. ;; Close any local lists before inserting a new header line
  23641. (while local-list-num
  23642. (org-close-li)
  23643. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23644. (pop local-list-num))
  23645. (setq local-list-indent nil
  23646. in-local-list nil))
  23647. (org-html-level-start 1 nil umax
  23648. (and org-export-with-toc (<= level umax))
  23649. head-count)
  23650. (unless body-only
  23651. (when (plist-get opt-plist :auto-postamble)
  23652. (insert "<div id=\"postamble\">")
  23653. (when (and org-export-author-info author)
  23654. (insert "<p class=\"author\"> "
  23655. (nth 1 lang-words) ": " author "\n")
  23656. (when email
  23657. (if (listp (split-string email ",+ *"))
  23658. (mapc (lambda(e)
  23659. (insert "<a href=\"mailto:" e "\">&lt;"
  23660. e "&gt;</a>\n"))
  23661. (split-string email ",+ *"))
  23662. (insert "<a href=\"mailto:" email "\">&lt;"
  23663. email "&gt;</a>\n")))
  23664. (insert "</p>\n"))
  23665. (when (and date org-export-time-stamp-file)
  23666. (insert "<p class=\"date\"> "
  23667. (nth 2 lang-words) ": "
  23668. date "</p>\n"))
  23669. (insert "</div>"))
  23670. (if org-export-html-with-timestamp
  23671. (insert org-export-html-html-helper-timestamp))
  23672. (insert (or (plist-get opt-plist :postamble) ""))
  23673. (insert "</body>\n</html>\n"))
  23674. (normal-mode)
  23675. (if (eq major-mode default-major-mode) (html-mode))
  23676. ;; insert the table of contents
  23677. (goto-char (point-min))
  23678. (when thetoc
  23679. (if (or (re-search-forward
  23680. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23681. (re-search-forward
  23682. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23683. (progn
  23684. (goto-char (match-beginning 0))
  23685. (replace-match ""))
  23686. (goto-char first-heading-pos)
  23687. (when (looking-at "\\s-*</p>")
  23688. (goto-char (match-end 0))
  23689. (insert "\n")))
  23690. (insert "<div id=\"table-of-contents\">\n")
  23691. (mapc 'insert thetoc)
  23692. (insert "</div>\n"))
  23693. ;; remove empty paragraphs and lists
  23694. (goto-char (point-min))
  23695. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23696. (replace-match ""))
  23697. (goto-char (point-min))
  23698. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23699. (replace-match ""))
  23700. (goto-char (point-min))
  23701. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23702. (replace-match ""))
  23703. ;; Convert whitespace place holders
  23704. (goto-char (point-min))
  23705. (let (beg end n)
  23706. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23707. (setq n (get-text-property beg 'org-whitespace)
  23708. end (next-single-property-change beg 'org-whitespace))
  23709. (goto-char beg)
  23710. (delete-region beg end)
  23711. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23712. (make-string n ?x)))))
  23713. (or to-buffer (save-buffer))
  23714. (goto-char (point-min))
  23715. (message "Exporting... done")
  23716. (if (eq to-buffer 'string)
  23717. (prog1 (buffer-substring (point-min) (point-max))
  23718. (kill-buffer (current-buffer)))
  23719. (current-buffer)))))
  23720. (defvar org-table-colgroup-info nil)
  23721. (defun org-format-table-ascii (lines)
  23722. "Format a table for ascii export."
  23723. (if (stringp lines)
  23724. (setq lines (org-split-string lines "\n")))
  23725. (if (not (string-match "^[ \t]*|" (car lines)))
  23726. ;; Table made by table.el - test for spanning
  23727. lines
  23728. ;; A normal org table
  23729. ;; Get rid of hlines at beginning and end
  23730. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23731. (setq lines (nreverse lines))
  23732. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23733. (setq lines (nreverse lines))
  23734. (when org-export-table-remove-special-lines
  23735. ;; Check if the table has a marking column. If yes remove the
  23736. ;; column and the special lines
  23737. (setq lines (org-table-clean-before-export lines)))
  23738. ;; Get rid of the vertical lines except for grouping
  23739. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23740. rtn line vl1 start)
  23741. (while (setq line (pop lines))
  23742. (if (string-match org-table-hline-regexp line)
  23743. (and (string-match "|\\(.*\\)|" line)
  23744. (setq line (replace-match " \\1" t nil line)))
  23745. (setq start 0 vl1 vl)
  23746. (while (string-match "|" line start)
  23747. (setq start (match-end 0))
  23748. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23749. (push line rtn))
  23750. (nreverse rtn))))
  23751. (defun org-colgroup-info-to-vline-list (info)
  23752. (let (vl new last)
  23753. (while info
  23754. (setq last new new (pop info))
  23755. (if (or (memq last '(:end :startend))
  23756. (memq new '(:start :startend)))
  23757. (push t vl)
  23758. (push nil vl)))
  23759. (setq vl (nreverse vl))
  23760. (and vl (setcar vl nil))
  23761. vl))
  23762. (defun org-format-table-html (lines olines)
  23763. "Find out which HTML converter to use and return the HTML code."
  23764. (if (stringp lines)
  23765. (setq lines (org-split-string lines "\n")))
  23766. (if (string-match "^[ \t]*|" (car lines))
  23767. ;; A normal org table
  23768. (org-format-org-table-html lines)
  23769. ;; Table made by table.el - test for spanning
  23770. (let* ((hlines (delq nil (mapcar
  23771. (lambda (x)
  23772. (if (string-match "^[ \t]*\\+-" x) x
  23773. nil))
  23774. lines)))
  23775. (first (car hlines))
  23776. (ll (and (string-match "\\S-+" first)
  23777. (match-string 0 first)))
  23778. (re (concat "^[ \t]*" (regexp-quote ll)))
  23779. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23780. hlines))))
  23781. (if (and (not spanning)
  23782. (not org-export-prefer-native-exporter-for-tables))
  23783. ;; We can use my own converter with HTML conversions
  23784. (org-format-table-table-html lines)
  23785. ;; Need to use the code generator in table.el, with the original text.
  23786. (org-format-table-table-html-using-table-generate-source olines)))))
  23787. (defun org-format-org-table-html (lines &optional splice)
  23788. "Format a table into HTML."
  23789. ;; Get rid of hlines at beginning and end
  23790. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23791. (setq lines (nreverse lines))
  23792. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23793. (setq lines (nreverse lines))
  23794. (when org-export-table-remove-special-lines
  23795. ;; Check if the table has a marking column. If yes remove the
  23796. ;; column and the special lines
  23797. (setq lines (org-table-clean-before-export lines)))
  23798. (let ((head (and org-export-highlight-first-table-line
  23799. (delq nil (mapcar
  23800. (lambda (x) (string-match "^[ \t]*|-" x))
  23801. (cdr lines)))))
  23802. (nlines 0) fnum i
  23803. tbopen line fields html gr colgropen)
  23804. (if splice (setq head nil))
  23805. (unless splice (push (if head "<thead>" "<tbody>") html))
  23806. (setq tbopen t)
  23807. (while (setq line (pop lines))
  23808. (catch 'next-line
  23809. (if (string-match "^[ \t]*|-" line)
  23810. (progn
  23811. (unless splice
  23812. (push (if head "</thead>" "</tbody>") html)
  23813. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23814. (setq head nil) ;; head ends here, first time around
  23815. ;; ignore this line
  23816. (throw 'next-line t)))
  23817. ;; Break the line into fields
  23818. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23819. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23820. (setq nlines (1+ nlines) i -1)
  23821. (push (concat "<tr>"
  23822. (mapconcat
  23823. (lambda (x)
  23824. (setq i (1+ i))
  23825. (if (and (< i nlines)
  23826. (string-match org-table-number-regexp x))
  23827. (incf (aref fnum i)))
  23828. (if head
  23829. (concat (car org-export-table-header-tags) x
  23830. (cdr org-export-table-header-tags))
  23831. (concat (car org-export-table-data-tags) x
  23832. (cdr org-export-table-data-tags))))
  23833. fields "")
  23834. "</tr>")
  23835. html)))
  23836. (unless splice (if tbopen (push "</tbody>" html)))
  23837. (unless splice (push "</table>\n" html))
  23838. (setq html (nreverse html))
  23839. (unless splice
  23840. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23841. (push (mapconcat
  23842. (lambda (x)
  23843. (setq gr (pop org-table-colgroup-info))
  23844. (format "%s<col align=\"%s\"></col>%s"
  23845. (if (memq gr '(:start :startend))
  23846. (prog1
  23847. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23848. (setq colgropen t))
  23849. "")
  23850. (if (> (/ (float x) nlines) org-table-number-fraction)
  23851. "right" "left")
  23852. (if (memq gr '(:end :startend))
  23853. (progn (setq colgropen nil) "</colgroup>")
  23854. "")))
  23855. fnum "")
  23856. html)
  23857. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23858. (push html-table-tag html))
  23859. (concat (mapconcat 'identity html "\n") "\n")))
  23860. (defun org-table-clean-before-export (lines)
  23861. "Check if the table has a marking column.
  23862. If yes remove the column and the special lines."
  23863. (setq org-table-colgroup-info nil)
  23864. (if (memq nil
  23865. (mapcar
  23866. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23867. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23868. lines))
  23869. (progn
  23870. (setq org-table-clean-did-remove-column nil)
  23871. (delq nil
  23872. (mapcar
  23873. (lambda (x)
  23874. (cond
  23875. ((string-match "^[ \t]*| */ *|" x)
  23876. (setq org-table-colgroup-info
  23877. (mapcar (lambda (x)
  23878. (cond ((member x '("<" "&lt;")) :start)
  23879. ((member x '(">" "&gt;")) :end)
  23880. ((member x '("<>" "&lt;&gt;")) :startend)
  23881. (t nil)))
  23882. (org-split-string x "[ \t]*|[ \t]*")))
  23883. nil)
  23884. (t x)))
  23885. lines)))
  23886. (setq org-table-clean-did-remove-column t)
  23887. (delq nil
  23888. (mapcar
  23889. (lambda (x)
  23890. (cond
  23891. ((string-match "^[ \t]*| */ *|" x)
  23892. (setq org-table-colgroup-info
  23893. (mapcar (lambda (x)
  23894. (cond ((member x '("<" "&lt;")) :start)
  23895. ((member x '(">" "&gt;")) :end)
  23896. ((member x '("<>" "&lt;&gt;")) :startend)
  23897. (t nil)))
  23898. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23899. nil)
  23900. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23901. nil) ; ignore this line
  23902. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23903. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23904. ;; remove the first column
  23905. (replace-match "\\1|" t nil x))))
  23906. lines))))
  23907. (defun org-format-table-table-html (lines)
  23908. "Format a table generated by table.el into HTML.
  23909. This conversion does *not* use `table-generate-source' from table.el.
  23910. This has the advantage that Org-mode's HTML conversions can be used.
  23911. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23912. (let (line field-buffer
  23913. (head org-export-highlight-first-table-line)
  23914. fields html empty)
  23915. (setq html (concat html-table-tag "\n"))
  23916. (while (setq line (pop lines))
  23917. (setq empty "&nbsp;")
  23918. (catch 'next-line
  23919. (if (string-match "^[ \t]*\\+-" line)
  23920. (progn
  23921. (if field-buffer
  23922. (progn
  23923. (setq
  23924. html
  23925. (concat
  23926. html
  23927. "<tr>"
  23928. (mapconcat
  23929. (lambda (x)
  23930. (if (equal x "") (setq x empty))
  23931. (if head
  23932. (concat (car org-export-table-header-tags) x
  23933. (cdr org-export-table-header-tags))
  23934. (concat (car org-export-table-data-tags) x
  23935. (cdr org-export-table-data-tags))))
  23936. field-buffer "\n")
  23937. "</tr>\n"))
  23938. (setq head nil)
  23939. (setq field-buffer nil)))
  23940. ;; Ignore this line
  23941. (throw 'next-line t)))
  23942. ;; Break the line into fields and store the fields
  23943. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23944. (if field-buffer
  23945. (setq field-buffer (mapcar
  23946. (lambda (x)
  23947. (concat x "<br/>" (pop fields)))
  23948. field-buffer))
  23949. (setq field-buffer fields))))
  23950. (setq html (concat html "</table>\n"))
  23951. html))
  23952. (defun org-format-table-table-html-using-table-generate-source (lines)
  23953. "Format a table into html, using `table-generate-source' from table.el.
  23954. This has the advantage that cell- or row-spanning is allowed.
  23955. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23956. (require 'table)
  23957. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23958. (erase-buffer)
  23959. (insert (mapconcat 'identity lines "\n"))
  23960. (goto-char (point-min))
  23961. (if (not (re-search-forward "|[^+]" nil t))
  23962. (error "Error processing table"))
  23963. (table-recognize-table)
  23964. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23965. (table-generate-source 'html " org-tmp2 ")
  23966. (set-buffer " org-tmp2 ")
  23967. (buffer-substring (point-min) (point-max))))
  23968. (defun org-html-handle-time-stamps (s)
  23969. "Format time stamps in string S, or remove them."
  23970. (catch 'exit
  23971. (let (r b)
  23972. (while (string-match org-maybe-keyword-time-regexp s)
  23973. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23974. ;; never export CLOCK
  23975. (throw 'exit ""))
  23976. (or b (setq b (substring s 0 (match-beginning 0))))
  23977. (if (not org-export-with-timestamps)
  23978. (setq r (concat r (substring s 0 (match-beginning 0)))
  23979. s (substring s (match-end 0)))
  23980. (setq r (concat
  23981. r (substring s 0 (match-beginning 0))
  23982. (if (match-end 1)
  23983. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23984. (match-string 1 s)))
  23985. (format " @<span class=\"timestamp\">%s@</span>"
  23986. (substring
  23987. (org-translate-time (match-string 3 s)) 1 -1)))
  23988. s (substring s (match-end 0)))))
  23989. ;; Line break if line started and ended with time stamp stuff
  23990. (if (not r)
  23991. s
  23992. (setq r (concat r s))
  23993. (unless (string-match "\\S-" (concat b s))
  23994. (setq r (concat r "@<br/>")))
  23995. r))))
  23996. (defun org-html-protect (s)
  23997. ;; convert & to &amp;, < to &lt; and > to &gt;
  23998. (let ((start 0))
  23999. (while (string-match "&" s start)
  24000. (setq s (replace-match "&amp;" t t s)
  24001. start (1+ (match-beginning 0))))
  24002. (while (string-match "<" s)
  24003. (setq s (replace-match "&lt;" t t s)))
  24004. (while (string-match ">" s)
  24005. (setq s (replace-match "&gt;" t t s))))
  24006. s)
  24007. (defun org-export-cleanup-toc-line (s)
  24008. "Remove tags and time staps from lines going into the toc."
  24009. (when (memq org-export-with-tags '(not-in-toc nil))
  24010. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24011. (setq s (replace-match "" t t s))))
  24012. (when org-export-remove-timestamps-from-toc
  24013. (while (string-match org-maybe-keyword-time-regexp s)
  24014. (setq s (replace-match "" t t s))))
  24015. (while (string-match org-bracket-link-regexp s)
  24016. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24017. t t s)))
  24018. s)
  24019. (defun org-html-expand (string)
  24020. "Prepare STRING for HTML export. Applies all active conversions.
  24021. If there are links in the string, don't modify these."
  24022. (let* ((re (concat org-bracket-link-regexp "\\|"
  24023. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24024. m s l res)
  24025. (while (setq m (string-match re string))
  24026. (setq s (substring string 0 m)
  24027. l (match-string 0 string)
  24028. string (substring string (match-end 0)))
  24029. (push (org-html-do-expand s) res)
  24030. (push l res))
  24031. (push (org-html-do-expand string) res)
  24032. (apply 'concat (nreverse res))))
  24033. (defun org-html-do-expand (s)
  24034. "Apply all active conversions to translate special ASCII to HTML."
  24035. (setq s (org-html-protect s))
  24036. (if org-export-html-expand
  24037. (let ((start 0))
  24038. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24039. (setq s (replace-match "<\\1>" t nil s)))))
  24040. (if org-export-with-emphasize
  24041. (setq s (org-export-html-convert-emphasize s)))
  24042. (if org-export-with-special-strings
  24043. (setq s (org-export-html-convert-special-strings s)))
  24044. (if org-export-with-sub-superscripts
  24045. (setq s (org-export-html-convert-sub-super s)))
  24046. (if org-export-with-TeX-macros
  24047. (let ((start 0) wd ass)
  24048. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24049. (if (get-text-property (match-beginning 0) 'org-protected s)
  24050. (setq start (match-end 0))
  24051. (setq wd (match-string 1 s))
  24052. (if (setq ass (assoc wd org-html-entities))
  24053. (setq s (replace-match (or (cdr ass)
  24054. (concat "&" (car ass) ";"))
  24055. t t s))
  24056. (setq start (+ start (length wd))))))))
  24057. s)
  24058. (defun org-create-multibrace-regexp (left right n)
  24059. "Create a regular expression which will match a balanced sexp.
  24060. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24061. as single character strings.
  24062. The regexp returned will match the entire expression including the
  24063. delimiters. It will also define a single group which contains the
  24064. match except for the outermost delimiters. The maximum depth of
  24065. stacked delimiters is N. Escaping delimiters is not possible."
  24066. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24067. (or "\\|")
  24068. (re nothing)
  24069. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24070. (while (> n 1)
  24071. (setq n (1- n)
  24072. re (concat re or next)
  24073. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24074. (concat left "\\(" re "\\)" right)))
  24075. (defvar org-match-substring-regexp
  24076. (concat
  24077. "\\([^\\]\\)\\([_^]\\)\\("
  24078. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24079. "\\|"
  24080. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24081. "\\|"
  24082. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24083. "The regular expression matching a sub- or superscript.")
  24084. (defvar org-match-substring-with-braces-regexp
  24085. (concat
  24086. "\\([^\\]\\)\\([_^]\\)\\("
  24087. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24088. "\\)")
  24089. "The regular expression matching a sub- or superscript, forcing braces.")
  24090. (defconst org-export-html-special-string-regexps
  24091. '(("\\\\-" . "&shy;")
  24092. ("---\\([^-]\\)" . "&mdash;\\1")
  24093. ("--\\([^-]\\)" . "&ndash;\\1")
  24094. ("\\.\\.\\." . "&hellip;"))
  24095. "Regular expressions for special string conversion.")
  24096. (defun org-export-html-convert-special-strings (string)
  24097. "Convert special characters in STRING to HTML."
  24098. (let ((all org-export-html-special-string-regexps)
  24099. e a re rpl start)
  24100. (while (setq a (pop all))
  24101. (setq re (car a) rpl (cdr a) start 0)
  24102. (while (string-match re string start)
  24103. (if (get-text-property (match-beginning 0) 'org-protected string)
  24104. (setq start (match-end 0))
  24105. (setq string (replace-match rpl t nil string)))))
  24106. string))
  24107. (defun org-export-html-convert-sub-super (string)
  24108. "Convert sub- and superscripts in STRING to HTML."
  24109. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24110. (while (string-match org-match-substring-regexp string s)
  24111. (cond
  24112. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24113. ((get-text-property (match-beginning 2) 'org-protected string)
  24114. (setq s (match-end 2)))
  24115. (t
  24116. (setq s (match-end 1)
  24117. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24118. c (or (match-string 8 string)
  24119. (match-string 6 string)
  24120. (match-string 5 string))
  24121. string (replace-match
  24122. (concat (match-string 1 string)
  24123. "<" key ">" c "</" key ">")
  24124. t t string)))))
  24125. (while (string-match "\\\\\\([_^]\\)" string)
  24126. (setq string (replace-match (match-string 1 string) t t string)))
  24127. string))
  24128. (defun org-export-html-convert-emphasize (string)
  24129. "Apply emphasis."
  24130. (let ((s 0) rpl)
  24131. (while (string-match org-emph-re string s)
  24132. (if (not (equal
  24133. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24134. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24135. (setq s (match-beginning 0)
  24136. rpl
  24137. (concat
  24138. (match-string 1 string)
  24139. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24140. (match-string 4 string)
  24141. (nth 3 (assoc (match-string 3 string)
  24142. org-emphasis-alist))
  24143. (match-string 5 string))
  24144. string (replace-match rpl t t string)
  24145. s (+ s (- (length rpl) 2)))
  24146. (setq s (1+ s))))
  24147. string))
  24148. (defvar org-par-open nil)
  24149. (defun org-open-par ()
  24150. "Insert <p>, but first close previous paragraph if any."
  24151. (org-close-par-maybe)
  24152. (insert "\n<p>")
  24153. (setq org-par-open t))
  24154. (defun org-close-par-maybe ()
  24155. "Close paragraph if there is one open."
  24156. (when org-par-open
  24157. (insert "</p>")
  24158. (setq org-par-open nil)))
  24159. (defun org-close-li ()
  24160. "Close <li> if necessary."
  24161. (org-close-par-maybe)
  24162. (insert "</li>\n"))
  24163. (defvar body-only) ; dynamically scoped into this.
  24164. (defun org-html-level-start (level title umax with-toc head-count)
  24165. "Insert a new level in HTML export.
  24166. When TITLE is nil, just close all open levels."
  24167. (org-close-par-maybe)
  24168. (let ((l org-level-max))
  24169. (while (>= l level)
  24170. (if (aref org-levels-open (1- l))
  24171. (progn
  24172. (org-html-level-close l umax)
  24173. (aset org-levels-open (1- l) nil)))
  24174. (setq l (1- l)))
  24175. (when title
  24176. ;; If title is nil, this means this function is called to close
  24177. ;; all levels, so the rest is done only if title is given
  24178. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24179. (setq title (replace-match
  24180. (if org-export-with-tags
  24181. (save-match-data
  24182. (concat
  24183. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24184. (mapconcat 'identity (org-split-string
  24185. (match-string 1 title) ":")
  24186. "&nbsp;")
  24187. "</span>"))
  24188. "")
  24189. t t title)))
  24190. (if (> level umax)
  24191. (progn
  24192. (if (aref org-levels-open (1- level))
  24193. (progn
  24194. (org-close-li)
  24195. (insert "<li>" title "<br/>\n"))
  24196. (aset org-levels-open (1- level) t)
  24197. (org-close-par-maybe)
  24198. (insert "<ul>\n<li>" title "<br/>\n")))
  24199. (aset org-levels-open (1- level) t)
  24200. (if (and org-export-with-section-numbers (not body-only))
  24201. (setq title (concat (org-section-number level) " " title)))
  24202. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24203. (if with-toc
  24204. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24205. level level head-count title level))
  24206. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24207. (org-open-par)))))
  24208. (defun org-html-level-close (level max-outline-level)
  24209. "Terminate one level in HTML export."
  24210. (if (<= level max-outline-level)
  24211. (insert "</div>\n")
  24212. (org-close-li)
  24213. (insert "</ul>\n")))
  24214. ;;; iCalendar export
  24215. ;;;###autoload
  24216. (defun org-export-icalendar-this-file ()
  24217. "Export current file as an iCalendar file.
  24218. The iCalendar file will be located in the same directory as the Org-mode
  24219. file, but with extension `.ics'."
  24220. (interactive)
  24221. (org-export-icalendar nil buffer-file-name))
  24222. ;;;###autoload
  24223. (defun org-export-icalendar-all-agenda-files ()
  24224. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24225. Each iCalendar file will be located in the same directory as the Org-mode
  24226. file, but with extension `.ics'."
  24227. (interactive)
  24228. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24229. ;;;###autoload
  24230. (defun org-export-icalendar-combine-agenda-files ()
  24231. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24232. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24233. (interactive)
  24234. (apply 'org-export-icalendar t (org-agenda-files t)))
  24235. (defun org-export-icalendar (combine &rest files)
  24236. "Create iCalendar files for all elements of FILES.
  24237. If COMBINE is non-nil, combine all calendar entries into a single large
  24238. file and store it under the name `org-combined-agenda-icalendar-file'."
  24239. (save-excursion
  24240. (org-prepare-agenda-buffers files)
  24241. (let* ((dir (org-export-directory
  24242. :ical (list :publishing-directory
  24243. org-export-publishing-directory)))
  24244. file ical-file ical-buffer category started org-agenda-new-buffers)
  24245. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24246. (when combine
  24247. (setq ical-file
  24248. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24249. org-combined-agenda-icalendar-file
  24250. (expand-file-name org-combined-agenda-icalendar-file dir))
  24251. ical-buffer (org-get-agenda-file-buffer ical-file))
  24252. (set-buffer ical-buffer) (erase-buffer))
  24253. (while (setq file (pop files))
  24254. (catch 'nextfile
  24255. (org-check-agenda-file file)
  24256. (set-buffer (org-get-agenda-file-buffer file))
  24257. (unless combine
  24258. (setq ical-file (concat (file-name-as-directory dir)
  24259. (file-name-sans-extension
  24260. (file-name-nondirectory buffer-file-name))
  24261. ".ics"))
  24262. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24263. (with-current-buffer ical-buffer (erase-buffer)))
  24264. (setq category (or org-category
  24265. (file-name-sans-extension
  24266. (file-name-nondirectory buffer-file-name))))
  24267. (if (symbolp category) (setq category (symbol-name category)))
  24268. (let ((standard-output ical-buffer))
  24269. (if combine
  24270. (and (not started) (setq started t)
  24271. (org-start-icalendar-file org-icalendar-combined-name))
  24272. (org-start-icalendar-file category))
  24273. (org-print-icalendar-entries combine)
  24274. (when (or (and combine (not files)) (not combine))
  24275. (org-finish-icalendar-file)
  24276. (set-buffer ical-buffer)
  24277. (save-buffer)
  24278. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24279. (org-release-buffers org-agenda-new-buffers))))
  24280. (defvar org-after-save-iCalendar-file-hook nil
  24281. "Hook run after an iCalendar file has been saved.
  24282. The iCalendar buffer is still current when this hook is run.
  24283. A good way to use this is to tell a desktop calenndar application to re-read
  24284. the iCalendar file.")
  24285. (defun org-print-icalendar-entries (&optional combine)
  24286. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24287. When COMBINE is non nil, add the category to each line."
  24288. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24289. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24290. (dts (org-ical-ts-to-string
  24291. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24292. "DTSTART"))
  24293. hd ts ts2 state status (inc t) pos b sexp rrule
  24294. scheduledp deadlinep tmp pri category entry location summary desc
  24295. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24296. (org-refresh-category-properties)
  24297. (save-excursion
  24298. (goto-char (point-min))
  24299. (while (re-search-forward re1 nil t)
  24300. (catch :skip
  24301. (org-agenda-skip)
  24302. (setq pos (match-beginning 0)
  24303. ts (match-string 0)
  24304. inc t
  24305. hd (org-get-heading)
  24306. summary (org-icalendar-cleanup-string
  24307. (org-entry-get nil "SUMMARY"))
  24308. desc (org-icalendar-cleanup-string
  24309. (or (org-entry-get nil "DESCRIPTION")
  24310. (and org-icalendar-include-body (org-get-entry)))
  24311. t org-icalendar-include-body)
  24312. location (org-icalendar-cleanup-string
  24313. (org-entry-get nil "LOCATION"))
  24314. category (org-get-category))
  24315. (if (looking-at re2)
  24316. (progn
  24317. (goto-char (match-end 0))
  24318. (setq ts2 (match-string 1) inc nil))
  24319. (setq tmp (buffer-substring (max (point-min)
  24320. (- pos org-ds-keyword-length))
  24321. pos)
  24322. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24323. (progn
  24324. (setq inc nil)
  24325. (replace-match "\\1" t nil ts))
  24326. ts)
  24327. deadlinep (string-match org-deadline-regexp tmp)
  24328. scheduledp (string-match org-scheduled-regexp tmp)
  24329. ;; donep (org-entry-is-done-p)
  24330. ))
  24331. (if (or (string-match org-tr-regexp hd)
  24332. (string-match org-ts-regexp hd))
  24333. (setq hd (replace-match "" t t hd)))
  24334. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24335. (setq rrule
  24336. (concat "\nRRULE:FREQ="
  24337. (cdr (assoc
  24338. (match-string 2 ts)
  24339. '(("d" . "DAILY")("w" . "WEEKLY")
  24340. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24341. ";INTERVAL=" (match-string 1 ts)))
  24342. (setq rrule ""))
  24343. (setq summary (or summary hd))
  24344. (if (string-match org-bracket-link-regexp summary)
  24345. (setq summary
  24346. (replace-match (if (match-end 3)
  24347. (match-string 3 summary)
  24348. (match-string 1 summary))
  24349. t t summary)))
  24350. (if deadlinep (setq summary (concat "DL: " summary)))
  24351. (if scheduledp (setq summary (concat "S: " summary)))
  24352. (if (string-match "\\`<%%" ts)
  24353. (with-current-buffer sexp-buffer
  24354. (insert (substring ts 1 -1) " " summary "\n"))
  24355. (princ (format "BEGIN:VEVENT
  24356. %s
  24357. %s%s
  24358. SUMMARY:%s%s%s
  24359. CATEGORIES:%s
  24360. END:VEVENT\n"
  24361. (org-ical-ts-to-string ts "DTSTART")
  24362. (org-ical-ts-to-string ts2 "DTEND" inc)
  24363. rrule summary
  24364. (if (and desc (string-match "\\S-" desc))
  24365. (concat "\nDESCRIPTION: " desc) "")
  24366. (if (and location (string-match "\\S-" location))
  24367. (concat "\nLOCATION: " location) "")
  24368. category)))))
  24369. (when (and org-icalendar-include-sexps
  24370. (condition-case nil (require 'icalendar) (error nil))
  24371. (fboundp 'icalendar-export-region))
  24372. ;; Get all the literal sexps
  24373. (goto-char (point-min))
  24374. (while (re-search-forward "^&?%%(" nil t)
  24375. (catch :skip
  24376. (org-agenda-skip)
  24377. (setq b (match-beginning 0))
  24378. (goto-char (1- (match-end 0)))
  24379. (forward-sexp 1)
  24380. (end-of-line 1)
  24381. (setq sexp (buffer-substring b (point)))
  24382. (with-current-buffer sexp-buffer
  24383. (insert sexp "\n"))
  24384. (princ (org-diary-to-ical-string sexp-buffer)))))
  24385. (when org-icalendar-include-todo
  24386. (goto-char (point-min))
  24387. (while (re-search-forward org-todo-line-regexp nil t)
  24388. (catch :skip
  24389. (org-agenda-skip)
  24390. (setq state (match-string 2))
  24391. (setq status (if (member state org-done-keywords)
  24392. "COMPLETED" "NEEDS-ACTION"))
  24393. (when (and state
  24394. (or (not (member state org-done-keywords))
  24395. (eq org-icalendar-include-todo 'all))
  24396. (not (member org-archive-tag (org-get-tags-at)))
  24397. )
  24398. (setq hd (match-string 3)
  24399. summary (org-icalendar-cleanup-string
  24400. (org-entry-get nil "SUMMARY"))
  24401. desc (org-icalendar-cleanup-string
  24402. (or (org-entry-get nil "DESCRIPTION")
  24403. (and org-icalendar-include-body (org-get-entry)))
  24404. t org-icalendar-include-body)
  24405. location (org-icalendar-cleanup-string
  24406. (org-entry-get nil "LOCATION")))
  24407. (if (string-match org-bracket-link-regexp hd)
  24408. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24409. (match-string 1 hd))
  24410. t t hd)))
  24411. (if (string-match org-priority-regexp hd)
  24412. (setq pri (string-to-char (match-string 2 hd))
  24413. hd (concat (substring hd 0 (match-beginning 1))
  24414. (substring hd (match-end 1))))
  24415. (setq pri org-default-priority))
  24416. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24417. (- org-lowest-priority org-highest-priority))))))
  24418. (princ (format "BEGIN:VTODO
  24419. %s
  24420. SUMMARY:%s%s%s
  24421. CATEGORIES:%s
  24422. SEQUENCE:1
  24423. PRIORITY:%d
  24424. STATUS:%s
  24425. END:VTODO\n"
  24426. dts
  24427. (or summary hd)
  24428. (if (and location (string-match "\\S-" location))
  24429. (concat "\nLOCATION: " location) "")
  24430. (if (and desc (string-match "\\S-" desc))
  24431. (concat "\nDESCRIPTION: " desc) "")
  24432. category pri status)))))))))
  24433. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24434. "Take out stuff and quote what needs to be quoted.
  24435. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24436. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24437. characters."
  24438. (if (not s)
  24439. nil
  24440. (when is-body
  24441. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24442. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24443. (while (string-match re s) (setq s (replace-match "" t t s)))
  24444. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24445. (let ((start 0))
  24446. (while (string-match "\\([,;\\]\\)" s start)
  24447. (setq start (+ (match-beginning 0) 2)
  24448. s (replace-match "\\\\\\1" nil nil s))))
  24449. (when is-body
  24450. (while (string-match "[ \t]*\n[ \t]*" s)
  24451. (setq s (replace-match "\\n" t t s))))
  24452. (setq s (org-trim s))
  24453. (if is-body
  24454. (if maxlength
  24455. (if (and (numberp maxlength)
  24456. (> (length s) maxlength))
  24457. (setq s (substring s 0 maxlength)))))
  24458. s))
  24459. (defun org-get-entry ()
  24460. "Clean-up description string."
  24461. (save-excursion
  24462. (org-back-to-heading t)
  24463. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24464. (defun org-start-icalendar-file (name)
  24465. "Start an iCalendar file by inserting the header."
  24466. (let ((user user-full-name)
  24467. (name (or name "unknown"))
  24468. (timezone (cadr (current-time-zone))))
  24469. (princ
  24470. (format "BEGIN:VCALENDAR
  24471. VERSION:2.0
  24472. X-WR-CALNAME:%s
  24473. PRODID:-//%s//Emacs with Org-mode//EN
  24474. X-WR-TIMEZONE:%s
  24475. CALSCALE:GREGORIAN\n" name user timezone))))
  24476. (defun org-finish-icalendar-file ()
  24477. "Finish an iCalendar file by inserting the END statement."
  24478. (princ "END:VCALENDAR\n"))
  24479. (defun org-ical-ts-to-string (s keyword &optional inc)
  24480. "Take a time string S and convert it to iCalendar format.
  24481. KEYWORD is added in front, to make a complete line like DTSTART....
  24482. When INC is non-nil, increase the hour by two (if time string contains
  24483. a time), or the day by one (if it does not contain a time)."
  24484. (let ((t1 (org-parse-time-string s 'nodefault))
  24485. t2 fmt have-time time)
  24486. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24487. (setq t2 t1 have-time t)
  24488. (setq t2 (org-parse-time-string s)))
  24489. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24490. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24491. (when inc
  24492. (if have-time
  24493. (if org-agenda-default-appointment-duration
  24494. (setq mi (+ org-agenda-default-appointment-duration mi))
  24495. (setq h (+ 2 h)))
  24496. (setq d (1+ d))))
  24497. (setq time (encode-time s mi h d m y)))
  24498. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24499. (concat keyword (format-time-string fmt time))))
  24500. ;;; XOXO export
  24501. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24502. (with-current-buffer buffer
  24503. (apply 'insert output)))
  24504. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24505. (defun org-export-as-xoxo (&optional buffer)
  24506. "Export the org buffer as XOXO.
  24507. The XOXO buffer is named *xoxo-<source buffer name>*"
  24508. (interactive (list (current-buffer)))
  24509. ;; A quickie abstraction
  24510. ;; Output everything as XOXO
  24511. (with-current-buffer (get-buffer buffer)
  24512. (let* ((pos (point))
  24513. (opt-plist (org-combine-plists (org-default-export-plist)
  24514. (org-infile-export-plist)))
  24515. (filename (concat (file-name-as-directory
  24516. (org-export-directory :xoxo opt-plist))
  24517. (file-name-sans-extension
  24518. (file-name-nondirectory buffer-file-name))
  24519. ".html"))
  24520. (out (find-file-noselect filename))
  24521. (last-level 1)
  24522. (hanging-li nil))
  24523. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24524. ;; Check the output buffer is empty.
  24525. (with-current-buffer out (erase-buffer))
  24526. ;; Kick off the output
  24527. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24528. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24529. (let* ((hd (match-string-no-properties 1))
  24530. (level (length hd))
  24531. (text (concat
  24532. (match-string-no-properties 2)
  24533. (save-excursion
  24534. (goto-char (match-end 0))
  24535. (let ((str ""))
  24536. (catch 'loop
  24537. (while 't
  24538. (forward-line)
  24539. (if (looking-at "^[ \t]\\(.*\\)")
  24540. (setq str (concat str (match-string-no-properties 1)))
  24541. (throw 'loop str)))))))))
  24542. ;; Handle level rendering
  24543. (cond
  24544. ((> level last-level)
  24545. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24546. ((< level last-level)
  24547. (dotimes (- (- last-level level) 1)
  24548. (if hanging-li
  24549. (org-export-as-xoxo-insert-into out "</li>\n"))
  24550. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24551. (when hanging-li
  24552. (org-export-as-xoxo-insert-into out "</li>\n")
  24553. (setq hanging-li nil)))
  24554. ((equal level last-level)
  24555. (if hanging-li
  24556. (org-export-as-xoxo-insert-into out "</li>\n")))
  24557. )
  24558. (setq last-level level)
  24559. ;; And output the new li
  24560. (setq hanging-li 't)
  24561. (if (equal ?+ (elt text 0))
  24562. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24563. (org-export-as-xoxo-insert-into out "<li>" text))))
  24564. ;; Finally finish off the ol
  24565. (dotimes (- last-level 1)
  24566. (if hanging-li
  24567. (org-export-as-xoxo-insert-into out "</li>\n"))
  24568. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24569. (goto-char pos)
  24570. ;; Finish the buffer off and clean it up.
  24571. (switch-to-buffer-other-window out)
  24572. (indent-region (point-min) (point-max) nil)
  24573. (save-buffer)
  24574. (goto-char (point-min))
  24575. )))
  24576. ;;;; Key bindings
  24577. ;; Make `C-c C-x' a prefix key
  24578. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24579. ;; TAB key with modifiers
  24580. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24581. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24582. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24583. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24584. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24585. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24586. ;; The following line is necessary under Suse GNU/Linux
  24587. (unless (featurep 'xemacs)
  24588. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24589. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24590. (define-key org-mode-map [backtab] 'org-shifttab)
  24591. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24592. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24593. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24594. ;; Cursor keys with modifiers
  24595. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24596. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24597. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24598. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24599. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24600. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24601. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24602. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24603. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24604. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24605. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24606. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24607. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24608. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24609. ;;; Extra keys for tty access.
  24610. ;; We only set them when really needed because otherwise the
  24611. ;; menus don't show the simple keys
  24612. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24613. (not window-system))
  24614. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24615. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24616. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24617. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24618. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24619. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24620. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24621. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24622. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24623. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24624. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24625. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24626. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24627. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24628. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24629. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24630. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24631. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24632. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24633. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24634. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24635. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24636. ;; All the other keys
  24637. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24638. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24639. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24640. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24641. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24642. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24643. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24644. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24645. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24646. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24647. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24648. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24649. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24650. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24651. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24652. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24653. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24654. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24655. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24656. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24657. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24658. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24659. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24660. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24661. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24662. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24663. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24664. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24665. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24666. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24667. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24668. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24669. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24670. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24671. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24672. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24673. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24674. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24675. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24676. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24677. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24678. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24679. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24680. (org-defkey org-mode-map "\C-m" 'org-return)
  24681. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24682. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24683. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24684. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24685. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24686. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24687. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24688. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24689. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24690. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24691. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24692. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24693. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24694. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24695. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24696. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24697. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24698. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24699. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24700. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24701. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24702. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24703. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24704. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24705. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24706. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24707. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24708. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24709. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24710. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24711. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24712. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24713. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24714. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24715. (when (featurep 'xemacs)
  24716. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24717. (defsubst org-table-p () (org-at-table-p))
  24718. (defun org-self-insert-command (N)
  24719. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24720. If the cursor is in a table looking at whitespace, the whitespace is
  24721. overwritten, and the table is not marked as requiring realignment."
  24722. (interactive "p")
  24723. (if (and (org-table-p)
  24724. (progn
  24725. ;; check if we blank the field, and if that triggers align
  24726. (and org-table-auto-blank-field
  24727. (member last-command
  24728. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24729. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24730. ;; got extra space, this field does not determine column width
  24731. (let (org-table-may-need-update) (org-table-blank-field))
  24732. ;; no extra space, this field may determine column width
  24733. (org-table-blank-field)))
  24734. t)
  24735. (eq N 1)
  24736. (looking-at "[^|\n]* |"))
  24737. (let (org-table-may-need-update)
  24738. (goto-char (1- (match-end 0)))
  24739. (delete-backward-char 1)
  24740. (goto-char (match-beginning 0))
  24741. (self-insert-command N))
  24742. (setq org-table-may-need-update t)
  24743. (self-insert-command N)
  24744. (org-fix-tags-on-the-fly)))
  24745. (defun org-fix-tags-on-the-fly ()
  24746. (when (and (equal (char-after (point-at-bol)) ?*)
  24747. (org-on-heading-p))
  24748. (org-align-tags-here org-tags-column)))
  24749. (defun org-delete-backward-char (N)
  24750. "Like `delete-backward-char', insert whitespace at field end in tables.
  24751. When deleting backwards, in tables this function will insert whitespace in
  24752. front of the next \"|\" separator, to keep the table aligned. The table will
  24753. still be marked for re-alignment if the field did fill the entire column,
  24754. because, in this case the deletion might narrow the column."
  24755. (interactive "p")
  24756. (if (and (org-table-p)
  24757. (eq N 1)
  24758. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24759. (looking-at ".*?|"))
  24760. (let ((pos (point))
  24761. (noalign (looking-at "[^|\n\r]* |"))
  24762. (c org-table-may-need-update))
  24763. (backward-delete-char N)
  24764. (skip-chars-forward "^|")
  24765. (insert " ")
  24766. (goto-char (1- pos))
  24767. ;; noalign: if there were two spaces at the end, this field
  24768. ;; does not determine the width of the column.
  24769. (if noalign (setq org-table-may-need-update c)))
  24770. (backward-delete-char N)
  24771. (org-fix-tags-on-the-fly)))
  24772. (defun org-delete-char (N)
  24773. "Like `delete-char', but insert whitespace at field end in tables.
  24774. When deleting characters, in tables this function will insert whitespace in
  24775. front of the next \"|\" separator, to keep the table aligned. The table will
  24776. still be marked for re-alignment if the field did fill the entire column,
  24777. because, in this case the deletion might narrow the column."
  24778. (interactive "p")
  24779. (if (and (org-table-p)
  24780. (not (bolp))
  24781. (not (= (char-after) ?|))
  24782. (eq N 1))
  24783. (if (looking-at ".*?|")
  24784. (let ((pos (point))
  24785. (noalign (looking-at "[^|\n\r]* |"))
  24786. (c org-table-may-need-update))
  24787. (replace-match (concat
  24788. (substring (match-string 0) 1 -1)
  24789. " |"))
  24790. (goto-char pos)
  24791. ;; noalign: if there were two spaces at the end, this field
  24792. ;; does not determine the width of the column.
  24793. (if noalign (setq org-table-may-need-update c)))
  24794. (delete-char N))
  24795. (delete-char N)
  24796. (org-fix-tags-on-the-fly)))
  24797. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24798. (put 'org-self-insert-command 'delete-selection t)
  24799. (put 'orgtbl-self-insert-command 'delete-selection t)
  24800. (put 'org-delete-char 'delete-selection 'supersede)
  24801. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24802. ;; Make `flyspell-mode' delay after some commands
  24803. (put 'org-self-insert-command 'flyspell-delayed t)
  24804. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24805. (put 'org-delete-char 'flyspell-delayed t)
  24806. (put 'org-delete-backward-char 'flyspell-delayed t)
  24807. ;; Make pabbrev-mode expand after org-mode commands
  24808. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24809. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24810. ;; How to do this: Measure non-white length of current string
  24811. ;; If equal to column width, we should realign.
  24812. (defun org-remap (map &rest commands)
  24813. "In MAP, remap the functions given in COMMANDS.
  24814. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24815. (let (new old)
  24816. (while commands
  24817. (setq old (pop commands) new (pop commands))
  24818. (if (fboundp 'command-remapping)
  24819. (org-defkey map (vector 'remap old) new)
  24820. (substitute-key-definition old new map global-map)))))
  24821. (when (eq org-enable-table-editor 'optimized)
  24822. ;; If the user wants maximum table support, we need to hijack
  24823. ;; some standard editing functions
  24824. (org-remap org-mode-map
  24825. 'self-insert-command 'org-self-insert-command
  24826. 'delete-char 'org-delete-char
  24827. 'delete-backward-char 'org-delete-backward-char)
  24828. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24829. (defun org-shiftcursor-error ()
  24830. "Throw an error because Shift-Cursor command was applied in wrong context."
  24831. (error "This command is active in special context like tables, headlines or timestamps"))
  24832. (defun org-shifttab (&optional arg)
  24833. "Global visibility cycling or move to previous table field.
  24834. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24835. on context.
  24836. See the individual commands for more information."
  24837. (interactive "P")
  24838. (cond
  24839. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24840. (arg (message "Content view to level: ")
  24841. (org-content (prefix-numeric-value arg))
  24842. (setq org-cycle-global-status 'overview))
  24843. (t (call-interactively 'org-global-cycle))))
  24844. (defun org-shiftmetaleft ()
  24845. "Promote subtree or delete table column.
  24846. Calls `org-promote-subtree', `org-outdent-item',
  24847. or `org-table-delete-column', depending on context.
  24848. See the individual commands for more information."
  24849. (interactive)
  24850. (cond
  24851. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24852. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24853. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24854. (t (org-shiftcursor-error))))
  24855. (defun org-shiftmetaright ()
  24856. "Demote subtree or insert table column.
  24857. Calls `org-demote-subtree', `org-indent-item',
  24858. or `org-table-insert-column', depending on context.
  24859. See the individual commands for more information."
  24860. (interactive)
  24861. (cond
  24862. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24863. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24864. ((org-at-item-p) (call-interactively 'org-indent-item))
  24865. (t (org-shiftcursor-error))))
  24866. (defun org-shiftmetaup (&optional arg)
  24867. "Move subtree up or kill table row.
  24868. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24869. `org-move-item-up' depending on context. See the individual commands
  24870. for more information."
  24871. (interactive "P")
  24872. (cond
  24873. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24874. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24875. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24876. (t (org-shiftcursor-error))))
  24877. (defun org-shiftmetadown (&optional arg)
  24878. "Move subtree down or insert table row.
  24879. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24880. `org-move-item-down', depending on context. See the individual
  24881. commands for more information."
  24882. (interactive "P")
  24883. (cond
  24884. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24885. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24886. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24887. (t (org-shiftcursor-error))))
  24888. (defun org-metaleft (&optional arg)
  24889. "Promote heading or move table column to left.
  24890. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24891. With no specific context, calls the Emacs default `backward-word'.
  24892. See the individual commands for more information."
  24893. (interactive "P")
  24894. (cond
  24895. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24896. ((or (org-on-heading-p) (org-region-active-p))
  24897. (call-interactively 'org-do-promote))
  24898. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24899. (t (call-interactively 'backward-word))))
  24900. (defun org-metaright (&optional arg)
  24901. "Demote subtree or move table column to right.
  24902. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24903. With no specific context, calls the Emacs default `forward-word'.
  24904. See the individual commands for more information."
  24905. (interactive "P")
  24906. (cond
  24907. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24908. ((or (org-on-heading-p) (org-region-active-p))
  24909. (call-interactively 'org-do-demote))
  24910. ((org-at-item-p) (call-interactively 'org-indent-item))
  24911. (t (call-interactively 'forward-word))))
  24912. (defun org-metaup (&optional arg)
  24913. "Move subtree up or move table row up.
  24914. Calls `org-move-subtree-up' or `org-table-move-row' or
  24915. `org-move-item-up', depending on context. See the individual commands
  24916. for more information."
  24917. (interactive "P")
  24918. (cond
  24919. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24920. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24921. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24922. (t (transpose-lines 1) (beginning-of-line -1))))
  24923. (defun org-metadown (&optional arg)
  24924. "Move subtree down or move table row down.
  24925. Calls `org-move-subtree-down' or `org-table-move-row' or
  24926. `org-move-item-down', depending on context. See the individual
  24927. commands for more information."
  24928. (interactive "P")
  24929. (cond
  24930. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24931. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24932. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24933. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24934. (defun org-shiftup (&optional arg)
  24935. "Increase item in timestamp or increase priority of current headline.
  24936. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24937. depending on context. See the individual commands for more information."
  24938. (interactive "P")
  24939. (cond
  24940. ((org-at-timestamp-p t)
  24941. (call-interactively (if org-edit-timestamp-down-means-later
  24942. 'org-timestamp-down 'org-timestamp-up)))
  24943. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24944. ((org-at-item-p) (call-interactively 'org-previous-item))
  24945. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24946. (defun org-shiftdown (&optional arg)
  24947. "Decrease item in timestamp or decrease priority of current headline.
  24948. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24949. depending on context. See the individual commands for more information."
  24950. (interactive "P")
  24951. (cond
  24952. ((org-at-timestamp-p t)
  24953. (call-interactively (if org-edit-timestamp-down-means-later
  24954. 'org-timestamp-up 'org-timestamp-down)))
  24955. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24956. (t (call-interactively 'org-next-item))))
  24957. (defun org-shiftright ()
  24958. "Next TODO keyword or timestamp one day later, depending on context."
  24959. (interactive)
  24960. (cond
  24961. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24962. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24963. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24964. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24965. (t (org-shiftcursor-error))))
  24966. (defun org-shiftleft ()
  24967. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24968. (interactive)
  24969. (cond
  24970. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24971. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24972. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24973. ((org-at-property-p)
  24974. (call-interactively 'org-property-previous-allowed-value))
  24975. (t (org-shiftcursor-error))))
  24976. (defun org-shiftcontrolright ()
  24977. "Switch to next TODO set."
  24978. (interactive)
  24979. (cond
  24980. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24981. (t (org-shiftcursor-error))))
  24982. (defun org-shiftcontrolleft ()
  24983. "Switch to previous TODO set."
  24984. (interactive)
  24985. (cond
  24986. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24987. (t (org-shiftcursor-error))))
  24988. (defun org-ctrl-c-ret ()
  24989. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24990. (interactive)
  24991. (cond
  24992. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24993. (t (call-interactively 'org-insert-heading))))
  24994. (defun org-copy-special ()
  24995. "Copy region in table or copy current subtree.
  24996. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24997. See the individual commands for more information."
  24998. (interactive)
  24999. (call-interactively
  25000. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25001. (defun org-cut-special ()
  25002. "Cut region in table or cut current subtree.
  25003. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25004. See the individual commands for more information."
  25005. (interactive)
  25006. (call-interactively
  25007. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25008. (defun org-paste-special (arg)
  25009. "Paste rectangular region into table, or past subtree relative to level.
  25010. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25011. See the individual commands for more information."
  25012. (interactive "P")
  25013. (if (org-at-table-p)
  25014. (org-table-paste-rectangle)
  25015. (org-paste-subtree arg)))
  25016. (defun org-ctrl-c-ctrl-c (&optional arg)
  25017. "Set tags in headline, or update according to changed information at point.
  25018. This command does many different things, depending on context:
  25019. - If the cursor is in a headline, prompt for tags and insert them
  25020. into the current line, aligned to `org-tags-column'. When called
  25021. with prefix arg, realign all tags in the current buffer.
  25022. - If the cursor is in one of the special #+KEYWORD lines, this
  25023. triggers scanning the buffer for these lines and updating the
  25024. information.
  25025. - If the cursor is inside a table, realign the table. This command
  25026. works even if the automatic table editor has been turned off.
  25027. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25028. the entire table.
  25029. - If the cursor is a the beginning of a dynamic block, update it.
  25030. - If the cursor is inside a table created by the table.el package,
  25031. activate that table.
  25032. - If the current buffer is a remember buffer, close note and file it.
  25033. with a prefix argument, file it without further interaction to the default
  25034. location.
  25035. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25036. links in this buffer.
  25037. - If the cursor is on a numbered item in a plain list, renumber the
  25038. ordered list.
  25039. - If the cursor is on a checkbox, toggle it."
  25040. (interactive "P")
  25041. (let ((org-enable-table-editor t))
  25042. (cond
  25043. ((or org-clock-overlays
  25044. org-occur-highlights
  25045. org-latex-fragment-image-overlays)
  25046. (org-remove-clock-overlays)
  25047. (org-remove-occur-highlights)
  25048. (org-remove-latex-fragment-image-overlays)
  25049. (message "Temporary highlights/overlays removed from current buffer"))
  25050. ((and (local-variable-p 'org-finish-function (current-buffer))
  25051. (fboundp org-finish-function))
  25052. (funcall org-finish-function))
  25053. ((org-at-property-p)
  25054. (call-interactively 'org-property-action))
  25055. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25056. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25057. ((org-at-table.el-p)
  25058. (require 'table)
  25059. (beginning-of-line 1)
  25060. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25061. (call-interactively 'table-recognize-table))
  25062. ((org-at-table-p)
  25063. (org-table-maybe-eval-formula)
  25064. (if arg
  25065. (call-interactively 'org-table-recalculate)
  25066. (org-table-maybe-recalculate-line))
  25067. (call-interactively 'org-table-align))
  25068. ((org-at-item-checkbox-p)
  25069. (call-interactively 'org-toggle-checkbox))
  25070. ((org-at-item-p)
  25071. (call-interactively 'org-maybe-renumber-ordered-list))
  25072. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25073. ;; Dynamic block
  25074. (beginning-of-line 1)
  25075. (org-update-dblock))
  25076. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25077. (cond
  25078. ((equal (match-string 1) "TBLFM")
  25079. ;; Recalculate the table before this line
  25080. (save-excursion
  25081. (beginning-of-line 1)
  25082. (skip-chars-backward " \r\n\t")
  25083. (if (org-at-table-p)
  25084. (org-call-with-arg 'org-table-recalculate t))))
  25085. (t
  25086. (call-interactively 'org-mode-restart))))
  25087. (t (error "C-c C-c can do nothing useful at this location.")))))
  25088. (defun org-mode-restart ()
  25089. "Restart Org-mode, to scan again for special lines.
  25090. Also updates the keyword regular expressions."
  25091. (interactive)
  25092. (let ((org-inhibit-startup t)) (org-mode))
  25093. (message "Org-mode restarted to refresh keyword and special line setup"))
  25094. (defun org-kill-note-or-show-branches ()
  25095. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25096. (interactive)
  25097. (if (not org-finish-function)
  25098. (call-interactively 'show-branches)
  25099. (let ((org-note-abort t))
  25100. (funcall org-finish-function))))
  25101. (defun org-return (&optional indent)
  25102. "Goto next table row or insert a newline.
  25103. Calls `org-table-next-row' or `newline', depending on context.
  25104. See the individual commands for more information."
  25105. (interactive)
  25106. (cond
  25107. ((bobp) (if indent (newline-and-indent) (newline)))
  25108. ((and (org-at-heading-p)
  25109. (looking-at
  25110. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25111. (org-show-entry)
  25112. (end-of-line 1)
  25113. (newline))
  25114. ((org-at-table-p)
  25115. (org-table-justify-field-maybe)
  25116. (call-interactively 'org-table-next-row))
  25117. (t (if indent (newline-and-indent) (newline)))))
  25118. (defun org-return-indent ()
  25119. (interactive)
  25120. "Goto next table row or insert a newline and indent.
  25121. Calls `org-table-next-row' or `newline-and-indent', depending on
  25122. context. See the individual commands for more information."
  25123. (org-return t))
  25124. (defun org-ctrl-c-minus ()
  25125. "Insert separator line in table or modify bullet type in list.
  25126. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  25127. depending on context."
  25128. (interactive)
  25129. (cond
  25130. ((org-at-table-p)
  25131. (call-interactively 'org-table-insert-hline))
  25132. ((org-on-heading-p)
  25133. ;; Convert to item
  25134. (save-excursion
  25135. (beginning-of-line 1)
  25136. (if (looking-at "\\*+ ")
  25137. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25138. ((org-in-item-p)
  25139. (call-interactively 'org-cycle-list-bullet))
  25140. (t (error "`C-c -' does have no function here."))))
  25141. (defun org-meta-return (&optional arg)
  25142. "Insert a new heading or wrap a region in a table.
  25143. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25144. See the individual commands for more information."
  25145. (interactive "P")
  25146. (cond
  25147. ((org-at-table-p)
  25148. (call-interactively 'org-table-wrap-region))
  25149. (t (call-interactively 'org-insert-heading))))
  25150. ;;; Menu entries
  25151. ;; Define the Org-mode menus
  25152. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25153. '("Tbl"
  25154. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25155. ["Next Field" org-cycle (org-at-table-p)]
  25156. ["Previous Field" org-shifttab (org-at-table-p)]
  25157. ["Next Row" org-return (org-at-table-p)]
  25158. "--"
  25159. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25160. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25161. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25162. "--"
  25163. ("Column"
  25164. ["Move Column Left" org-metaleft (org-at-table-p)]
  25165. ["Move Column Right" org-metaright (org-at-table-p)]
  25166. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25167. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25168. ("Row"
  25169. ["Move Row Up" org-metaup (org-at-table-p)]
  25170. ["Move Row Down" org-metadown (org-at-table-p)]
  25171. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25172. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25173. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25174. "--"
  25175. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25176. ("Rectangle"
  25177. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25178. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25179. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25180. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25181. "--"
  25182. ("Calculate"
  25183. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25184. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25185. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25186. "--"
  25187. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25188. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25189. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25190. "--"
  25191. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25192. "--"
  25193. ["Sum Column/Rectangle" org-table-sum
  25194. (or (org-at-table-p) (org-region-active-p))]
  25195. ["Which Column?" org-table-current-column (org-at-table-p)])
  25196. ["Debug Formulas"
  25197. org-table-toggle-formula-debugger
  25198. :style toggle :selected org-table-formula-debug]
  25199. ["Show Col/Row Numbers"
  25200. org-table-toggle-coordinate-overlays
  25201. :style toggle :selected org-table-overlay-coordinates]
  25202. "--"
  25203. ["Create" org-table-create (and (not (org-at-table-p))
  25204. org-enable-table-editor)]
  25205. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25206. ["Import from File" org-table-import (not (org-at-table-p))]
  25207. ["Export to File" org-table-export (org-at-table-p)]
  25208. "--"
  25209. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25210. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25211. '("Org"
  25212. ("Show/Hide"
  25213. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25214. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25215. ["Sparse Tree" org-occur t]
  25216. ["Reveal Context" org-reveal t]
  25217. ["Show All" show-all t]
  25218. "--"
  25219. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25220. "--"
  25221. ["New Heading" org-insert-heading t]
  25222. ("Navigate Headings"
  25223. ["Up" outline-up-heading t]
  25224. ["Next" outline-next-visible-heading t]
  25225. ["Previous" outline-previous-visible-heading t]
  25226. ["Next Same Level" outline-forward-same-level t]
  25227. ["Previous Same Level" outline-backward-same-level t]
  25228. "--"
  25229. ["Jump" org-goto t])
  25230. ("Edit Structure"
  25231. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25232. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25233. "--"
  25234. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25235. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25236. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25237. "--"
  25238. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25239. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25240. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25241. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25242. "--"
  25243. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25244. "--"
  25245. ["Convert to odd levels" org-convert-to-odd-levels t]
  25246. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25247. ("Editing"
  25248. ["Emphasis..." org-emphasize t])
  25249. ("Archive"
  25250. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25251. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25252. ; :active t :keys "C-u C-c C-x C-a"]
  25253. ["Sparse trees open ARCHIVE trees"
  25254. (setq org-sparse-tree-open-archived-trees
  25255. (not org-sparse-tree-open-archived-trees))
  25256. :style toggle :selected org-sparse-tree-open-archived-trees]
  25257. ["Cycling opens ARCHIVE trees"
  25258. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25259. :style toggle :selected org-cycle-open-archived-trees]
  25260. ["Agenda includes ARCHIVE trees"
  25261. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25262. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25263. "--"
  25264. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25265. ; ["Check and Move Children" (org-archive-subtree '(4))
  25266. ; :active t :keys "C-u C-c C-x C-s"]
  25267. )
  25268. "--"
  25269. ("TODO Lists"
  25270. ["TODO/DONE/-" org-todo t]
  25271. ("Select keyword"
  25272. ["Next keyword" org-shiftright (org-on-heading-p)]
  25273. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25274. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25275. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25276. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25277. ["Show TODO Tree" org-show-todo-tree t]
  25278. ["Global TODO list" org-todo-list t]
  25279. "--"
  25280. ["Set Priority" org-priority t]
  25281. ["Priority Up" org-shiftup t]
  25282. ["Priority Down" org-shiftdown t])
  25283. ("TAGS and Properties"
  25284. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25285. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25286. "--"
  25287. ["Set property" 'org-set-property t]
  25288. ["Column view of properties" org-columns t]
  25289. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25290. ("Dates and Scheduling"
  25291. ["Timestamp" org-time-stamp t]
  25292. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25293. ("Change Date"
  25294. ["1 Day Later" org-shiftright t]
  25295. ["1 Day Earlier" org-shiftleft t]
  25296. ["1 ... Later" org-shiftup t]
  25297. ["1 ... Earlier" org-shiftdown t])
  25298. ["Compute Time Range" org-evaluate-time-range t]
  25299. ["Schedule Item" org-schedule t]
  25300. ["Deadline" org-deadline t]
  25301. "--"
  25302. ["Custom time format" org-toggle-time-stamp-overlays
  25303. :style radio :selected org-display-custom-times]
  25304. "--"
  25305. ["Goto Calendar" org-goto-calendar t]
  25306. ["Date from Calendar" org-date-from-calendar t])
  25307. ("Logging work"
  25308. ["Clock in" org-clock-in t]
  25309. ["Clock out" org-clock-out t]
  25310. ["Clock cancel" org-clock-cancel t]
  25311. ["Goto running clock" org-clock-goto t]
  25312. ["Display times" org-clock-display t]
  25313. ["Create clock table" org-clock-report t]
  25314. "--"
  25315. ["Record DONE time"
  25316. (progn (setq org-log-done (not org-log-done))
  25317. (message "Switching to %s will %s record a timestamp"
  25318. (car org-done-keywords)
  25319. (if org-log-done "automatically" "not")))
  25320. :style toggle :selected org-log-done])
  25321. "--"
  25322. ["Agenda Command..." org-agenda t]
  25323. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25324. ("File List for Agenda")
  25325. ("Special views current file"
  25326. ["TODO Tree" org-show-todo-tree t]
  25327. ["Check Deadlines" org-check-deadlines t]
  25328. ["Timeline" org-timeline t]
  25329. ["Tags Tree" org-tags-sparse-tree t])
  25330. "--"
  25331. ("Hyperlinks"
  25332. ["Store Link (Global)" org-store-link t]
  25333. ["Insert Link" org-insert-link t]
  25334. ["Follow Link" org-open-at-point t]
  25335. "--"
  25336. ["Next link" org-next-link t]
  25337. ["Previous link" org-previous-link t]
  25338. "--"
  25339. ["Descriptive Links"
  25340. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25341. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25342. ["Literal Links"
  25343. (progn
  25344. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25345. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25346. "--"
  25347. ["Export/Publish..." org-export t]
  25348. ("LaTeX"
  25349. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25350. :selected org-cdlatex-mode]
  25351. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25352. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25353. ["Modify math symbol" org-cdlatex-math-modify
  25354. (org-inside-LaTeX-fragment-p)]
  25355. ["Export LaTeX fragments as images"
  25356. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25357. :style toggle :selected org-export-with-LaTeX-fragments])
  25358. "--"
  25359. ("Documentation"
  25360. ["Show Version" org-version t]
  25361. ["Info Documentation" org-info t])
  25362. ("Customize"
  25363. ["Browse Org Group" org-customize t]
  25364. "--"
  25365. ["Expand This Menu" org-create-customize-menu
  25366. (fboundp 'customize-menu-create)])
  25367. "--"
  25368. ["Refresh setup" org-mode-restart t]
  25369. ))
  25370. (defun org-info (&optional node)
  25371. "Read documentation for Org-mode in the info system.
  25372. With optional NODE, go directly to that node."
  25373. (interactive)
  25374. (require 'info)
  25375. (info (format "(org)%s" (or node ""))))
  25376. (defun org-install-agenda-files-menu ()
  25377. (let ((bl (buffer-list)))
  25378. (save-excursion
  25379. (while bl
  25380. (set-buffer (pop bl))
  25381. (if (org-mode-p) (setq bl nil)))
  25382. (when (org-mode-p)
  25383. (easy-menu-change
  25384. '("Org") "File List for Agenda"
  25385. (append
  25386. (list
  25387. ["Edit File List" (org-edit-agenda-file-list) t]
  25388. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25389. ["Remove Current File from List" org-remove-file t]
  25390. ["Cycle through agenda files" org-cycle-agenda-files t]
  25391. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25392. "--")
  25393. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25394. ;;;; Documentation
  25395. (defun org-customize ()
  25396. "Call the customize function with org as argument."
  25397. (interactive)
  25398. (customize-browse 'org))
  25399. (defun org-create-customize-menu ()
  25400. "Create a full customization menu for Org-mode, insert it into the menu."
  25401. (interactive)
  25402. (if (fboundp 'customize-menu-create)
  25403. (progn
  25404. (easy-menu-change
  25405. '("Org") "Customize"
  25406. `(["Browse Org group" org-customize t]
  25407. "--"
  25408. ,(customize-menu-create 'org)
  25409. ["Set" Custom-set t]
  25410. ["Save" Custom-save t]
  25411. ["Reset to Current" Custom-reset-current t]
  25412. ["Reset to Saved" Custom-reset-saved t]
  25413. ["Reset to Standard Settings" Custom-reset-standard t]))
  25414. (message "\"Org\"-menu now contains full customization menu"))
  25415. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25416. ;;;; Miscellaneous stuff
  25417. ;;; Generally useful functions
  25418. (defun org-context ()
  25419. "Return a list of contexts of the current cursor position.
  25420. If several contexts apply, all are returned.
  25421. Each context entry is a list with a symbol naming the context, and
  25422. two positions indicating start and end of the context. Possible
  25423. contexts are:
  25424. :headline anywhere in a headline
  25425. :headline-stars on the leading stars in a headline
  25426. :todo-keyword on a TODO keyword (including DONE) in a headline
  25427. :tags on the TAGS in a headline
  25428. :priority on the priority cookie in a headline
  25429. :item on the first line of a plain list item
  25430. :item-bullet on the bullet/number of a plain list item
  25431. :checkbox on the checkbox in a plain list item
  25432. :table in an org-mode table
  25433. :table-special on a special filed in a table
  25434. :table-table in a table.el table
  25435. :link on a hyperlink
  25436. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25437. :target on a <<target>>
  25438. :radio-target on a <<<radio-target>>>
  25439. :latex-fragment on a LaTeX fragment
  25440. :latex-preview on a LaTeX fragment with overlayed preview image
  25441. This function expects the position to be visible because it uses font-lock
  25442. faces as a help to recognize the following contexts: :table-special, :link,
  25443. and :keyword."
  25444. (let* ((f (get-text-property (point) 'face))
  25445. (faces (if (listp f) f (list f)))
  25446. (p (point)) clist o)
  25447. ;; First the large context
  25448. (cond
  25449. ((org-on-heading-p t)
  25450. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25451. (when (progn
  25452. (beginning-of-line 1)
  25453. (looking-at org-todo-line-tags-regexp))
  25454. (push (org-point-in-group p 1 :headline-stars) clist)
  25455. (push (org-point-in-group p 2 :todo-keyword) clist)
  25456. (push (org-point-in-group p 4 :tags) clist))
  25457. (goto-char p)
  25458. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25459. (if (looking-at "\\[#[A-Z0-9]\\]")
  25460. (push (org-point-in-group p 0 :priority) clist)))
  25461. ((org-at-item-p)
  25462. (push (org-point-in-group p 2 :item-bullet) clist)
  25463. (push (list :item (point-at-bol)
  25464. (save-excursion (org-end-of-item) (point)))
  25465. clist)
  25466. (and (org-at-item-checkbox-p)
  25467. (push (org-point-in-group p 0 :checkbox) clist)))
  25468. ((org-at-table-p)
  25469. (push (list :table (org-table-begin) (org-table-end)) clist)
  25470. (if (memq 'org-formula faces)
  25471. (push (list :table-special
  25472. (previous-single-property-change p 'face)
  25473. (next-single-property-change p 'face)) clist)))
  25474. ((org-at-table-p 'any)
  25475. (push (list :table-table) clist)))
  25476. (goto-char p)
  25477. ;; Now the small context
  25478. (cond
  25479. ((org-at-timestamp-p)
  25480. (push (org-point-in-group p 0 :timestamp) clist))
  25481. ((memq 'org-link faces)
  25482. (push (list :link
  25483. (previous-single-property-change p 'face)
  25484. (next-single-property-change p 'face)) clist))
  25485. ((memq 'org-special-keyword faces)
  25486. (push (list :keyword
  25487. (previous-single-property-change p 'face)
  25488. (next-single-property-change p 'face)) clist))
  25489. ((org-on-target-p)
  25490. (push (org-point-in-group p 0 :target) clist)
  25491. (goto-char (1- (match-beginning 0)))
  25492. (if (looking-at org-radio-target-regexp)
  25493. (push (org-point-in-group p 0 :radio-target) clist))
  25494. (goto-char p))
  25495. ((setq o (car (delq nil
  25496. (mapcar
  25497. (lambda (x)
  25498. (if (memq x org-latex-fragment-image-overlays) x))
  25499. (org-overlays-at (point))))))
  25500. (push (list :latex-fragment
  25501. (org-overlay-start o) (org-overlay-end o)) clist)
  25502. (push (list :latex-preview
  25503. (org-overlay-start o) (org-overlay-end o)) clist))
  25504. ((org-inside-LaTeX-fragment-p)
  25505. ;; FIXME: positions wrong.
  25506. (push (list :latex-fragment (point) (point)) clist)))
  25507. (setq clist (nreverse (delq nil clist)))
  25508. clist))
  25509. ;; FIXME: Compare with at-regexp-p Do we need both?
  25510. (defun org-in-regexp (re &optional nlines visually)
  25511. "Check if point is inside a match of regexp.
  25512. Normally only the current line is checked, but you can include NLINES extra
  25513. lines both before and after point into the search.
  25514. If VISUALLY is set, require that the cursor is not after the match but
  25515. really on, so that the block visually is on the match."
  25516. (catch 'exit
  25517. (let ((pos (point))
  25518. (eol (point-at-eol (+ 1 (or nlines 0))))
  25519. (inc (if visually 1 0)))
  25520. (save-excursion
  25521. (beginning-of-line (- 1 (or nlines 0)))
  25522. (while (re-search-forward re eol t)
  25523. (if (and (<= (match-beginning 0) pos)
  25524. (>= (+ inc (match-end 0)) pos))
  25525. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25526. (defun org-at-regexp-p (regexp)
  25527. "Is point inside a match of REGEXP in the current line?"
  25528. (catch 'exit
  25529. (save-excursion
  25530. (let ((pos (point)) (end (point-at-eol)))
  25531. (beginning-of-line 1)
  25532. (while (re-search-forward regexp end t)
  25533. (if (and (<= (match-beginning 0) pos)
  25534. (>= (match-end 0) pos))
  25535. (throw 'exit t)))
  25536. nil))))
  25537. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25538. "Call `multi-occur' with buffers for all agenda files."
  25539. (interactive "sOrg-files matching: \np")
  25540. (let* ((files (org-agenda-files))
  25541. (tnames (mapcar 'file-truename files))
  25542. (extra org-agenda-multi-occur-extra-files)
  25543. f)
  25544. (while (setq f (pop extra))
  25545. (unless (member (file-truename f) tnames)
  25546. (add-to-list 'files f 'append)
  25547. (add-to-list 'tnames (file-truename f) 'append)))
  25548. (multi-occur
  25549. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25550. regexp)))
  25551. (if (boundp 'occur-mode-find-occurrence-hook)
  25552. ;; Emacs 23
  25553. (add-hook 'occur-mode-find-occurrence-hook
  25554. (lambda ()
  25555. (when (org-mode-p)
  25556. (org-reveal))))
  25557. ;; Emacs 22
  25558. (defadvice occur-mode-goto-occurrence
  25559. (after org-occur-reveal activate)
  25560. (and (org-mode-p) (org-reveal)))
  25561. (defadvice occur-mode-goto-occurrence-other-window
  25562. (after org-occur-reveal activate)
  25563. (and (org-mode-p) (org-reveal)))
  25564. (defadvice occur-mode-display-occurrence
  25565. (after org-occur-reveal activate)
  25566. (when (org-mode-p)
  25567. (let ((pos (occur-mode-find-occurrence)))
  25568. (with-current-buffer (marker-buffer pos)
  25569. (save-excursion
  25570. (goto-char pos)
  25571. (org-reveal)))))))
  25572. (defun org-uniquify (list)
  25573. "Remove duplicate elements from LIST."
  25574. (let (res)
  25575. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25576. res))
  25577. (defun org-delete-all (elts list)
  25578. "Remove all elements in ELTS from LIST."
  25579. (while elts
  25580. (setq list (delete (pop elts) list)))
  25581. list)
  25582. (defun org-back-over-empty-lines ()
  25583. "Move backwards over witespace, to the beginning of the first empty line.
  25584. Returns the number o empty lines passed."
  25585. (let ((pos (point)))
  25586. (skip-chars-backward " \t\n\r")
  25587. (beginning-of-line 2)
  25588. (goto-char (min (point) pos))
  25589. (count-lines (point) pos)))
  25590. (defun org-skip-whitespace ()
  25591. (skip-chars-forward " \t\n\r"))
  25592. (defun org-point-in-group (point group &optional context)
  25593. "Check if POINT is in match-group GROUP.
  25594. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25595. match. If the match group does ot exist or point is not inside it,
  25596. return nil."
  25597. (and (match-beginning group)
  25598. (>= point (match-beginning group))
  25599. (<= point (match-end group))
  25600. (if context
  25601. (list context (match-beginning group) (match-end group))
  25602. t)))
  25603. (defun org-switch-to-buffer-other-window (&rest args)
  25604. "Switch to buffer in a second window on the current frame.
  25605. In particular, do not allow pop-up frames."
  25606. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25607. special-display-function)
  25608. (apply 'switch-to-buffer-other-window args)))
  25609. (defun org-combine-plists (&rest plists)
  25610. "Create a single property list from all plists in PLISTS.
  25611. The process starts by copying the first list, and then setting properties
  25612. from the other lists. Settings in the last list are the most significant
  25613. ones and overrule settings in the other lists."
  25614. (let ((rtn (copy-sequence (pop plists)))
  25615. p v ls)
  25616. (while plists
  25617. (setq ls (pop plists))
  25618. (while ls
  25619. (setq p (pop ls) v (pop ls))
  25620. (setq rtn (plist-put rtn p v))))
  25621. rtn))
  25622. (defun org-move-line-down (arg)
  25623. "Move the current line down. With prefix argument, move it past ARG lines."
  25624. (interactive "p")
  25625. (let ((col (current-column))
  25626. beg end pos)
  25627. (beginning-of-line 1) (setq beg (point))
  25628. (beginning-of-line 2) (setq end (point))
  25629. (beginning-of-line (+ 1 arg))
  25630. (setq pos (move-marker (make-marker) (point)))
  25631. (insert (delete-and-extract-region beg end))
  25632. (goto-char pos)
  25633. (move-to-column col)))
  25634. (defun org-move-line-up (arg)
  25635. "Move the current line up. With prefix argument, move it past ARG lines."
  25636. (interactive "p")
  25637. (let ((col (current-column))
  25638. beg end pos)
  25639. (beginning-of-line 1) (setq beg (point))
  25640. (beginning-of-line 2) (setq end (point))
  25641. (beginning-of-line (- arg))
  25642. (setq pos (move-marker (make-marker) (point)))
  25643. (insert (delete-and-extract-region beg end))
  25644. (goto-char pos)
  25645. (move-to-column col)))
  25646. (defun org-replace-escapes (string table)
  25647. "Replace %-escapes in STRING with values in TABLE.
  25648. TABLE is an association list with keys like \"%a\" and string values.
  25649. The sequences in STRING may contain normal field width and padding information,
  25650. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25651. so values can contain further %-escapes if they are define later in TABLE."
  25652. (let ((case-fold-search nil)
  25653. e re rpl)
  25654. (while (setq e (pop table))
  25655. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25656. (while (string-match re string)
  25657. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25658. (cdr e)))
  25659. (setq string (replace-match rpl t t string))))
  25660. string))
  25661. (defun org-sublist (list start end)
  25662. "Return a section of LIST, from START to END.
  25663. Counting starts at 1."
  25664. (let (rtn (c start))
  25665. (setq list (nthcdr (1- start) list))
  25666. (while (and list (<= c end))
  25667. (push (pop list) rtn)
  25668. (setq c (1+ c)))
  25669. (nreverse rtn)))
  25670. (defun org-find-base-buffer-visiting (file)
  25671. "Like `find-buffer-visiting' but alway return the base buffer and
  25672. not an indirect buffer"
  25673. (let ((buf (find-buffer-visiting file)))
  25674. (if buf
  25675. (or (buffer-base-buffer buf) buf)
  25676. nil)))
  25677. (defun org-image-file-name-regexp ()
  25678. "Return regexp matching the file names of images."
  25679. (if (fboundp 'image-file-name-regexp)
  25680. (image-file-name-regexp)
  25681. (let ((image-file-name-extensions
  25682. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25683. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25684. (concat "\\."
  25685. (regexp-opt (nconc (mapcar 'upcase
  25686. image-file-name-extensions)
  25687. image-file-name-extensions)
  25688. t)
  25689. "\\'"))))
  25690. (defun org-file-image-p (file)
  25691. "Return non-nil if FILE is an image."
  25692. (save-match-data
  25693. (string-match (org-image-file-name-regexp) file)))
  25694. ;;; Paragraph filling stuff.
  25695. ;; We want this to be just right, so use the full arsenal.
  25696. (defun org-indent-line-function ()
  25697. "Indent line like previous, but further if previous was headline or item."
  25698. (interactive)
  25699. (let* ((pos (point))
  25700. (itemp (org-at-item-p))
  25701. column bpos bcol tpos tcol bullet btype bullet-type)
  25702. ;; Find the previous relevant line
  25703. (beginning-of-line 1)
  25704. (cond
  25705. ((looking-at "#") (setq column 0))
  25706. ((looking-at "\\*+ ") (setq column 0))
  25707. (t
  25708. (beginning-of-line 0)
  25709. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25710. (beginning-of-line 0))
  25711. (cond
  25712. ((looking-at "\\*+[ \t]+")
  25713. (goto-char (match-end 0))
  25714. (setq column (current-column)))
  25715. ((org-in-item-p)
  25716. (org-beginning-of-item)
  25717. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25718. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25719. (setq bpos (match-beginning 1) tpos (match-end 0)
  25720. bcol (progn (goto-char bpos) (current-column))
  25721. tcol (progn (goto-char tpos) (current-column))
  25722. bullet (match-string 1)
  25723. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25724. (if (not itemp)
  25725. (setq column tcol)
  25726. (goto-char pos)
  25727. (beginning-of-line 1)
  25728. (if (looking-at "\\S-")
  25729. (progn
  25730. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25731. (setq bullet (match-string 1)
  25732. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25733. (setq column (if (equal btype bullet-type) bcol tcol)))
  25734. (setq column (org-get-indentation)))))
  25735. (t (setq column (org-get-indentation))))))
  25736. (goto-char pos)
  25737. (if (<= (current-column) (current-indentation))
  25738. (indent-line-to column)
  25739. (save-excursion (indent-line-to column)))
  25740. (setq column (current-column))
  25741. (beginning-of-line 1)
  25742. (if (looking-at
  25743. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25744. (replace-match (concat "\\1" (format org-property-format
  25745. (match-string 2) (match-string 3)))
  25746. t nil))
  25747. (move-to-column column)))
  25748. (defun org-set-autofill-regexps ()
  25749. (interactive)
  25750. ;; In the paragraph separator we include headlines, because filling
  25751. ;; text in a line directly attached to a headline would otherwise
  25752. ;; fill the headline as well.
  25753. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25754. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25755. ;; The paragraph starter includes hand-formatted lists.
  25756. (org-set-local 'paragraph-start
  25757. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25758. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25759. ;; But only if the user has not turned off tables or fixed-width regions
  25760. (org-set-local
  25761. 'auto-fill-inhibit-regexp
  25762. (concat "\\*+ \\|#\\+"
  25763. "\\|[ \t]*" org-keyword-time-regexp
  25764. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25765. (concat
  25766. "\\|[ \t]*["
  25767. (if org-enable-table-editor "|" "")
  25768. (if org-enable-fixed-width-editor ":" "")
  25769. "]"))))
  25770. ;; We use our own fill-paragraph function, to make sure that tables
  25771. ;; and fixed-width regions are not wrapped. That function will pass
  25772. ;; through to `fill-paragraph' when appropriate.
  25773. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25774. ; Adaptive filling: To get full control, first make sure that
  25775. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25776. (org-set-local 'adaptive-fill-regexp "\000")
  25777. (org-set-local 'adaptive-fill-function
  25778. 'org-adaptive-fill-function)
  25779. (org-set-local
  25780. 'align-mode-rules-list
  25781. '((org-in-buffer-settings
  25782. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25783. (modes . '(org-mode))))))
  25784. (defun org-fill-paragraph (&optional justify)
  25785. "Re-align a table, pass through to fill-paragraph if no table."
  25786. (let ((table-p (org-at-table-p))
  25787. (table.el-p (org-at-table.el-p)))
  25788. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25789. (save-excursion (goto-char (point-at-bol))
  25790. (looking-at outline-regexp)))
  25791. t) ; skip headlines
  25792. (table.el-p t) ; skip table.el tables
  25793. (table-p (org-table-align) t) ; align org-mode tables
  25794. (t nil)))) ; call paragraph-fill
  25795. ;; For reference, this is the default value of adaptive-fill-regexp
  25796. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25797. (defun org-adaptive-fill-function ()
  25798. "Return a fill prefix for org-mode files.
  25799. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25800. work correctly."
  25801. (cond ((looking-at "#[ \t]+")
  25802. (match-string 0))
  25803. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25804. (save-excursion
  25805. (goto-char (match-end 0))
  25806. (make-string (current-column) ?\ )))
  25807. (t nil)))
  25808. ;;;; Functions extending outline functionality
  25809. (defun org-beginning-of-line (&optional arg)
  25810. "Go to the beginning of the current line. If that is invisible, continue
  25811. to a visible line beginning. This makes the function of C-a more intuitive.
  25812. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25813. first attempt, and only move to after the tags when the cursor is already
  25814. beyond the end of the headline."
  25815. (interactive "P")
  25816. (let ((pos (point)))
  25817. (beginning-of-line 1)
  25818. (if (bobp)
  25819. nil
  25820. (backward-char 1)
  25821. (if (org-invisible-p)
  25822. (while (and (not (bobp)) (org-invisible-p))
  25823. (backward-char 1)
  25824. (beginning-of-line 1))
  25825. (forward-char 1)))
  25826. (when org-special-ctrl-a/e
  25827. (cond
  25828. ((and (looking-at org-todo-line-regexp)
  25829. (= (char-after (match-end 1)) ?\ ))
  25830. (goto-char
  25831. (if (eq org-special-ctrl-a/e t)
  25832. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25833. ((= pos (point)) (match-beginning 3))
  25834. (t (point)))
  25835. (cond ((> pos (point)) (point))
  25836. ((not (eq last-command this-command)) (point))
  25837. (t (match-beginning 3))))))
  25838. ((org-at-item-p)
  25839. (goto-char
  25840. (if (eq org-special-ctrl-a/e t)
  25841. (cond ((> pos (match-end 4)) (match-end 4))
  25842. ((= pos (point)) (match-end 4))
  25843. (t (point)))
  25844. (cond ((> pos (point)) (point))
  25845. ((not (eq last-command this-command)) (point))
  25846. (t (match-end 4))))))))))
  25847. (defun org-end-of-line (&optional arg)
  25848. "Go to the end of the line.
  25849. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25850. first attempt, and only move to after the tags when the cursor is already
  25851. beyond the end of the headline."
  25852. (interactive "P")
  25853. (if (or (not org-special-ctrl-a/e)
  25854. (not (org-on-heading-p)))
  25855. (end-of-line arg)
  25856. (let ((pos (point)))
  25857. (beginning-of-line 1)
  25858. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25859. (if (eq org-special-ctrl-a/e t)
  25860. (if (or (< pos (match-beginning 1))
  25861. (= pos (match-end 0)))
  25862. (goto-char (match-beginning 1))
  25863. (goto-char (match-end 0)))
  25864. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25865. (goto-char (match-end 0))
  25866. (goto-char (match-beginning 1))))
  25867. (end-of-line arg)))))
  25868. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25869. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25870. (defun org-kill-line (&optional arg)
  25871. "Kill line, to tags or end of line."
  25872. (interactive "P")
  25873. (cond
  25874. ((or (not org-special-ctrl-k)
  25875. (bolp)
  25876. (not (org-on-heading-p)))
  25877. (call-interactively 'kill-line))
  25878. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  25879. (kill-region (point) (match-beginning 1))
  25880. (org-set-tags nil t))
  25881. (t (kill-region (point) (point-at-eol)))))
  25882. (define-key org-mode-map "\C-k" 'org-kill-line)
  25883. (defun org-invisible-p ()
  25884. "Check if point is at a character currently not visible."
  25885. ;; Early versions of noutline don't have `outline-invisible-p'.
  25886. (if (fboundp 'outline-invisible-p)
  25887. (outline-invisible-p)
  25888. (get-char-property (point) 'invisible)))
  25889. (defun org-invisible-p2 ()
  25890. "Check if point is at a character currently not visible."
  25891. (save-excursion
  25892. (if (and (eolp) (not (bobp))) (backward-char 1))
  25893. ;; Early versions of noutline don't have `outline-invisible-p'.
  25894. (if (fboundp 'outline-invisible-p)
  25895. (outline-invisible-p)
  25896. (get-char-property (point) 'invisible))))
  25897. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25898. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25899. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25900. (defun org-at-heading-or-item-p ()
  25901. (or (org-on-heading-p) (org-at-item-p)))
  25902. (defun org-on-target-p ()
  25903. (or (org-in-regexp org-radio-target-regexp)
  25904. (org-in-regexp org-target-regexp)))
  25905. (defun org-up-heading-all (arg)
  25906. "Move to the heading line of which the present line is a subheading.
  25907. This function considers both visible and invisible heading lines.
  25908. With argument, move up ARG levels."
  25909. (if (fboundp 'outline-up-heading-all)
  25910. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25911. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25912. (defun org-up-heading-safe ()
  25913. "Move to the heading line of which the present line is a subheading.
  25914. This version will not throw an error. It will return the level of the
  25915. headline found, or nil if no higher level is found."
  25916. (let ((pos (point)) start-level level
  25917. (re (concat "^" outline-regexp)))
  25918. (catch 'exit
  25919. (outline-back-to-heading t)
  25920. (setq start-level (funcall outline-level))
  25921. (if (equal start-level 1) (throw 'exit nil))
  25922. (while (re-search-backward re nil t)
  25923. (setq level (funcall outline-level))
  25924. (if (< level start-level) (throw 'exit level)))
  25925. nil)))
  25926. (defun org-first-sibling-p ()
  25927. "Is this heading the first child of its parents?"
  25928. (interactive)
  25929. (let ((re (concat "^" outline-regexp))
  25930. level l)
  25931. (unless (org-at-heading-p t)
  25932. (error "Not at a heading"))
  25933. (setq level (funcall outline-level))
  25934. (save-excursion
  25935. (if (not (re-search-backward re nil t))
  25936. t
  25937. (setq l (funcall outline-level))
  25938. (< l level)))))
  25939. (defun org-goto-sibling (&optional previous)
  25940. "Goto the next sibling, even if it is invisible.
  25941. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25942. when a sibling was found. When none is found, return nil and don't
  25943. move point."
  25944. (let ((fun (if previous 're-search-backward 're-search-forward))
  25945. (pos (point))
  25946. (re (concat "^" outline-regexp))
  25947. level l)
  25948. (when (condition-case nil (org-back-to-heading t) (error nil))
  25949. (setq level (funcall outline-level))
  25950. (catch 'exit
  25951. (or previous (forward-char 1))
  25952. (while (funcall fun re nil t)
  25953. (setq l (funcall outline-level))
  25954. (when (< l level) (goto-char pos) (throw 'exit nil))
  25955. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25956. (goto-char pos)
  25957. nil))))
  25958. (defun org-show-siblings ()
  25959. "Show all siblings of the current headline."
  25960. (save-excursion
  25961. (while (org-goto-sibling) (org-flag-heading nil)))
  25962. (save-excursion
  25963. (while (org-goto-sibling 'previous)
  25964. (org-flag-heading nil))))
  25965. (defun org-show-hidden-entry ()
  25966. "Show an entry where even the heading is hidden."
  25967. (save-excursion
  25968. (org-show-entry)))
  25969. (defun org-flag-heading (flag &optional entry)
  25970. "Flag the current heading. FLAG non-nil means make invisible.
  25971. When ENTRY is non-nil, show the entire entry."
  25972. (save-excursion
  25973. (org-back-to-heading t)
  25974. ;; Check if we should show the entire entry
  25975. (if entry
  25976. (progn
  25977. (org-show-entry)
  25978. (save-excursion
  25979. (and (outline-next-heading)
  25980. (org-flag-heading nil))))
  25981. (outline-flag-region (max (point-min) (1- (point)))
  25982. (save-excursion (outline-end-of-heading) (point))
  25983. flag))))
  25984. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25985. ;; This is an exact copy of the original function, but it uses
  25986. ;; `org-back-to-heading', to make it work also in invisible
  25987. ;; trees. And is uses an invisible-OK argument.
  25988. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25989. (org-back-to-heading invisible-OK)
  25990. (let ((first t)
  25991. (level (funcall outline-level)))
  25992. (while (and (not (eobp))
  25993. (or first (> (funcall outline-level) level)))
  25994. (setq first nil)
  25995. (outline-next-heading))
  25996. (unless to-heading
  25997. (if (memq (preceding-char) '(?\n ?\^M))
  25998. (progn
  25999. ;; Go to end of line before heading
  26000. (forward-char -1)
  26001. (if (memq (preceding-char) '(?\n ?\^M))
  26002. ;; leave blank line before heading
  26003. (forward-char -1))))))
  26004. (point))
  26005. (defun org-show-subtree ()
  26006. "Show everything after this heading at deeper levels."
  26007. (outline-flag-region
  26008. (point)
  26009. (save-excursion
  26010. (outline-end-of-subtree) (outline-next-heading) (point))
  26011. nil))
  26012. (defun org-show-entry ()
  26013. "Show the body directly following this heading.
  26014. Show the heading too, if it is currently invisible."
  26015. (interactive)
  26016. (save-excursion
  26017. (condition-case nil
  26018. (progn
  26019. (org-back-to-heading t)
  26020. (outline-flag-region
  26021. (max (point-min) (1- (point)))
  26022. (save-excursion
  26023. (re-search-forward
  26024. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26025. (or (match-beginning 1) (point-max)))
  26026. nil))
  26027. (error nil))))
  26028. (defun org-make-options-regexp (kwds)
  26029. "Make a regular expression for keyword lines."
  26030. (concat
  26031. "^"
  26032. "#?[ \t]*\\+\\("
  26033. (mapconcat 'regexp-quote kwds "\\|")
  26034. "\\):[ \t]*"
  26035. "\\(.+\\)"))
  26036. ;; Make isearch reveal the necessary context
  26037. (defun org-isearch-end ()
  26038. "Reveal context after isearch exits."
  26039. (when isearch-success ; only if search was successful
  26040. (if (featurep 'xemacs)
  26041. ;; Under XEmacs, the hook is run in the correct place,
  26042. ;; we directly show the context.
  26043. (org-show-context 'isearch)
  26044. ;; In Emacs the hook runs *before* restoring the overlays.
  26045. ;; So we have to use a one-time post-command-hook to do this.
  26046. ;; (Emacs 22 has a special variable, see function `org-mode')
  26047. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26048. isearch-mode-end-hook-quit)
  26049. ;; Only when the isearch was not quitted.
  26050. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26051. 'append 'local)))))
  26052. (defun org-isearch-post-command ()
  26053. "Remove self from hook, and show context."
  26054. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26055. (org-show-context 'isearch))
  26056. ;;;; Integration with and fixes for other packages
  26057. ;;; Imenu support
  26058. (defvar org-imenu-markers nil
  26059. "All markers currently used by Imenu.")
  26060. (make-variable-buffer-local 'org-imenu-markers)
  26061. (defun org-imenu-new-marker (&optional pos)
  26062. "Return a new marker for use by Imenu, and remember the marker."
  26063. (let ((m (make-marker)))
  26064. (move-marker m (or pos (point)))
  26065. (push m org-imenu-markers)
  26066. m))
  26067. (defun org-imenu-get-tree ()
  26068. "Produce the index for Imenu."
  26069. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26070. (setq org-imenu-markers nil)
  26071. (let* ((n org-imenu-depth)
  26072. (re (concat "^" outline-regexp))
  26073. (subs (make-vector (1+ n) nil))
  26074. (last-level 0)
  26075. m tree level head)
  26076. (save-excursion
  26077. (save-restriction
  26078. (widen)
  26079. (goto-char (point-max))
  26080. (while (re-search-backward re nil t)
  26081. (setq level (org-reduced-level (funcall outline-level)))
  26082. (when (<= level n)
  26083. (looking-at org-complex-heading-regexp)
  26084. (setq head (org-match-string-no-properties 4)
  26085. m (org-imenu-new-marker))
  26086. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26087. (if (>= level last-level)
  26088. (push (cons head m) (aref subs level))
  26089. (push (cons head (aref subs (1+ level))) (aref subs level))
  26090. (loop for i from (1+ level) to n do (aset subs i nil)))
  26091. (setq last-level level)))))
  26092. (aref subs 1)))
  26093. (eval-after-load "imenu"
  26094. '(progn
  26095. (add-hook 'imenu-after-jump-hook
  26096. (lambda () (org-show-context 'org-goto)))))
  26097. ;; Speedbar support
  26098. (defun org-speedbar-set-agenda-restriction ()
  26099. "Restrict future agenda commands to the location at point in speedbar.
  26100. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26101. (interactive)
  26102. (let (p m tp np dir txt w)
  26103. (cond
  26104. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26105. 'org-imenu t))
  26106. (setq m (get-text-property p 'org-imenu-marker))
  26107. (save-excursion
  26108. (save-restriction
  26109. (set-buffer (marker-buffer m))
  26110. (goto-char m)
  26111. (org-agenda-set-restriction-lock 'subtree))))
  26112. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26113. 'speedbar-function 'speedbar-find-file))
  26114. (setq tp (previous-single-property-change
  26115. (1+ p) 'speedbar-function)
  26116. np (next-single-property-change
  26117. tp 'speedbar-function)
  26118. dir (speedbar-line-directory)
  26119. txt (buffer-substring-no-properties (or tp (point-min))
  26120. (or np (point-max))))
  26121. (save-excursion
  26122. (save-restriction
  26123. (set-buffer (find-file-noselect
  26124. (let ((default-directory dir))
  26125. (expand-file-name txt))))
  26126. (unless (org-mode-p)
  26127. (error "Cannot restrict to non-Org-mode file"))
  26128. (org-agenda-set-restriction-lock 'file))))
  26129. (t (error "Don't know how to restrict Org-mode's agenda")))
  26130. (org-move-overlay org-speedbar-restriction-lock-overlay
  26131. (point-at-bol) (point-at-eol))
  26132. (setq current-prefix-arg nil)
  26133. (org-agenda-maybe-redo)))
  26134. (eval-after-load "speedbar"
  26135. '(progn
  26136. (speedbar-add-supported-extension ".org")
  26137. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26138. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26139. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26140. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26141. (add-hook 'speedbar-visiting-tag-hook
  26142. (lambda () (org-show-context 'org-goto)))))
  26143. ;;; Fixes and Hacks
  26144. ;; Make flyspell not check words in links, to not mess up our keymap
  26145. (defun org-mode-flyspell-verify ()
  26146. "Don't let flyspell put overlays at active buttons."
  26147. (not (get-text-property (point) 'keymap)))
  26148. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26149. (eval-after-load "bookmark"
  26150. '(if (boundp 'bookmark-after-jump-hook)
  26151. ;; We can use the hook
  26152. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26153. ;; Hook not available, use advice
  26154. (defadvice bookmark-jump (after org-make-visible activate)
  26155. "Make the position visible."
  26156. (org-bookmark-jump-unhide))))
  26157. (defun org-bookmark-jump-unhide ()
  26158. "Unhide the current position, to show the bookmark location."
  26159. (and (org-mode-p)
  26160. (or (org-invisible-p)
  26161. (save-excursion (goto-char (max (point-min) (1- (point))))
  26162. (org-invisible-p)))
  26163. (org-show-context 'bookmark-jump)))
  26164. ;; Fix a bug in htmlize where there are text properties (face nil)
  26165. (eval-after-load "htmlize"
  26166. '(progn
  26167. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26168. "Make sure there are no nil faces"
  26169. (setq ad-return-value (delq nil ad-return-value)))))
  26170. ;; Make session.el ignore our circular variable
  26171. (eval-after-load "session"
  26172. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26173. ;;;; Experimental code
  26174. (defun org-closed-in-range ()
  26175. "Sparse tree of items closed in a certain time range.
  26176. Still experimental, may disappear in the future."
  26177. (interactive)
  26178. ;; Get the time interval from the user.
  26179. (let* ((time1 (time-to-seconds
  26180. (org-read-date nil 'to-time nil "Starting date: ")))
  26181. (time2 (time-to-seconds
  26182. (org-read-date nil 'to-time nil "End date:")))
  26183. ;; callback function
  26184. (callback (lambda ()
  26185. (let ((time
  26186. (time-to-seconds
  26187. (apply 'encode-time
  26188. (org-parse-time-string
  26189. (match-string 1))))))
  26190. ;; check if time in interval
  26191. (and (>= time time1) (<= time time2))))))
  26192. ;; make tree, check each match with the callback
  26193. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26194. ;;;; Finish up
  26195. (provide 'org)
  26196. (run-hooks 'org-load-hook)
  26197. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26198. ;;; org.el ends here