org.el 1.0 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 5.23a-test
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  77. (require 'easymenu)
  78. ;;;; Customization variables
  79. ;;; Version
  80. (defconst org-version "5.23a-test"
  81. "The version number of the file org.el.")
  82. (defun org-version (&optional here)
  83. "Show the org-mode version in the echo area.
  84. With prefix arg HERE, insert it at point."
  85. (interactive "P")
  86. (let ((version (format "Org-mode version %s" org-version)))
  87. (message version)
  88. (if here
  89. (insert version))))
  90. ;;; Compatibility constants
  91. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  92. (defconst org-format-transports-properties-p
  93. (let ((x "a"))
  94. (add-text-properties 0 1 '(test t) x)
  95. (get-text-property 0 'test (format "%s" x)))
  96. "Does format transport text properties?")
  97. (defmacro org-bound-and-true-p (var)
  98. "Return the value of symbol VAR if it is bound, else nil."
  99. `(and (boundp (quote ,var)) ,var))
  100. (defmacro org-unmodified (&rest body)
  101. "Execute body without changing `buffer-modified-p'."
  102. `(set-buffer-modified-p
  103. (prog1 (buffer-modified-p) ,@body)))
  104. (defmacro org-re (s)
  105. "Replace posix classes in regular expression."
  106. (if (featurep 'xemacs)
  107. (let ((ss s))
  108. (save-match-data
  109. (while (string-match "\\[:alnum:\\]" ss)
  110. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  111. (while (string-match "\\[:alpha:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z" t t ss)))
  113. ss))
  114. s))
  115. (defmacro org-preserve-lc (&rest body)
  116. `(let ((_line (org-current-line))
  117. (_col (current-column)))
  118. (unwind-protect
  119. (progn ,@body)
  120. (goto-line _line)
  121. (move-to-column _col))))
  122. (defmacro org-without-partial-completion (&rest body)
  123. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  124. partial-completion-mode)))
  125. (unwind-protect
  126. (progn
  127. (if pc-mode (partial-completion-mode -1))
  128. ,@body)
  129. (if pc-mode (partial-completion-mode 1)))))
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'hypermedia
  136. :group 'calendar)
  137. (defcustom org-load-hook nil
  138. "Hook that is run after org.el has been loaded."
  139. :group 'org
  140. :type 'hook)
  141. (defvar org-modules) ; defined below
  142. (defvar org-modules-loaded nil
  143. "Have the modules been loaded already?")
  144. (defun org-load-modules-maybe (&optional force)
  145. "Load all extensions listed in `org-default-extensions'."
  146. (when (or force (not org-modules-loaded))
  147. (mapc (lambda (ext)
  148. (condition-case nil (require ext)
  149. (error (message "Problems while trying to load feature `%s'" ext))))
  150. org-modules)
  151. (setq org-modules-loaded t)))
  152. (defun org-set-modules (var value)
  153. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  154. (set var value)
  155. (when (featurep 'org)
  156. (org-load-modules-maybe 'force)))
  157. (defcustom org-modules '(org-bbdb org-gnus org-info org-irc org-mhe org-rmail org-vm org-wl)
  158. "Modules that should always be loaded together with org.el.
  159. If the description starts with <A>, this means the extension
  160. will be autoloaded when needed, preloading is not necessary.
  161. If a description starts with <C>, the file is not part of emacs
  162. and loading it will require that you have downloaded and properly installed
  163. the org-mode distribution."
  164. :group 'org
  165. :set 'org-set-modules
  166. :type
  167. '(set :greedy t
  168. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  169. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  170. (const :tag " info: Links to Info nodes" org-info)
  171. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  172. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  173. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  174. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  175. (const :tag " vm: Links to VM folders/messages" org-vm)
  176. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  177. (const :tag " mouse: Additional mouse support" org-mouse)
  178. ; (const :tag "A export-latex: LaTeX export" org-export-latex)
  179. ; (const :tag "A publish: Publishing" org-publish)
  180. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  181. (const :tag "C bibtex: Org links to BibTeX entries" org-bibtex)
  182. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  183. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  184. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  185. (const :tag "C id: Global id's for identifying entries" org-id)
  186. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  187. (const :tag "C iswitchb: Use iswitchb to select Org buffer" org-iswitchb)
  188. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  189. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  190. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  191. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  192. (const :tag "C registry: A registry for Org links" org-registry)
  193. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  194. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  195. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)))
  196. ;; FIXME: Needs a separate group...
  197. (defcustom org-completion-fallback-command 'hippie-expand
  198. "The expansion command called by \\[org-complete] in normal context.
  199. Normal means, no org-mode-specific context."
  200. :group 'org
  201. :type 'function)
  202. (defgroup org-startup nil
  203. "Options concerning startup of Org-mode."
  204. :tag "Org Startup"
  205. :group 'org)
  206. (defcustom org-startup-folded t
  207. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  208. This can also be configured on a per-file basis by adding one of
  209. the following lines anywhere in the buffer:
  210. #+STARTUP: fold
  211. #+STARTUP: nofold
  212. #+STARTUP: content"
  213. :group 'org-startup
  214. :type '(choice
  215. (const :tag "nofold: show all" nil)
  216. (const :tag "fold: overview" t)
  217. (const :tag "content: all headlines" content)))
  218. (defcustom org-startup-truncated t
  219. "Non-nil means, entering Org-mode will set `truncate-lines'.
  220. This is useful since some lines containing links can be very long and
  221. uninteresting. Also tables look terrible when wrapped."
  222. :group 'org-startup
  223. :type 'boolean)
  224. (defcustom org-startup-align-all-tables nil
  225. "Non-nil means, align all tables when visiting a file.
  226. This is useful when the column width in tables is forced with <N> cookies
  227. in table fields. Such tables will look correct only after the first re-align.
  228. This can also be configured on a per-file basis by adding one of
  229. the following lines anywhere in the buffer:
  230. #+STARTUP: align
  231. #+STARTUP: noalign"
  232. :group 'org-startup
  233. :type 'boolean)
  234. (defcustom org-insert-mode-line-in-empty-file nil
  235. "Non-nil means insert the first line setting Org-mode in empty files.
  236. When the function `org-mode' is called interactively in an empty file, this
  237. normally means that the file name does not automatically trigger Org-mode.
  238. To ensure that the file will always be in Org-mode in the future, a
  239. line enforcing Org-mode will be inserted into the buffer, if this option
  240. has been set."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-replace-disputed-keys nil
  244. "Non-nil means use alternative key bindings for some keys.
  245. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  246. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  247. If you want to use Org-mode together with one of these other modes,
  248. or more generally if you would like to move some Org-mode commands to
  249. other keys, set this variable and configure the keys with the variable
  250. `org-disputed-keys'.
  251. This option is only relevant at load-time of Org-mode, and must be set
  252. *before* org.el is loaded. Changing it requires a restart of Emacs to
  253. become effective."
  254. :group 'org-startup
  255. :type 'boolean)
  256. (if (fboundp 'defvaralias)
  257. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  258. (defcustom org-disputed-keys
  259. '(([(shift up)] . [(meta p)])
  260. ([(shift down)] . [(meta n)])
  261. ([(shift left)] . [(meta -)])
  262. ([(shift right)] . [(meta +)])
  263. ([(control shift right)] . [(meta shift +)])
  264. ([(control shift left)] . [(meta shift -)]))
  265. "Keys for which Org-mode and other modes compete.
  266. This is an alist, cars are the default keys, second element specifies
  267. the alternative to use when `org-replace-disputed-keys' is t.
  268. Keys can be specified in any syntax supported by `define-key'.
  269. The value of this option takes effect only at Org-mode's startup,
  270. therefore you'll have to restart Emacs to apply it after changing."
  271. :group 'org-startup
  272. :type 'alist)
  273. (defun org-key (key)
  274. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  275. Or return the original if not disputed."
  276. (if org-replace-disputed-keys
  277. (let* ((nkey (key-description key))
  278. (x (org-find-if (lambda (x)
  279. (equal (key-description (car x)) nkey))
  280. org-disputed-keys)))
  281. (if x (cdr x) key))
  282. key))
  283. (defun org-find-if (predicate seq)
  284. (catch 'exit
  285. (while seq
  286. (if (funcall predicate (car seq))
  287. (throw 'exit (car seq))
  288. (pop seq)))))
  289. (defun org-defkey (keymap key def)
  290. "Define a key, possibly translated, as returned by `org-key'."
  291. (define-key keymap (org-key key) def))
  292. (defcustom org-ellipsis nil
  293. "The ellipsis to use in the Org-mode outline.
  294. When nil, just use the standard three dots. When a string, use that instead,
  295. When a face, use the standart 3 dots, but with the specified face.
  296. The change affects only Org-mode (which will then use its own display table).
  297. Changing this requires executing `M-x org-mode' in a buffer to become
  298. effective."
  299. :group 'org-startup
  300. :type '(choice (const :tag "Default" nil)
  301. (face :tag "Face" :value org-warning)
  302. (string :tag "String" :value "...#")))
  303. (defvar org-display-table nil
  304. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  305. (defgroup org-keywords nil
  306. "Keywords in Org-mode."
  307. :tag "Org Keywords"
  308. :group 'org)
  309. (defcustom org-deadline-string "DEADLINE:"
  310. "String to mark deadline entries.
  311. A deadline is this string, followed by a time stamp. Should be a word,
  312. terminated by a colon. You can insert a schedule keyword and
  313. a timestamp with \\[org-deadline].
  314. Changes become only effective after restarting Emacs."
  315. :group 'org-keywords
  316. :type 'string)
  317. (defcustom org-scheduled-string "SCHEDULED:"
  318. "String to mark scheduled TODO entries.
  319. A schedule is this string, followed by a time stamp. Should be a word,
  320. terminated by a colon. You can insert a schedule keyword and
  321. a timestamp with \\[org-schedule].
  322. Changes become only effective after restarting Emacs."
  323. :group 'org-keywords
  324. :type 'string)
  325. (defcustom org-closed-string "CLOSED:"
  326. "String used as the prefix for timestamps logging closing a TODO entry."
  327. :group 'org-keywords
  328. :type 'string)
  329. (defcustom org-clock-string "CLOCK:"
  330. "String used as prefix for timestamps clocking work hours on an item."
  331. :group 'org-keywords
  332. :type 'string)
  333. (defcustom org-comment-string "COMMENT"
  334. "Entries starting with this keyword will never be exported.
  335. An entry can be toggled between COMMENT and normal with
  336. \\[org-toggle-comment].
  337. Changes become only effective after restarting Emacs."
  338. :group 'org-keywords
  339. :type 'string)
  340. (defcustom org-quote-string "QUOTE"
  341. "Entries starting with this keyword will be exported in fixed-width font.
  342. Quoting applies only to the text in the entry following the headline, and does
  343. not extend beyond the next headline, even if that is lower level.
  344. An entry can be toggled between QUOTE and normal with
  345. \\[org-toggle-fixed-width-section]."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defconst org-repeat-re
  349. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  350. "Regular expression for specifying repeated events.
  351. After a match, group 1 contains the repeat expression.")
  352. (defgroup org-structure nil
  353. "Options concerning the general structure of Org-mode files."
  354. :tag "Org Structure"
  355. :group 'org)
  356. (defgroup org-reveal-location nil
  357. "Options about how to make context of a location visible."
  358. :tag "Org Reveal Location"
  359. :group 'org-structure)
  360. (defconst org-context-choice
  361. '(choice
  362. (const :tag "Always" t)
  363. (const :tag "Never" nil)
  364. (repeat :greedy t :tag "Individual contexts"
  365. (cons
  366. (choice :tag "Context"
  367. (const agenda)
  368. (const org-goto)
  369. (const occur-tree)
  370. (const tags-tree)
  371. (const link-search)
  372. (const mark-goto)
  373. (const bookmark-jump)
  374. (const isearch)
  375. (const default))
  376. (boolean))))
  377. "Contexts for the reveal options.")
  378. (defcustom org-show-hierarchy-above '((default . t))
  379. "Non-nil means, show full hierarchy when revealing a location.
  380. Org-mode often shows locations in an org-mode file which might have
  381. been invisible before. When this is set, the hierarchy of headings
  382. above the exposed location is shown.
  383. Turning this off for example for sparse trees makes them very compact.
  384. Instead of t, this can also be an alist specifying this option for different
  385. contexts. Valid contexts are
  386. agenda when exposing an entry from the agenda
  387. org-goto when using the command `org-goto' on key C-c C-j
  388. occur-tree when using the command `org-occur' on key C-c /
  389. tags-tree when constructing a sparse tree based on tags matches
  390. link-search when exposing search matches associated with a link
  391. mark-goto when exposing the jump goal of a mark
  392. bookmark-jump when exposing a bookmark location
  393. isearch when exiting from an incremental search
  394. default default for all contexts not set explicitly"
  395. :group 'org-reveal-location
  396. :type org-context-choice)
  397. (defcustom org-show-following-heading '((default . nil))
  398. "Non-nil means, show following heading when revealing a location.
  399. Org-mode often shows locations in an org-mode file which might have
  400. been invisible before. When this is set, the heading following the
  401. match is shown.
  402. Turning this off for example for sparse trees makes them very compact,
  403. but makes it harder to edit the location of the match. In such a case,
  404. use the command \\[org-reveal] to show more context.
  405. Instead of t, this can also be an alist specifying this option for different
  406. contexts. See `org-show-hierarchy-above' for valid contexts."
  407. :group 'org-reveal-location
  408. :type org-context-choice)
  409. (defcustom org-show-siblings '((default . nil) (isearch t))
  410. "Non-nil means, show all sibling heading when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the sibling of the current entry
  413. heading are all made visible. If `org-show-hierarchy-above' is t,
  414. the same happens on each level of the hierarchy above the current entry.
  415. By default this is on for the isearch context, off for all other contexts.
  416. Turning this off for example for sparse trees makes them very compact,
  417. but makes it harder to edit the location of the match. In such a case,
  418. use the command \\[org-reveal] to show more context.
  419. Instead of t, this can also be an alist specifying this option for different
  420. contexts. See `org-show-hierarchy-above' for valid contexts."
  421. :group 'org-reveal-location
  422. :type org-context-choice)
  423. (defcustom org-show-entry-below '((default . nil))
  424. "Non-nil means, show the entry below a headline when revealing a location.
  425. Org-mode often shows locations in an org-mode file which might have
  426. been invisible before. When this is set, the text below the headline that is
  427. exposed is also shown.
  428. By default this is off for all contexts.
  429. Instead of t, this can also be an alist specifying this option for different
  430. contexts. See `org-show-hierarchy-above' for valid contexts."
  431. :group 'org-reveal-location
  432. :type org-context-choice)
  433. (defgroup org-cycle nil
  434. "Options concerning visibility cycling in Org-mode."
  435. :tag "Org Cycle"
  436. :group 'org-structure)
  437. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  438. "Names of drawers. Drawers are not opened by cycling on the headline above.
  439. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  440. this:
  441. :DRAWERNAME:
  442. .....
  443. :END:
  444. The drawer \"PROPERTIES\" is special for capturing properties through
  445. the property API.
  446. Drawers can be defined on the per-file basis with a line like:
  447. #+DRAWERS: HIDDEN STATE PROPERTIES"
  448. :group 'org-structure
  449. :type '(repeat (string :tag "Drawer Name")))
  450. (defcustom org-cycle-global-at-bob nil
  451. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  452. This makes it possible to do global cycling without having to use S-TAB or
  453. C-u TAB. For this special case to work, the first line of the buffer
  454. must not be a headline - it may be empty ot some other text. When used in
  455. this way, `org-cycle-hook' is disables temporarily, to make sure the
  456. cursor stays at the beginning of the buffer.
  457. When this option is nil, don't do anything special at the beginning
  458. of the buffer."
  459. :group 'org-cycle
  460. :type 'boolean)
  461. (defcustom org-cycle-emulate-tab t
  462. "Where should `org-cycle' emulate TAB.
  463. nil Never
  464. white Only in completely white lines
  465. whitestart Only at the beginning of lines, before the first non-white char
  466. t Everywhere except in headlines
  467. exc-hl-bol Everywhere except at the start of a headline
  468. If TAB is used in a place where it does not emulate TAB, the current subtree
  469. visibility is cycled."
  470. :group 'org-cycle
  471. :type '(choice (const :tag "Never" nil)
  472. (const :tag "Only in completely white lines" white)
  473. (const :tag "Before first char in a line" whitestart)
  474. (const :tag "Everywhere except in headlines" t)
  475. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  476. ))
  477. (defcustom org-cycle-separator-lines 2
  478. "Number of empty lines needed to keep an empty line between collapsed trees.
  479. If you leave an empty line between the end of a subtree and the following
  480. headline, this empty line is hidden when the subtree is folded.
  481. Org-mode will leave (exactly) one empty line visible if the number of
  482. empty lines is equal or larger to the number given in this variable.
  483. So the default 2 means, at least 2 empty lines after the end of a subtree
  484. are needed to produce free space between a collapsed subtree and the
  485. following headline.
  486. Special case: when 0, never leave empty lines in collapsed view."
  487. :group 'org-cycle
  488. :type 'integer)
  489. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  490. org-cycle-hide-drawers
  491. org-cycle-show-empty-lines
  492. org-optimize-window-after-visibility-change)
  493. "Hook that is run after `org-cycle' has changed the buffer visibility.
  494. The function(s) in this hook must accept a single argument which indicates
  495. the new state that was set by the most recent `org-cycle' command. The
  496. argument is a symbol. After a global state change, it can have the values
  497. `overview', `content', or `all'. After a local state change, it can have
  498. the values `folded', `children', or `subtree'."
  499. :group 'org-cycle
  500. :type 'hook)
  501. (defgroup org-edit-structure nil
  502. "Options concerning structure editing in Org-mode."
  503. :tag "Org Edit Structure"
  504. :group 'org-structure)
  505. (defcustom org-odd-levels-only nil
  506. "Non-nil means, skip even levels and only use odd levels for the outline.
  507. This has the effect that two stars are being added/taken away in
  508. promotion/demotion commands. It also influences how levels are
  509. handled by the exporters.
  510. Changing it requires restart of `font-lock-mode' to become effective
  511. for fontification also in regions already fontified.
  512. You may also set this on a per-file basis by adding one of the following
  513. lines to the buffer:
  514. #+STARTUP: odd
  515. #+STARTUP: oddeven"
  516. :group 'org-edit-structure
  517. :group 'org-font-lock
  518. :type 'boolean)
  519. (defcustom org-adapt-indentation t
  520. "Non-nil means, adapt indentation when promoting and demoting.
  521. When this is set and the *entire* text in an entry is indented, the
  522. indentation is increased by one space in a demotion command, and
  523. decreased by one in a promotion command. If any line in the entry
  524. body starts at column 0, indentation is not changed at all."
  525. :group 'org-edit-structure
  526. :type 'boolean)
  527. (defcustom org-special-ctrl-a/e nil
  528. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  529. When t, `C-a' will bring back the cursor to the beginning of the
  530. headline text, i.e. after the stars and after a possible TODO keyword.
  531. In an item, this will be the position after the bullet.
  532. When the cursor is already at that position, another `C-a' will bring
  533. it to the beginning of the line.
  534. `C-e' will jump to the end of the headline, ignoring the presence of tags
  535. in the headline. A second `C-e' will then jump to the true end of the
  536. line, after any tags.
  537. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  538. and only a directly following, identical keypress will bring the cursor
  539. to the special positions."
  540. :group 'org-edit-structure
  541. :type '(choice
  542. (const :tag "off" nil)
  543. (const :tag "after bullet first" t)
  544. (const :tag "border first" reversed)))
  545. (if (fboundp 'defvaralias)
  546. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  547. (defcustom org-special-ctrl-k nil
  548. "Non-nil means `C-k' will behave specially in headlines.
  549. When nil, `C-k' will call the default `kill-line' command.
  550. When t, the following will happen while the cursor is in the headline:
  551. - When the cursor is at the beginning of a headline, kill the entire
  552. line and possible the folded subtree below the line.
  553. - When in the middle of the headline text, kill the headline up to the tags.
  554. - When after the headline text, kill the tags."
  555. :group 'org-edit-structure
  556. :type 'boolean)
  557. (defcustom org-M-RET-may-split-line '((default . t))
  558. "Non-nil means, M-RET will split the line at the cursor position.
  559. When nil, it will go to the end of the line before making a
  560. new line.
  561. You may also set this option in a different way for different
  562. contexts. Valid contexts are:
  563. headline when creating a new headline
  564. item when creating a new item
  565. table in a table field
  566. default the value to be used for all contexts not explicitly
  567. customized"
  568. :group 'org-structure
  569. :group 'org-table
  570. :type '(choice
  571. (const :tag "Always" t)
  572. (const :tag "Never" nil)
  573. (repeat :greedy t :tag "Individual contexts"
  574. (cons
  575. (choice :tag "Context"
  576. (const headline)
  577. (const item)
  578. (const table)
  579. (const default))
  580. (boolean)))))
  581. (defcustom org-blank-before-new-entry '((heading . nil)
  582. (plain-list-item . nil))
  583. "Should `org-insert-heading' leave a blank line before new heading/item?
  584. The value is an alist, with `heading' and `plain-list-item' as car,
  585. and a boolean flag as cdr."
  586. :group 'org-edit-structure
  587. :type '(list
  588. (cons (const heading) (boolean))
  589. (cons (const plain-list-item) (boolean))))
  590. (defcustom org-insert-heading-hook nil
  591. "Hook being run after inserting a new heading."
  592. :group 'org-edit-structure
  593. :type 'hook)
  594. (defcustom org-enable-fixed-width-editor t
  595. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  596. This currently only means, they are never auto-wrapped.
  597. When nil, such lines will be treated like ordinary lines.
  598. See also the QUOTE keyword."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-goto-auto-isearch t
  602. "Non-nil means, typing characters in org-goto starts incremental search."
  603. :group 'org-edit-structure
  604. :type 'boolean)
  605. (defgroup org-sparse-trees nil
  606. "Options concerning sparse trees in Org-mode."
  607. :tag "Org Sparse Trees"
  608. :group 'org-structure)
  609. (defcustom org-highlight-sparse-tree-matches t
  610. "Non-nil means, highlight all matches that define a sparse tree.
  611. The highlights will automatically disappear the next time the buffer is
  612. changed by an edit command."
  613. :group 'org-sparse-trees
  614. :type 'boolean)
  615. (defcustom org-remove-highlights-with-change t
  616. "Non-nil means, any change to the buffer will remove temporary highlights.
  617. Such highlights are created by `org-occur' and `org-clock-display'.
  618. When nil, `C-c C-c needs to be used to get rid of the highlights.
  619. The highlights created by `org-preview-latex-fragment' always need
  620. `C-c C-c' to be removed."
  621. :group 'org-sparse-trees
  622. :group 'org-time
  623. :type 'boolean)
  624. (defcustom org-occur-hook '(org-first-headline-recenter)
  625. "Hook that is run after `org-occur' has constructed a sparse tree.
  626. This can be used to recenter the window to show as much of the structure
  627. as possible."
  628. :group 'org-sparse-trees
  629. :type 'hook)
  630. (defgroup org-plain-lists nil
  631. "Options concerning plain lists in Org-mode."
  632. :tag "Org Plain lists"
  633. :group 'org-structure)
  634. (defcustom org-cycle-include-plain-lists nil
  635. "Non-nil means, include plain lists into visibility cycling.
  636. This means that during cycling, plain list items will *temporarily* be
  637. interpreted as outline headlines with a level given by 1000+i where i is the
  638. indentation of the bullet. In all other operations, plain list items are
  639. not seen as headlines. For example, you cannot assign a TODO keyword to
  640. such an item."
  641. :group 'org-plain-lists
  642. :type 'boolean)
  643. (defcustom org-plain-list-ordered-item-terminator t
  644. "The character that makes a line with leading number an ordered list item.
  645. Valid values are ?. and ?\). To get both terminators, use t. While
  646. ?. may look nicer, it creates the danger that a line with leading
  647. number may be incorrectly interpreted as an item. ?\) therefore is
  648. the safe choice."
  649. :group 'org-plain-lists
  650. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  651. (const :tag "paren like in \"2)\"" ?\))
  652. (const :tab "both" t)))
  653. (defcustom org-auto-renumber-ordered-lists t
  654. "Non-nil means, automatically renumber ordered plain lists.
  655. Renumbering happens when the sequence have been changed with
  656. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  657. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  658. :group 'org-plain-lists
  659. :type 'boolean)
  660. (defcustom org-provide-checkbox-statistics t
  661. "Non-nil means, update checkbox statistics after insert and toggle.
  662. When this is set, checkbox statistics is updated each time you either insert
  663. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  664. with \\[org-ctrl-c-ctrl-c\\]."
  665. :group 'org-plain-lists
  666. :type 'boolean)
  667. (defgroup org-archive nil
  668. "Options concerning archiving in Org-mode."
  669. :tag "Org Archive"
  670. :group 'org-structure)
  671. (defcustom org-archive-tag "ARCHIVE"
  672. "The tag that marks a subtree as archived.
  673. An archived subtree does not open during visibility cycling, and does
  674. not contribute to the agenda listings.
  675. After changing this, font-lock must be restarted in the relevant buffers to
  676. get the proper fontification."
  677. :group 'org-archive
  678. :group 'org-keywords
  679. :type 'string)
  680. (defcustom org-agenda-skip-archived-trees t
  681. "Non-nil means, the agenda will skip any items located in archived trees.
  682. An archived tree is a tree marked with the tag ARCHIVE."
  683. :group 'org-archive
  684. :group 'org-agenda-skip
  685. :type 'boolean)
  686. (defcustom org-cycle-open-archived-trees nil
  687. "Non-nil means, `org-cycle' will open archived trees.
  688. An archived tree is a tree marked with the tag ARCHIVE.
  689. When nil, archived trees will stay folded. You can still open them with
  690. normal outline commands like `show-all', but not with the cycling commands."
  691. :group 'org-archive
  692. :group 'org-cycle
  693. :type 'boolean)
  694. (defcustom org-sparse-tree-open-archived-trees nil
  695. "Non-nil means sparse tree construction shows matches in archived trees.
  696. When nil, matches in these trees are highlighted, but the trees are kept in
  697. collapsed state."
  698. :group 'org-archive
  699. :group 'org-sparse-trees
  700. :type 'boolean)
  701. (defcustom org-archive-location "%s_archive::"
  702. "The location where subtrees should be archived.
  703. This string consists of two parts, separated by a double-colon.
  704. The first part is a file name - when omitted, archiving happens in the same
  705. file. %s will be replaced by the current file name (without directory part).
  706. Archiving to a different file is useful to keep archived entries from
  707. contributing to the Org-mode Agenda.
  708. The part after the double colon is a headline. The archived entries will be
  709. filed under that headline. When omitted, the subtrees are simply filed away
  710. at the end of the file, as top-level entries.
  711. Here are a few examples:
  712. \"%s_archive::\"
  713. If the current file is Projects.org, archive in file
  714. Projects.org_archive, as top-level trees. This is the default.
  715. \"::* Archived Tasks\"
  716. Archive in the current file, under the top-level headline
  717. \"* Archived Tasks\".
  718. \"~/org/archive.org::\"
  719. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  720. \"basement::** Finished Tasks\"
  721. Archive in file ./basement (relative path), as level 3 trees
  722. below the level 2 heading \"** Finished Tasks\".
  723. You may set this option on a per-file basis by adding to the buffer a
  724. line like
  725. #+ARCHIVE: basement::** Finished Tasks"
  726. :group 'org-archive
  727. :type 'string)
  728. (defcustom org-archive-mark-done t
  729. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  730. This can be a string to set the keyword to use. When t, Org-mode will
  731. use the first keyword in its list that means done."
  732. :group 'org-archive
  733. :type '(choice
  734. (const :tag "No" nil)
  735. (const :tag "Yes" t)
  736. (string :tag "Use this keyword")))
  737. (defcustom org-archive-stamp-time t
  738. "Non-nil means, add a time stamp to entries moved to an archive file.
  739. This variable is obsolete and has no effect anymore, instead add ot remove
  740. `time' from the variablle `org-archive-save-context-info'."
  741. :group 'org-archive
  742. :type 'boolean)
  743. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  744. "Parts of context info that should be stored as properties when archiving.
  745. When a subtree is moved to an archive file, it looses information given by
  746. context, like inherited tags, the category, and possibly also the TODO
  747. state (depending on the variable `org-archive-mark-done').
  748. This variable can be a list of any of the following symbols:
  749. time The time of archiving.
  750. file The file where the entry originates.
  751. itags The local tags, in the headline of the subtree.
  752. ltags The tags the subtree inherits from further up the hierarchy.
  753. todo The pre-archive TODO state.
  754. category The category, taken from file name or #+CATEGORY lines.
  755. olpath The outline path to the item. These are all headlines above
  756. the current item, separated by /, like a file path.
  757. For each symbol present in the list, a property will be created in
  758. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  759. information."
  760. :group 'org-archive
  761. :type '(set :greedy t
  762. (const :tag "Time" time)
  763. (const :tag "File" file)
  764. (const :tag "Category" category)
  765. (const :tag "TODO state" todo)
  766. (const :tag "TODO state" priority)
  767. (const :tag "Inherited tags" itags)
  768. (const :tag "Outline path" olpath)
  769. (const :tag "Local tags" ltags)))
  770. (defgroup org-imenu-and-speedbar nil
  771. "Options concerning imenu and speedbar in Org-mode."
  772. :tag "Org Imenu and Speedbar"
  773. :group 'org-structure)
  774. (defcustom org-imenu-depth 2
  775. "The maximum level for Imenu access to Org-mode headlines.
  776. This also applied for speedbar access."
  777. :group 'org-imenu-and-speedbar
  778. :type 'number)
  779. (defgroup org-table nil
  780. "Options concerning tables in Org-mode."
  781. :tag "Org Table"
  782. :group 'org)
  783. (defcustom org-enable-table-editor 'optimized
  784. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  785. When nil, such lines will be treated like ordinary lines.
  786. When equal to the symbol `optimized', the table editor will be optimized to
  787. do the following:
  788. - Automatic overwrite mode in front of whitespace in table fields.
  789. This makes the structure of the table stay in tact as long as the edited
  790. field does not exceed the column width.
  791. - Minimize the number of realigns. Normally, the table is aligned each time
  792. TAB or RET are pressed to move to another field. With optimization this
  793. happens only if changes to a field might have changed the column width.
  794. Optimization requires replacing the functions `self-insert-command',
  795. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  796. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  797. very good at guessing when a re-align will be necessary, but you can always
  798. force one with \\[org-ctrl-c-ctrl-c].
  799. If you would like to use the optimized version in Org-mode, but the
  800. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  801. This variable can be used to turn on and off the table editor during a session,
  802. but in order to toggle optimization, a restart is required.
  803. See also the variable `org-table-auto-blank-field'."
  804. :group 'org-table
  805. :type '(choice
  806. (const :tag "off" nil)
  807. (const :tag "on" t)
  808. (const :tag "on, optimized" optimized)))
  809. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  810. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  811. In the optimized version, the table editor takes over all simple keys that
  812. normally just insert a character. In tables, the characters are inserted
  813. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  814. for empty fields). Outside tables, the correct binding of the keys is
  815. restored.
  816. The default for this option is t if the optimized version is also used in
  817. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  818. this variable requires a restart of Emacs to become effective."
  819. :group 'org-table
  820. :type 'boolean)
  821. (defcustom orgtbl-radio-table-templates
  822. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  823. % END RECEIVE ORGTBL %n
  824. \\begin{comment}
  825. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  826. | | |
  827. \\end{comment}\n")
  828. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  829. @c END RECEIVE ORGTBL %n
  830. @ignore
  831. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  832. | | |
  833. @end ignore\n")
  834. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  835. <!-- END RECEIVE ORGTBL %n -->
  836. <!--
  837. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  838. | | |
  839. -->\n"))
  840. "Templates for radio tables in different major modes.
  841. All occurrences of %n in a template will be replaced with the name of the
  842. table, obtained by prompting the user."
  843. :group 'org-table
  844. :type '(repeat
  845. (list (symbol :tag "Major mode")
  846. (string :tag "Format"))))
  847. (defgroup org-table-settings nil
  848. "Settings for tables in Org-mode."
  849. :tag "Org Table Settings"
  850. :group 'org-table)
  851. (defcustom org-table-default-size "5x2"
  852. "The default size for newly created tables, Columns x Rows."
  853. :group 'org-table-settings
  854. :type 'string)
  855. (defcustom org-table-number-regexp
  856. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  857. "Regular expression for recognizing numbers in table columns.
  858. If a table column contains mostly numbers, it will be aligned to the
  859. right. If not, it will be aligned to the left.
  860. The default value of this option is a regular expression which allows
  861. anything which looks remotely like a number as used in scientific
  862. context. For example, all of the following will be considered a
  863. number:
  864. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  865. Other options offered by the customize interface are more restrictive."
  866. :group 'org-table-settings
  867. :type '(choice
  868. (const :tag "Positive Integers"
  869. "^[0-9]+$")
  870. (const :tag "Integers"
  871. "^[-+]?[0-9]+$")
  872. (const :tag "Floating Point Numbers"
  873. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  874. (const :tag "Floating Point Number or Integer"
  875. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  876. (const :tag "Exponential, Floating point, Integer"
  877. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  878. (const :tag "Very General Number-Like, including hex"
  879. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  880. (string :tag "Regexp:")))
  881. (defcustom org-table-number-fraction 0.5
  882. "Fraction of numbers in a column required to make the column align right.
  883. In a column all non-white fields are considered. If at least this
  884. fraction of fields is matched by `org-table-number-fraction',
  885. alignment to the right border applies."
  886. :group 'org-table-settings
  887. :type 'number)
  888. (defgroup org-table-editing nil
  889. "Behavior of tables during editing in Org-mode."
  890. :tag "Org Table Editing"
  891. :group 'org-table)
  892. (defcustom org-table-automatic-realign t
  893. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  894. When nil, aligning is only done with \\[org-table-align], or after column
  895. removal/insertion."
  896. :group 'org-table-editing
  897. :type 'boolean)
  898. (defcustom org-table-auto-blank-field t
  899. "Non-nil means, automatically blank table field when starting to type into it.
  900. This only happens when typing immediately after a field motion
  901. command (TAB, S-TAB or RET).
  902. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  903. :group 'org-table-editing
  904. :type 'boolean)
  905. (defcustom org-table-tab-jumps-over-hlines t
  906. "Non-nil means, tab in the last column of a table with jump over a hline.
  907. If a horizontal separator line is following the current line,
  908. `org-table-next-field' can either create a new row before that line, or jump
  909. over the line. When this option is nil, a new line will be created before
  910. this line."
  911. :group 'org-table-editing
  912. :type 'boolean)
  913. (defcustom org-table-tab-recognizes-table.el t
  914. "Non-nil means, TAB will automatically notice a table.el table.
  915. When it sees such a table, it moves point into it and - if necessary -
  916. calls `table-recognize-table'."
  917. :group 'org-table-editing
  918. :type 'boolean)
  919. (defgroup org-table-calculation nil
  920. "Options concerning tables in Org-mode."
  921. :tag "Org Table Calculation"
  922. :group 'org-table)
  923. (defcustom org-table-use-standard-references t
  924. "Should org-mode work with table refrences like B3 instead of @3$2?
  925. Possible values are:
  926. nil never use them
  927. from accept as input, do not present for editing
  928. t: accept as input and present for editing"
  929. :group 'org-table-calculation
  930. :type '(choice
  931. (const :tag "Never, don't even check unser input for them" nil)
  932. (const :tag "Always, both as user input, and when editing" t)
  933. (const :tag "Convert user input, don't offer during editing" 'from)))
  934. (defcustom org-table-copy-increment t
  935. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  936. :group 'org-table-calculation
  937. :type 'boolean)
  938. (defcustom org-calc-default-modes
  939. '(calc-internal-prec 12
  940. calc-float-format (float 5)
  941. calc-angle-mode deg
  942. calc-prefer-frac nil
  943. calc-symbolic-mode nil
  944. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  945. calc-display-working-message t
  946. )
  947. "List with Calc mode settings for use in calc-eval for table formulas.
  948. The list must contain alternating symbols (Calc modes variables and values).
  949. Don't remove any of the default settings, just change the values. Org-mode
  950. relies on the variables to be present in the list."
  951. :group 'org-table-calculation
  952. :type 'plist)
  953. (defcustom org-table-formula-evaluate-inline t
  954. "Non-nil means, TAB and RET evaluate a formula in current table field.
  955. If the current field starts with an equal sign, it is assumed to be a formula
  956. which should be evaluated as described in the manual and in the documentation
  957. string of the command `org-table-eval-formula'. This feature requires the
  958. Emacs calc package.
  959. When this variable is nil, formula calculation is only available through
  960. the command \\[org-table-eval-formula]."
  961. :group 'org-table-calculation
  962. :type 'boolean)
  963. (defcustom org-table-formula-use-constants t
  964. "Non-nil means, interpret constants in formulas in tables.
  965. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  966. by the value given in `org-table-formula-constants', or by a value obtained
  967. from the `constants.el' package."
  968. :group 'org-table-calculation
  969. :type 'boolean)
  970. (defcustom org-table-formula-constants nil
  971. "Alist with constant names and values, for use in table formulas.
  972. The car of each element is a name of a constant, without the `$' before it.
  973. The cdr is the value as a string. For example, if you'd like to use the
  974. speed of light in a formula, you would configure
  975. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  976. and then use it in an equation like `$1*$c'.
  977. Constants can also be defined on a per-file basis using a line like
  978. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  979. :group 'org-table-calculation
  980. :type '(repeat
  981. (cons (string :tag "name")
  982. (string :tag "value"))))
  983. (defvar org-table-formula-constants-local nil
  984. "Local version of `org-table-formula-constants'.")
  985. (make-variable-buffer-local 'org-table-formula-constants-local)
  986. (defcustom org-table-allow-automatic-line-recalculation t
  987. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  988. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  989. :group 'org-table-calculation
  990. :type 'boolean)
  991. (defgroup org-link nil
  992. "Options concerning links in Org-mode."
  993. :tag "Org Link"
  994. :group 'org)
  995. (defvar org-link-abbrev-alist-local nil
  996. "Buffer-local version of `org-link-abbrev-alist', which see.
  997. The value of this is taken from the #+LINK lines.")
  998. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  999. (defcustom org-link-abbrev-alist nil
  1000. "Alist of link abbreviations.
  1001. The car of each element is a string, to be replaced at the start of a link.
  1002. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1003. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1004. [[linkkey:tag][description]]
  1005. If REPLACE is a string, the tag will simply be appended to create the link.
  1006. If the string contains \"%s\", the tag will be inserted there.
  1007. REPLACE may also be a function that will be called with the tag as the
  1008. only argument to create the link, which should be returned as a string.
  1009. See the manual for examples."
  1010. :group 'org-link
  1011. :type 'alist)
  1012. (defcustom org-descriptive-links t
  1013. "Non-nil means, hide link part and only show description of bracket links.
  1014. Bracket links are like [[link][descritpion]]. This variable sets the initial
  1015. state in new org-mode buffers. The setting can then be toggled on a
  1016. per-buffer basis from the Org->Hyperlinks menu."
  1017. :group 'org-link
  1018. :type 'boolean)
  1019. (defcustom org-link-file-path-type 'adaptive
  1020. "How the path name in file links should be stored.
  1021. Valid values are:
  1022. relative Relative to the current directory, i.e. the directory of the file
  1023. into which the link is being inserted.
  1024. absolute Absolute path, if possible with ~ for home directory.
  1025. noabbrev Absolute path, no abbreviation of home directory.
  1026. adaptive Use relative path for files in the current directory and sub-
  1027. directories of it. For other files, use an absolute path."
  1028. :group 'org-link
  1029. :type '(choice
  1030. (const relative)
  1031. (const absolute)
  1032. (const noabbrev)
  1033. (const adaptive)))
  1034. (defcustom org-activate-links '(bracket angle plain radio tag date)
  1035. "Types of links that should be activated in Org-mode files.
  1036. This is a list of symbols, each leading to the activation of a certain link
  1037. type. In principle, it does not hurt to turn on most link types - there may
  1038. be a small gain when turning off unused link types. The types are:
  1039. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1040. angular Links in angular brackes that may contain whitespace like
  1041. <bbdb:Carsten Dominik>.
  1042. plain Plain links in normal text, no whitespace, like http://google.com.
  1043. radio Text that is matched by a radio target, see manual for details.
  1044. tag Tag settings in a headline (link to tag search).
  1045. date Time stamps (link to calendar).
  1046. Changing this variable requires a restart of Emacs to become effective."
  1047. :group 'org-link
  1048. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1049. (const :tag "Angular bracket links (old style)" angular)
  1050. (const :tag "Plain text links" plain)
  1051. (const :tag "Radio target matches" radio)
  1052. (const :tag "Tags" tag)
  1053. (const :tag "Timestamps" date)))
  1054. (defgroup org-link-store nil
  1055. "Options concerning storing links in Org-mode"
  1056. :tag "Org Store Link"
  1057. :group 'org-link)
  1058. (defcustom org-email-link-description-format "Email %c: %.30s"
  1059. "Format of the description part of a link to an email or usenet message.
  1060. The following %-excapes will be replaced by corresponding information:
  1061. %F full \"From\" field
  1062. %f name, taken from \"From\" field, address if no name
  1063. %T full \"To\" field
  1064. %t first name in \"To\" field, address if no name
  1065. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1066. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1067. %s subject
  1068. %m message-id.
  1069. You may use normal field width specification between the % and the letter.
  1070. This is for example useful to limit the length of the subject.
  1071. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1072. :group 'org-link-store
  1073. :type 'string)
  1074. (defcustom org-from-is-user-regexp
  1075. (let (r1 r2)
  1076. (when (and user-mail-address (not (string= user-mail-address "")))
  1077. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1078. (when (and user-full-name (not (string= user-full-name "")))
  1079. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1080. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1081. "Regexp mached against the \"From:\" header of an email or usenet message.
  1082. It should match if the message is from the user him/herself."
  1083. :group 'org-link-store
  1084. :type 'regexp)
  1085. (defcustom org-context-in-file-links t
  1086. "Non-nil means, file links from `org-store-link' contain context.
  1087. A search string will be added to the file name with :: as separator and
  1088. used to find the context when the link is activated by the command
  1089. `org-open-at-point'.
  1090. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1091. negates this setting for the duration of the command."
  1092. :group 'org-link-store
  1093. :type 'boolean)
  1094. (defcustom org-keep-stored-link-after-insertion nil
  1095. "Non-nil means, keep link in list for entire session.
  1096. The command `org-store-link' adds a link pointing to the current
  1097. location to an internal list. These links accumulate during a session.
  1098. The command `org-insert-link' can be used to insert links into any
  1099. Org-mode file (offering completion for all stored links). When this
  1100. option is nil, every link which has been inserted once using \\[org-insert-link]
  1101. will be removed from the list, to make completing the unused links
  1102. more efficient."
  1103. :group 'org-link-store
  1104. :type 'boolean)
  1105. (defgroup org-link-follow nil
  1106. "Options concerning following links in Org-mode"
  1107. :tag "Org Follow Link"
  1108. :group 'org-link)
  1109. (defcustom org-follow-link-hook nil
  1110. "Hook that is run after a link has been followed."
  1111. :group 'org-link-follow
  1112. :type 'hook)
  1113. (defcustom org-tab-follows-link nil
  1114. "Non-nil means, on links TAB will follow the link.
  1115. Needs to be set before org.el is loaded."
  1116. :group 'org-link-follow
  1117. :type 'boolean)
  1118. (defcustom org-return-follows-link nil
  1119. "Non-nil means, on links RET will follow the link.
  1120. Needs to be set before org.el is loaded."
  1121. :group 'org-link-follow
  1122. :type 'boolean)
  1123. (defcustom org-mouse-1-follows-link
  1124. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1125. "Non-nil means, mouse-1 on a link will follow the link.
  1126. A longer mouse click will still set point. Does not work on XEmacs.
  1127. Needs to be set before org.el is loaded."
  1128. :group 'org-link-follow
  1129. :type 'boolean)
  1130. (defcustom org-mark-ring-length 4
  1131. "Number of different positions to be recorded in the ring
  1132. Changing this requires a restart of Emacs to work correctly."
  1133. :group 'org-link-follow
  1134. :type 'interger)
  1135. (defcustom org-link-frame-setup
  1136. '((vm . vm-visit-folder-other-frame)
  1137. (gnus . gnus-other-frame)
  1138. (file . find-file-other-window))
  1139. "Setup the frame configuration for following links.
  1140. When following a link with Emacs, it may often be useful to display
  1141. this link in another window or frame. This variable can be used to
  1142. set this up for the different types of links.
  1143. For VM, use any of
  1144. `vm-visit-folder'
  1145. `vm-visit-folder-other-frame'
  1146. For Gnus, use any of
  1147. `gnus'
  1148. `gnus-other-frame'
  1149. For FILE, use any of
  1150. `find-file'
  1151. `find-file-other-window'
  1152. `find-file-other-frame'
  1153. For the calendar, use the variable `calendar-setup'.
  1154. For BBDB, it is currently only possible to display the matches in
  1155. another window."
  1156. :group 'org-link-follow
  1157. :type '(list
  1158. (cons (const vm)
  1159. (choice
  1160. (const vm-visit-folder)
  1161. (const vm-visit-folder-other-window)
  1162. (const vm-visit-folder-other-frame)))
  1163. (cons (const gnus)
  1164. (choice
  1165. (const gnus)
  1166. (const gnus-other-frame)))
  1167. (cons (const file)
  1168. (choice
  1169. (const find-file)
  1170. (const find-file-other-window)
  1171. (const find-file-other-frame)))))
  1172. (defcustom org-display-internal-link-with-indirect-buffer nil
  1173. "Non-nil means, use indirect buffer to display infile links.
  1174. Activating internal links (from one location in a file to another location
  1175. in the same file) normally just jumps to the location. When the link is
  1176. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1177. another window. When this option is set, the other window actually displays
  1178. an indirect buffer clone of the current buffer, to avoid any visibility
  1179. changes to the current buffer."
  1180. :group 'org-link-follow
  1181. :type 'boolean)
  1182. (defcustom org-open-non-existing-files nil
  1183. "Non-nil means, `org-open-file' will open non-existing files.
  1184. When nil, an error will be generated."
  1185. :group 'org-link-follow
  1186. :type 'boolean)
  1187. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1188. "Function and arguments to call for following mailto links.
  1189. This is a list with the first element being a lisp function, and the
  1190. remaining elements being arguments to the function. In string arguments,
  1191. %a will be replaced by the address, and %s will be replaced by the subject
  1192. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1193. :group 'org-link-follow
  1194. :type '(choice
  1195. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1196. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1197. (const :tag "message-mail" (message-mail "%a" "%s"))
  1198. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1199. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1200. "Non-nil means, ask for confirmation before executing shell links.
  1201. Shell links can be dangerous: just think about a link
  1202. [[shell:rm -rf ~/*][Google Search]]
  1203. This link would show up in your Org-mode document as \"Google Search\",
  1204. but really it would remove your entire home directory.
  1205. Therefore we advise against setting this variable to nil.
  1206. Just change it to `y-or-n-p' of you want to confirm with a
  1207. single keystroke rather than having to type \"yes\"."
  1208. :group 'org-link-follow
  1209. :type '(choice
  1210. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1211. (const :tag "with y-or-n (faster)" y-or-n-p)
  1212. (const :tag "no confirmation (dangerous)" nil)))
  1213. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1214. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1215. Elisp links can be dangerous: just think about a link
  1216. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1217. This link would show up in your Org-mode document as \"Google Search\",
  1218. but really it would remove your entire home directory.
  1219. Therefore we advise against setting this variable to nil.
  1220. Just change it to `y-or-n-p' of you want to confirm with a
  1221. single keystroke rather than having to type \"yes\"."
  1222. :group 'org-link-follow
  1223. :type '(choice
  1224. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1225. (const :tag "with y-or-n (faster)" y-or-n-p)
  1226. (const :tag "no confirmation (dangerous)" nil)))
  1227. (defconst org-file-apps-defaults-gnu
  1228. '((remote . emacs)
  1229. (t . mailcap))
  1230. "Default file applications on a UNIX or GNU/Linux system.
  1231. See `org-file-apps'.")
  1232. (defconst org-file-apps-defaults-macosx
  1233. '((remote . emacs)
  1234. (t . "open %s")
  1235. ("ps" . "gv %s")
  1236. ("ps.gz" . "gv %s")
  1237. ("eps" . "gv %s")
  1238. ("eps.gz" . "gv %s")
  1239. ("dvi" . "xdvi %s")
  1240. ("fig" . "xfig %s"))
  1241. "Default file applications on a MacOS X system.
  1242. The system \"open\" is known as a default, but we use X11 applications
  1243. for some files for which the OS does not have a good default.
  1244. See `org-file-apps'.")
  1245. (defconst org-file-apps-defaults-windowsnt
  1246. (list
  1247. '(remote . emacs)
  1248. (cons t
  1249. (list (if (featurep 'xemacs)
  1250. 'mswindows-shell-execute
  1251. 'w32-shell-execute)
  1252. "open" 'file)))
  1253. "Default file applications on a Windows NT system.
  1254. The system \"open\" is used for most files.
  1255. See `org-file-apps'.")
  1256. (defcustom org-file-apps
  1257. '(
  1258. ("txt" . emacs)
  1259. ("tex" . emacs)
  1260. ("ltx" . emacs)
  1261. ("org" . emacs)
  1262. ("el" . emacs)
  1263. ("bib" . emacs)
  1264. )
  1265. "External applications for opening `file:path' items in a document.
  1266. Org-mode uses system defaults for different file types, but
  1267. you can use this variable to set the application for a given file
  1268. extension. The entries in this list are cons cells where the car identifies
  1269. files and the cdr the corresponding command. Possible values for the
  1270. file identifier are
  1271. \"ext\" A string identifying an extension
  1272. `directory' Matches a directory
  1273. `remote' Matches a remote file, accessible through tramp or efs.
  1274. Remote files most likely should be visited through Emacs
  1275. because external applications cannot handle such paths.
  1276. t Default for all remaining files
  1277. Possible values for the command are:
  1278. `emacs' The file will be visited by the current Emacs process.
  1279. `default' Use the default application for this file type.
  1280. string A command to be executed by a shell; %s will be replaced
  1281. by the path to the file.
  1282. sexp A Lisp form which will be evaluated. The file path will
  1283. be available in the Lisp variable `file'.
  1284. For more examples, see the system specific constants
  1285. `org-file-apps-defaults-macosx'
  1286. `org-file-apps-defaults-windowsnt'
  1287. `org-file-apps-defaults-gnu'."
  1288. :group 'org-link-follow
  1289. :type '(repeat
  1290. (cons (choice :value ""
  1291. (string :tag "Extension")
  1292. (const :tag "Default for unrecognized files" t)
  1293. (const :tag "Remote file" remote)
  1294. (const :tag "Links to a directory" directory))
  1295. (choice :value ""
  1296. (const :tag "Visit with Emacs" emacs)
  1297. (const :tag "Use system default" default)
  1298. (string :tag "Command")
  1299. (sexp :tag "Lisp form")))))
  1300. (defgroup org-remember nil
  1301. "Options concerning interaction with remember.el."
  1302. :tag "Org Remember"
  1303. :group 'org)
  1304. (defcustom org-directory "~/org"
  1305. "Directory with org files.
  1306. This directory will be used as default to prompt for org files.
  1307. Used by the hooks for remember.el."
  1308. :group 'org-remember
  1309. :type 'directory)
  1310. (defcustom org-default-notes-file "~/.notes"
  1311. "Default target for storing notes.
  1312. Used by the hooks for remember.el. This can be a string, or nil to mean
  1313. the value of `remember-data-file'.
  1314. You can set this on a per-template basis with the variable
  1315. `org-remember-templates'."
  1316. :group 'org-remember
  1317. :type '(choice
  1318. (const :tag "Default from remember-data-file" nil)
  1319. file))
  1320. (defcustom org-remember-store-without-prompt t
  1321. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1322. In this case, you need `C-u C-c C-c' to get the prompts for
  1323. note file and headline.
  1324. When this variable is nil, `C-c C-c' give you the prompts, and
  1325. `C-u C-c C-c' trigger the fasttrack."
  1326. :group 'org-remember
  1327. :type 'boolean)
  1328. (defcustom org-remember-interactive-interface 'refile
  1329. "The interface to be used for interactive filing of remember notes.
  1330. This is only used when the interactive mode for selecting a filing
  1331. location is used (see the variable `org-remember-store-without-prompt').
  1332. Allowed vaues are:
  1333. outline The interface shows an outline of the relevant file
  1334. and the correct heading is found by moving through
  1335. the outline or by searching with incremental search.
  1336. outline-path-completion Headlines in the current buffer are offered via
  1337. completion.
  1338. refile Use the refile interface, and offer headlines,
  1339. possibly from different buffers."
  1340. :group 'org-remember
  1341. :type '(choice
  1342. (const :tag "Refile" refile)
  1343. (const :tag "Outline" outline)
  1344. (const :tag "Outline-path-completion" outline-path-completion)))
  1345. (defcustom org-goto-interface 'outline
  1346. "The default interface to be used for `org-goto'.
  1347. Allowed vaues are:
  1348. outline The interface shows an outline of the relevant file
  1349. and the correct heading is found by moving through
  1350. the outline or by searching with incremental search.
  1351. outline-path-completion Headlines in the current buffer are offered via
  1352. completion."
  1353. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1354. :type '(choice
  1355. (const :tag "Outline" outline)
  1356. (const :tag "Outline-path-completion" outline-path-completion)))
  1357. (defcustom org-remember-default-headline ""
  1358. "The headline that should be the default location in the notes file.
  1359. When filing remember notes, the cursor will start at that position.
  1360. You can set this on a per-template basis with the variable
  1361. `org-remember-templates'."
  1362. :group 'org-remember
  1363. :type 'string)
  1364. (defcustom org-remember-templates nil
  1365. "Templates for the creation of remember buffers.
  1366. When nil, just let remember make the buffer.
  1367. When not nil, this is a list of 5-element lists. In each entry, the first
  1368. element is the name of the template, which should be a single short word.
  1369. The second element is a character, a unique key to select this template.
  1370. The third element is the template. The fourth element is optional and can
  1371. specify a destination file for remember items created with this template.
  1372. The default file is given by `org-default-notes-file'. An optional fifth
  1373. element can specify the headline in that file that should be offered
  1374. first when the user is asked to file the entry. The default headline is
  1375. given in the variable `org-remember-default-headline'.
  1376. An optional sixth element specifies the contexts in which the user can
  1377. select the template. This element can be either a list of major modes
  1378. or a function. `org-remember' will first check whether the function
  1379. returns `t' or if we are in any of the listed major mode, and select
  1380. the template accordingly.
  1381. The template specifies the structure of the remember buffer. It should have
  1382. a first line starting with a star, to act as the org-mode headline.
  1383. Furthermore, the following %-escapes will be replaced with content:
  1384. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1385. A default value and a completion table ca be specified like this:
  1386. %^{prompt|default|completion2|completion3|...}
  1387. %t time stamp, date only
  1388. %T time stamp with date and time
  1389. %u, %U like the above, but inactive time stamps
  1390. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1391. You may define a prompt like %^{Please specify birthday}t
  1392. %n user name (taken from `user-full-name')
  1393. %a annotation, normally the link created with org-store-link
  1394. %i initial content, the region active. If %i is indented,
  1395. the entire inserted text will be indented as well.
  1396. %c content of the clipboard, or current kill ring head
  1397. %^g prompt for tags, with completion on tags in target file
  1398. %^G prompt for tags, with completion all tags in all agenda files
  1399. %:keyword specific information for certain link types, see below
  1400. %[pathname] insert the contents of the file given by `pathname'
  1401. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1402. %! Store this note immediately after filling the template
  1403. %? After completing the template, position cursor here.
  1404. Apart from these general escapes, you can access information specific to the
  1405. link type that is created. For example, calling `remember' in emails or gnus
  1406. will record the author and the subject of the message, which you can access
  1407. with %:author and %:subject, respectively. Here is a complete list of what
  1408. is recorded for each link type.
  1409. Link type | Available information
  1410. -------------------+------------------------------------------------------
  1411. bbdb | %:type %:name %:company
  1412. vm, wl, mh, rmail | %:type %:subject %:message-id
  1413. | %:from %:fromname %:fromaddress
  1414. | %:to %:toname %:toaddress
  1415. | %:fromto (either \"to NAME\" or \"from NAME\")
  1416. gnus | %:group, for messages also all email fields
  1417. w3, w3m | %:type %:url
  1418. info | %:type %:file %:node
  1419. calendar | %:type %:date"
  1420. :group 'org-remember
  1421. :get (lambda (var) ; Make sure all entries have at least 5 elements
  1422. (mapcar (lambda (x)
  1423. (if (not (stringp (car x))) (setq x (cons "" x)))
  1424. (cond ((= (length x) 4) (append x '("")))
  1425. ((= (length x) 3) (append x '("" "")))
  1426. (t x)))
  1427. (default-value var)))
  1428. :type '(repeat
  1429. :tag "enabled"
  1430. (list :value ("" ?a "\n" nil nil nil)
  1431. (string :tag "Name")
  1432. (character :tag "Selection Key")
  1433. (string :tag "Template")
  1434. (choice
  1435. (file :tag "Destination file")
  1436. (const :tag "Prompt for file" nil))
  1437. (choice
  1438. (string :tag "Destination headline")
  1439. (const :tag "Selection interface for heading"))
  1440. (choice
  1441. (const :tag "Use by default" nil)
  1442. (const :tag "Use in all contexts" t)
  1443. (repeat :tag "Use only if in major mode"
  1444. (symbol :tag "Major mode"))
  1445. (function :tag "Perform a check against function")))))
  1446. (defcustom org-reverse-note-order nil
  1447. "Non-nil means, store new notes at the beginning of a file or entry.
  1448. When nil, new notes will be filed to the end of a file or entry.
  1449. This can also be a list with cons cells of regular expressions that
  1450. are matched against file names, and values."
  1451. :group 'org-remember
  1452. :type '(choice
  1453. (const :tag "Reverse always" t)
  1454. (const :tag "Reverse never" nil)
  1455. (repeat :tag "By file name regexp"
  1456. (cons regexp boolean))))
  1457. (defcustom org-refile-targets nil
  1458. "Targets for refiling entries with \\[org-refile].
  1459. This is list of cons cells. Each cell contains:
  1460. - a specification of the files to be considered, either a list of files,
  1461. or a symbol whose function or value fields will be used to retrieve
  1462. a file name or a list of file names. Nil means, refile to a different
  1463. heading in the current buffer.
  1464. - A specification of how to find candidate refile targets. This may be
  1465. any of
  1466. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1467. This tag has to be present in all target headlines, inheritance will
  1468. not be considered.
  1469. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1470. todo keyword.
  1471. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1472. headlines that are refiling targets.
  1473. - a cons cell (:level . N). Any headline of level N is considered a target.
  1474. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1475. ;; FIXME: what if there are a var and func with same name???
  1476. :group 'org-remember
  1477. :type '(repeat
  1478. (cons
  1479. (choice :value org-agenda-files
  1480. (const :tag "All agenda files" org-agenda-files)
  1481. (const :tag "Current buffer" nil)
  1482. (function) (variable) (file))
  1483. (choice :tag "Identify target headline by"
  1484. (cons :tag "Specific tag" (const :tag) (string))
  1485. (cons :tag "TODO keyword" (const :todo) (string))
  1486. (cons :tag "Regular expression" (const :regexp) (regexp))
  1487. (cons :tag "Level number" (const :level) (integer))
  1488. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1489. (defcustom org-refile-use-outline-path nil
  1490. "Non-nil means, provide refile targets as paths.
  1491. So a level 3 headline will be available as level1/level2/level3.
  1492. When the value is `file', also include the file name (without directory)
  1493. into the path. When `full-file-path', include the full file path."
  1494. :group 'org-remember
  1495. :type '(choice
  1496. (const :tag "Not" nil)
  1497. (const :tag "Yes" t)
  1498. (const :tag "Start with file name" file)
  1499. (const :tag "Start with full file path" full-file-path)))
  1500. (defgroup org-todo nil
  1501. "Options concerning TODO items in Org-mode."
  1502. :tag "Org TODO"
  1503. :group 'org)
  1504. (defgroup org-progress nil
  1505. "Options concerning Progress logging in Org-mode."
  1506. :tag "Org Progress"
  1507. :group 'org-time)
  1508. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1509. "List of TODO entry keyword sequences and their interpretation.
  1510. \\<org-mode-map>This is a list of sequences.
  1511. Each sequence starts with a symbol, either `sequence' or `type',
  1512. indicating if the keywords should be interpreted as a sequence of
  1513. action steps, or as different types of TODO items. The first
  1514. keywords are states requiring action - these states will select a headline
  1515. for inclusion into the global TODO list Org-mode produces. If one of
  1516. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1517. signify that no further action is necessary. If \"|\" is not found,
  1518. the last keyword is treated as the only DONE state of the sequence.
  1519. The command \\[org-todo] cycles an entry through these states, and one
  1520. additional state where no keyword is present. For details about this
  1521. cycling, see the manual.
  1522. TODO keywords and interpretation can also be set on a per-file basis with
  1523. the special #+SEQ_TODO and #+TYP_TODO lines.
  1524. Each keyword can optionally specify a character for fast state selection
  1525. \(in combination with the variable `org-use-fast-todo-selection')
  1526. and specifiers for state change logging, using the same syntax
  1527. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1528. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1529. indicates to record a time stamp each time this state is selected.
  1530. Each keyword may also specify if a timestamp or a note should be
  1531. recorded when entering or leaving the state, by adding additional
  1532. characters in the parenthesis after the keyword. This looks like this:
  1533. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1534. record only the time of the state change. With X and Y being either
  1535. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1536. Y when leaving the state if and only if the *target* state does not
  1537. define X. You may omit any of the fast-selection key or X or /Y,
  1538. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1539. For backward compatibility, this variable may also be just a list
  1540. of keywords - in this case the interptetation (sequence or type) will be
  1541. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1542. :group 'org-todo
  1543. :group 'org-keywords
  1544. :type '(choice
  1545. (repeat :tag "Old syntax, just keywords"
  1546. (string :tag "Keyword"))
  1547. (repeat :tag "New syntax"
  1548. (cons
  1549. (choice
  1550. :tag "Interpretation"
  1551. (const :tag "Sequence (cycling hits every state)" sequence)
  1552. (const :tag "Type (cycling directly to DONE)" type))
  1553. (repeat
  1554. (string :tag "Keyword"))))))
  1555. (defvar org-todo-keywords-1 nil
  1556. "All TODO and DONE keywords active in a buffer.")
  1557. (make-variable-buffer-local 'org-todo-keywords-1)
  1558. (defvar org-todo-keywords-for-agenda nil)
  1559. (defvar org-done-keywords-for-agenda nil)
  1560. (defvar org-not-done-keywords nil)
  1561. (make-variable-buffer-local 'org-not-done-keywords)
  1562. (defvar org-done-keywords nil)
  1563. (make-variable-buffer-local 'org-done-keywords)
  1564. (defvar org-todo-heads nil)
  1565. (make-variable-buffer-local 'org-todo-heads)
  1566. (defvar org-todo-sets nil)
  1567. (make-variable-buffer-local 'org-todo-sets)
  1568. (defvar org-todo-log-states nil)
  1569. (make-variable-buffer-local 'org-todo-log-states)
  1570. (defvar org-todo-kwd-alist nil)
  1571. (make-variable-buffer-local 'org-todo-kwd-alist)
  1572. (defvar org-todo-key-alist nil)
  1573. (make-variable-buffer-local 'org-todo-key-alist)
  1574. (defvar org-todo-key-trigger nil)
  1575. (make-variable-buffer-local 'org-todo-key-trigger)
  1576. (defcustom org-todo-interpretation 'sequence
  1577. "Controls how TODO keywords are interpreted.
  1578. This variable is in principle obsolete and is only used for
  1579. backward compatibility, if the interpretation of todo keywords is
  1580. not given already in `org-todo-keywords'. See that variable for
  1581. more information."
  1582. :group 'org-todo
  1583. :group 'org-keywords
  1584. :type '(choice (const sequence)
  1585. (const type)))
  1586. (defcustom org-use-fast-todo-selection 'prefix
  1587. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1588. This variable describes if and under what circumstances the cycling
  1589. mechanism for TODO keywords will be replaced by a single-key, direct
  1590. selection scheme.
  1591. When nil, fast selection is never used.
  1592. When the symbol `prefix', it will be used when `org-todo' is called with
  1593. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1594. in an agenda buffer.
  1595. When t, fast selection is used by default. In this case, the prefix
  1596. argument forces cycling instead.
  1597. In all cases, the special interface is only used if access keys have actually
  1598. been assigned by the user, i.e. if keywords in the configuration are followed
  1599. by a letter in parenthesis, like TODO(t)."
  1600. :group 'org-todo
  1601. :type '(choice
  1602. (const :tag "Never" nil)
  1603. (const :tag "By default" t)
  1604. (const :tag "Only with C-u C-c C-t" prefix)))
  1605. (defcustom org-after-todo-state-change-hook nil
  1606. "Hook which is run after the state of a TODO item was changed.
  1607. The new state (a string with a TODO keyword, or nil) is available in the
  1608. Lisp variable `state'."
  1609. :group 'org-todo
  1610. :type 'hook)
  1611. (defcustom org-log-done nil
  1612. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1613. When equal to the list (done), also prompt for a closing note.
  1614. This can also be configured on a per-file basis by adding one of
  1615. the following lines anywhere in the buffer:
  1616. #+STARTUP: logdone
  1617. #+STARTUP: lognotedone
  1618. #+STARTUP: nologdone"
  1619. :group 'org-todo
  1620. :group 'org-progress
  1621. :type '(choice
  1622. (const :tag "No logging" nil)
  1623. (const :tag "Record CLOSED timestamp" time)
  1624. (const :tag "Record CLOSED timestamp with closing note." note)))
  1625. ;; Normalize old uses of org-log-done.
  1626. (cond
  1627. ((eq org-log-done t) (setq org-log-done 'time))
  1628. ((and (listp org-log-done) (memq 'done org-log-done))
  1629. (setq org-log-done 'note)))
  1630. ;; FIXME: document
  1631. (defcustom org-log-note-clock-out nil
  1632. "Non-nil means, recored a note when clocking out of an item.
  1633. This can also be configured on a per-file basis by adding one of
  1634. the following lines anywhere in the buffer:
  1635. #+STARTUP: lognoteclock-out
  1636. #+STARTUP: nolognoteclock-out"
  1637. :group 'org-todo
  1638. :group 'org-progress
  1639. :type 'boolean)
  1640. (defcustom org-log-done-with-time t
  1641. "Non-nil means, the CLOSED time stamp will contain date and time.
  1642. When nil, only the date will be recorded."
  1643. :group 'org-progress
  1644. :type 'boolean)
  1645. (defcustom org-log-note-headings
  1646. '((done . "CLOSING NOTE %t")
  1647. (state . "State %-12s %t")
  1648. (clock-out . ""))
  1649. "Headings for notes added when clocking out or closing TODO items.
  1650. The value is an alist, with the car being a symbol indicating the note
  1651. context, and the cdr is the heading to be used. The heading may also be the
  1652. empty string.
  1653. %t in the heading will be replaced by a time stamp.
  1654. %s will be replaced by the new TODO state, in double quotes.
  1655. %u will be replaced by the user name.
  1656. %U will be replaced by the full user name."
  1657. :group 'org-todo
  1658. :group 'org-progress
  1659. :type '(list :greedy t
  1660. (cons (const :tag "Heading when closing an item" done) string)
  1661. (cons (const :tag
  1662. "Heading when changing todo state (todo sequence only)"
  1663. state) string)
  1664. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1665. (defcustom org-log-states-order-reversed t
  1666. "Non-nil means, the latest state change note will be directly after heading.
  1667. When nil, the notes will be orderer according to time."
  1668. :group 'org-todo
  1669. :group 'org-progress
  1670. :type 'boolean)
  1671. (defcustom org-log-repeat 'time
  1672. "Non-nil means, record moving through the DONE state when triggering repeat.
  1673. An auto-repeating tasks is immediately switched back to TODO when marked
  1674. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1675. the TODO keyword definition, or recording a cloing note by setting
  1676. `org-log-done', there will be no record of the task moving trhough DONE.
  1677. This variable forces taking a note anyway. Possible values are:
  1678. nil Don't force a record
  1679. time Record a time stamp
  1680. note Record a note
  1681. This option can also be set with on a per-file-basis with
  1682. #+STARTUP: logrepeat
  1683. #+STARTUP: lognoterepeat
  1684. #+STARTUP: nologrepeat
  1685. You can have local logging settings for a subtree by setting the LOGGING
  1686. property to one or more of these keywords."
  1687. :group 'org-todo
  1688. :group 'org-progress
  1689. :type '(choice
  1690. (const :tag "Don't force a record" nil)
  1691. (const :tag "Force recording the DONE state" time)
  1692. (const :tag "Force recording a note with the DONE state" note)))
  1693. (defcustom org-clock-into-drawer 2
  1694. "Should clocking info be wrapped into a drawer?
  1695. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1696. If necessary, the drawer will be created.
  1697. When nil, the drawer will not be created, but used when present.
  1698. When an integer and the number of clocking entries in an item
  1699. reaches or exceeds this number, a drawer will be created."
  1700. :group 'org-todo
  1701. :group 'org-progress
  1702. :type '(choice
  1703. (const :tag "Always" t)
  1704. (const :tag "Only when drawer exists" nil)
  1705. (integer :tag "When at least N clock entries")))
  1706. (defcustom org-clock-out-when-done t
  1707. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1708. Nil means, clock will keep running until stopped explicitly with
  1709. `C-c C-x C-o', or until the clock is started in a different item."
  1710. :group 'org-progress
  1711. :type 'boolean)
  1712. (defcustom org-clock-in-switch-to-state nil
  1713. "Set task to a special todo state while clocking it.
  1714. The value should be the state to which the entry should be switched."
  1715. :group 'org-progress
  1716. :group 'org-todo
  1717. :type '(choice
  1718. (const :tag "Don't force a state" nil)
  1719. (string :tag "State")))
  1720. (defgroup org-priorities nil
  1721. "Priorities in Org-mode."
  1722. :tag "Org Priorities"
  1723. :group 'org-todo)
  1724. (defcustom org-highest-priority ?A
  1725. "The highest priority of TODO items. A character like ?A, ?B etc.
  1726. Must have a smaller ASCII number than `org-lowest-priority'."
  1727. :group 'org-priorities
  1728. :type 'character)
  1729. (defcustom org-lowest-priority ?C
  1730. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1731. Must have a larger ASCII number than `org-highest-priority'."
  1732. :group 'org-priorities
  1733. :type 'character)
  1734. (defcustom org-default-priority ?B
  1735. "The default priority of TODO items.
  1736. This is the priority an item get if no explicit priority is given."
  1737. :group 'org-priorities
  1738. :type 'character)
  1739. (defcustom org-priority-start-cycle-with-default t
  1740. "Non-nil means, start with default priority when starting to cycle.
  1741. When this is nil, the first step in the cycle will be (depending on the
  1742. command used) one higher or lower that the default priority."
  1743. :group 'org-priorities
  1744. :type 'boolean)
  1745. (defgroup org-time nil
  1746. "Options concerning time stamps and deadlines in Org-mode."
  1747. :tag "Org Time"
  1748. :group 'org)
  1749. (defcustom org-insert-labeled-timestamps-at-point nil
  1750. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1751. When nil, these labeled time stamps are forces into the second line of an
  1752. entry, just after the headline. When scheduling from the global TODO list,
  1753. the time stamp will always be forced into the second line."
  1754. :group 'org-time
  1755. :type 'boolean)
  1756. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1757. "Formats for `format-time-string' which are used for time stamps.
  1758. It is not recommended to change this constant.")
  1759. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1760. "Number of minutes to round time stamps to.
  1761. These are two values, the first applies when first creating a time stamp.
  1762. The second applies when changing it with the commands `S-up' and `S-down'.
  1763. When changing the time stamp, this means that it will change in steps
  1764. of N minues, as given by the second value.
  1765. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1766. numbers should be factors of 60, so for example 5, 10, 15.
  1767. When this is larger than 1, you can still force an exact time-stamp by using
  1768. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1769. and by using a prefix arg to `S-up/down' to specify the exact number
  1770. of minutes to shift."
  1771. :group 'org-time
  1772. :get '(lambda (var) ; Make sure all entries have 5 elements
  1773. (if (integerp (default-value var))
  1774. (list (default-value var) 5)
  1775. (default-value var)))
  1776. :type '(list
  1777. (integer :tag "when inserting times")
  1778. (integer :tag "when modifying times")))
  1779. ;; Make sure old customizations of this variable don't lead to problems.
  1780. (when (integerp org-time-stamp-rounding-minutes)
  1781. (setq org-time-stamp-rounding-minutes
  1782. (list org-time-stamp-rounding-minutes
  1783. org-time-stamp-rounding-minutes)))
  1784. (defcustom org-display-custom-times nil
  1785. "Non-nil means, overlay custom formats over all time stamps.
  1786. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1787. To turn this on on a per-file basis, insert anywhere in the file:
  1788. #+STARTUP: customtime"
  1789. :group 'org-time
  1790. :set 'set-default
  1791. :type 'sexp)
  1792. (make-variable-buffer-local 'org-display-custom-times)
  1793. (defcustom org-time-stamp-custom-formats
  1794. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1795. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1796. These are overlayed over the default ISO format if the variable
  1797. `org-display-custom-times' is set. Time like %H:%M should be at the
  1798. end of the second format."
  1799. :group 'org-time
  1800. :type 'sexp)
  1801. (defun org-time-stamp-format (&optional long inactive)
  1802. "Get the right format for a time string."
  1803. (let ((f (if long (cdr org-time-stamp-formats)
  1804. (car org-time-stamp-formats))))
  1805. (if inactive
  1806. (concat "[" (substring f 1 -1) "]")
  1807. f)))
  1808. (defcustom org-read-date-prefer-future t
  1809. "Non-nil means, assume future for incomplete date input from user.
  1810. This affects the following situations:
  1811. 1. The user gives a day, but no month.
  1812. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1813. read this as the third of *next* month. However, if you enter \"17\",
  1814. it will be considered as *this* month.
  1815. 2. The user gives a month but not a year.
  1816. For example, if it is april and you enter \"feb 2\", this will be read
  1817. as feb 2, *next* year. \"May 5\", however, will be this year.
  1818. When this option is nil, the current month and year will always be used
  1819. as defaults."
  1820. :group 'org-time
  1821. :type 'boolean)
  1822. (defcustom org-read-date-display-live t
  1823. "Non-nil means, display current interpretation of date prompt live.
  1824. This display will be in an overlay, in the minibuffer."
  1825. :group 'org-time
  1826. :type 'boolean)
  1827. (defcustom org-read-date-popup-calendar t
  1828. "Non-nil means, pop up a calendar when prompting for a date.
  1829. In the calendar, the date can be selected with mouse-1. However, the
  1830. minibuffer will also be active, and you can simply enter the date as well.
  1831. When nil, only the minibuffer will be available."
  1832. :group 'org-time
  1833. :type 'boolean)
  1834. (if (fboundp 'defvaralias)
  1835. (defvaralias 'org-popup-calendar-for-date-prompt
  1836. 'org-read-date-popup-calendar))
  1837. (defcustom org-extend-today-until 0
  1838. "The hour when your day really ends.
  1839. This has influence for the following applications:
  1840. - When switching the agenda to \"today\". It it is still earlier than
  1841. the time given here, the day recognized as TODAY is actually yesterday.
  1842. - When a date is read from the user and it is still before the time given
  1843. here, the current date and time will be assumed to be yesterday, 23:59.
  1844. FIXME:
  1845. IMPORTANT: This is still a very experimental feature, it may disappear
  1846. again or it may be extended to mean more things."
  1847. :group 'org-time
  1848. :type 'number)
  1849. (defcustom org-edit-timestamp-down-means-later nil
  1850. "Non-nil means, S-down will increase the time in a time stamp.
  1851. When nil, S-up will increase."
  1852. :group 'org-time
  1853. :type 'boolean)
  1854. (defcustom org-calendar-follow-timestamp-change t
  1855. "Non-nil means, make the calendar window follow timestamp changes.
  1856. When a timestamp is modified and the calendar window is visible, it will be
  1857. moved to the new date."
  1858. :group 'org-time
  1859. :type 'boolean)
  1860. (defcustom org-clock-heading-function nil
  1861. "When non-nil, should be a function to create `org-clock-heading'.
  1862. This is the string shown in the mode line when a clock is running.
  1863. The function is called with point at the beginning of the headline."
  1864. :group 'org-time ; FIXME: Should we have a separate group????
  1865. :type 'function)
  1866. (defgroup org-tags nil
  1867. "Options concerning tags in Org-mode."
  1868. :tag "Org Tags"
  1869. :group 'org)
  1870. (defcustom org-tag-alist nil
  1871. "List of tags allowed in Org-mode files.
  1872. When this list is nil, Org-mode will base TAG input on what is already in the
  1873. buffer.
  1874. The value of this variable is an alist, the car of each entry must be a
  1875. keyword as a string, the cdr may be a character that is used to select
  1876. that tag through the fast-tag-selection interface.
  1877. See the manual for details."
  1878. :group 'org-tags
  1879. :type '(repeat
  1880. (choice
  1881. (cons (string :tag "Tag name")
  1882. (character :tag "Access char"))
  1883. (const :tag "Start radio group" (:startgroup))
  1884. (const :tag "End radio group" (:endgroup)))))
  1885. (defcustom org-use-fast-tag-selection 'auto
  1886. "Non-nil means, use fast tag selection scheme.
  1887. This is a special interface to select and deselect tags with single keys.
  1888. When nil, fast selection is never used.
  1889. When the symbol `auto', fast selection is used if and only if selection
  1890. characters for tags have been configured, either through the variable
  1891. `org-tag-alist' or through a #+TAGS line in the buffer.
  1892. When t, fast selection is always used and selection keys are assigned
  1893. automatically if necessary."
  1894. :group 'org-tags
  1895. :type '(choice
  1896. (const :tag "Always" t)
  1897. (const :tag "Never" nil)
  1898. (const :tag "When selection characters are configured" 'auto)))
  1899. (defcustom org-fast-tag-selection-single-key nil
  1900. "Non-nil means, fast tag selection exits after first change.
  1901. When nil, you have to press RET to exit it.
  1902. During fast tag selection, you can toggle this flag with `C-c'.
  1903. This variable can also have the value `expert'. In this case, the window
  1904. displaying the tags menu is not even shown, until you press C-c again."
  1905. :group 'org-tags
  1906. :type '(choice
  1907. (const :tag "No" nil)
  1908. (const :tag "Yes" t)
  1909. (const :tag "Expert" expert)))
  1910. (defvar org-fast-tag-selection-include-todo nil
  1911. "Non-nil means, fast tags selection interface will also offer TODO states.
  1912. This is an undocumented feature, you should not rely on it.")
  1913. (defcustom org-tags-column -80
  1914. "The column to which tags should be indented in a headline.
  1915. If this number is positive, it specifies the column. If it is negative,
  1916. it means that the tags should be flushright to that column. For example,
  1917. -80 works well for a normal 80 character screen."
  1918. :group 'org-tags
  1919. :type 'integer)
  1920. (defcustom org-auto-align-tags t
  1921. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1922. These operations change the length of a headline and therefore shift
  1923. the tags around. With this options turned on, after each such operation
  1924. the tags are again aligned to `org-tags-column'."
  1925. :group 'org-tags
  1926. :type 'boolean)
  1927. (defcustom org-use-tag-inheritance t
  1928. "Non-nil means, tags in levels apply also for sublevels.
  1929. When nil, only the tags directly given in a specific line apply there.
  1930. If you turn off this option, you very likely want to turn on the
  1931. companion option `org-tags-match-list-sublevels'."
  1932. :group 'org-tags
  1933. :type 'boolean)
  1934. (defcustom org-tags-match-list-sublevels nil
  1935. "Non-nil means list also sublevels of headlines matching tag search.
  1936. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1937. the sublevels of a headline matching a tag search often also match
  1938. the same search. Listing all of them can create very long lists.
  1939. Setting this variable to nil causes subtrees of a match to be skipped.
  1940. This option is off by default, because inheritance in on. If you turn
  1941. inheritance off, you very likely want to turn this option on.
  1942. As a special case, if the tag search is restricted to TODO items, the
  1943. value of this variable is ignored and sublevels are always checked, to
  1944. make sure all corresponding TODO items find their way into the list."
  1945. :group 'org-tags
  1946. :type 'boolean)
  1947. (defvar org-tags-history nil
  1948. "History of minibuffer reads for tags.")
  1949. (defvar org-last-tags-completion-table nil
  1950. "The last used completion table for tags.")
  1951. (defvar org-after-tags-change-hook nil
  1952. "Hook that is run after the tags in a line have changed.")
  1953. (defgroup org-properties nil
  1954. "Options concerning properties in Org-mode."
  1955. :tag "Org Properties"
  1956. :group 'org)
  1957. (defcustom org-property-format "%-10s %s"
  1958. "How property key/value pairs should be formatted by `indent-line'.
  1959. When `indent-line' hits a property definition, it will format the line
  1960. according to this format, mainly to make sure that the values are
  1961. lined-up with respect to each other."
  1962. :group 'org-properties
  1963. :type 'string)
  1964. (defcustom org-use-property-inheritance nil
  1965. "Non-nil means, properties apply also for sublevels.
  1966. This setting is only relevant during property searches, not when querying
  1967. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1968. you need to call `org-entry-get' with the inheritance flag.
  1969. Turning this on can cause significant overhead when doing a search, so
  1970. this is turned off by default.
  1971. When nil, only the properties directly given in the current entry count.
  1972. The value may also be a list of properties that shouldhave inheritance.
  1973. However, note that some special properties use inheritance under special
  1974. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1975. and the properties ending in \"_ALL\" when they are used as descriptor
  1976. for valid values of a property."
  1977. :group 'org-properties
  1978. :type '(choice
  1979. (const :tag "Not" nil)
  1980. (const :tag "Always" nil)
  1981. (repeat :tag "Specific properties" (string :tag "Property"))))
  1982. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1983. "The default column format, if no other format has been defined.
  1984. This variable can be set on the per-file basis by inserting a line
  1985. #+COLUMNS: %25ITEM ....."
  1986. :group 'org-properties
  1987. :type 'string)
  1988. (defcustom org-global-properties nil
  1989. "List of property/value pairs that can be inherited by any entry.
  1990. You can set buffer-local values for this by adding lines like
  1991. #+PROPERTY: NAME VALUE"
  1992. :group 'org-properties
  1993. :type '(repeat
  1994. (cons (string :tag "Property")
  1995. (string :tag "Value"))))
  1996. (defvar org-local-properties nil
  1997. "List of property/value pairs that can be inherited by any entry.
  1998. Valid for the current buffer.
  1999. This variable is populated from #+PROPERTY lines.")
  2000. (defgroup org-agenda nil
  2001. "Options concerning agenda views in Org-mode."
  2002. :tag "Org Agenda"
  2003. :group 'org)
  2004. (defvar org-category nil
  2005. "Variable used by org files to set a category for agenda display.
  2006. Such files should use a file variable to set it, for example
  2007. # -*- mode: org; org-category: \"ELisp\"
  2008. or contain a special line
  2009. #+CATEGORY: ELisp
  2010. If the file does not specify a category, then file's base name
  2011. is used instead.")
  2012. (make-variable-buffer-local 'org-category)
  2013. (defcustom org-agenda-files nil
  2014. "The files to be used for agenda display.
  2015. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2016. \\[org-remove-file]. You can also use customize to edit the list.
  2017. If an entry is a directory, all files in that directory that are matched by
  2018. `org-agenda-file-regexp' will be part of the file list.
  2019. If the value of the variable is not a list but a single file name, then
  2020. the list of agenda files is actually stored and maintained in that file, one
  2021. agenda file per line."
  2022. :group 'org-agenda
  2023. :type '(choice
  2024. (repeat :tag "List of files and directories" file)
  2025. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2026. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2027. "Regular expression to match files for `org-agenda-files'.
  2028. If any element in the list in that variable contains a directory instead
  2029. of a normal file, all files in that directory that are matched by this
  2030. regular expression will be included."
  2031. :group 'org-agenda
  2032. :type 'regexp)
  2033. (defcustom org-agenda-skip-unavailable-files nil
  2034. "t means to just skip non-reachable files in `org-agenda-files'.
  2035. Nil means to remove them, after a query, from the list."
  2036. :group 'org-agenda
  2037. :type 'boolean)
  2038. (defcustom org-agenda-text-search-extra-files nil
  2039. "List of extra files to be searched by text search commands.
  2040. These files will be search in addition to the agenda files bu the
  2041. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2042. Note that these files will only be searched for text search commands,
  2043. not for the other agenda views like todo lists, tag earches or the weekly
  2044. agenda. This variable is intended to list notes and possibly archive files
  2045. that should also be searched by these two commands."
  2046. :group 'org-agenda
  2047. :type '(repeat file))
  2048. (if (fboundp 'defvaralias)
  2049. (defvaralias 'org-agenda-multi-occur-extra-files
  2050. 'org-agenda-text-search-extra-files))
  2051. (defcustom org-agenda-confirm-kill 1
  2052. "When set, remote killing from the agenda buffer needs confirmation.
  2053. When t, a confirmation is always needed. When a number N, confirmation is
  2054. only needed when the text to be killed contains more than N non-white lines."
  2055. :group 'org-agenda
  2056. :type '(choice
  2057. (const :tag "Never" nil)
  2058. (const :tag "Always" t)
  2059. (number :tag "When more than N lines")))
  2060. (defcustom org-calendar-to-agenda-key [?c]
  2061. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2062. The command `org-calendar-goto-agenda' will be bound to this key. The
  2063. default is the character `c' because then `c' can be used to switch back and
  2064. forth between agenda and calendar."
  2065. :group 'org-agenda
  2066. :type 'sexp)
  2067. (defcustom org-agenda-compact-blocks nil
  2068. "Non-nil means, make the block agenda more compact.
  2069. This is done by leaving out unnecessary lines."
  2070. :group 'org-agenda
  2071. :type nil)
  2072. (defgroup org-agenda-export nil
  2073. "Options concerning exporting agenda views in Org-mode."
  2074. :tag "Org Agenda Export"
  2075. :group 'org-agenda)
  2076. (defcustom org-agenda-with-colors t
  2077. "Non-nil means, use colors in agenda views."
  2078. :group 'org-agenda-export
  2079. :type 'boolean)
  2080. (defcustom org-agenda-exporter-settings nil
  2081. "Alist of variable/value pairs that should be active during agenda export.
  2082. This is a good place to set uptions for ps-print and for htmlize."
  2083. :group 'org-agenda-export
  2084. :type '(repeat
  2085. (list
  2086. (variable)
  2087. (sexp :tag "Value"))))
  2088. (defcustom org-agenda-export-html-style ""
  2089. "The style specification for exported HTML Agenda files.
  2090. If this variable contains a string, it will replace the default <style>
  2091. section as produced by `htmlize'.
  2092. Since there are different ways of setting style information, this variable
  2093. needs to contain the full HTML structure to provide a style, including the
  2094. surrounding HTML tags. The style specifications should include definitions
  2095. the fonts used by the agenda, here is an example:
  2096. <style type=\"text/css\">
  2097. p { font-weight: normal; color: gray; }
  2098. .org-agenda-structure {
  2099. font-size: 110%;
  2100. color: #003399;
  2101. font-weight: 600;
  2102. }
  2103. .org-todo {
  2104. color: #cc6666;
  2105. font-weight: bold;
  2106. }
  2107. .org-done {
  2108. color: #339933;
  2109. }
  2110. .title { text-align: center; }
  2111. .todo, .deadline { color: red; }
  2112. .done { color: green; }
  2113. </style>
  2114. or, if you want to keep the style in a file,
  2115. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2116. As the value of this option simply gets inserted into the HTML <head> header,
  2117. you can \"misuse\" it to also add other text to the header. However,
  2118. <style>...</style> is required, if not present the variable will be ignored."
  2119. :group 'org-agenda-export
  2120. :group 'org-export-html
  2121. :type 'string)
  2122. (defgroup org-agenda-custom-commands nil
  2123. "Options concerning agenda views in Org-mode."
  2124. :tag "Org Agenda Custom Commands"
  2125. :group 'org-agenda)
  2126. (defconst org-sorting-choice
  2127. '(choice
  2128. (const time-up) (const time-down)
  2129. (const category-keep) (const category-up) (const category-down)
  2130. (const tag-down) (const tag-up)
  2131. (const priority-up) (const priority-down))
  2132. "Sorting choices.")
  2133. (defconst org-agenda-custom-commands-local-options
  2134. `(repeat :tag "Local settings for this command. Remember to quote values"
  2135. (choice :tag "Setting"
  2136. (list :tag "Any variable"
  2137. (variable :tag "Variable")
  2138. (sexp :tag "Value"))
  2139. (list :tag "Files to be searched"
  2140. (const org-agenda-files)
  2141. (list
  2142. (const :format "" quote)
  2143. (repeat
  2144. (file))))
  2145. (list :tag "Sorting strategy"
  2146. (const org-agenda-sorting-strategy)
  2147. (list
  2148. (const :format "" quote)
  2149. (repeat
  2150. ,org-sorting-choice)))
  2151. (list :tag "Prefix format"
  2152. (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
  2153. (string))
  2154. (list :tag "Number of days in agenda"
  2155. (const org-agenda-ndays)
  2156. (integer :value 1))
  2157. (list :tag "Fixed starting date"
  2158. (const org-agenda-start-day)
  2159. (string :value "2007-11-01"))
  2160. (list :tag "Start on day of week"
  2161. (const org-agenda-start-on-weekday)
  2162. (choice :value 1
  2163. (const :tag "Today" nil)
  2164. (number :tag "Weekday No.")))
  2165. (list :tag "Include data from diary"
  2166. (const org-agenda-include-diary)
  2167. (boolean))
  2168. (list :tag "Deadline Warning days"
  2169. (const org-deadline-warning-days)
  2170. (integer :value 1))
  2171. (list :tag "Standard skipping condition"
  2172. :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
  2173. (const org-agenda-skip-function)
  2174. (list
  2175. (const :format "" quote)
  2176. (list
  2177. (choice
  2178. :tag "Skiping range"
  2179. (const :tag "Skip entry" org-agenda-skip-entry-if)
  2180. (const :tag "Skip subtree" org-agenda-skip-subtree-if))
  2181. (repeat :inline t :tag "Conditions for skipping"
  2182. (choice
  2183. :tag "Condition type"
  2184. (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
  2185. (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
  2186. (const :tag "scheduled" 'scheduled)
  2187. (const :tag "not scheduled" 'notscheduled)
  2188. (const :tag "deadline" 'deadline)
  2189. (const :tag "no deadline" 'notdeadline))))))
  2190. (list :tag "Non-standard skipping condition"
  2191. :value (org-agenda-skip-function)
  2192. (list
  2193. (const org-agenda-skip-function)
  2194. (sexp :tag "Function or form (quoted!)")))))
  2195. "Selection of examples for agenda command settings.
  2196. This will be spliced into the custom type of
  2197. `org-agenda-custom-commands'.")
  2198. (defcustom org-agenda-custom-commands nil
  2199. "Custom commands for the agenda.
  2200. These commands will be offered on the splash screen displayed by the
  2201. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2202. (key desc type match settings files)
  2203. key The key (one or more characters as a string) to be associated
  2204. with the command.
  2205. desc A description of the command, when omitted or nil, a default
  2206. description is built using MATCH.
  2207. type The command type, any of the following symbols:
  2208. agenda The daily/weekly agenda.
  2209. todo Entries with a specific TODO keyword, in all agenda files.
  2210. search Entries containing search words entry or headline.
  2211. tags Tags/Property/TODO match in all agenda files.
  2212. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  2213. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2214. tags-tree Sparse tree with all tags matches in *current* file.
  2215. occur-tree Occur sparse tree for *current* file.
  2216. ... A user-defined function.
  2217. match What to search for:
  2218. - a single keyword for TODO keyword searches
  2219. - a tags match expression for tags searches
  2220. - a word search expression for text searches.
  2221. - a regular expression for occur searches
  2222. For all other commands, this should be the empty string.
  2223. settings A list of option settings, similar to that in a let form, so like
  2224. this: ((opt1 val1) (opt2 val2) ...). The values will be
  2225. evaluated at the moment of execution, so quote them when needed.
  2226. files A list of files file to write the produced agenda buffer to
  2227. with the command `org-store-agenda-views'.
  2228. If a file name ends in \".html\", an HTML version of the buffer
  2229. is written out. If it ends in \".ps\", a postscript version is
  2230. produced. Otherwide, only the plain text is written to the file.
  2231. You can also define a set of commands, to create a composite agenda buffer.
  2232. In this case, an entry looks like this:
  2233. (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
  2234. where
  2235. desc A description string to be displayed in the dispatcher menu.
  2236. cmd An agenda command, similar to the above. However, tree commands
  2237. are no allowed, but instead you can get agenda and global todo list.
  2238. So valid commands for a set are:
  2239. (agenda \"\" settings)
  2240. (alltodo \"\" settings)
  2241. (stuck \"\" settings)
  2242. (todo \"match\" settings files)
  2243. (search \"match\" settings files)
  2244. (tags \"match\" settings files)
  2245. (tags-todo \"match\" settings files)
  2246. Each command can carry a list of options, and another set of options can be
  2247. given for the whole set of commands. Individual command options take
  2248. precedence over the general options.
  2249. When using several characters as key to a command, the first characters
  2250. are prefix commands. For the dispatcher to display useful information, you
  2251. should provide a description for the prefix, like
  2252. (setq org-agenda-custom-commands
  2253. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2254. (\"hl\" tags \"+HOME+Lisa\")
  2255. (\"hp\" tags \"+HOME+Peter\")
  2256. (\"hk\" tags \"+HOME+Kim\")))"
  2257. :group 'org-agenda-custom-commands
  2258. :type `(repeat
  2259. (choice :value ("x" "Describe command here" tags "" nil)
  2260. (list :tag "Single command"
  2261. (string :tag "Access Key(s) ")
  2262. (option (string :tag "Description"))
  2263. (choice
  2264. (const :tag "Agenda" agenda)
  2265. (const :tag "TODO list" alltodo)
  2266. (const :tag "Search words" search)
  2267. (const :tag "Stuck projects" stuck)
  2268. (const :tag "Tags search (all agenda files)" tags)
  2269. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2270. (const :tag "TODO keyword search (all agenda files)" todo)
  2271. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2272. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2273. (const :tag "Occur tree (current buffer)" occur-tree)
  2274. (sexp :tag "Other, user-defined function"))
  2275. (string :tag "Match (only for some commands)")
  2276. ,org-agenda-custom-commands-local-options
  2277. (option (repeat :tag "Export" (file :tag "Export to"))))
  2278. (list :tag "Command series, all agenda files"
  2279. (string :tag "Access Key(s)")
  2280. (string :tag "Description ")
  2281. (repeat :tag "Component"
  2282. (choice
  2283. (list :tag "Agenda"
  2284. (const :format "" agenda)
  2285. (const :tag "" :format "" "")
  2286. ,org-agenda-custom-commands-local-options)
  2287. (list :tag "TODO list (all keywords)"
  2288. (const :format "" alltodo)
  2289. (const :tag "" :format "" "")
  2290. ,org-agenda-custom-commands-local-options)
  2291. (list :tag "Search words"
  2292. (const :format "" search)
  2293. (string :tag "Match")
  2294. ,org-agenda-custom-commands-local-options)
  2295. (list :tag "Stuck projects"
  2296. (const :format "" stuck)
  2297. (const :tag "" :format "" "")
  2298. ,org-agenda-custom-commands-local-options)
  2299. (list :tag "Tags search"
  2300. (const :format "" tags)
  2301. (string :tag "Match")
  2302. ,org-agenda-custom-commands-local-options)
  2303. (list :tag "Tags search, TODO entries only"
  2304. (const :format "" tags-todo)
  2305. (string :tag "Match")
  2306. ,org-agenda-custom-commands-local-options)
  2307. (list :tag "TODO keyword search"
  2308. (const :format "" todo)
  2309. (string :tag "Match")
  2310. ,org-agenda-custom-commands-local-options)
  2311. (list :tag "Other, user-defined function"
  2312. (symbol :tag "function")
  2313. (string :tag "Match")
  2314. ,org-agenda-custom-commands-local-options)))
  2315. (repeat :tag "Settings for entire command set"
  2316. (list (variable :tag "Any variable")
  2317. (sexp :tag "Value")))
  2318. (option (repeat :tag "Export" (file :tag "Export to"))))
  2319. (cons :tag "Prefix key documentation"
  2320. (string :tag "Access Key(s)")
  2321. (string :tag "Description ")))))
  2322. (defcustom org-agenda-query-register ?o
  2323. "The register holding the current query string.
  2324. The prupose of this is that if you construct a query string interactively,
  2325. you can then use it to define a custom command."
  2326. :group 'org-agenda-custom-commands
  2327. :type 'character)
  2328. (defcustom org-stuck-projects
  2329. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2330. "How to identify stuck projects.
  2331. This is a list of four items:
  2332. 1. A tags/todo matcher string that is used to identify a project.
  2333. The entire tree below a headline matched by this is considered one project.
  2334. 2. A list of TODO keywords identifying non-stuck projects.
  2335. If the project subtree contains any headline with one of these todo
  2336. keywords, the project is considered to be not stuck. If you specify
  2337. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2338. 3. A list of tags identifying non-stuck projects.
  2339. If the project subtree contains any headline with one of these tags,
  2340. the project is considered to be not stuck. If you specify \"*\" as
  2341. a tag, any tag will mark the project unstuck.
  2342. 4. An arbitrary regular expression matching non-stuck projects.
  2343. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2344. or `C-c a #' to produce the list."
  2345. :group 'org-agenda-custom-commands
  2346. :type '(list
  2347. (string :tag "Tags/TODO match to identify a project")
  2348. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2349. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2350. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2351. (defgroup org-agenda-skip nil
  2352. "Options concerning skipping parts of agenda files."
  2353. :tag "Org Agenda Skip"
  2354. :group 'org-agenda)
  2355. (defcustom org-agenda-todo-list-sublevels t
  2356. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2357. When nil, the sublevels of a TODO entry are not checked, resulting in
  2358. potentially much shorter TODO lists."
  2359. :group 'org-agenda-skip
  2360. :group 'org-todo
  2361. :type 'boolean)
  2362. (defcustom org-agenda-todo-ignore-with-date nil
  2363. "Non-nil means, don't show entries with a date in the global todo list.
  2364. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2365. but don't want them to show up in the TODO list.
  2366. When this is set, it also covers deadlines and scheduled items, the settings
  2367. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2368. will be ignored."
  2369. :group 'org-agenda-skip
  2370. :group 'org-todo
  2371. :type 'boolean)
  2372. (defcustom org-agenda-todo-ignore-scheduled nil
  2373. "Non-nil means, don't show scheduled entries in the global todo list.
  2374. The idea behind this is that by scheduling it, you have already taken care
  2375. of this item.
  2376. See also `org-agenda-todo-ignore-with-date'."
  2377. :group 'org-agenda-skip
  2378. :group 'org-todo
  2379. :type 'boolean)
  2380. (defcustom org-agenda-todo-ignore-deadlines nil
  2381. "Non-nil means, don't show near deadline entries in the global todo list.
  2382. Near means closer than `org-deadline-warning-days' days.
  2383. The idea behind this is that such items will appear in the agenda anyway.
  2384. See also `org-agenda-todo-ignore-with-date'."
  2385. :group 'org-agenda-skip
  2386. :group 'org-todo
  2387. :type 'boolean)
  2388. (defcustom org-agenda-skip-scheduled-if-done nil
  2389. "Non-nil means don't show scheduled items in agenda when they are done.
  2390. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2391. it applies only to the actual date of the scheduling. Warnings about
  2392. an item with a past scheduling dates are always turned off when the item
  2393. is DONE."
  2394. :group 'org-agenda-skip
  2395. :type 'boolean)
  2396. (defcustom org-agenda-skip-deadline-if-done nil
  2397. "Non-nil means don't show deadines when the corresponding item is done.
  2398. When nil, the deadline is still shown and should give you a happy feeling.
  2399. This is relevant for the daily/weekly agenda. And it applied only to the
  2400. actualy date of the deadline. Warnings about approching and past-due
  2401. deadlines are always turned off when the item is DONE."
  2402. :group 'org-agenda-skip
  2403. :type 'boolean)
  2404. (defcustom org-agenda-skip-timestamp-if-done nil
  2405. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2406. :group 'org-agenda-skip
  2407. :type 'boolean)
  2408. (defcustom org-timeline-show-empty-dates 3
  2409. "Non-nil means, `org-timeline' also shows dates without an entry.
  2410. When nil, only the days which actually have entries are shown.
  2411. When t, all days between the first and the last date are shown.
  2412. When an integer, show also empty dates, but if there is a gap of more than
  2413. N days, just insert a special line indicating the size of the gap."
  2414. :group 'org-agenda-skip
  2415. :type '(choice
  2416. (const :tag "None" nil)
  2417. (const :tag "All" t)
  2418. (number :tag "at most")))
  2419. (defgroup org-agenda-startup nil
  2420. "Options concerning initial settings in the Agenda in Org Mode."
  2421. :tag "Org Agenda Startup"
  2422. :group 'org-agenda)
  2423. (defcustom org-finalize-agenda-hook nil
  2424. "Hook run just before displaying an agenda buffer."
  2425. :group 'org-agenda-startup
  2426. :type 'hook)
  2427. (defcustom org-agenda-mouse-1-follows-link nil
  2428. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2429. A longer mouse click will still set point. Does not work on XEmacs.
  2430. Needs to be set before org.el is loaded."
  2431. :group 'org-agenda-startup
  2432. :type 'boolean)
  2433. (defcustom org-agenda-start-with-follow-mode nil
  2434. "The initial value of follow-mode in a newly created agenda window."
  2435. :group 'org-agenda-startup
  2436. :type 'boolean)
  2437. (defgroup org-agenda-windows nil
  2438. "Options concerning the windows used by the Agenda in Org Mode."
  2439. :tag "Org Agenda Windows"
  2440. :group 'org-agenda)
  2441. (defcustom org-agenda-window-setup 'reorganize-frame
  2442. "How the agenda buffer should be displayed.
  2443. Possible values for this option are:
  2444. current-window Show agenda in the current window, keeping all other windows.
  2445. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2446. other-window Use `switch-to-buffer-other-window' to display agenda.
  2447. reorganize-frame Show only two windows on the current frame, the current
  2448. window and the agenda.
  2449. See also the variable `org-agenda-restore-windows-after-quit'."
  2450. :group 'org-agenda-windows
  2451. :type '(choice
  2452. (const current-window)
  2453. (const other-frame)
  2454. (const other-window)
  2455. (const reorganize-frame)))
  2456. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2457. "The min and max height of the agenda window as a fraction of frame height.
  2458. The value of the variable is a cons cell with two numbers between 0 and 1.
  2459. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2460. :group 'org-agenda-windows
  2461. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2462. (defcustom org-agenda-restore-windows-after-quit nil
  2463. "Non-nil means, restore window configuration open exiting agenda.
  2464. Before the window configuration is changed for displaying the agenda,
  2465. the current status is recorded. When the agenda is exited with
  2466. `q' or `x' and this option is set, the old state is restored. If
  2467. `org-agenda-window-setup' is `other-frame', the value of this
  2468. option will be ignored.."
  2469. :group 'org-agenda-windows
  2470. :type 'boolean)
  2471. (defcustom org-indirect-buffer-display 'other-window
  2472. "How should indirect tree buffers be displayed?
  2473. This applies to indirect buffers created with the commands
  2474. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2475. Valid values are:
  2476. current-window Display in the current window
  2477. other-window Just display in another window.
  2478. dedicated-frame Create one new frame, and re-use it each time.
  2479. new-frame Make a new frame each time. Note that in this case
  2480. previously-made indirect buffers are kept, and you need to
  2481. kill these buffers yourself."
  2482. :group 'org-structure
  2483. :group 'org-agenda-windows
  2484. :type '(choice
  2485. (const :tag "In current window" current-window)
  2486. (const :tag "In current frame, other window" other-window)
  2487. (const :tag "Each time a new frame" new-frame)
  2488. (const :tag "One dedicated frame" dedicated-frame)))
  2489. (defgroup org-agenda-daily/weekly nil
  2490. "Options concerning the daily/weekly agenda."
  2491. :tag "Org Agenda Daily/Weekly"
  2492. :group 'org-agenda)
  2493. (defcustom org-agenda-ndays 7
  2494. "Number of days to include in overview display.
  2495. Should be 1 or 7."
  2496. :group 'org-agenda-daily/weekly
  2497. :type 'number)
  2498. (defcustom org-agenda-start-on-weekday 1
  2499. "Non-nil means, start the overview always on the specified weekday.
  2500. 0 denotes Sunday, 1 denotes Monday etc.
  2501. When nil, always start on the current day."
  2502. :group 'org-agenda-daily/weekly
  2503. :type '(choice (const :tag "Today" nil)
  2504. (number :tag "Weekday No.")))
  2505. (defcustom org-agenda-show-all-dates t
  2506. "Non-nil means, `org-agenda' shows every day in the selected range.
  2507. When nil, only the days which actually have entries are shown."
  2508. :group 'org-agenda-daily/weekly
  2509. :type 'boolean)
  2510. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2511. "Format string for displaying dates in the agenda.
  2512. Used by the daily/weekly agenda and by the timeline. This should be
  2513. a format string understood by `format-time-string', or a function returning
  2514. the formatted date as a string. The function must take a single argument,
  2515. a calendar-style date list like (month day year)."
  2516. :group 'org-agenda-daily/weekly
  2517. :type '(choice
  2518. (string :tag "Format string")
  2519. (function :tag "Function")))
  2520. (defun org-agenda-format-date-aligned (date)
  2521. "Format a date string for display in the daily/weekly agenda, or timeline.
  2522. This function makes sure that dates are aligned for easy reading."
  2523. (format "%-9s %2d %s %4d"
  2524. (calendar-day-name date)
  2525. (extract-calendar-day date)
  2526. (calendar-month-name (extract-calendar-month date))
  2527. (extract-calendar-year date)))
  2528. (defcustom org-agenda-include-diary nil
  2529. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2530. :group 'org-agenda-daily/weekly
  2531. :type 'boolean)
  2532. (defcustom org-agenda-include-all-todo nil
  2533. "Set means weekly/daily agenda will always contain all TODO entries.
  2534. The TODO entries will be listed at the top of the agenda, before
  2535. the entries for specific days."
  2536. :group 'org-agenda-daily/weekly
  2537. :type 'boolean)
  2538. (defcustom org-agenda-repeating-timestamp-show-all t
  2539. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2540. When nil, only one occurence is shown, either today or the
  2541. nearest into the future."
  2542. :group 'org-agenda-daily/weekly
  2543. :type 'boolean)
  2544. (defcustom org-deadline-warning-days 14
  2545. "No. of days before expiration during which a deadline becomes active.
  2546. This variable governs the display in sparse trees and in the agenda.
  2547. When 0 or negative, it means use this number (the absolute value of it)
  2548. even if a deadline has a different individual lead time specified."
  2549. :group 'org-time
  2550. :group 'org-agenda-daily/weekly
  2551. :type 'number)
  2552. (defcustom org-scheduled-past-days 10000
  2553. "No. of days to continue listing scheduled items that are not marked DONE.
  2554. When an item is scheduled on a date, it shows up in the agenda on this
  2555. day and will be listed until it is marked done for the number of days
  2556. given here."
  2557. :group 'org-agenda-daily/weekly
  2558. :type 'number)
  2559. (defgroup org-agenda-time-grid nil
  2560. "Options concerning the time grid in the Org-mode Agenda."
  2561. :tag "Org Agenda Time Grid"
  2562. :group 'org-agenda)
  2563. (defcustom org-agenda-use-time-grid t
  2564. "Non-nil means, show a time grid in the agenda schedule.
  2565. A time grid is a set of lines for specific times (like every two hours between
  2566. 8:00 and 20:00). The items scheduled for a day at specific times are
  2567. sorted in between these lines.
  2568. For details about when the grid will be shown, and what it will look like, see
  2569. the variable `org-agenda-time-grid'."
  2570. :group 'org-agenda-time-grid
  2571. :type 'boolean)
  2572. (defcustom org-agenda-time-grid
  2573. '((daily today require-timed)
  2574. "----------------"
  2575. (800 1000 1200 1400 1600 1800 2000))
  2576. "The settings for time grid for agenda display.
  2577. This is a list of three items. The first item is again a list. It contains
  2578. symbols specifying conditions when the grid should be displayed:
  2579. daily if the agenda shows a single day
  2580. weekly if the agenda shows an entire week
  2581. today show grid on current date, independent of daily/weekly display
  2582. require-timed show grid only if at least one item has a time specification
  2583. The second item is a string which will be places behing the grid time.
  2584. The third item is a list of integers, indicating the times that should have
  2585. a grid line."
  2586. :group 'org-agenda-time-grid
  2587. :type
  2588. '(list
  2589. (set :greedy t :tag "Grid Display Options"
  2590. (const :tag "Show grid in single day agenda display" daily)
  2591. (const :tag "Show grid in weekly agenda display" weekly)
  2592. (const :tag "Always show grid for today" today)
  2593. (const :tag "Show grid only if any timed entries are present"
  2594. require-timed)
  2595. (const :tag "Skip grid times already present in an entry"
  2596. remove-match))
  2597. (string :tag "Grid String")
  2598. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2599. (defgroup org-agenda-sorting nil
  2600. "Options concerning sorting in the Org-mode Agenda."
  2601. :tag "Org Agenda Sorting"
  2602. :group 'org-agenda)
  2603. (defcustom org-agenda-sorting-strategy
  2604. '((agenda time-up category-keep priority-down)
  2605. (todo category-keep priority-down)
  2606. (tags category-keep priority-down)
  2607. (search category-keep))
  2608. "Sorting structure for the agenda items of a single day.
  2609. This is a list of symbols which will be used in sequence to determine
  2610. if an entry should be listed before another entry. The following
  2611. symbols are recognized:
  2612. time-up Put entries with time-of-day indications first, early first
  2613. time-down Put entries with time-of-day indications first, late first
  2614. category-keep Keep the default order of categories, corresponding to the
  2615. sequence in `org-agenda-files'.
  2616. category-up Sort alphabetically by category, A-Z.
  2617. category-down Sort alphabetically by category, Z-A.
  2618. tag-up Sort alphabetically by last tag, A-Z.
  2619. tag-down Sort alphabetically by last tag, Z-A.
  2620. priority-up Sort numerically by priority, high priority last.
  2621. priority-down Sort numerically by priority, high priority first.
  2622. The different possibilities will be tried in sequence, and testing stops
  2623. if one comparison returns a \"not-equal\". For example, the default
  2624. '(time-up category-keep priority-down)
  2625. means: Pull out all entries having a specified time of day and sort them,
  2626. in order to make a time schedule for the current day the first thing in the
  2627. agenda listing for the day. Of the entries without a time indication, keep
  2628. the grouped in categories, don't sort the categories, but keep them in
  2629. the sequence given in `org-agenda-files'. Within each category sort by
  2630. priority.
  2631. Leaving out `category-keep' would mean that items will be sorted across
  2632. categories by priority.
  2633. Instead of a single list, this can also be a set of list for specific
  2634. contents, with a context symbol in the car of the list, any of
  2635. `agenda', `todo', `tags' for the corresponding agenda views."
  2636. :group 'org-agenda-sorting
  2637. :type `(choice
  2638. (repeat :tag "General" ,org-sorting-choice)
  2639. (list :tag "Individually"
  2640. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2641. (repeat ,org-sorting-choice))
  2642. (cons (const :tag "Strategy for TODO lists" todo)
  2643. (repeat ,org-sorting-choice))
  2644. (cons (const :tag "Strategy for Tags matches" tags)
  2645. (repeat ,org-sorting-choice)))))
  2646. (defcustom org-sort-agenda-notime-is-late t
  2647. "Non-nil means, items without time are considered late.
  2648. This is only relevant for sorting. When t, items which have no explicit
  2649. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2650. do have a time. When nil, the default time is before 0:00. You can use this
  2651. option to decide if the schedule for today should come before or after timeless
  2652. agenda entries."
  2653. :group 'org-agenda-sorting
  2654. :type 'boolean)
  2655. (defgroup org-agenda-line-format nil
  2656. "Options concerning the entry prefix in the Org-mode agenda display."
  2657. :tag "Org Agenda Line Format"
  2658. :group 'org-agenda)
  2659. (defcustom org-agenda-prefix-format
  2660. '((agenda . " %-12:c%?-12t% s")
  2661. (timeline . " % s")
  2662. (todo . " %-12:c")
  2663. (tags . " %-12:c")
  2664. (search . " %-12:c"))
  2665. "Format specifications for the prefix of items in the agenda views.
  2666. An alist with four entries, for the different agenda types. The keys to the
  2667. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2668. are format strings.
  2669. This format works similar to a printf format, with the following meaning:
  2670. %c the category of the item, \"Diary\" for entries from the diary, or
  2671. as given by the CATEGORY keyword or derived from the file name.
  2672. %T the *last* tag of the item. Last because inherited tags come
  2673. first in the list.
  2674. %t the time-of-day specification if one applies to the entry, in the
  2675. format HH:MM
  2676. %s Scheduling/Deadline information, a short string
  2677. All specifiers work basically like the standard `%s' of printf, but may
  2678. contain two additional characters: A question mark just after the `%' and
  2679. a whitespace/punctuation character just before the final letter.
  2680. If the first character after `%' is a question mark, the entire field
  2681. will only be included if the corresponding value applies to the
  2682. current entry. This is useful for fields which should have fixed
  2683. width when present, but zero width when absent. For example,
  2684. \"%?-12t\" will result in a 12 character time field if a time of the
  2685. day is specified, but will completely disappear in entries which do
  2686. not contain a time.
  2687. If there is punctuation or whitespace character just before the final
  2688. format letter, this character will be appended to the field value if
  2689. the value is not empty. For example, the format \"%-12:c\" leads to
  2690. \"Diary: \" if the category is \"Diary\". If the category were be
  2691. empty, no additional colon would be interted.
  2692. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2693. - Indent the line with two space characters
  2694. - Give the category in a 12 chars wide field, padded with whitespace on
  2695. the right (because of `-'). Append a colon if there is a category
  2696. (because of `:').
  2697. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2698. time, don't put in an empty field, just skip it (because of '?').
  2699. - Finally, put the scheduling information and append a whitespace.
  2700. As another example, if you don't want the time-of-day of entries in
  2701. the prefix, you could use:
  2702. (setq org-agenda-prefix-format \" %-11:c% s\")
  2703. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2704. `org-agenda-remove-tags'."
  2705. :type '(choice
  2706. (string :tag "General format")
  2707. (list :greedy t :tag "View dependent"
  2708. (cons (const agenda) (string :tag "Format"))
  2709. (cons (const timeline) (string :tag "Format"))
  2710. (cons (const todo) (string :tag "Format"))
  2711. (cons (const tags) (string :tag "Format"))
  2712. (cons (const search) (string :tag "Format"))))
  2713. :group 'org-agenda-line-format)
  2714. (defvar org-prefix-format-compiled nil
  2715. "The compiled version of the most recently used prefix format.
  2716. See the variable `org-agenda-prefix-format'.")
  2717. (defcustom org-agenda-todo-keyword-format "%-1s"
  2718. "Format for the TODO keyword in agenda lines.
  2719. Set this to something like \"%-12s\" if you want all TODO keywords
  2720. to occupy a fixed space in the agenda display."
  2721. :group 'org-agenda-line-format
  2722. :type 'string)
  2723. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2724. "Text preceeding scheduled items in the agenda view.
  2725. This is a list with two strings. The first applies when the item is
  2726. scheduled on the current day. The second applies when it has been scheduled
  2727. previously, it may contain a %d to capture how many days ago the item was
  2728. scheduled."
  2729. :group 'org-agenda-line-format
  2730. :type '(list
  2731. (string :tag "Scheduled today ")
  2732. (string :tag "Scheduled previously")))
  2733. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2734. "Text preceeding deadline items in the agenda view.
  2735. This is a list with two strings. The first applies when the item has its
  2736. deadline on the current day. The second applies when it is in the past or
  2737. in the future, it may contain %d to capture how many days away the deadline
  2738. is (was)."
  2739. :group 'org-agenda-line-format
  2740. :type '(list
  2741. (string :tag "Deadline today ")
  2742. (string :tag "Deadline relative")))
  2743. (defcustom org-agenda-remove-times-when-in-prefix t
  2744. "Non-nil means, remove duplicate time specifications in agenda items.
  2745. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2746. time-of-day specification in a headline or diary entry is extracted and
  2747. placed into the prefix. If this option is non-nil, the original specification
  2748. \(a timestamp or -range, or just a plain time(range) specification like
  2749. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2750. cluttered.
  2751. The option can be t or nil. It may also be the symbol `beg', indicating
  2752. that the time should only be removed what it is located at the beginning of
  2753. the headline/diary entry."
  2754. :group 'org-agenda-line-format
  2755. :type '(choice
  2756. (const :tag "Always" t)
  2757. (const :tag "Never" nil)
  2758. (const :tag "When at beginning of entry" beg)))
  2759. (defcustom org-agenda-default-appointment-duration nil
  2760. "Default duration for appointments that only have a starting time.
  2761. When nil, no duration is specified in such cases.
  2762. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2763. :group 'org-agenda-line-format
  2764. :type '(choice
  2765. (integer :tag "Minutes")
  2766. (const :tag "No default duration")))
  2767. (defcustom org-agenda-remove-tags nil
  2768. "Non-nil means, remove the tags from the headline copy in the agenda.
  2769. When this is the symbol `prefix', only remove tags when
  2770. `org-agenda-prefix-format' contains a `%T' specifier."
  2771. :group 'org-agenda-line-format
  2772. :type '(choice
  2773. (const :tag "Always" t)
  2774. (const :tag "Never" nil)
  2775. (const :tag "When prefix format contains %T" prefix)))
  2776. (if (fboundp 'defvaralias)
  2777. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2778. 'org-agenda-remove-tags))
  2779. (defcustom org-agenda-tags-column -80
  2780. "Shift tags in agenda items to this column.
  2781. If this number is positive, it specifies the column. If it is negative,
  2782. it means that the tags should be flushright to that column. For example,
  2783. -80 works well for a normal 80 character screen."
  2784. :group 'org-agenda-line-format
  2785. :type 'integer)
  2786. (if (fboundp 'defvaralias)
  2787. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2788. (defcustom org-agenda-fontify-priorities t
  2789. "Non-nil means, highlight low and high priorities in agenda.
  2790. When t, the highest priority entries are bold, lowest priority italic.
  2791. This may also be an association list of priority faces. The face may be
  2792. a names face, or a list like `(:background \"Red\")'."
  2793. :group 'org-agenda-line-format
  2794. :type '(choice
  2795. (const :tag "Never" nil)
  2796. (const :tag "Defaults" t)
  2797. (repeat :tag "Specify"
  2798. (list (character :tag "Priority" :value ?A)
  2799. (sexp :tag "face")))))
  2800. (defgroup org-latex nil
  2801. "Options for embedding LaTeX code into Org-mode"
  2802. :tag "Org LaTeX"
  2803. :group 'org)
  2804. (defcustom org-format-latex-options
  2805. '(:foreground default :background default :scale 1.0
  2806. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2807. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2808. "Options for creating images from LaTeX fragments.
  2809. This is a property list with the following properties:
  2810. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2811. `default' means use the forground of the default face.
  2812. :background the background color, or \"Transparent\".
  2813. `default' means use the background of the default face.
  2814. :scale a scaling factor for the size of the images
  2815. :html-foreground, :html-background, :html-scale
  2816. The same numbers for HTML export.
  2817. :matchers a list indicating which matchers should be used to
  2818. find LaTeX fragments. Valid members of this list are:
  2819. \"begin\" find environments
  2820. \"$\" find math expressions surrounded by $...$
  2821. \"$$\" find math expressions surrounded by $$....$$
  2822. \"\\(\" find math expressions surrounded by \\(...\\)
  2823. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2824. :group 'org-latex
  2825. :type 'plist)
  2826. (defcustom org-format-latex-header "\\documentclass{article}
  2827. \\usepackage{fullpage} % do not remove
  2828. \\usepackage{amssymb}
  2829. \\usepackage[usenames]{color}
  2830. \\usepackage{amsmath}
  2831. \\usepackage{latexsym}
  2832. \\usepackage[mathscr]{eucal}
  2833. \\pagestyle{empty} % do not remove"
  2834. "The document header used for processing LaTeX fragments."
  2835. :group 'org-latex
  2836. :type 'string)
  2837. (defgroup org-export nil
  2838. "Options for exporting org-listings."
  2839. :tag "Org Export"
  2840. :group 'org)
  2841. (defgroup org-export-general nil
  2842. "General options for exporting Org-mode files."
  2843. :tag "Org Export General"
  2844. :group 'org-export)
  2845. ;; FIXME
  2846. (defvar org-export-publishing-directory nil)
  2847. (defcustom org-export-with-special-strings t
  2848. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2849. When this option is turned on, these strings will be exported as:
  2850. Org HTML LaTeX
  2851. -----+----------+--------
  2852. \\- &shy; \\-
  2853. -- &ndash; --
  2854. --- &mdash; ---
  2855. ... &hellip; \ldots
  2856. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2857. :group 'org-export-translation
  2858. :type 'boolean)
  2859. (defcustom org-export-language-setup
  2860. '(("en" "Author" "Date" "Table of Contents")
  2861. ("cs" "Autor" "Datum" "Obsah")
  2862. ("da" "Ophavsmand" "Dato" "Indhold")
  2863. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2864. ("es" "Autor" "Fecha" "\xcdndice")
  2865. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2866. ("it" "Autore" "Data" "Indice")
  2867. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2868. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2869. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2870. "Terms used in export text, translated to different languages.
  2871. Use the variable `org-export-default-language' to set the language,
  2872. or use the +OPTION lines for a per-file setting."
  2873. :group 'org-export-general
  2874. :type '(repeat
  2875. (list
  2876. (string :tag "HTML language tag")
  2877. (string :tag "Author")
  2878. (string :tag "Date")
  2879. (string :tag "Table of Contents"))))
  2880. (defcustom org-export-default-language "en"
  2881. "The default language of HTML export, as a string.
  2882. This should have an association in `org-export-language-setup'."
  2883. :group 'org-export-general
  2884. :type 'string)
  2885. (defcustom org-export-skip-text-before-1st-heading t
  2886. "Non-nil means, skip all text before the first headline when exporting.
  2887. When nil, that text is exported as well."
  2888. :group 'org-export-general
  2889. :type 'boolean)
  2890. (defcustom org-export-headline-levels 3
  2891. "The last level which is still exported as a headline.
  2892. Inferior levels will produce itemize lists when exported.
  2893. Note that a numeric prefix argument to an exporter function overrides
  2894. this setting.
  2895. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2896. :group 'org-export-general
  2897. :type 'number)
  2898. (defcustom org-export-with-section-numbers t
  2899. "Non-nil means, add section numbers to headlines when exporting.
  2900. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2901. :group 'org-export-general
  2902. :type 'boolean)
  2903. (defcustom org-export-with-toc t
  2904. "Non-nil means, create a table of contents in exported files.
  2905. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2906. When an integer, include levels up to N in the toc, this may then be
  2907. different from `org-export-headline-levels', but it will not be allowed
  2908. to be larger than the number of headline levels.
  2909. When nil, no table of contents is made.
  2910. Headlines which contain any TODO items will be marked with \"(*)\" in
  2911. ASCII export, and with red color in HTML output, if the option
  2912. `org-export-mark-todo-in-toc' is set.
  2913. In HTML output, the TOC will be clickable.
  2914. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2915. or \"toc:3\"."
  2916. :group 'org-export-general
  2917. :type '(choice
  2918. (const :tag "No Table of Contents" nil)
  2919. (const :tag "Full Table of Contents" t)
  2920. (integer :tag "TOC to level")))
  2921. (defcustom org-export-mark-todo-in-toc nil
  2922. "Non-nil means, mark TOC lines that contain any open TODO items."
  2923. :group 'org-export-general
  2924. :type 'boolean)
  2925. (defcustom org-export-preserve-breaks nil
  2926. "Non-nil means, preserve all line breaks when exporting.
  2927. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2928. export, line breaks will always be preserved, regardless of this variable.
  2929. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2930. :group 'org-export-general
  2931. :type 'boolean)
  2932. (defcustom org-export-with-archived-trees 'headline
  2933. "Whether subtrees with the ARCHIVE tag should be exported.
  2934. This can have three different values
  2935. nil Do not export, pretend this tree is not present
  2936. t Do export the entire tree
  2937. headline Only export the headline, but skip the tree below it."
  2938. :group 'org-export-general
  2939. :group 'org-archive
  2940. :type '(choice
  2941. (const :tag "not at all" nil)
  2942. (const :tag "headline only" 'headline)
  2943. (const :tag "entirely" t)))
  2944. (defcustom org-export-author-info t
  2945. "Non-nil means, insert author name and email into the exported file.
  2946. This option can also be set with the +OPTIONS line,
  2947. e.g. \"author-info:nil\"."
  2948. :group 'org-export-general
  2949. :type 'boolean)
  2950. (defcustom org-export-time-stamp-file t
  2951. "Non-nil means, insert a time stamp into the exported file.
  2952. The time stamp shows when the file was created.
  2953. This option can also be set with the +OPTIONS line,
  2954. e.g. \"timestamp:nil\"."
  2955. :group 'org-export-general
  2956. :type 'boolean)
  2957. (defcustom org-export-with-timestamps t
  2958. "If nil, do not export time stamps and associated keywords."
  2959. :group 'org-export-general
  2960. :type 'boolean)
  2961. (defcustom org-export-remove-timestamps-from-toc t
  2962. "If nil, remove timestamps from the table of contents entries."
  2963. :group 'org-export-general
  2964. :type 'boolean)
  2965. (defcustom org-export-with-tags 'not-in-toc
  2966. "If nil, do not export tags, just remove them from headlines.
  2967. If this is the symbol `not-in-toc', tags will be removed from table of
  2968. contents entries, but still be shown in the headlines of the document.
  2969. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2970. :group 'org-export-general
  2971. :type '(choice
  2972. (const :tag "Off" nil)
  2973. (const :tag "Not in TOC" not-in-toc)
  2974. (const :tag "On" t)))
  2975. (defcustom org-export-with-drawers nil
  2976. "Non-nil means, export with drawers like the property drawer.
  2977. When t, all drawers are exported. This may also be a list of
  2978. drawer names to export."
  2979. :group 'org-export-general
  2980. :type '(choice
  2981. (const :tag "All drawers" t)
  2982. (const :tag "None" nil)
  2983. (repeat :tag "Selected drawers"
  2984. (string :tag "Drawer name"))))
  2985. (defgroup org-export-translation nil
  2986. "Options for translating special ascii sequences for the export backends."
  2987. :tag "Org Export Translation"
  2988. :group 'org-export)
  2989. (defcustom org-export-with-emphasize t
  2990. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2991. If the export target supports emphasizing text, the word will be
  2992. typeset in bold, italic, or underlined, respectively. Works only for
  2993. single words, but you can say: I *really* *mean* *this*.
  2994. Not all export backends support this.
  2995. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2996. :group 'org-export-translation
  2997. :type 'boolean)
  2998. (defcustom org-export-with-footnotes t
  2999. "If nil, export [1] as a footnote marker.
  3000. Lines starting with [1] will be formatted as footnotes.
  3001. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  3002. :group 'org-export-translation
  3003. :type 'boolean)
  3004. (defcustom org-export-with-sub-superscripts t
  3005. "Non-nil means, interpret \"_\" and \"^\" for export.
  3006. When this option is turned on, you can use TeX-like syntax for sub- and
  3007. superscripts. Several characters after \"_\" or \"^\" will be
  3008. considered as a single item - so grouping with {} is normally not
  3009. needed. For example, the following things will be parsed as single
  3010. sub- or superscripts.
  3011. 10^24 or 10^tau several digits will be considered 1 item.
  3012. 10^-12 or 10^-tau a leading sign with digits or a word
  3013. x^2-y^3 will be read as x^2 - y^3, because items are
  3014. terminated by almost any nonword/nondigit char.
  3015. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  3016. Still, ambiguity is possible - so when in doubt use {} to enclose the
  3017. sub/superscript. If you set this variable to the symbol `{}',
  3018. the braces are *required* in order to trigger interpretations as
  3019. sub/superscript. This can be helpful in documents that need \"_\"
  3020. frequently in plain text.
  3021. Not all export backends support this, but HTML does.
  3022. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  3023. :group 'org-export-translation
  3024. :type '(choice
  3025. (const :tag "Always interpret" t)
  3026. (const :tag "Only with braces" {})
  3027. (const :tag "Never interpret" nil)))
  3028. (defcustom org-export-with-special-strings t
  3029. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  3030. When this option is turned on, these strings will be exported as:
  3031. \\- : &shy;
  3032. -- : &ndash;
  3033. --- : &mdash;
  3034. Not all export backends support this, but HTML does.
  3035. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  3036. :group 'org-export-translation
  3037. :type 'boolean)
  3038. (defcustom org-export-with-TeX-macros t
  3039. "Non-nil means, interpret simple TeX-like macros when exporting.
  3040. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  3041. No only real TeX macros will work here, but the standard HTML entities
  3042. for math can be used as macro names as well. For a list of supported
  3043. names in HTML export, see the constant `org-html-entities'.
  3044. Not all export backends support this.
  3045. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  3046. :group 'org-export-translation
  3047. :group 'org-export-latex
  3048. :type 'boolean)
  3049. (defcustom org-export-with-LaTeX-fragments nil
  3050. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  3051. When set, the exporter will find LaTeX environments if the \\begin line is
  3052. the first non-white thing on a line. It will also find the math delimiters
  3053. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  3054. display math.
  3055. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  3056. :group 'org-export-translation
  3057. :group 'org-export-latex
  3058. :type 'boolean)
  3059. (defcustom org-export-with-fixed-width t
  3060. "Non-nil means, lines starting with \":\" will be in fixed width font.
  3061. This can be used to have pre-formatted text, fragments of code etc. For
  3062. example:
  3063. : ;; Some Lisp examples
  3064. : (while (defc cnt)
  3065. : (ding))
  3066. will be looking just like this in also HTML. See also the QUOTE keyword.
  3067. Not all export backends support this.
  3068. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  3069. :group 'org-export-translation
  3070. :type 'boolean)
  3071. (defcustom org-match-sexp-depth 3
  3072. "Number of stacked braces for sub/superscript matching.
  3073. This has to be set before loading org.el to be effective."
  3074. :group 'org-export-translation
  3075. :type 'integer)
  3076. (defgroup org-export-tables nil
  3077. "Options for exporting tables in Org-mode."
  3078. :tag "Org Export Tables"
  3079. :group 'org-export)
  3080. (defcustom org-export-with-tables t
  3081. "If non-nil, lines starting with \"|\" define a table.
  3082. For example:
  3083. | Name | Address | Birthday |
  3084. |-------------+----------+-----------|
  3085. | Arthur Dent | England | 29.2.2100 |
  3086. Not all export backends support this.
  3087. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  3088. :group 'org-export-tables
  3089. :type 'boolean)
  3090. (defcustom org-export-highlight-first-table-line t
  3091. "Non-nil means, highlight the first table line.
  3092. In HTML export, this means use <th> instead of <td>.
  3093. In tables created with table.el, this applies to the first table line.
  3094. In Org-mode tables, all lines before the first horizontal separator
  3095. line will be formatted with <th> tags."
  3096. :group 'org-export-tables
  3097. :type 'boolean)
  3098. (defcustom org-export-table-remove-special-lines t
  3099. "Remove special lines and marking characters in calculating tables.
  3100. This removes the special marking character column from tables that are set
  3101. up for spreadsheet calculations. It also removes the entire lines
  3102. marked with `!', `_', or `^'. The lines with `$' are kept, because
  3103. the values of constants may be useful to have."
  3104. :group 'org-export-tables
  3105. :type 'boolean)
  3106. (defcustom org-export-prefer-native-exporter-for-tables nil
  3107. "Non-nil means, always export tables created with table.el natively.
  3108. Natively means, use the HTML code generator in table.el.
  3109. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  3110. the table does not use row- or column-spanning). This has the
  3111. advantage, that the automatic HTML conversions for math symbols and
  3112. sub/superscripts can be applied. Org-mode's HTML generator is also
  3113. much faster."
  3114. :group 'org-export-tables
  3115. :type 'boolean)
  3116. (defgroup org-export-ascii nil
  3117. "Options specific for ASCII export of Org-mode files."
  3118. :tag "Org Export ASCII"
  3119. :group 'org-export)
  3120. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  3121. "Characters for underlining headings in ASCII export.
  3122. In the given sequence, these characters will be used for level 1, 2, ..."
  3123. :group 'org-export-ascii
  3124. :type '(repeat character))
  3125. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  3126. "Bullet characters for headlines converted to lists in ASCII export.
  3127. The first character is used for the first lest level generated in this
  3128. way, and so on. If there are more levels than characters given here,
  3129. the list will be repeated.
  3130. Note that plain lists will keep the same bullets as the have in the
  3131. Org-mode file."
  3132. :group 'org-export-ascii
  3133. :type '(repeat character))
  3134. (defgroup org-export-xml nil
  3135. "Options specific for XML export of Org-mode files."
  3136. :tag "Org Export XML"
  3137. :group 'org-export)
  3138. (defgroup org-export-html nil
  3139. "Options specific for HTML export of Org-mode files."
  3140. :tag "Org Export HTML"
  3141. :group 'org-export)
  3142. (defcustom org-export-html-coding-system nil
  3143. ""
  3144. :group 'org-export-html
  3145. :type 'coding-system)
  3146. (defcustom org-export-html-extension "html"
  3147. "The extension for exported HTML files."
  3148. :group 'org-export-html
  3149. :type 'string)
  3150. (defcustom org-export-html-style
  3151. "<style type=\"text/css\">
  3152. html {
  3153. font-family: Times, serif;
  3154. font-size: 12pt;
  3155. }
  3156. .title { text-align: center; }
  3157. .todo { color: red; }
  3158. .done { color: green; }
  3159. .timestamp { color: grey }
  3160. .timestamp-kwd { color: CadetBlue }
  3161. .tag { background-color:lightblue; font-weight:normal }
  3162. .target { background-color: lavender; }
  3163. pre {
  3164. border: 1pt solid #AEBDCC;
  3165. background-color: #F3F5F7;
  3166. padding: 5pt;
  3167. font-family: courier, monospace;
  3168. }
  3169. table { border-collapse: collapse; }
  3170. td, th {
  3171. vertical-align: top;
  3172. <!--border: 1pt solid #ADB9CC;-->
  3173. }
  3174. </style>"
  3175. "The default style specification for exported HTML files.
  3176. Since there are different ways of setting style information, this variable
  3177. needs to contain the full HTML structure to provide a style, including the
  3178. surrounding HTML tags. The style specifications should include definitions
  3179. for new classes todo, done, title, and deadline. For example, valid values
  3180. would be:
  3181. <style type=\"text/css\">
  3182. p { font-weight: normal; color: gray; }
  3183. h1 { color: black; }
  3184. .title { text-align: center; }
  3185. .todo, .deadline { color: red; }
  3186. .done { color: green; }
  3187. </style>
  3188. or, if you want to keep the style in a file,
  3189. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3190. As the value of this option simply gets inserted into the HTML <head> header,
  3191. you can \"misuse\" it to add arbitrary text to the header."
  3192. :group 'org-export-html
  3193. :type 'string)
  3194. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3195. "Format for typesetting the document title in HTML export."
  3196. :group 'org-export-html
  3197. :type 'string)
  3198. (defcustom org-export-html-toplevel-hlevel 2
  3199. "The <H> level for level 1 headings in HTML export."
  3200. :group 'org-export-html
  3201. :type 'string)
  3202. (defcustom org-export-html-link-org-files-as-html t
  3203. "Non-nil means, make file links to `file.org' point to `file.html'.
  3204. When org-mode is exporting an org-mode file to HTML, links to
  3205. non-html files are directly put into a href tag in HTML.
  3206. However, links to other Org-mode files (recognized by the
  3207. extension `.org.) should become links to the corresponding html
  3208. file, assuming that the linked org-mode file will also be
  3209. converted to HTML.
  3210. When nil, the links still point to the plain `.org' file."
  3211. :group 'org-export-html
  3212. :type 'boolean)
  3213. (defcustom org-export-html-inline-images 'maybe
  3214. "Non-nil means, inline images into exported HTML pages.
  3215. This is done using an <img> tag. When nil, an anchor with href is used to
  3216. link to the image. If this option is `maybe', then images in links with
  3217. an empty description will be inlined, while images with a description will
  3218. be linked only."
  3219. :group 'org-export-html
  3220. :type '(choice (const :tag "Never" nil)
  3221. (const :tag "Always" t)
  3222. (const :tag "When there is no description" maybe)))
  3223. ;; FIXME: rename
  3224. (defcustom org-export-html-expand t
  3225. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3226. When nil, these tags will be exported as plain text and therefore
  3227. not be interpreted by a browser.
  3228. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3229. :group 'org-export-html
  3230. :type 'boolean)
  3231. (defcustom org-export-html-table-tag
  3232. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3233. "The HTML tag that is used to start a table.
  3234. This must be a <table> tag, but you may change the options like
  3235. borders and spacing."
  3236. :group 'org-export-html
  3237. :type 'string)
  3238. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3239. "The opening tag for table header fields.
  3240. This is customizable so that alignment options can be specified."
  3241. :group 'org-export-tables
  3242. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3243. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3244. "The opening tag for table data fields.
  3245. This is customizable so that alignment options can be specified."
  3246. :group 'org-export-tables
  3247. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3248. (defcustom org-export-html-with-timestamp nil
  3249. "If non-nil, write `org-export-html-html-helper-timestamp'
  3250. into the exported HTML text. Otherwise, the buffer will just be saved
  3251. to a file."
  3252. :group 'org-export-html
  3253. :type 'boolean)
  3254. (defcustom org-export-html-html-helper-timestamp
  3255. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3256. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3257. :group 'org-export-html
  3258. :type 'string)
  3259. (defgroup org-export-icalendar nil
  3260. "Options specific for iCalendar export of Org-mode files."
  3261. :tag "Org Export iCalendar"
  3262. :group 'org-export)
  3263. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3264. "The file name for the iCalendar file covering all agenda files.
  3265. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3266. The file name should be absolute, the file will be overwritten without warning."
  3267. :group 'org-export-icalendar
  3268. :type 'file)
  3269. (defcustom org-icalendar-include-todo nil
  3270. "Non-nil means, export to iCalendar files should also cover TODO items."
  3271. :group 'org-export-icalendar
  3272. :type '(choice
  3273. (const :tag "None" nil)
  3274. (const :tag "Unfinished" t)
  3275. (const :tag "All" all)))
  3276. (defcustom org-icalendar-include-sexps t
  3277. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3278. These are entries like in the diary, but directly in an Org-mode file."
  3279. :group 'org-export-icalendar
  3280. :type 'boolean)
  3281. (defcustom org-icalendar-include-body 100
  3282. "Amount of text below headline to be included in iCalendar export.
  3283. This is a number of characters that should maximally be included.
  3284. Properties, scheduling and clocking lines will always be removed.
  3285. The text will be inserted into the DESCRIPTION field."
  3286. :group 'org-export-icalendar
  3287. :type '(choice
  3288. (const :tag "Nothing" nil)
  3289. (const :tag "Everything" t)
  3290. (integer :tag "Max characters")))
  3291. (defcustom org-icalendar-combined-name "OrgMode"
  3292. "Calendar name for the combined iCalendar representing all agenda files."
  3293. :group 'org-export-icalendar
  3294. :type 'string)
  3295. (defgroup org-font-lock nil
  3296. "Font-lock settings for highlighting in Org-mode."
  3297. :tag "Org Font Lock"
  3298. :group 'org)
  3299. (defcustom org-level-color-stars-only nil
  3300. "Non-nil means fontify only the stars in each headline.
  3301. When nil, the entire headline is fontified.
  3302. Changing it requires restart of `font-lock-mode' to become effective
  3303. also in regions already fontified."
  3304. :group 'org-font-lock
  3305. :type 'boolean)
  3306. (defcustom org-hide-leading-stars nil
  3307. "Non-nil means, hide the first N-1 stars in a headline.
  3308. This works by using the face `org-hide' for these stars. This
  3309. face is white for a light background, and black for a dark
  3310. background. You may have to customize the face `org-hide' to
  3311. make this work.
  3312. Changing it requires restart of `font-lock-mode' to become effective
  3313. also in regions already fontified.
  3314. You may also set this on a per-file basis by adding one of the following
  3315. lines to the buffer:
  3316. #+STARTUP: hidestars
  3317. #+STARTUP: showstars"
  3318. :group 'org-font-lock
  3319. :type 'boolean)
  3320. (defcustom org-fontify-done-headline nil
  3321. "Non-nil means, change the face of a headline if it is marked DONE.
  3322. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3323. When this is non-nil, the headline after the keyword is set to the
  3324. `org-headline-done' as an additional indication."
  3325. :group 'org-font-lock
  3326. :type 'boolean)
  3327. (defcustom org-fontify-emphasized-text t
  3328. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3329. Changing this variable requires a restart of Emacs to take effect."
  3330. :group 'org-font-lock
  3331. :type 'boolean)
  3332. (defcustom org-highlight-latex-fragments-and-specials nil
  3333. "Non-nil means, fontify what is treated specially by the exporters."
  3334. :group 'org-font-lock
  3335. :type 'boolean)
  3336. (defcustom org-hide-emphasis-markers nil
  3337. "Non-nil mean font-lock should hide the emphasis marker characters."
  3338. :group 'org-font-lock
  3339. :type 'boolean)
  3340. (defvar org-emph-re nil
  3341. "Regular expression for matching emphasis.")
  3342. (defvar org-verbatim-re nil
  3343. "Regular expression for matching verbatim text.")
  3344. (defvar org-emphasis-regexp-components) ; defined just below
  3345. (defvar org-emphasis-alist) ; defined just below
  3346. (defun org-set-emph-re (var val)
  3347. "Set variable and compute the emphasis regular expression."
  3348. (set var val)
  3349. (when (and (boundp 'org-emphasis-alist)
  3350. (boundp 'org-emphasis-regexp-components)
  3351. org-emphasis-alist org-emphasis-regexp-components)
  3352. (let* ((e org-emphasis-regexp-components)
  3353. (pre (car e))
  3354. (post (nth 1 e))
  3355. (border (nth 2 e))
  3356. (body (nth 3 e))
  3357. (nl (nth 4 e))
  3358. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3359. (body1 (concat body "*?"))
  3360. (markers (mapconcat 'car org-emphasis-alist ""))
  3361. (vmarkers (mapconcat
  3362. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3363. org-emphasis-alist "")))
  3364. ;; make sure special characters appear at the right position in the class
  3365. (if (string-match "\\^" markers)
  3366. (setq markers (concat (replace-match "" t t markers) "^")))
  3367. (if (string-match "-" markers)
  3368. (setq markers (concat (replace-match "" t t markers) "-")))
  3369. (if (string-match "\\^" vmarkers)
  3370. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3371. (if (string-match "-" vmarkers)
  3372. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3373. (if (> nl 0)
  3374. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3375. (int-to-string nl) "\\}")))
  3376. ;; Make the regexp
  3377. (setq org-emph-re
  3378. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3379. "\\("
  3380. "\\([" markers "]\\)"
  3381. "\\("
  3382. "[^" border "]\\|"
  3383. "[^" border (if (and nil stacked) markers) "]"
  3384. body1
  3385. "[^" border (if (and nil stacked) markers) "]"
  3386. "\\)"
  3387. "\\3\\)"
  3388. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3389. (setq org-verbatim-re
  3390. (concat "\\([" pre "]\\|^\\)"
  3391. "\\("
  3392. "\\([" vmarkers "]\\)"
  3393. "\\("
  3394. "[^" border "]\\|"
  3395. "[^" border "]"
  3396. body1
  3397. "[^" border "]"
  3398. "\\)"
  3399. "\\3\\)"
  3400. "\\([" post "]\\|$\\)")))))
  3401. (defcustom org-emphasis-regexp-components
  3402. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3403. "Components used to build the regular expression for emphasis.
  3404. This is a list with 6 entries. Terminology: In an emphasis string
  3405. like \" *strong word* \", we call the initial space PREMATCH, the final
  3406. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3407. and \"trong wor\" is the body. The different components in this variable
  3408. specify what is allowed/forbidden in each part:
  3409. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3410. post Chars allowed as postmatch. End of line will be allowed too.
  3411. border The chars *forbidden* as border characters.
  3412. body-regexp A regexp like \".\" to match a body character. Don't use
  3413. non-shy groups here, and don't allow newline here.
  3414. newline The maximum number of newlines allowed in an emphasis exp.
  3415. Use customize to modify this, or restart Emacs after changing it."
  3416. :group 'org-font-lock
  3417. :set 'org-set-emph-re
  3418. :type '(list
  3419. (sexp :tag "Allowed chars in pre ")
  3420. (sexp :tag "Allowed chars in post ")
  3421. (sexp :tag "Forbidden chars in border ")
  3422. (sexp :tag "Regexp for body ")
  3423. (integer :tag "number of newlines allowed")
  3424. (option (boolean :tag "Stacking (DISABLED) "))))
  3425. (defcustom org-emphasis-alist
  3426. '(("*" bold "<b>" "</b>")
  3427. ("/" italic "<i>" "</i>")
  3428. ("_" underline "<u>" "</u>")
  3429. ("=" org-code "<code>" "</code>" verbatim)
  3430. ("~" org-verbatim "" "" verbatim)
  3431. ("+" (:strike-through t) "<del>" "</del>")
  3432. )
  3433. "Special syntax for emphasized text.
  3434. Text starting and ending with a special character will be emphasized, for
  3435. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3436. characters, the face to be used by font-lock for highlighting in Org-mode
  3437. Emacs buffers, and the HTML tags to be used for this.
  3438. Use customize to modify this, or restart Emacs after changing it."
  3439. :group 'org-font-lock
  3440. :set 'org-set-emph-re
  3441. :type '(repeat
  3442. (list
  3443. (string :tag "Marker character")
  3444. (choice
  3445. (face :tag "Font-lock-face")
  3446. (plist :tag "Face property list"))
  3447. (string :tag "HTML start tag")
  3448. (string :tag "HTML end tag")
  3449. (option (const verbatim)))))
  3450. ;;; The faces
  3451. (defgroup org-faces nil
  3452. "Faces in Org-mode."
  3453. :tag "Org Faces"
  3454. :group 'org-font-lock)
  3455. (defun org-compatible-face (inherits specs)
  3456. "Make a compatible face specification.
  3457. If INHERITS is an existing face and if the Emacs version supports it,
  3458. just inherit the face. If not, use SPECS to define the face.
  3459. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3460. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3461. to the top of the list. The `min-colors' attribute will be removed from
  3462. any other entries, and any resulting duplicates will be removed entirely."
  3463. (cond
  3464. ((and inherits (facep inherits)
  3465. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3466. ;; In Emacs 23, we use inheritance where possible.
  3467. ;; We only do this in Emacs 23, because only there the outline
  3468. ;; faces have been changed to the original org-mode-level-faces.
  3469. (list (list t :inherit inherits)))
  3470. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3471. ;; These do not understand the `min-colors' attribute.
  3472. (let (r e a)
  3473. (while (setq e (pop specs))
  3474. (cond
  3475. ((memq (car e) '(t default)) (push e r))
  3476. ((setq a (member '(min-colors 8) (car e)))
  3477. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3478. (cdr e)))))
  3479. ((setq a (assq 'min-colors (car e)))
  3480. (setq e (cons (delq a (car e)) (cdr e)))
  3481. (or (assoc (car e) r) (push e r)))
  3482. (t (or (assoc (car e) r) (push e r)))))
  3483. (nreverse r)))
  3484. (t specs)))
  3485. (put 'org-compatible-face 'lisp-indent-function 1)
  3486. (defface org-hide
  3487. '((((background light)) (:foreground "white"))
  3488. (((background dark)) (:foreground "black")))
  3489. "Face used to hide leading stars in headlines.
  3490. The forground color of this face should be equal to the background
  3491. color of the frame."
  3492. :group 'org-faces)
  3493. (defface org-level-1 ;; font-lock-function-name-face
  3494. (org-compatible-face 'outline-1
  3495. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3496. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3497. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3498. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3499. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3500. (t (:bold t))))
  3501. "Face used for level 1 headlines."
  3502. :group 'org-faces)
  3503. (defface org-level-2 ;; font-lock-variable-name-face
  3504. (org-compatible-face 'outline-2
  3505. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3506. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3507. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3508. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3509. (t (:bold t))))
  3510. "Face used for level 2 headlines."
  3511. :group 'org-faces)
  3512. (defface org-level-3 ;; font-lock-keyword-face
  3513. (org-compatible-face 'outline-3
  3514. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3515. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3516. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3517. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3518. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3519. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3520. (t (:bold t))))
  3521. "Face used for level 3 headlines."
  3522. :group 'org-faces)
  3523. (defface org-level-4 ;; font-lock-comment-face
  3524. (org-compatible-face 'outline-4
  3525. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3526. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3527. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3528. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3529. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3530. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3531. (t (:bold t))))
  3532. "Face used for level 4 headlines."
  3533. :group 'org-faces)
  3534. (defface org-level-5 ;; font-lock-type-face
  3535. (org-compatible-face 'outline-5
  3536. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3537. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3538. (((class color) (min-colors 8)) (:foreground "green"))))
  3539. "Face used for level 5 headlines."
  3540. :group 'org-faces)
  3541. (defface org-level-6 ;; font-lock-constant-face
  3542. (org-compatible-face 'outline-6
  3543. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3544. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3545. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3546. "Face used for level 6 headlines."
  3547. :group 'org-faces)
  3548. (defface org-level-7 ;; font-lock-builtin-face
  3549. (org-compatible-face 'outline-7
  3550. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3551. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3552. (((class color) (min-colors 8)) (:foreground "blue"))))
  3553. "Face used for level 7 headlines."
  3554. :group 'org-faces)
  3555. (defface org-level-8 ;; font-lock-string-face
  3556. (org-compatible-face 'outline-8
  3557. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3558. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3559. (((class color) (min-colors 8)) (:foreground "green"))))
  3560. "Face used for level 8 headlines."
  3561. :group 'org-faces)
  3562. (defface org-special-keyword ;; font-lock-string-face
  3563. (org-compatible-face nil
  3564. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3565. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3566. (t (:italic t))))
  3567. "Face used for special keywords."
  3568. :group 'org-faces)
  3569. (defface org-drawer ;; font-lock-function-name-face
  3570. (org-compatible-face nil
  3571. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3572. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3573. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3574. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3575. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3576. (t (:bold t))))
  3577. "Face used for drawers."
  3578. :group 'org-faces)
  3579. (defface org-property-value nil
  3580. "Face used for the value of a property."
  3581. :group 'org-faces)
  3582. (defface org-column
  3583. (org-compatible-face nil
  3584. '((((class color) (min-colors 16) (background light))
  3585. (:background "grey90"))
  3586. (((class color) (min-colors 16) (background dark))
  3587. (:background "grey30"))
  3588. (((class color) (min-colors 8))
  3589. (:background "cyan" :foreground "black"))
  3590. (t (:inverse-video t))))
  3591. "Face for column display of entry properties."
  3592. :group 'org-faces)
  3593. (when (fboundp 'set-face-attribute)
  3594. ;; Make sure that a fixed-width face is used when we have a column table.
  3595. (set-face-attribute 'org-column nil
  3596. :height (face-attribute 'default :height)
  3597. :family (face-attribute 'default :family)))
  3598. (defface org-warning
  3599. (org-compatible-face 'font-lock-warning-face
  3600. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3601. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3602. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3603. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3604. (t (:bold t))))
  3605. "Face for deadlines and TODO keywords."
  3606. :group 'org-faces)
  3607. (defface org-archived ; similar to shadow
  3608. (org-compatible-face 'shadow
  3609. '((((class color grayscale) (min-colors 88) (background light))
  3610. (:foreground "grey50"))
  3611. (((class color grayscale) (min-colors 88) (background dark))
  3612. (:foreground "grey70"))
  3613. (((class color) (min-colors 8) (background light))
  3614. (:foreground "green"))
  3615. (((class color) (min-colors 8) (background dark))
  3616. (:foreground "yellow"))))
  3617. "Face for headline with the ARCHIVE tag."
  3618. :group 'org-faces)
  3619. (defface org-link
  3620. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3621. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3622. (t (:underline t)))
  3623. "Face for links."
  3624. :group 'org-faces)
  3625. (defface org-ellipsis
  3626. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3627. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3628. (t (:strike-through t)))
  3629. "Face for the ellipsis in folded text."
  3630. :group 'org-faces)
  3631. (defface org-target
  3632. '((((class color) (background light)) (:underline t))
  3633. (((class color) (background dark)) (:underline t))
  3634. (t (:underline t)))
  3635. "Face for links."
  3636. :group 'org-faces)
  3637. (defface org-date
  3638. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3639. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3640. (t (:underline t)))
  3641. "Face for links."
  3642. :group 'org-faces)
  3643. (defface org-sexp-date
  3644. '((((class color) (background light)) (:foreground "Purple"))
  3645. (((class color) (background dark)) (:foreground "Cyan"))
  3646. (t (:underline t)))
  3647. "Face for links."
  3648. :group 'org-faces)
  3649. (defface org-tag
  3650. '((t (:bold t)))
  3651. "Face for tags."
  3652. :group 'org-faces)
  3653. (defface org-todo ; font-lock-warning-face
  3654. (org-compatible-face nil
  3655. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3656. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3657. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3658. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3659. (t (:inverse-video t :bold t))))
  3660. "Face for TODO keywords."
  3661. :group 'org-faces)
  3662. (defface org-done ;; font-lock-type-face
  3663. (org-compatible-face nil
  3664. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3665. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3666. (((class color) (min-colors 8)) (:foreground "green"))
  3667. (t (:bold t))))
  3668. "Face used for todo keywords that indicate DONE items."
  3669. :group 'org-faces)
  3670. (defface org-headline-done ;; font-lock-string-face
  3671. (org-compatible-face nil
  3672. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3673. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3674. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3675. "Face used to indicate that a headline is DONE.
  3676. This face is only used if `org-fontify-done-headline' is set. If applies
  3677. to the part of the headline after the DONE keyword."
  3678. :group 'org-faces)
  3679. (defcustom org-todo-keyword-faces nil
  3680. "Faces for specific TODO keywords.
  3681. This is a list of cons cells, with TODO keywords in the car
  3682. and faces in the cdr. The face can be a symbol, or a property
  3683. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3684. :group 'org-faces
  3685. :group 'org-todo
  3686. :type '(repeat
  3687. (cons
  3688. (string :tag "keyword")
  3689. (sexp :tag "face"))))
  3690. (defface org-table ;; font-lock-function-name-face
  3691. (org-compatible-face nil
  3692. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3693. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3694. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3695. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3696. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3697. (((class color) (min-colors 8) (background dark)))))
  3698. "Face used for tables."
  3699. :group 'org-faces)
  3700. (defface org-formula
  3701. (org-compatible-face nil
  3702. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3703. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3704. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3705. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3706. (t (:bold t :italic t))))
  3707. "Face for formulas."
  3708. :group 'org-faces)
  3709. (defface org-code
  3710. (org-compatible-face nil
  3711. '((((class color grayscale) (min-colors 88) (background light))
  3712. (:foreground "grey50"))
  3713. (((class color grayscale) (min-colors 88) (background dark))
  3714. (:foreground "grey70"))
  3715. (((class color) (min-colors 8) (background light))
  3716. (:foreground "green"))
  3717. (((class color) (min-colors 8) (background dark))
  3718. (:foreground "yellow"))))
  3719. "Face for fixed-with text like code snippets."
  3720. :group 'org-faces
  3721. :version "22.1")
  3722. (defface org-verbatim
  3723. (org-compatible-face nil
  3724. '((((class color grayscale) (min-colors 88) (background light))
  3725. (:foreground "grey50" :underline t))
  3726. (((class color grayscale) (min-colors 88) (background dark))
  3727. (:foreground "grey70" :underline t))
  3728. (((class color) (min-colors 8) (background light))
  3729. (:foreground "green" :underline t))
  3730. (((class color) (min-colors 8) (background dark))
  3731. (:foreground "yellow" :underline t))))
  3732. "Face for fixed-with text like code snippets."
  3733. :group 'org-faces
  3734. :version "22.1")
  3735. (defface org-agenda-structure ;; font-lock-function-name-face
  3736. (org-compatible-face nil
  3737. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3738. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3739. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3740. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3741. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3742. (t (:bold t))))
  3743. "Face used in agenda for captions and dates."
  3744. :group 'org-faces)
  3745. (defface org-scheduled-today
  3746. (org-compatible-face nil
  3747. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3748. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3749. (((class color) (min-colors 8)) (:foreground "green"))
  3750. (t (:bold t :italic t))))
  3751. "Face for items scheduled for a certain day."
  3752. :group 'org-faces)
  3753. (defface org-scheduled-previously
  3754. (org-compatible-face nil
  3755. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3756. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3757. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3758. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3759. (t (:bold t))))
  3760. "Face for items scheduled previously, and not yet done."
  3761. :group 'org-faces)
  3762. (defface org-upcoming-deadline
  3763. (org-compatible-face nil
  3764. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3765. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3766. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3767. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3768. (t (:bold t))))
  3769. "Face for items scheduled previously, and not yet done."
  3770. :group 'org-faces)
  3771. (defcustom org-agenda-deadline-faces
  3772. '((1.0 . org-warning)
  3773. (0.5 . org-upcoming-deadline)
  3774. (0.0 . default))
  3775. "Faces for showing deadlines in the agenda.
  3776. This is a list of cons cells. The cdr of each cell is a face to be used,
  3777. and it can also just be like '(:foreground \"yellow\").
  3778. Each car is a fraction of the head-warning time that must have passed for
  3779. this the face in the cdr to be used for display. The numbers must be
  3780. given in descending order. The head-warning time is normally taken
  3781. from `org-deadline-warning-days', but can also be specified in the deadline
  3782. timestamp itself, like this:
  3783. DEADLINE: <2007-08-13 Mon -8d>
  3784. You may use d for days, w for weeks, m for months and y for years. Months
  3785. and years will only be treated in an approximate fashion (30.4 days for a
  3786. month and 365.24 days for a year)."
  3787. :group 'org-faces
  3788. :group 'org-agenda-daily/weekly
  3789. :type '(repeat
  3790. (cons
  3791. (number :tag "Fraction of head-warning time passed")
  3792. (sexp :tag "Face"))))
  3793. ;; FIXME: this is not a good face yet.
  3794. (defface org-agenda-restriction-lock
  3795. (org-compatible-face nil
  3796. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3797. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3798. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3799. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3800. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3801. (t (:inverse-video t))))
  3802. "Face for showing the agenda restriction lock."
  3803. :group 'org-faces)
  3804. (defface org-time-grid ;; font-lock-variable-name-face
  3805. (org-compatible-face nil
  3806. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3807. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3808. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3809. "Face used for time grids."
  3810. :group 'org-faces)
  3811. (defconst org-level-faces
  3812. '(org-level-1 org-level-2 org-level-3 org-level-4
  3813. org-level-5 org-level-6 org-level-7 org-level-8
  3814. ))
  3815. (defcustom org-n-level-faces (length org-level-faces)
  3816. "The number of different faces to be used for headlines.
  3817. Org-mode defines 8 different headline faces, so this can be at most 8.
  3818. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3819. :type 'number
  3820. :group 'org-faces)
  3821. ;;; Functions and variables from ther packages
  3822. ;; Declared here to avoid compiler warnings
  3823. (eval-and-compile
  3824. (unless (fboundp 'declare-function)
  3825. (defmacro declare-function (fn file &optional arglist fileonly))))
  3826. ;; XEmacs only
  3827. (defvar outline-mode-menu-heading)
  3828. (defvar outline-mode-menu-show)
  3829. (defvar outline-mode-menu-hide)
  3830. (defvar zmacs-regions) ; XEmacs regions
  3831. ;; Emacs only
  3832. (defvar mark-active)
  3833. ;; Various packages
  3834. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3835. (declare-function add-to-diary-list "diary-lib"
  3836. (date string specifier &optional marker globcolor literal))
  3837. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3838. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3839. (declare-function bibtex-generate-autokey "bibtex" ())
  3840. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3841. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3842. (defvar calc-embedded-close-formula)
  3843. (defvar calc-embedded-open-formula)
  3844. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3845. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3846. (declare-function calendar-check-holidays "holidays" (date))
  3847. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3848. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3849. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3850. (declare-function calendar-forward-day "cal-move" (arg))
  3851. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3852. (declare-function calendar-goto-date "cal-move" (date))
  3853. (declare-function calendar-goto-today "cal-move" ())
  3854. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3855. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3856. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3857. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3858. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3859. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3860. (defvar calendar-mode-map)
  3861. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3862. (declare-function cdlatex-tab "ext:cdlatex" ())
  3863. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3864. (defvar font-lock-unfontify-region-function)
  3865. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3866. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3867. (declare-function parse-time-string "parse-time" (string))
  3868. (declare-function remember "remember" (&optional initial))
  3869. (declare-function remember-buffer-desc "remember" ())
  3870. (declare-function remember-finalize "remember" ())
  3871. (defvar remember-save-after-remembering)
  3872. (defvar remember-data-file)
  3873. (defvar remember-register)
  3874. (defvar remember-buffer)
  3875. (defvar remember-handler-functions)
  3876. (defvar remember-annotation-functions)
  3877. (defvar texmathp-why)
  3878. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3879. (defvar w3m-current-url)
  3880. (defvar w3m-current-title)
  3881. (defvar org-latex-regexps)
  3882. (defvar constants-unit-system)
  3883. ;;; Variables for pre-computed regular expressions, all buffer local
  3884. (defvar org-drawer-regexp nil
  3885. "Matches first line of a hidden block.")
  3886. (make-variable-buffer-local 'org-drawer-regexp)
  3887. (defvar org-todo-regexp nil
  3888. "Matches any of the TODO state keywords.")
  3889. (make-variable-buffer-local 'org-todo-regexp)
  3890. (defvar org-not-done-regexp nil
  3891. "Matches any of the TODO state keywords except the last one.")
  3892. (make-variable-buffer-local 'org-not-done-regexp)
  3893. (defvar org-todo-line-regexp nil
  3894. "Matches a headline and puts TODO state into group 2 if present.")
  3895. (make-variable-buffer-local 'org-todo-line-regexp)
  3896. (defvar org-complex-heading-regexp nil
  3897. "Matches a headline and puts everything into groups:
  3898. group 1: the stars
  3899. group 2: The todo keyword, maybe
  3900. group 3: Priority cookie
  3901. group 4: True headline
  3902. group 5: Tags")
  3903. (make-variable-buffer-local 'org-complex-heading-regexp)
  3904. (defvar org-todo-line-tags-regexp nil
  3905. "Matches a headline and puts TODO state into group 2 if present.
  3906. Also put tags into group 4 if tags are present.")
  3907. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3908. (defvar org-nl-done-regexp nil
  3909. "Matches newline followed by a headline with the DONE keyword.")
  3910. (make-variable-buffer-local 'org-nl-done-regexp)
  3911. (defvar org-looking-at-done-regexp nil
  3912. "Matches the DONE keyword a point.")
  3913. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3914. (defvar org-ds-keyword-length 12
  3915. "Maximum length of the Deadline and SCHEDULED keywords.")
  3916. (make-variable-buffer-local 'org-ds-keyword-length)
  3917. (defvar org-deadline-regexp nil
  3918. "Matches the DEADLINE keyword.")
  3919. (make-variable-buffer-local 'org-deadline-regexp)
  3920. (defvar org-deadline-time-regexp nil
  3921. "Matches the DEADLINE keyword together with a time stamp.")
  3922. (make-variable-buffer-local 'org-deadline-time-regexp)
  3923. (defvar org-deadline-line-regexp nil
  3924. "Matches the DEADLINE keyword and the rest of the line.")
  3925. (make-variable-buffer-local 'org-deadline-line-regexp)
  3926. (defvar org-scheduled-regexp nil
  3927. "Matches the SCHEDULED keyword.")
  3928. (make-variable-buffer-local 'org-scheduled-regexp)
  3929. (defvar org-scheduled-time-regexp nil
  3930. "Matches the SCHEDULED keyword together with a time stamp.")
  3931. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3932. (defvar org-closed-time-regexp nil
  3933. "Matches the CLOSED keyword together with a time stamp.")
  3934. (make-variable-buffer-local 'org-closed-time-regexp)
  3935. (defvar org-keyword-time-regexp nil
  3936. "Matches any of the 4 keywords, together with the time stamp.")
  3937. (make-variable-buffer-local 'org-keyword-time-regexp)
  3938. (defvar org-keyword-time-not-clock-regexp nil
  3939. "Matches any of the 3 keywords, together with the time stamp.")
  3940. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3941. (defvar org-maybe-keyword-time-regexp nil
  3942. "Matches a timestamp, possibly preceeded by a keyword.")
  3943. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3944. (defvar org-planning-or-clock-line-re nil
  3945. "Matches a line with planning or clock info.")
  3946. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3947. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3948. rear-nonsticky t mouse-map t fontified t)
  3949. "Properties to remove when a string without properties is wanted.")
  3950. (defsubst org-match-string-no-properties (num &optional string)
  3951. (if (featurep 'xemacs)
  3952. (let ((s (match-string num string)))
  3953. (remove-text-properties 0 (length s) org-rm-props s)
  3954. s)
  3955. (match-string-no-properties num string)))
  3956. (defsubst org-no-properties (s)
  3957. (if (fboundp 'set-text-properties)
  3958. (set-text-properties 0 (length s) nil s)
  3959. (remove-text-properties 0 (length s) org-rm-props s))
  3960. s)
  3961. (defsubst org-get-alist-option (option key)
  3962. (cond ((eq key t) t)
  3963. ((eq option t) t)
  3964. ((assoc key option) (cdr (assoc key option)))
  3965. (t (cdr (assq 'default option)))))
  3966. (defsubst org-inhibit-invisibility ()
  3967. "Modified `buffer-invisibility-spec' for Emacs 21.
  3968. Some ops with invisible text do not work correctly on Emacs 21. For these
  3969. we turn off invisibility temporarily. Use this in a `let' form."
  3970. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3971. (defsubst org-set-local (var value)
  3972. "Make VAR local in current buffer and set it to VALUE."
  3973. (set (make-variable-buffer-local var) value))
  3974. (defsubst org-mode-p ()
  3975. "Check if the current buffer is in Org-mode."
  3976. (eq major-mode 'org-mode))
  3977. (defsubst org-last (list)
  3978. "Return the last element of LIST."
  3979. (car (last list)))
  3980. (defun org-let (list &rest body)
  3981. (eval (cons 'let (cons list body))))
  3982. (put 'org-let 'lisp-indent-function 1)
  3983. (defun org-let2 (list1 list2 &rest body)
  3984. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3985. (put 'org-let2 'lisp-indent-function 2)
  3986. (defconst org-startup-options
  3987. '(("fold" org-startup-folded t)
  3988. ("overview" org-startup-folded t)
  3989. ("nofold" org-startup-folded nil)
  3990. ("showall" org-startup-folded nil)
  3991. ("content" org-startup-folded content)
  3992. ("hidestars" org-hide-leading-stars t)
  3993. ("showstars" org-hide-leading-stars nil)
  3994. ("odd" org-odd-levels-only t)
  3995. ("oddeven" org-odd-levels-only nil)
  3996. ("align" org-startup-align-all-tables t)
  3997. ("noalign" org-startup-align-all-tables nil)
  3998. ("customtime" org-display-custom-times t)
  3999. ("logdone" org-log-done time)
  4000. ("lognotedone" org-log-done note)
  4001. ("nologdone" org-log-done nil)
  4002. ("lognoteclock-out" org-log-note-clock-out t)
  4003. ("nolognoteclock-out" org-log-note-clock-out nil)
  4004. ("logrepeat" org-log-repeat state)
  4005. ("lognoterepeat" org-log-repeat note)
  4006. ("nologrepeat" org-log-repeat nil)
  4007. ("constcgs" constants-unit-system cgs)
  4008. ("constSI" constants-unit-system SI))
  4009. "Variable associated with STARTUP options for org-mode.
  4010. Each element is a list of three items: The startup options as written
  4011. in the #+STARTUP line, the corresponding variable, and the value to
  4012. set this variable to if the option is found. An optional forth element PUSH
  4013. means to push this value onto the list in the variable.")
  4014. (defun org-set-regexps-and-options ()
  4015. "Precompute regular expressions for current buffer."
  4016. (when (org-mode-p)
  4017. (org-set-local 'org-todo-kwd-alist nil)
  4018. (org-set-local 'org-todo-key-alist nil)
  4019. (org-set-local 'org-todo-key-trigger nil)
  4020. (org-set-local 'org-todo-keywords-1 nil)
  4021. (org-set-local 'org-done-keywords nil)
  4022. (org-set-local 'org-todo-heads nil)
  4023. (org-set-local 'org-todo-sets nil)
  4024. (org-set-local 'org-todo-log-states nil)
  4025. (let ((re (org-make-options-regexp
  4026. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  4027. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  4028. "CONSTANTS" "PROPERTY" "DRAWERS")))
  4029. (splitre "[ \t]+")
  4030. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4031. tail sep kws1 prio props drawers)
  4032. (save-excursion
  4033. (save-restriction
  4034. (widen)
  4035. (goto-char (point-min))
  4036. (while (re-search-forward re nil t)
  4037. (setq key (match-string 1) value (org-match-string-no-properties 2))
  4038. (cond
  4039. ((equal key "CATEGORY")
  4040. (if (string-match "[ \t]+$" value)
  4041. (setq value (replace-match "" t t value)))
  4042. (setq cat value))
  4043. ((member key '("SEQ_TODO" "TODO"))
  4044. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4045. ((equal key "TYP_TODO")
  4046. (push (cons 'type (org-split-string value splitre)) kwds))
  4047. ((equal key "TAGS")
  4048. (setq tags (append tags (org-split-string value splitre))))
  4049. ((equal key "COLUMNS")
  4050. (org-set-local 'org-columns-default-format value))
  4051. ((equal key "LINK")
  4052. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4053. (push (cons (match-string 1 value)
  4054. (org-trim (match-string 2 value)))
  4055. links)))
  4056. ((equal key "PRIORITIES")
  4057. (setq prio (org-split-string value " +")))
  4058. ((equal key "PROPERTY")
  4059. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4060. (push (cons (match-string 1 value) (match-string 2 value))
  4061. props)))
  4062. ((equal key "DRAWERS")
  4063. (setq drawers (org-split-string value splitre)))
  4064. ((equal key "CONSTANTS")
  4065. (setq const (append const (org-split-string value splitre))))
  4066. ((equal key "STARTUP")
  4067. (let ((opts (org-split-string value splitre))
  4068. l var val)
  4069. (while (setq l (pop opts))
  4070. (when (setq l (assoc l org-startup-options))
  4071. (setq var (nth 1 l) val (nth 2 l))
  4072. (if (not (nth 3 l))
  4073. (set (make-local-variable var) val)
  4074. (if (not (listp (symbol-value var)))
  4075. (set (make-local-variable var) nil))
  4076. (set (make-local-variable var) (symbol-value var))
  4077. (add-to-list var val))))))
  4078. ((equal key "ARCHIVE")
  4079. (string-match " *$" value)
  4080. (setq arch (replace-match "" t t value))
  4081. (remove-text-properties 0 (length arch)
  4082. '(face t fontified t) arch)))
  4083. )))
  4084. (when cat
  4085. (org-set-local 'org-category (intern cat))
  4086. (push (cons "CATEGORY" cat) props))
  4087. (when prio
  4088. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4089. (setq prio (mapcar 'string-to-char prio))
  4090. (org-set-local 'org-highest-priority (nth 0 prio))
  4091. (org-set-local 'org-lowest-priority (nth 1 prio))
  4092. (org-set-local 'org-default-priority (nth 2 prio)))
  4093. (and props (org-set-local 'org-local-properties (nreverse props)))
  4094. (and drawers (org-set-local 'org-drawers drawers))
  4095. (and arch (org-set-local 'org-archive-location arch))
  4096. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4097. ;; Process the TODO keywords
  4098. (unless kwds
  4099. ;; Use the global values as if they had been given locally.
  4100. (setq kwds (default-value 'org-todo-keywords))
  4101. (if (stringp (car kwds))
  4102. (setq kwds (list (cons org-todo-interpretation
  4103. (default-value 'org-todo-keywords)))))
  4104. (setq kwds (reverse kwds)))
  4105. (setq kwds (nreverse kwds))
  4106. (let (inter kws kw)
  4107. (while (setq kws (pop kwds))
  4108. (setq inter (pop kws) sep (member "|" kws)
  4109. kws0 (delete "|" (copy-sequence kws))
  4110. kwsa nil
  4111. kws1 (mapcar
  4112. (lambda (x)
  4113. ;; 1 2
  4114. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4115. (progn
  4116. (setq kw (match-string 1 x)
  4117. key (and (match-end 2) (match-string 2 x))
  4118. log (org-extract-log-state-settings x))
  4119. (push (cons kw (and key (string-to-char key))) kwsa)
  4120. (and log (push log org-todo-log-states))
  4121. kw)
  4122. (error "Invalid TODO keyword %s" x)))
  4123. kws0)
  4124. kwsa (if kwsa (append '((:startgroup))
  4125. (nreverse kwsa)
  4126. '((:endgroup))))
  4127. hw (car kws1)
  4128. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4129. tail (list inter hw (car dws) (org-last dws)))
  4130. (add-to-list 'org-todo-heads hw 'append)
  4131. (push kws1 org-todo-sets)
  4132. (setq org-done-keywords (append org-done-keywords dws nil))
  4133. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4134. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4135. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4136. (setq org-todo-sets (nreverse org-todo-sets)
  4137. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4138. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4139. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4140. ;; Process the constants
  4141. (when const
  4142. (let (e cst)
  4143. (while (setq e (pop const))
  4144. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4145. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4146. (setq org-table-formula-constants-local cst)))
  4147. ;; Process the tags.
  4148. (when tags
  4149. (let (e tgs)
  4150. (while (setq e (pop tags))
  4151. (cond
  4152. ((equal e "{") (push '(:startgroup) tgs))
  4153. ((equal e "}") (push '(:endgroup) tgs))
  4154. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4155. (push (cons (match-string 1 e)
  4156. (string-to-char (match-string 2 e)))
  4157. tgs))
  4158. (t (push (list e) tgs))))
  4159. (org-set-local 'org-tag-alist nil)
  4160. (while (setq e (pop tgs))
  4161. (or (and (stringp (car e))
  4162. (assoc (car e) org-tag-alist))
  4163. (push e org-tag-alist))))))
  4164. ;; Compute the regular expressions and other local variables
  4165. (if (not org-done-keywords)
  4166. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4167. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4168. (length org-scheduled-string)))
  4169. org-drawer-regexp
  4170. (concat "^[ \t]*:\\("
  4171. (mapconcat 'regexp-quote org-drawers "\\|")
  4172. "\\):[ \t]*$")
  4173. org-not-done-keywords
  4174. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4175. org-todo-regexp
  4176. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4177. "\\|") "\\)\\>")
  4178. org-not-done-regexp
  4179. (concat "\\<\\("
  4180. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4181. "\\)\\>")
  4182. org-todo-line-regexp
  4183. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4184. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4185. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4186. org-complex-heading-regexp
  4187. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4188. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4189. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4190. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4191. org-nl-done-regexp
  4192. (concat "\n\\*+[ \t]+"
  4193. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4194. "\\)" "\\>")
  4195. org-todo-line-tags-regexp
  4196. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4197. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4198. (org-re
  4199. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4200. org-looking-at-done-regexp
  4201. (concat "^" "\\(?:"
  4202. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4203. "\\>")
  4204. org-deadline-regexp (concat "\\<" org-deadline-string)
  4205. org-deadline-time-regexp
  4206. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4207. org-deadline-line-regexp
  4208. (concat "\\<\\(" org-deadline-string "\\).*")
  4209. org-scheduled-regexp
  4210. (concat "\\<" org-scheduled-string)
  4211. org-scheduled-time-regexp
  4212. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4213. org-closed-time-regexp
  4214. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4215. org-keyword-time-regexp
  4216. (concat "\\<\\(" org-scheduled-string
  4217. "\\|" org-deadline-string
  4218. "\\|" org-closed-string
  4219. "\\|" org-clock-string "\\)"
  4220. " *[[<]\\([^]>]+\\)[]>]")
  4221. org-keyword-time-not-clock-regexp
  4222. (concat "\\<\\(" org-scheduled-string
  4223. "\\|" org-deadline-string
  4224. "\\|" org-closed-string
  4225. "\\)"
  4226. " *[[<]\\([^]>]+\\)[]>]")
  4227. org-maybe-keyword-time-regexp
  4228. (concat "\\(\\<\\(" org-scheduled-string
  4229. "\\|" org-deadline-string
  4230. "\\|" org-closed-string
  4231. "\\|" org-clock-string "\\)\\)?"
  4232. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4233. org-planning-or-clock-line-re
  4234. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4235. "\\|" org-deadline-string
  4236. "\\|" org-closed-string "\\|" org-clock-string
  4237. "\\)\\>\\)")
  4238. )
  4239. (org-compute-latex-and-specials-regexp)
  4240. (org-set-font-lock-defaults)))
  4241. (defun org-extract-log-state-settings (x)
  4242. "Extract the log state setting from a TODO keyword string.
  4243. This will extract info from a string like \"WAIT(w@/!)\"."
  4244. (let (kw key log1 log2)
  4245. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4246. (setq kw (match-string 1 x)
  4247. key (and (match-end 2) (match-string 2 x))
  4248. log1 (and (match-end 3) (match-string 3 x))
  4249. log2 (and (match-end 4) (match-string 4 x)))
  4250. (and (or log1 log2)
  4251. (list kw
  4252. (and log1 (if (equal log1 "!") 'time 'note))
  4253. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4254. (defun org-remove-keyword-keys (list)
  4255. "Remove a pair of parenthesis at the end of each string in LIST."
  4256. (mapcar (lambda (x)
  4257. (if (string-match "(.*)$" x)
  4258. (substring x 0 (match-beginning 0))
  4259. x))
  4260. list))
  4261. ;; FIXME: this could be done much better, using second characters etc.
  4262. (defun org-assign-fast-keys (alist)
  4263. "Assign fast keys to a keyword-key alist.
  4264. Respect keys that are already there."
  4265. (let (new e k c c1 c2 (char ?a))
  4266. (while (setq e (pop alist))
  4267. (cond
  4268. ((equal e '(:startgroup)) (push e new))
  4269. ((equal e '(:endgroup)) (push e new))
  4270. (t
  4271. (setq k (car e) c2 nil)
  4272. (if (cdr e)
  4273. (setq c (cdr e))
  4274. ;; automatically assign a character.
  4275. (setq c1 (string-to-char
  4276. (downcase (substring
  4277. k (if (= (string-to-char k) ?@) 1 0)))))
  4278. (if (or (rassoc c1 new) (rassoc c1 alist))
  4279. (while (or (rassoc char new) (rassoc char alist))
  4280. (setq char (1+ char)))
  4281. (setq c2 c1))
  4282. (setq c (or c2 char)))
  4283. (push (cons k c) new))))
  4284. (nreverse new)))
  4285. ;;; Some variables ujsed in various places
  4286. (defvar org-window-configuration nil
  4287. "Used in various places to store a window configuration.")
  4288. (defvar org-finish-function nil
  4289. "Function to be called when `C-c C-c' is used.
  4290. This is for getting out of special buffers like remember.")
  4291. ;; FIXME: Occasionally check by commenting these, to make sure
  4292. ;; no other functions uses these, forgetting to let-bind them.
  4293. (defvar entry)
  4294. (defvar state)
  4295. (defvar last-state)
  4296. (defvar date)
  4297. (defvar description)
  4298. ;; Defined somewhere in this file, but used before definition.
  4299. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4300. (defvar org-agenda-buffer-name)
  4301. (defvar org-agenda-undo-list)
  4302. (defvar org-agenda-pending-undo-list)
  4303. (defvar org-agenda-overriding-header)
  4304. (defvar orgtbl-mode)
  4305. (defvar org-html-entities)
  4306. (defvar org-struct-menu)
  4307. (defvar org-org-menu)
  4308. (defvar org-tbl-menu)
  4309. (defvar org-agenda-keymap)
  4310. ;;;; Emacs/XEmacs compatibility
  4311. ;; Overlay compatibility functions
  4312. (defun org-make-overlay (beg end &optional buffer)
  4313. (if (featurep 'xemacs)
  4314. (make-extent beg end buffer)
  4315. (make-overlay beg end buffer)))
  4316. (defun org-delete-overlay (ovl)
  4317. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4318. (defun org-detach-overlay (ovl)
  4319. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4320. (defun org-move-overlay (ovl beg end &optional buffer)
  4321. (if (featurep 'xemacs)
  4322. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4323. (move-overlay ovl beg end buffer)))
  4324. (defun org-overlay-put (ovl prop value)
  4325. (if (featurep 'xemacs)
  4326. (set-extent-property ovl prop value)
  4327. (overlay-put ovl prop value)))
  4328. (defun org-overlay-display (ovl text &optional face evap)
  4329. "Make overlay OVL display TEXT with face FACE."
  4330. (if (featurep 'xemacs)
  4331. (let ((gl (make-glyph text)))
  4332. (and face (set-glyph-face gl face))
  4333. (set-extent-property ovl 'invisible t)
  4334. (set-extent-property ovl 'end-glyph gl))
  4335. (overlay-put ovl 'display text)
  4336. (if face (overlay-put ovl 'face face))
  4337. (if evap (overlay-put ovl 'evaporate t))))
  4338. (defun org-overlay-before-string (ovl text &optional face evap)
  4339. "Make overlay OVL display TEXT with face FACE."
  4340. (if (featurep 'xemacs)
  4341. (let ((gl (make-glyph text)))
  4342. (and face (set-glyph-face gl face))
  4343. (set-extent-property ovl 'begin-glyph gl))
  4344. (if face (org-add-props text nil 'face face))
  4345. (overlay-put ovl 'before-string text)
  4346. (if evap (overlay-put ovl 'evaporate t))))
  4347. (defun org-overlay-get (ovl prop)
  4348. (if (featurep 'xemacs)
  4349. (extent-property ovl prop)
  4350. (overlay-get ovl prop)))
  4351. (defun org-overlays-at (pos)
  4352. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4353. (defun org-overlays-in (&optional start end)
  4354. (if (featurep 'xemacs)
  4355. (extent-list nil start end)
  4356. (overlays-in start end)))
  4357. (defun org-overlay-start (o)
  4358. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4359. (defun org-overlay-end (o)
  4360. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4361. (defun org-find-overlays (prop &optional pos delete)
  4362. "Find all overlays specifying PROP at POS or point.
  4363. If DELETE is non-nil, delete all those overlays."
  4364. (let ((overlays (org-overlays-at (or pos (point))))
  4365. ov found)
  4366. (while (setq ov (pop overlays))
  4367. (if (org-overlay-get ov prop)
  4368. (if delete (org-delete-overlay ov) (push ov found))))
  4369. found))
  4370. ;; Region compatibility
  4371. (defun org-add-hook (hook function &optional append local)
  4372. "Add-hook, compatible with both Emacsen."
  4373. (if (and local (featurep 'xemacs))
  4374. (add-local-hook hook function append)
  4375. (add-hook hook function append local)))
  4376. (defvar org-ignore-region nil
  4377. "To temporarily disable the active region.")
  4378. (defun org-region-active-p ()
  4379. "Is `transient-mark-mode' on and the region active?
  4380. Works on both Emacs and XEmacs."
  4381. (if org-ignore-region
  4382. nil
  4383. (if (featurep 'xemacs)
  4384. (and zmacs-regions (region-active-p))
  4385. (if (fboundp 'use-region-p)
  4386. (use-region-p)
  4387. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4388. ;; Invisibility compatibility
  4389. (defun org-add-to-invisibility-spec (arg)
  4390. "Add elements to `buffer-invisibility-spec'.
  4391. See documentation for `buffer-invisibility-spec' for the kind of elements
  4392. that can be added."
  4393. (cond
  4394. ((fboundp 'add-to-invisibility-spec)
  4395. (add-to-invisibility-spec arg))
  4396. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4397. (setq buffer-invisibility-spec (list arg)))
  4398. (t
  4399. (setq buffer-invisibility-spec
  4400. (cons arg buffer-invisibility-spec)))))
  4401. (defun org-remove-from-invisibility-spec (arg)
  4402. "Remove elements from `buffer-invisibility-spec'."
  4403. (if (fboundp 'remove-from-invisibility-spec)
  4404. (remove-from-invisibility-spec arg)
  4405. (if (consp buffer-invisibility-spec)
  4406. (setq buffer-invisibility-spec
  4407. (delete arg buffer-invisibility-spec)))))
  4408. (defun org-in-invisibility-spec-p (arg)
  4409. "Is ARG a member of `buffer-invisibility-spec'?"
  4410. (if (consp buffer-invisibility-spec)
  4411. (member arg buffer-invisibility-spec)
  4412. nil))
  4413. ;;;; Define the Org-mode
  4414. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4415. (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."))
  4416. ;; We use a before-change function to check if a table might need
  4417. ;; an update.
  4418. (defvar org-table-may-need-update t
  4419. "Indicates that a table might need an update.
  4420. This variable is set by `org-before-change-function'.
  4421. `org-table-align' sets it back to nil.")
  4422. (defvar org-mode-map)
  4423. (defvar org-mode-hook nil)
  4424. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4425. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4426. (defvar org-table-buffer-is-an nil)
  4427. (defconst org-outline-regexp "\\*+ ")
  4428. ;;;###autoload
  4429. (define-derived-mode org-mode outline-mode "Org"
  4430. "Outline-based notes management and organizer, alias
  4431. \"Carsten's outline-mode for keeping track of everything.\"
  4432. Org-mode develops organizational tasks around a NOTES file which
  4433. contains information about projects as plain text. Org-mode is
  4434. implemented on top of outline-mode, which is ideal to keep the content
  4435. of large files well structured. It supports ToDo items, deadlines and
  4436. time stamps, which magically appear in the diary listing of the Emacs
  4437. calendar. Tables are easily created with a built-in table editor.
  4438. Plain text URL-like links connect to websites, emails (VM), Usenet
  4439. messages (Gnus), BBDB entries, and any files related to the project.
  4440. For printing and sharing of notes, an Org-mode file (or a part of it)
  4441. can be exported as a structured ASCII or HTML file.
  4442. The following commands are available:
  4443. \\{org-mode-map}"
  4444. ;; Get rid of Outline menus, they are not needed
  4445. ;; Need to do this here because define-derived-mode sets up
  4446. ;; the keymap so late. Still, it is a waste to call this each time
  4447. ;; we switch another buffer into org-mode.
  4448. (if (featurep 'xemacs)
  4449. (when (boundp 'outline-mode-menu-heading)
  4450. ;; Assume this is Greg's port, it used easymenu
  4451. (easy-menu-remove outline-mode-menu-heading)
  4452. (easy-menu-remove outline-mode-menu-show)
  4453. (easy-menu-remove outline-mode-menu-hide))
  4454. (define-key org-mode-map [menu-bar headings] 'undefined)
  4455. (define-key org-mode-map [menu-bar hide] 'undefined)
  4456. (define-key org-mode-map [menu-bar show] 'undefined))
  4457. (org-load-modules-maybe)
  4458. (easy-menu-add org-org-menu)
  4459. (easy-menu-add org-tbl-menu)
  4460. (org-install-agenda-files-menu)
  4461. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4462. (org-add-to-invisibility-spec '(org-cwidth))
  4463. (when (featurep 'xemacs)
  4464. (org-set-local 'line-move-ignore-invisible t))
  4465. (org-set-local 'outline-regexp org-outline-regexp)
  4466. (org-set-local 'outline-level 'org-outline-level)
  4467. (when (and org-ellipsis
  4468. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4469. (fboundp 'make-glyph-code))
  4470. (unless org-display-table
  4471. (setq org-display-table (make-display-table)))
  4472. (set-display-table-slot
  4473. org-display-table 4
  4474. (vconcat (mapcar
  4475. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4476. org-ellipsis)))
  4477. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4478. (setq buffer-display-table org-display-table))
  4479. (org-set-regexps-and-options)
  4480. ;; Calc embedded
  4481. (org-set-local 'calc-embedded-open-mode "# ")
  4482. (modify-syntax-entry ?# "<")
  4483. (modify-syntax-entry ?@ "w")
  4484. (if org-startup-truncated (setq truncate-lines t))
  4485. (org-set-local 'font-lock-unfontify-region-function
  4486. 'org-unfontify-region)
  4487. ;; Activate before-change-function
  4488. (org-set-local 'org-table-may-need-update t)
  4489. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4490. 'local)
  4491. ;; Check for running clock before killing a buffer
  4492. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4493. ;; Paragraphs and auto-filling
  4494. (org-set-autofill-regexps)
  4495. (setq indent-line-function 'org-indent-line-function)
  4496. (org-update-radio-target-regexp)
  4497. ;; Comment characters
  4498. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4499. (org-set-local 'comment-padding " ")
  4500. ;; Align options lines
  4501. (org-set-local
  4502. 'align-mode-rules-list
  4503. '((org-in-buffer-settings
  4504. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4505. (modes . '(org-mode)))))
  4506. ;; Imenu
  4507. (org-set-local 'imenu-create-index-function
  4508. 'org-imenu-get-tree)
  4509. ;; Make isearch reveal context
  4510. (if (or (featurep 'xemacs)
  4511. (not (boundp 'outline-isearch-open-invisible-function)))
  4512. ;; Emacs 21 and XEmacs make use of the hook
  4513. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4514. ;; Emacs 22 deals with this through a special variable
  4515. (org-set-local 'outline-isearch-open-invisible-function
  4516. (lambda (&rest ignore) (org-show-context 'isearch))))
  4517. ;; If empty file that did not turn on org-mode automatically, make it to.
  4518. (if (and org-insert-mode-line-in-empty-file
  4519. (interactive-p)
  4520. (= (point-min) (point-max)))
  4521. (insert "# -*- mode: org -*-\n\n"))
  4522. (unless org-inhibit-startup
  4523. (when org-startup-align-all-tables
  4524. (let ((bmp (buffer-modified-p)))
  4525. (org-table-map-tables 'org-table-align)
  4526. (set-buffer-modified-p bmp)))
  4527. (org-cycle-hide-drawers 'all)
  4528. (cond
  4529. ((eq org-startup-folded t)
  4530. (org-cycle '(4)))
  4531. ((eq org-startup-folded 'content)
  4532. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4533. (org-cycle '(4)) (org-cycle '(4)))))))
  4534. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4535. (defsubst org-call-with-arg (command arg)
  4536. "Call COMMAND interactively, but pretend prefix are was ARG."
  4537. (let ((current-prefix-arg arg)) (call-interactively command)))
  4538. (defsubst org-current-line (&optional pos)
  4539. (save-excursion
  4540. (and pos (goto-char pos))
  4541. ;; works also in narrowed buffer, because we start at 1, not point-min
  4542. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4543. (defun org-current-time ()
  4544. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4545. (if (> (car org-time-stamp-rounding-minutes) 1)
  4546. (let ((r (car org-time-stamp-rounding-minutes))
  4547. (time (decode-time)))
  4548. (apply 'encode-time
  4549. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4550. (nthcdr 2 time))))
  4551. (current-time)))
  4552. (defun org-add-props (string plist &rest props)
  4553. "Add text properties to entire string, from beginning to end.
  4554. PLIST may be a list of properties, PROPS are individual properties and values
  4555. that will be added to PLIST. Returns the string that was modified."
  4556. (add-text-properties
  4557. 0 (length string) (if props (append plist props) plist) string)
  4558. string)
  4559. (put 'org-add-props 'lisp-indent-function 2)
  4560. ;;;; Font-Lock stuff, including the activators
  4561. (defvar org-mouse-map (make-sparse-keymap))
  4562. (org-defkey org-mouse-map
  4563. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4564. (org-defkey org-mouse-map
  4565. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4566. (when org-mouse-1-follows-link
  4567. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4568. (when org-tab-follows-link
  4569. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4570. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4571. (when org-return-follows-link
  4572. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4573. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4574. (require 'font-lock)
  4575. (defconst org-non-link-chars "]\t\n\r<>")
  4576. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4577. "shell" "elisp"))
  4578. (defvar org-link-re-with-space nil
  4579. "Matches a link with spaces, optional angular brackets around it.")
  4580. (defvar org-link-re-with-space2 nil
  4581. "Matches a link with spaces, optional angular brackets around it.")
  4582. (defvar org-angle-link-re nil
  4583. "Matches link with angular brackets, spaces are allowed.")
  4584. (defvar org-plain-link-re nil
  4585. "Matches plain link, without spaces.")
  4586. (defvar org-bracket-link-regexp nil
  4587. "Matches a link in double brackets.")
  4588. (defvar org-bracket-link-analytic-regexp nil
  4589. "Regular expression used to analyze links.
  4590. Here is what the match groups contain after a match:
  4591. 1: http:
  4592. 2: http
  4593. 3: path
  4594. 4: [desc]
  4595. 5: desc")
  4596. (defvar org-any-link-re nil
  4597. "Regular expression matching any link.")
  4598. (defun org-make-link-regexps ()
  4599. "Update the link regular expressions.
  4600. This should be called after the variable `org-link-types' has changed."
  4601. (setq org-link-re-with-space
  4602. (concat
  4603. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4604. "\\([^" org-non-link-chars " ]"
  4605. "[^" org-non-link-chars "]*"
  4606. "[^" org-non-link-chars " ]\\)>?")
  4607. org-link-re-with-space2
  4608. (concat
  4609. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4610. "\\([^" org-non-link-chars " ]"
  4611. "[^]\t\n\r]*"
  4612. "[^" org-non-link-chars " ]\\)>?")
  4613. org-angle-link-re
  4614. (concat
  4615. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4616. "\\([^" org-non-link-chars " ]"
  4617. "[^" org-non-link-chars "]*"
  4618. "\\)>")
  4619. org-plain-link-re
  4620. (concat
  4621. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4622. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4623. org-bracket-link-regexp
  4624. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4625. org-bracket-link-analytic-regexp
  4626. (concat
  4627. "\\[\\["
  4628. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4629. "\\([^]]+\\)"
  4630. "\\]"
  4631. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4632. "\\]")
  4633. org-any-link-re
  4634. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4635. org-angle-link-re "\\)\\|\\("
  4636. org-plain-link-re "\\)")))
  4637. (org-make-link-regexps)
  4638. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4639. "Regular expression for fast time stamp matching.")
  4640. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4641. "Regular expression for fast time stamp matching.")
  4642. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4643. "Regular expression matching time strings for analysis.
  4644. This one does not require the space after the date, so it can be used
  4645. on a string that terminates immediately after the date.")
  4646. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4647. "Regular expression matching time strings for analysis.")
  4648. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4649. "Regular expression matching time stamps, with groups.")
  4650. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4651. "Regular expression matching time stamps (also [..]), with groups.")
  4652. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4653. "Regular expression matching a time stamp range.")
  4654. (defconst org-tr-regexp-both
  4655. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4656. "Regular expression matching a time stamp range.")
  4657. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4658. org-ts-regexp "\\)?")
  4659. "Regular expression matching a time stamp or time stamp range.")
  4660. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4661. org-ts-regexp-both "\\)?")
  4662. "Regular expression matching a time stamp or time stamp range.
  4663. The time stamps may be either active or inactive.")
  4664. (defvar org-emph-face nil)
  4665. (defun org-do-emphasis-faces (limit)
  4666. "Run through the buffer and add overlays to links."
  4667. (let (rtn)
  4668. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4669. (if (not (= (char-after (match-beginning 3))
  4670. (char-after (match-beginning 4))))
  4671. (progn
  4672. (setq rtn t)
  4673. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4674. 'face
  4675. (nth 1 (assoc (match-string 3)
  4676. org-emphasis-alist)))
  4677. (add-text-properties (match-beginning 2) (match-end 2)
  4678. '(font-lock-multiline t))
  4679. (when org-hide-emphasis-markers
  4680. (add-text-properties (match-end 4) (match-beginning 5)
  4681. '(invisible org-link))
  4682. (add-text-properties (match-beginning 3) (match-end 3)
  4683. '(invisible org-link)))))
  4684. (backward-char 1))
  4685. rtn))
  4686. (defun org-emphasize (&optional char)
  4687. "Insert or change an emphasis, i.e. a font like bold or italic.
  4688. If there is an active region, change that region to a new emphasis.
  4689. If there is no region, just insert the marker characters and position
  4690. the cursor between them.
  4691. CHAR should be either the marker character, or the first character of the
  4692. HTML tag associated with that emphasis. If CHAR is a space, the means
  4693. to remove the emphasis of the selected region.
  4694. If char is not given (for example in an interactive call) it
  4695. will be prompted for."
  4696. (interactive)
  4697. (let ((eal org-emphasis-alist) e det
  4698. (erc org-emphasis-regexp-components)
  4699. (prompt "")
  4700. (string "") beg end move tag c s)
  4701. (if (org-region-active-p)
  4702. (setq beg (region-beginning) end (region-end)
  4703. string (buffer-substring beg end))
  4704. (setq move t))
  4705. (while (setq e (pop eal))
  4706. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4707. c (aref tag 0))
  4708. (push (cons c (string-to-char (car e))) det)
  4709. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4710. (substring tag 1)))))
  4711. (unless char
  4712. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4713. (setq char (read-char-exclusive)))
  4714. (setq char (or (cdr (assoc char det)) char))
  4715. (if (equal char ?\ )
  4716. (setq s "" move nil)
  4717. (unless (assoc (char-to-string char) org-emphasis-alist)
  4718. (error "No such emphasis marker: \"%c\"" char))
  4719. (setq s (char-to-string char)))
  4720. (while (and (> (length string) 1)
  4721. (equal (substring string 0 1) (substring string -1))
  4722. (assoc (substring string 0 1) org-emphasis-alist))
  4723. (setq string (substring string 1 -1)))
  4724. (setq string (concat s string s))
  4725. (if beg (delete-region beg end))
  4726. (unless (or (bolp)
  4727. (string-match (concat "[" (nth 0 erc) "\n]")
  4728. (char-to-string (char-before (point)))))
  4729. (insert " "))
  4730. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4731. (char-to-string (char-after (point))))
  4732. (insert " ") (backward-char 1))
  4733. (insert string)
  4734. (and move (backward-char 1))))
  4735. (defconst org-nonsticky-props
  4736. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4737. (defun org-activate-plain-links (limit)
  4738. "Run through the buffer and add overlays to links."
  4739. (catch 'exit
  4740. (let (f)
  4741. (while (re-search-forward org-plain-link-re limit t)
  4742. (setq f (get-text-property (match-beginning 0) 'face))
  4743. (if (or (eq f 'org-tag)
  4744. (and (listp f) (memq 'org-tag f)))
  4745. nil
  4746. (add-text-properties (match-beginning 0) (match-end 0)
  4747. (list 'mouse-face 'highlight
  4748. 'rear-nonsticky org-nonsticky-props
  4749. 'keymap org-mouse-map
  4750. ))
  4751. (throw 'exit t))))))
  4752. (defun org-activate-code (limit)
  4753. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4754. (unless (get-text-property (match-beginning 1) 'face)
  4755. (remove-text-properties (match-beginning 0) (match-end 0)
  4756. '(display t invisible t intangible t))
  4757. t)))
  4758. (defun org-activate-angle-links (limit)
  4759. "Run through the buffer and add overlays to links."
  4760. (if (re-search-forward org-angle-link-re limit t)
  4761. (progn
  4762. (add-text-properties (match-beginning 0) (match-end 0)
  4763. (list 'mouse-face 'highlight
  4764. 'rear-nonsticky org-nonsticky-props
  4765. 'keymap org-mouse-map
  4766. ))
  4767. t)))
  4768. (defmacro org-maybe-intangible (props)
  4769. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4770. In emacs 21, invisible text is not avoided by the command loop, so the
  4771. intangible property is needed to make sure point skips this text.
  4772. In Emacs 22, this is not necessary. The intangible text property has
  4773. led to problems with flyspell. These problems are fixed in flyspell.el,
  4774. but we still avoid setting the property in Emacs 22 and later.
  4775. We use a macro so that the test can happen at compilation time."
  4776. (if (< emacs-major-version 22)
  4777. `(append '(intangible t) ,props)
  4778. props))
  4779. (defun org-activate-bracket-links (limit)
  4780. "Run through the buffer and add overlays to bracketed links."
  4781. (if (re-search-forward org-bracket-link-regexp limit t)
  4782. (let* ((help (concat "LINK: "
  4783. (org-match-string-no-properties 1)))
  4784. ;; FIXME: above we should remove the escapes.
  4785. ;; but that requires another match, protecting match data,
  4786. ;; a lot of overhead for font-lock.
  4787. (ip (org-maybe-intangible
  4788. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4789. 'keymap org-mouse-map 'mouse-face 'highlight
  4790. 'font-lock-multiline t 'help-echo help)))
  4791. (vp (list 'rear-nonsticky org-nonsticky-props
  4792. 'keymap org-mouse-map 'mouse-face 'highlight
  4793. ' font-lock-multiline t 'help-echo help)))
  4794. ;; We need to remove the invisible property here. Table narrowing
  4795. ;; may have made some of this invisible.
  4796. (remove-text-properties (match-beginning 0) (match-end 0)
  4797. '(invisible nil))
  4798. (if (match-end 3)
  4799. (progn
  4800. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4801. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4802. (add-text-properties (match-end 3) (match-end 0) ip))
  4803. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4804. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4805. (add-text-properties (match-end 1) (match-end 0) ip))
  4806. t)))
  4807. (defun org-activate-dates (limit)
  4808. "Run through the buffer and add overlays to dates."
  4809. (if (re-search-forward org-tsr-regexp-both limit t)
  4810. (progn
  4811. (add-text-properties (match-beginning 0) (match-end 0)
  4812. (list 'mouse-face 'highlight
  4813. 'rear-nonsticky org-nonsticky-props
  4814. 'keymap org-mouse-map))
  4815. (when org-display-custom-times
  4816. (if (match-end 3)
  4817. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4818. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4819. t)))
  4820. (defvar org-target-link-regexp nil
  4821. "Regular expression matching radio targets in plain text.")
  4822. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4823. "Regular expression matching a link target.")
  4824. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4825. "Regular expression matching a radio target.")
  4826. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4827. "Regular expression matching any target.")
  4828. (defun org-activate-target-links (limit)
  4829. "Run through the buffer and add overlays to target matches."
  4830. (when org-target-link-regexp
  4831. (let ((case-fold-search t))
  4832. (if (re-search-forward org-target-link-regexp limit t)
  4833. (progn
  4834. (add-text-properties (match-beginning 0) (match-end 0)
  4835. (list 'mouse-face 'highlight
  4836. 'rear-nonsticky org-nonsticky-props
  4837. 'keymap org-mouse-map
  4838. 'help-echo "Radio target link"
  4839. 'org-linked-text t))
  4840. t)))))
  4841. (defun org-update-radio-target-regexp ()
  4842. "Find all radio targets in this file and update the regular expression."
  4843. (interactive)
  4844. (when (memq 'radio org-activate-links)
  4845. (setq org-target-link-regexp
  4846. (org-make-target-link-regexp (org-all-targets 'radio)))
  4847. (org-restart-font-lock)))
  4848. (defun org-hide-wide-columns (limit)
  4849. (let (s e)
  4850. (setq s (text-property-any (point) (or limit (point-max))
  4851. 'org-cwidth t))
  4852. (when s
  4853. (setq e (next-single-property-change s 'org-cwidth))
  4854. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4855. (goto-char e)
  4856. t)))
  4857. (defvar org-latex-and-specials-regexp nil
  4858. "Regular expression for highlighting export special stuff.")
  4859. (defvar org-match-substring-regexp)
  4860. (defvar org-match-substring-with-braces-regexp)
  4861. (defvar org-export-html-special-string-regexps)
  4862. (defun org-compute-latex-and-specials-regexp ()
  4863. "Compute regular expression for stuff treated specially by exporters."
  4864. (if (not org-highlight-latex-fragments-and-specials)
  4865. (org-set-local 'org-latex-and-specials-regexp nil)
  4866. (let*
  4867. ((matchers (plist-get org-format-latex-options :matchers))
  4868. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4869. org-latex-regexps)))
  4870. (options (org-combine-plists (org-default-export-plist)
  4871. (org-infile-export-plist)))
  4872. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4873. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4874. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4875. (org-export-html-expand (plist-get options :expand-quoted-html))
  4876. (org-export-with-special-strings (plist-get options :special-strings))
  4877. (re-sub
  4878. (cond
  4879. ((equal org-export-with-sub-superscripts '{})
  4880. (list org-match-substring-with-braces-regexp))
  4881. (org-export-with-sub-superscripts
  4882. (list org-match-substring-regexp))
  4883. (t nil)))
  4884. (re-latex
  4885. (if org-export-with-LaTeX-fragments
  4886. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4887. (re-macros
  4888. (if org-export-with-TeX-macros
  4889. (list (concat "\\\\"
  4890. (regexp-opt
  4891. (append (mapcar 'car org-html-entities)
  4892. (if (boundp 'org-latex-entities)
  4893. org-latex-entities nil))
  4894. 'words))) ; FIXME
  4895. ))
  4896. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4897. (re-special (if org-export-with-special-strings
  4898. (mapcar (lambda (x) (car x))
  4899. org-export-html-special-string-regexps)))
  4900. (re-rest
  4901. (delq nil
  4902. (list
  4903. (if org-export-html-expand "@<[^>\n]+>")
  4904. ))))
  4905. (org-set-local
  4906. 'org-latex-and-specials-regexp
  4907. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4908. re-rest) "\\|")))))
  4909. (defface org-latex-and-export-specials
  4910. (let ((font (cond ((assq :inherit custom-face-attributes)
  4911. '(:inherit underline))
  4912. (t '(:underline t)))))
  4913. `((((class grayscale) (background light))
  4914. (:foreground "DimGray" ,@font))
  4915. (((class grayscale) (background dark))
  4916. (:foreground "LightGray" ,@font))
  4917. (((class color) (background light))
  4918. (:foreground "SaddleBrown"))
  4919. (((class color) (background dark))
  4920. (:foreground "burlywood"))
  4921. (t (,@font))))
  4922. "Face used to highlight math latex and other special exporter stuff."
  4923. :group 'org-faces)
  4924. (defun org-do-latex-and-special-faces (limit)
  4925. "Run through the buffer and add overlays to links."
  4926. (when org-latex-and-specials-regexp
  4927. (let (rtn d)
  4928. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4929. limit t))
  4930. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4931. 'face))
  4932. '(org-code org-verbatim underline)))
  4933. (progn
  4934. (setq rtn t
  4935. d (cond ((member (char-after (1+ (match-beginning 0)))
  4936. '(?_ ?^)) 1)
  4937. (t 0)))
  4938. (font-lock-prepend-text-property
  4939. (+ d (match-beginning 0)) (match-end 0)
  4940. 'face 'org-latex-and-export-specials)
  4941. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4942. '(font-lock-multiline t)))))
  4943. rtn)))
  4944. (defun org-restart-font-lock ()
  4945. "Restart font-lock-mode, to force refontification."
  4946. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4947. (font-lock-mode -1)
  4948. (font-lock-mode 1)))
  4949. (defun org-all-targets (&optional radio)
  4950. "Return a list of all targets in this file.
  4951. With optional argument RADIO, only find radio targets."
  4952. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4953. rtn)
  4954. (save-excursion
  4955. (goto-char (point-min))
  4956. (while (re-search-forward re nil t)
  4957. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4958. rtn)))
  4959. (defun org-make-target-link-regexp (targets)
  4960. "Make regular expression matching all strings in TARGETS.
  4961. The regular expression finds the targets also if there is a line break
  4962. between words."
  4963. (and targets
  4964. (concat
  4965. "\\<\\("
  4966. (mapconcat
  4967. (lambda (x)
  4968. (while (string-match " +" x)
  4969. (setq x (replace-match "\\s-+" t t x)))
  4970. x)
  4971. targets
  4972. "\\|")
  4973. "\\)\\>")))
  4974. (defun org-activate-tags (limit)
  4975. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4976. (progn
  4977. (add-text-properties (match-beginning 1) (match-end 1)
  4978. (list 'mouse-face 'highlight
  4979. 'rear-nonsticky org-nonsticky-props
  4980. 'keymap org-mouse-map))
  4981. t)))
  4982. (defun org-outline-level ()
  4983. (save-excursion
  4984. (looking-at outline-regexp)
  4985. (if (match-beginning 1)
  4986. (+ (org-get-string-indentation (match-string 1)) 1000)
  4987. (1- (- (match-end 0) (match-beginning 0))))))
  4988. (defvar org-font-lock-keywords nil)
  4989. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4990. "Regular expression matching a property line.")
  4991. (defun org-set-font-lock-defaults ()
  4992. (let* ((em org-fontify-emphasized-text)
  4993. (lk org-activate-links)
  4994. (org-font-lock-extra-keywords
  4995. (list
  4996. ;; Headlines
  4997. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4998. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4999. ;; Table lines
  5000. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5001. (1 'org-table t))
  5002. ;; Table internals
  5003. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5004. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5005. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5006. ;; Drawers
  5007. (list org-drawer-regexp '(0 'org-special-keyword t))
  5008. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5009. ;; Properties
  5010. (list org-property-re
  5011. '(1 'org-special-keyword t)
  5012. '(3 'org-property-value t))
  5013. (if org-format-transports-properties-p
  5014. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  5015. ;; Links
  5016. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5017. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5018. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5019. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5020. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5021. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5022. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5023. '(org-hide-wide-columns (0 nil append))
  5024. ;; TODO lines
  5025. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  5026. '(1 (org-get-todo-face 1) t))
  5027. ;; DONE
  5028. (if org-fontify-done-headline
  5029. (list (concat "^[*]+ +\\<\\("
  5030. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5031. "\\)\\(.*\\)")
  5032. '(2 'org-headline-done t))
  5033. nil)
  5034. ;; Priorities
  5035. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  5036. ;; Special keywords
  5037. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5038. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5039. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5040. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5041. ;; Emphasis
  5042. (if em
  5043. (if (featurep 'xemacs)
  5044. '(org-do-emphasis-faces (0 nil append))
  5045. '(org-do-emphasis-faces)))
  5046. ;; Checkboxes
  5047. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  5048. 2 'bold prepend)
  5049. (if org-provide-checkbox-statistics
  5050. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5051. (0 (org-get-checkbox-statistics-face) t)))
  5052. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  5053. '(1 'org-archived prepend))
  5054. ;; Specials
  5055. '(org-do-latex-and-special-faces)
  5056. ;; Code
  5057. '(org-activate-code (1 'org-code t))
  5058. ;; COMMENT
  5059. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5060. "\\|" org-quote-string "\\)\\>")
  5061. '(1 'org-special-keyword t))
  5062. '("^#.*" (0 'font-lock-comment-face t))
  5063. )))
  5064. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5065. ;; Now set the full font-lock-keywords
  5066. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5067. (org-set-local 'font-lock-defaults
  5068. '(org-font-lock-keywords t nil nil backward-paragraph))
  5069. (kill-local-variable 'font-lock-keywords) nil))
  5070. (defvar org-m nil)
  5071. (defvar org-l nil)
  5072. (defvar org-f nil)
  5073. (defun org-get-level-face (n)
  5074. "Get the right face for match N in font-lock matching of healdines."
  5075. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5076. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5077. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5078. (cond
  5079. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5080. ((eq n 2) org-f)
  5081. (t (if org-level-color-stars-only nil org-f))))
  5082. (defun org-get-todo-face (kwd)
  5083. "Get the right face for a TODO keyword KWD.
  5084. If KWD is a number, get the corresponding match group."
  5085. (if (numberp kwd) (setq kwd (match-string kwd)))
  5086. (or (cdr (assoc kwd org-todo-keyword-faces))
  5087. (and (member kwd org-done-keywords) 'org-done)
  5088. 'org-todo))
  5089. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5090. "Remove fontification and activation overlays from links."
  5091. (font-lock-default-unfontify-region beg end)
  5092. (let* ((buffer-undo-list t)
  5093. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5094. (inhibit-modification-hooks t)
  5095. deactivate-mark buffer-file-name buffer-file-truename)
  5096. (remove-text-properties beg end
  5097. '(mouse-face t keymap t org-linked-text t
  5098. invisible t intangible t))))
  5099. ;;;; Visibility cycling, including org-goto and indirect buffer
  5100. ;;; Cycling
  5101. (defvar org-cycle-global-status nil)
  5102. (make-variable-buffer-local 'org-cycle-global-status)
  5103. (defvar org-cycle-subtree-status nil)
  5104. (make-variable-buffer-local 'org-cycle-subtree-status)
  5105. ;;;###autoload
  5106. (defun org-cycle (&optional arg)
  5107. "Visibility cycling for Org-mode.
  5108. - When this function is called with a prefix argument, rotate the entire
  5109. buffer through 3 states (global cycling)
  5110. 1. OVERVIEW: Show only top-level headlines.
  5111. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5112. 3. SHOW ALL: Show everything.
  5113. - When point is at the beginning of a headline, rotate the subtree started
  5114. by this line through 3 different states (local cycling)
  5115. 1. FOLDED: Only the main headline is shown.
  5116. 2. CHILDREN: The main headline and the direct children are shown.
  5117. From this state, you can move to one of the children
  5118. and zoom in further.
  5119. 3. SUBTREE: Show the entire subtree, including body text.
  5120. - When there is a numeric prefix, go up to a heading with level ARG, do
  5121. a `show-subtree' and return to the previous cursor position. If ARG
  5122. is negative, go up that many levels.
  5123. - When point is not at the beginning of a headline, execute
  5124. `indent-relative', like TAB normally does. See the option
  5125. `org-cycle-emulate-tab' for details.
  5126. - Special case: if point is at the beginning of the buffer and there is
  5127. no headline in line 1, this function will act as if called with prefix arg.
  5128. But only if also the variable `org-cycle-global-at-bob' is t."
  5129. (interactive "P")
  5130. (org-load-modules-maybe)
  5131. (let* ((outline-regexp
  5132. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5133. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5134. outline-regexp))
  5135. (bob-special (and org-cycle-global-at-bob (bobp)
  5136. (not (looking-at outline-regexp))))
  5137. (org-cycle-hook
  5138. (if bob-special
  5139. (delq 'org-optimize-window-after-visibility-change
  5140. (copy-sequence org-cycle-hook))
  5141. org-cycle-hook))
  5142. (pos (point)))
  5143. (if (or bob-special (equal arg '(4)))
  5144. ;; special case: use global cycling
  5145. (setq arg t))
  5146. (cond
  5147. ((org-at-table-p 'any)
  5148. ;; Enter the table or move to the next field in the table
  5149. (or (org-table-recognize-table.el)
  5150. (progn
  5151. (if arg (org-table-edit-field t)
  5152. (org-table-justify-field-maybe)
  5153. (call-interactively 'org-table-next-field)))))
  5154. ((eq arg t) ;; Global cycling
  5155. (cond
  5156. ((and (eq last-command this-command)
  5157. (eq org-cycle-global-status 'overview))
  5158. ;; We just created the overview - now do table of contents
  5159. ;; This can be slow in very large buffers, so indicate action
  5160. (message "CONTENTS...")
  5161. (org-content)
  5162. (message "CONTENTS...done")
  5163. (setq org-cycle-global-status 'contents)
  5164. (run-hook-with-args 'org-cycle-hook 'contents))
  5165. ((and (eq last-command this-command)
  5166. (eq org-cycle-global-status 'contents))
  5167. ;; We just showed the table of contents - now show everything
  5168. (show-all)
  5169. (message "SHOW ALL")
  5170. (setq org-cycle-global-status 'all)
  5171. (run-hook-with-args 'org-cycle-hook 'all))
  5172. (t
  5173. ;; Default action: go to overview
  5174. (org-overview)
  5175. (message "OVERVIEW")
  5176. (setq org-cycle-global-status 'overview)
  5177. (run-hook-with-args 'org-cycle-hook 'overview))))
  5178. ((and org-drawers org-drawer-regexp
  5179. (save-excursion
  5180. (beginning-of-line 1)
  5181. (looking-at org-drawer-regexp)))
  5182. ;; Toggle block visibility
  5183. (org-flag-drawer
  5184. (not (get-char-property (match-end 0) 'invisible))))
  5185. ((integerp arg)
  5186. ;; Show-subtree, ARG levels up from here.
  5187. (save-excursion
  5188. (org-back-to-heading)
  5189. (outline-up-heading (if (< arg 0) (- arg)
  5190. (- (funcall outline-level) arg)))
  5191. (org-show-subtree)))
  5192. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5193. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5194. ;; At a heading: rotate between three different views
  5195. (org-back-to-heading)
  5196. (let ((goal-column 0) eoh eol eos)
  5197. ;; First, some boundaries
  5198. (save-excursion
  5199. (org-back-to-heading)
  5200. (save-excursion
  5201. (beginning-of-line 2)
  5202. (while (and (not (eobp)) ;; this is like `next-line'
  5203. (get-char-property (1- (point)) 'invisible))
  5204. (beginning-of-line 2)) (setq eol (point)))
  5205. (outline-end-of-heading) (setq eoh (point))
  5206. (org-end-of-subtree t)
  5207. (unless (eobp)
  5208. (skip-chars-forward " \t\n")
  5209. (beginning-of-line 1) ; in case this is an item
  5210. )
  5211. (setq eos (1- (point))))
  5212. ;; Find out what to do next and set `this-command'
  5213. (cond
  5214. ((= eos eoh)
  5215. ;; Nothing is hidden behind this heading
  5216. (message "EMPTY ENTRY")
  5217. (setq org-cycle-subtree-status nil)
  5218. (save-excursion
  5219. (goto-char eos)
  5220. (outline-next-heading)
  5221. (if (org-invisible-p) (org-flag-heading nil))))
  5222. ((or (>= eol eos)
  5223. (not (string-match "\\S-" (buffer-substring eol eos))))
  5224. ;; Entire subtree is hidden in one line: open it
  5225. (org-show-entry)
  5226. (show-children)
  5227. (message "CHILDREN")
  5228. (save-excursion
  5229. (goto-char eos)
  5230. (outline-next-heading)
  5231. (if (org-invisible-p) (org-flag-heading nil)))
  5232. (setq org-cycle-subtree-status 'children)
  5233. (run-hook-with-args 'org-cycle-hook 'children))
  5234. ((and (eq last-command this-command)
  5235. (eq org-cycle-subtree-status 'children))
  5236. ;; We just showed the children, now show everything.
  5237. (org-show-subtree)
  5238. (message "SUBTREE")
  5239. (setq org-cycle-subtree-status 'subtree)
  5240. (run-hook-with-args 'org-cycle-hook 'subtree))
  5241. (t
  5242. ;; Default action: hide the subtree.
  5243. (hide-subtree)
  5244. (message "FOLDED")
  5245. (setq org-cycle-subtree-status 'folded)
  5246. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5247. ;; TAB emulation
  5248. (buffer-read-only (org-back-to-heading))
  5249. ((org-try-cdlatex-tab))
  5250. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5251. (or (not (bolp))
  5252. (not (looking-at outline-regexp))))
  5253. (call-interactively (global-key-binding "\t")))
  5254. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5255. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5256. (or (and (eq org-cycle-emulate-tab 'white)
  5257. (= (match-end 0) (point-at-eol)))
  5258. (and (eq org-cycle-emulate-tab 'whitestart)
  5259. (>= (match-end 0) pos))))
  5260. t
  5261. (eq org-cycle-emulate-tab t))
  5262. ; (if (and (looking-at "[ \n\r\t]")
  5263. ; (string-match "^[ \t]*$" (buffer-substring
  5264. ; (point-at-bol) (point))))
  5265. ; (progn
  5266. ; (beginning-of-line 1)
  5267. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5268. (call-interactively (global-key-binding "\t")))
  5269. (t (save-excursion
  5270. (org-back-to-heading)
  5271. (org-cycle))))))
  5272. ;;;###autoload
  5273. (defun org-global-cycle (&optional arg)
  5274. "Cycle the global visibility. For details see `org-cycle'."
  5275. (interactive "P")
  5276. (let ((org-cycle-include-plain-lists
  5277. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5278. (if (integerp arg)
  5279. (progn
  5280. (show-all)
  5281. (hide-sublevels arg)
  5282. (setq org-cycle-global-status 'contents))
  5283. (org-cycle '(4)))))
  5284. (defun org-overview ()
  5285. "Switch to overview mode, shoing only top-level headlines.
  5286. Really, this shows all headlines with level equal or greater than the level
  5287. of the first headline in the buffer. This is important, because if the
  5288. first headline is not level one, then (hide-sublevels 1) gives confusing
  5289. results."
  5290. (interactive)
  5291. (let ((level (save-excursion
  5292. (goto-char (point-min))
  5293. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5294. (progn
  5295. (goto-char (match-beginning 0))
  5296. (funcall outline-level))))))
  5297. (and level (hide-sublevels level))))
  5298. (defun org-content (&optional arg)
  5299. "Show all headlines in the buffer, like a table of contents.
  5300. With numerical argument N, show content up to level N."
  5301. (interactive "P")
  5302. (save-excursion
  5303. ;; Visit all headings and show their offspring
  5304. (and (integerp arg) (org-overview))
  5305. (goto-char (point-max))
  5306. (catch 'exit
  5307. (while (and (progn (condition-case nil
  5308. (outline-previous-visible-heading 1)
  5309. (error (goto-char (point-min))))
  5310. t)
  5311. (looking-at outline-regexp))
  5312. (if (integerp arg)
  5313. (show-children (1- arg))
  5314. (show-branches))
  5315. (if (bobp) (throw 'exit nil))))))
  5316. (defun org-optimize-window-after-visibility-change (state)
  5317. "Adjust the window after a change in outline visibility.
  5318. This function is the default value of the hook `org-cycle-hook'."
  5319. (when (get-buffer-window (current-buffer))
  5320. (cond
  5321. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5322. ; ((eq state 'overview) (org-beginning-of-line))
  5323. ((eq state 'content) nil)
  5324. ((eq state 'all) nil)
  5325. ((eq state 'folded) nil)
  5326. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5327. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5328. (defun org-compact-display-after-subtree-move ()
  5329. (let (beg end)
  5330. (save-excursion
  5331. (if (org-up-heading-safe)
  5332. (progn
  5333. (hide-subtree)
  5334. (show-entry)
  5335. (show-children)
  5336. (org-cycle-show-empty-lines 'children)
  5337. (org-cycle-hide-drawers 'children))
  5338. (org-overview)))))
  5339. (defun org-cycle-show-empty-lines (state)
  5340. "Show empty lines above all visible headlines.
  5341. The region to be covered depends on STATE when called through
  5342. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5343. entire buffer covered. Note that an empty line is only shown if there
  5344. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5345. (when (> org-cycle-separator-lines 0)
  5346. (save-excursion
  5347. (let* ((n org-cycle-separator-lines)
  5348. (re (cond
  5349. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5350. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5351. (t (let ((ns (number-to-string (- n 2))))
  5352. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5353. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5354. beg end)
  5355. (cond
  5356. ((memq state '(overview contents t))
  5357. (setq beg (point-min) end (point-max)))
  5358. ((memq state '(children folded))
  5359. (setq beg (point) end (progn (org-end-of-subtree t t)
  5360. (beginning-of-line 2)
  5361. (point)))))
  5362. (when beg
  5363. (goto-char beg)
  5364. (while (re-search-forward re end t)
  5365. (if (not (get-char-property (match-end 1) 'invisible))
  5366. (outline-flag-region
  5367. (match-beginning 1) (match-end 1) nil)))))))
  5368. ;; Never hide empty lines at the end of the file.
  5369. (save-excursion
  5370. (goto-char (point-max))
  5371. (outline-previous-heading)
  5372. (outline-end-of-heading)
  5373. (if (and (looking-at "[ \t\n]+")
  5374. (= (match-end 0) (point-max)))
  5375. (outline-flag-region (point) (match-end 0) nil))))
  5376. (defun org-subtree-end-visible-p ()
  5377. "Is the end of the current subtree visible?"
  5378. (pos-visible-in-window-p
  5379. (save-excursion (org-end-of-subtree t) (point))))
  5380. (defun org-first-headline-recenter (&optional N)
  5381. "Move cursor to the first headline and recenter the headline.
  5382. Optional argument N means, put the headline into the Nth line of the window."
  5383. (goto-char (point-min))
  5384. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5385. (beginning-of-line)
  5386. (recenter (prefix-numeric-value N))))
  5387. ;;; Org-goto
  5388. (defvar org-goto-window-configuration nil)
  5389. (defvar org-goto-marker nil)
  5390. (defvar org-goto-map
  5391. (let ((map (make-sparse-keymap)))
  5392. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5393. (while (setq cmd (pop cmds))
  5394. (substitute-key-definition cmd cmd map global-map)))
  5395. (suppress-keymap map)
  5396. (org-defkey map "\C-m" 'org-goto-ret)
  5397. (org-defkey map [(return)] 'org-goto-ret)
  5398. (org-defkey map [(left)] 'org-goto-left)
  5399. (org-defkey map [(right)] 'org-goto-right)
  5400. (org-defkey map [(control ?g)] 'org-goto-quit)
  5401. (org-defkey map "\C-i" 'org-cycle)
  5402. (org-defkey map [(tab)] 'org-cycle)
  5403. (org-defkey map [(down)] 'outline-next-visible-heading)
  5404. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5405. (if org-goto-auto-isearch
  5406. (if (fboundp 'define-key-after)
  5407. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5408. nil)
  5409. (org-defkey map "q" 'org-goto-quit)
  5410. (org-defkey map "n" 'outline-next-visible-heading)
  5411. (org-defkey map "p" 'outline-previous-visible-heading)
  5412. (org-defkey map "f" 'outline-forward-same-level)
  5413. (org-defkey map "b" 'outline-backward-same-level)
  5414. (org-defkey map "u" 'outline-up-heading))
  5415. (org-defkey map "/" 'org-occur)
  5416. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5417. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5418. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5419. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5420. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5421. map))
  5422. (defconst org-goto-help
  5423. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5424. RET=jump to location [Q]uit and return to previous location
  5425. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5426. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5427. (defun org-goto (&optional alternative-interface)
  5428. "Look up a different location in the current file, keeping current visibility.
  5429. When you want look-up or go to a different location in a document, the
  5430. fastest way is often to fold the entire buffer and then dive into the tree.
  5431. This method has the disadvantage, that the previous location will be folded,
  5432. which may not be what you want.
  5433. This command works around this by showing a copy of the current buffer
  5434. in an indirect buffer, in overview mode. You can dive into the tree in
  5435. that copy, use org-occur and incremental search to find a location.
  5436. When pressing RET or `Q', the command returns to the original buffer in
  5437. which the visibility is still unchanged. After RET is will also jump to
  5438. the location selected in the indirect buffer and expose the
  5439. the headline hierarchy above."
  5440. (interactive "P")
  5441. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5442. (org-refile-use-outline-path t)
  5443. (interface
  5444. (if (not alternative-interface)
  5445. org-goto-interface
  5446. (if (eq org-goto-interface 'outline)
  5447. 'outline-path-completion
  5448. 'outline)))
  5449. (org-goto-start-pos (point))
  5450. (selected-point
  5451. (if (eq interface 'outline)
  5452. (car (org-get-location (current-buffer) org-goto-help))
  5453. (nth 3 (org-refile-get-location "Goto: ")))))
  5454. (if selected-point
  5455. (progn
  5456. (org-mark-ring-push org-goto-start-pos)
  5457. (goto-char selected-point)
  5458. (if (or (org-invisible-p) (org-invisible-p2))
  5459. (org-show-context 'org-goto)))
  5460. (message "Quit"))))
  5461. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5462. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5463. (defvar org-goto-local-auto-isearch-map) ; defined below
  5464. (defun org-get-location (buf help)
  5465. "Let the user select a location in the Org-mode buffer BUF.
  5466. This function uses a recursive edit. It returns the selected position
  5467. or nil."
  5468. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5469. (isearch-hide-immediately nil)
  5470. (isearch-search-fun-function
  5471. (lambda () 'org-goto-local-search-forward-headings))
  5472. (org-goto-selected-point org-goto-exit-command))
  5473. (save-excursion
  5474. (save-window-excursion
  5475. (delete-other-windows)
  5476. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5477. (switch-to-buffer
  5478. (condition-case nil
  5479. (make-indirect-buffer (current-buffer) "*org-goto*")
  5480. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5481. (with-output-to-temp-buffer "*Help*"
  5482. (princ help))
  5483. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5484. (setq buffer-read-only nil)
  5485. (let ((org-startup-truncated t)
  5486. (org-startup-folded nil)
  5487. (org-startup-align-all-tables nil))
  5488. (org-mode)
  5489. (org-overview))
  5490. (setq buffer-read-only t)
  5491. (if (and (boundp 'org-goto-start-pos)
  5492. (integer-or-marker-p org-goto-start-pos))
  5493. (let ((org-show-hierarchy-above t)
  5494. (org-show-siblings t)
  5495. (org-show-following-heading t))
  5496. (goto-char org-goto-start-pos)
  5497. (and (org-invisible-p) (org-show-context)))
  5498. (goto-char (point-min)))
  5499. (org-beginning-of-line)
  5500. (message "Select location and press RET")
  5501. (use-local-map org-goto-map)
  5502. (recursive-edit)
  5503. ))
  5504. (kill-buffer "*org-goto*")
  5505. (cons org-goto-selected-point org-goto-exit-command)))
  5506. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5507. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5508. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5509. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5510. (defun org-goto-local-search-forward-headings (string bound noerror)
  5511. "Search and make sure that anu matches are in headlines."
  5512. (catch 'return
  5513. (while (search-forward string bound noerror)
  5514. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5515. (and (member :headline context)
  5516. (not (member :tags context))))
  5517. (throw 'return (point))))))
  5518. (defun org-goto-local-auto-isearch ()
  5519. "Start isearch."
  5520. (interactive)
  5521. (goto-char (point-min))
  5522. (let ((keys (this-command-keys)))
  5523. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5524. (isearch-mode t)
  5525. (isearch-process-search-char (string-to-char keys)))))
  5526. (defun org-goto-ret (&optional arg)
  5527. "Finish `org-goto' by going to the new location."
  5528. (interactive "P")
  5529. (setq org-goto-selected-point (point)
  5530. org-goto-exit-command 'return)
  5531. (throw 'exit nil))
  5532. (defun org-goto-left ()
  5533. "Finish `org-goto' by going to the new location."
  5534. (interactive)
  5535. (if (org-on-heading-p)
  5536. (progn
  5537. (beginning-of-line 1)
  5538. (setq org-goto-selected-point (point)
  5539. org-goto-exit-command 'left)
  5540. (throw 'exit nil))
  5541. (error "Not on a heading")))
  5542. (defun org-goto-right ()
  5543. "Finish `org-goto' by going to the new location."
  5544. (interactive)
  5545. (if (org-on-heading-p)
  5546. (progn
  5547. (setq org-goto-selected-point (point)
  5548. org-goto-exit-command 'right)
  5549. (throw 'exit nil))
  5550. (error "Not on a heading")))
  5551. (defun org-goto-quit ()
  5552. "Finish `org-goto' without cursor motion."
  5553. (interactive)
  5554. (setq org-goto-selected-point nil)
  5555. (setq org-goto-exit-command 'quit)
  5556. (throw 'exit nil))
  5557. ;;; Indirect buffer display of subtrees
  5558. (defvar org-indirect-dedicated-frame nil
  5559. "This is the frame being used for indirect tree display.")
  5560. (defvar org-last-indirect-buffer nil)
  5561. (defun org-tree-to-indirect-buffer (&optional arg)
  5562. "Create indirect buffer and narrow it to current subtree.
  5563. With numerical prefix ARG, go up to this level and then take that tree.
  5564. If ARG is negative, go up that many levels.
  5565. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5566. indirect buffer previously made with this command, to avoid proliferation of
  5567. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5568. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5569. is kept so that you can work with several indirect buffers at the same time.
  5570. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5571. requests that a new frame be made for the new buffer, so that the dedicated
  5572. frame is not changed."
  5573. (interactive "P")
  5574. (let ((cbuf (current-buffer))
  5575. (cwin (selected-window))
  5576. (pos (point))
  5577. beg end level heading ibuf)
  5578. (save-excursion
  5579. (org-back-to-heading t)
  5580. (when (numberp arg)
  5581. (setq level (org-outline-level))
  5582. (if (< arg 0) (setq arg (+ level arg)))
  5583. (while (> (setq level (org-outline-level)) arg)
  5584. (outline-up-heading 1 t)))
  5585. (setq beg (point)
  5586. heading (org-get-heading))
  5587. (org-end-of-subtree t) (setq end (point)))
  5588. (if (and (buffer-live-p org-last-indirect-buffer)
  5589. (not (eq org-indirect-buffer-display 'new-frame))
  5590. (not arg))
  5591. (kill-buffer org-last-indirect-buffer))
  5592. (setq ibuf (org-get-indirect-buffer cbuf)
  5593. org-last-indirect-buffer ibuf)
  5594. (cond
  5595. ((or (eq org-indirect-buffer-display 'new-frame)
  5596. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5597. (select-frame (make-frame))
  5598. (delete-other-windows)
  5599. (switch-to-buffer ibuf)
  5600. (org-set-frame-title heading))
  5601. ((eq org-indirect-buffer-display 'dedicated-frame)
  5602. (raise-frame
  5603. (select-frame (or (and org-indirect-dedicated-frame
  5604. (frame-live-p org-indirect-dedicated-frame)
  5605. org-indirect-dedicated-frame)
  5606. (setq org-indirect-dedicated-frame (make-frame)))))
  5607. (delete-other-windows)
  5608. (switch-to-buffer ibuf)
  5609. (org-set-frame-title (concat "Indirect: " heading)))
  5610. ((eq org-indirect-buffer-display 'current-window)
  5611. (switch-to-buffer ibuf))
  5612. ((eq org-indirect-buffer-display 'other-window)
  5613. (pop-to-buffer ibuf))
  5614. (t (error "Invalid value.")))
  5615. (if (featurep 'xemacs)
  5616. (save-excursion (org-mode) (turn-on-font-lock)))
  5617. (narrow-to-region beg end)
  5618. (show-all)
  5619. (goto-char pos)
  5620. (and (window-live-p cwin) (select-window cwin))))
  5621. (defun org-get-indirect-buffer (&optional buffer)
  5622. (setq buffer (or buffer (current-buffer)))
  5623. (let ((n 1) (base (buffer-name buffer)) bname)
  5624. (while (buffer-live-p
  5625. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5626. (setq n (1+ n)))
  5627. (condition-case nil
  5628. (make-indirect-buffer buffer bname 'clone)
  5629. (error (make-indirect-buffer buffer bname)))))
  5630. (defun org-set-frame-title (title)
  5631. "Set the title of the current frame to the string TITLE."
  5632. ;; FIXME: how to name a single frame in XEmacs???
  5633. (unless (featurep 'xemacs)
  5634. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5635. ;;;; Structure editing
  5636. ;;; Inserting headlines
  5637. (defun org-insert-heading (&optional force-heading)
  5638. "Insert a new heading or item with same depth at point.
  5639. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5640. If point is at the beginning of a headline, insert a sibling before the
  5641. current headline. If point is not at the beginning, do not split the line,
  5642. but create the new hedline after the current line."
  5643. (interactive "P")
  5644. (if (= (buffer-size) 0)
  5645. (insert "\n* ")
  5646. (when (or force-heading (not (org-insert-item)))
  5647. (let* ((head (save-excursion
  5648. (condition-case nil
  5649. (progn
  5650. (org-back-to-heading)
  5651. (match-string 0))
  5652. (error "*"))))
  5653. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5654. pos)
  5655. (cond
  5656. ((and (org-on-heading-p) (bolp)
  5657. (or (bobp)
  5658. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5659. ;; insert before the current line
  5660. (open-line (if blank 2 1)))
  5661. ((and (bolp)
  5662. (or (bobp)
  5663. (save-excursion
  5664. (backward-char 1) (not (org-invisible-p)))))
  5665. ;; insert right here
  5666. nil)
  5667. (t
  5668. ; ;; in the middle of the line
  5669. ; (org-show-entry)
  5670. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5671. ; (if (and
  5672. ; (org-on-heading-p)
  5673. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5674. ; ;; protect the tags
  5675. ;; (let ((tags (match-string 2)) pos)
  5676. ; (delete-region (match-beginning 1) (match-end 1))
  5677. ; (setq pos (point-at-bol))
  5678. ; (newline (if blank 2 1))
  5679. ; (save-excursion
  5680. ; (goto-char pos)
  5681. ; (end-of-line 1)
  5682. ; (insert " " tags)
  5683. ; (org-set-tags nil 'align)))
  5684. ; (newline (if blank 2 1)))
  5685. ; (newline (if blank 2 1))))
  5686. ;; in the middle of the line
  5687. (org-show-entry)
  5688. (let ((split
  5689. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5690. tags pos)
  5691. (if (org-on-heading-p)
  5692. (progn
  5693. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5694. (setq tags (and (match-end 2) (match-string 2)))
  5695. (and (match-end 1)
  5696. (delete-region (match-beginning 1) (match-end 1)))
  5697. (setq pos (point-at-bol))
  5698. (or split (end-of-line 1))
  5699. (delete-horizontal-space)
  5700. (newline (if blank 2 1))
  5701. (when tags
  5702. (save-excursion
  5703. (goto-char pos)
  5704. (end-of-line 1)
  5705. (insert " " tags)
  5706. (org-set-tags nil 'align))))
  5707. (or split (end-of-line 1))
  5708. (newline (if blank 2 1))))))
  5709. (insert head) (just-one-space)
  5710. (setq pos (point))
  5711. (end-of-line 1)
  5712. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5713. (run-hooks 'org-insert-heading-hook)))))
  5714. (defun org-insert-heading-after-current ()
  5715. "Insert a new heading with same level as current, after current subtree."
  5716. (interactive)
  5717. (org-back-to-heading)
  5718. (org-insert-heading)
  5719. (org-move-subtree-down)
  5720. (end-of-line 1))
  5721. (defun org-insert-todo-heading (arg)
  5722. "Insert a new heading with the same level and TODO state as current heading.
  5723. If the heading has no TODO state, or if the state is DONE, use the first
  5724. state (TODO by default). Also with prefix arg, force first state."
  5725. (interactive "P")
  5726. (when (not (org-insert-item 'checkbox))
  5727. (org-insert-heading)
  5728. (save-excursion
  5729. (org-back-to-heading)
  5730. (outline-previous-heading)
  5731. (looking-at org-todo-line-regexp))
  5732. (if (or arg
  5733. (not (match-beginning 2))
  5734. (member (match-string 2) org-done-keywords))
  5735. (insert (car org-todo-keywords-1) " ")
  5736. (insert (match-string 2) " "))))
  5737. (defun org-insert-subheading (arg)
  5738. "Insert a new subheading and demote it.
  5739. Works for outline headings and for plain lists alike."
  5740. (interactive "P")
  5741. (org-insert-heading arg)
  5742. (cond
  5743. ((org-on-heading-p) (org-do-demote))
  5744. ((org-at-item-p) (org-indent-item 1))))
  5745. (defun org-insert-todo-subheading (arg)
  5746. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5747. Works for outline headings and for plain lists alike."
  5748. (interactive "P")
  5749. (org-insert-todo-heading arg)
  5750. (cond
  5751. ((org-on-heading-p) (org-do-demote))
  5752. ((org-at-item-p) (org-indent-item 1))))
  5753. ;;; Promotion and Demotion
  5754. (defun org-promote-subtree ()
  5755. "Promote the entire subtree.
  5756. See also `org-promote'."
  5757. (interactive)
  5758. (save-excursion
  5759. (org-map-tree 'org-promote))
  5760. (org-fix-position-after-promote))
  5761. (defun org-demote-subtree ()
  5762. "Demote the entire subtree. See `org-demote'.
  5763. See also `org-promote'."
  5764. (interactive)
  5765. (save-excursion
  5766. (org-map-tree 'org-demote))
  5767. (org-fix-position-after-promote))
  5768. (defun org-do-promote ()
  5769. "Promote the current heading higher up the tree.
  5770. If the region is active in `transient-mark-mode', promote all headings
  5771. in the region."
  5772. (interactive)
  5773. (save-excursion
  5774. (if (org-region-active-p)
  5775. (org-map-region 'org-promote (region-beginning) (region-end))
  5776. (org-promote)))
  5777. (org-fix-position-after-promote))
  5778. (defun org-do-demote ()
  5779. "Demote the current heading lower down the tree.
  5780. If the region is active in `transient-mark-mode', demote all headings
  5781. in the region."
  5782. (interactive)
  5783. (save-excursion
  5784. (if (org-region-active-p)
  5785. (org-map-region 'org-demote (region-beginning) (region-end))
  5786. (org-demote)))
  5787. (org-fix-position-after-promote))
  5788. (defun org-fix-position-after-promote ()
  5789. "Make sure that after pro/demotion cursor position is right."
  5790. (let ((pos (point)))
  5791. (when (save-excursion
  5792. (beginning-of-line 1)
  5793. (looking-at org-todo-line-regexp)
  5794. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5795. (cond ((eobp) (insert " "))
  5796. ((eolp) (insert " "))
  5797. ((equal (char-after) ?\ ) (forward-char 1))))))
  5798. (defun org-reduced-level (l)
  5799. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5800. (defun org-get-valid-level (level &optional change)
  5801. "Rectify a level change under the influence of `org-odd-levels-only'
  5802. LEVEL is a current level, CHANGE is by how much the level should be
  5803. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5804. even level numbers will become the next higher odd number."
  5805. (if org-odd-levels-only
  5806. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5807. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5808. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5809. (max 1 (+ level change))))
  5810. (if (featurep 'xemacs)
  5811. (define-obsolete-function-alias 'org-get-legal-level
  5812. 'org-get-valid-level)
  5813. (define-obsolete-function-alias 'org-get-legal-level
  5814. 'org-get-valid-level "23.1"))
  5815. (defun org-promote ()
  5816. "Promote the current heading higher up the tree.
  5817. If the region is active in `transient-mark-mode', promote all headings
  5818. in the region."
  5819. (org-back-to-heading t)
  5820. (let* ((level (save-match-data (funcall outline-level)))
  5821. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5822. (diff (abs (- level (length up-head) -1))))
  5823. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5824. (replace-match up-head nil t)
  5825. ;; Fixup tag positioning
  5826. (and org-auto-align-tags (org-set-tags nil t))
  5827. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5828. (defun org-demote ()
  5829. "Demote the current heading lower down the tree.
  5830. If the region is active in `transient-mark-mode', demote all headings
  5831. in the region."
  5832. (org-back-to-heading t)
  5833. (let* ((level (save-match-data (funcall outline-level)))
  5834. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5835. (diff (abs (- level (length down-head) -1))))
  5836. (replace-match down-head nil t)
  5837. ;; Fixup tag positioning
  5838. (and org-auto-align-tags (org-set-tags nil t))
  5839. (if org-adapt-indentation (org-fixup-indentation diff))))
  5840. (defun org-map-tree (fun)
  5841. "Call FUN for every heading underneath the current one."
  5842. (org-back-to-heading)
  5843. (let ((level (funcall outline-level)))
  5844. (save-excursion
  5845. (funcall fun)
  5846. (while (and (progn
  5847. (outline-next-heading)
  5848. (> (funcall outline-level) level))
  5849. (not (eobp)))
  5850. (funcall fun)))))
  5851. (defun org-map-region (fun beg end)
  5852. "Call FUN for every heading between BEG and END."
  5853. (let ((org-ignore-region t))
  5854. (save-excursion
  5855. (setq end (copy-marker end))
  5856. (goto-char beg)
  5857. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5858. (< (point) end))
  5859. (funcall fun))
  5860. (while (and (progn
  5861. (outline-next-heading)
  5862. (< (point) end))
  5863. (not (eobp)))
  5864. (funcall fun)))))
  5865. (defun org-fixup-indentation (diff)
  5866. "Change the indentation in the current entry by DIFF
  5867. However, if any line in the current entry has no indentation, or if it
  5868. would end up with no indentation after the change, nothing at all is done."
  5869. (save-excursion
  5870. (let ((end (save-excursion (outline-next-heading)
  5871. (point-marker)))
  5872. (prohibit (if (> diff 0)
  5873. "^\\S-"
  5874. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5875. col)
  5876. (unless (save-excursion (end-of-line 1)
  5877. (re-search-forward prohibit end t))
  5878. (while (and (< (point) end)
  5879. (re-search-forward "^[ \t]+" end t))
  5880. (goto-char (match-end 0))
  5881. (setq col (current-column))
  5882. (if (< diff 0) (replace-match ""))
  5883. (indent-to (+ diff col))))
  5884. (move-marker end nil))))
  5885. (defun org-convert-to-odd-levels ()
  5886. "Convert an org-mode file with all levels allowed to one with odd levels.
  5887. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5888. level 5 etc."
  5889. (interactive)
  5890. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5891. (let ((org-odd-levels-only nil) n)
  5892. (save-excursion
  5893. (goto-char (point-min))
  5894. (while (re-search-forward "^\\*\\*+ " nil t)
  5895. (setq n (- (length (match-string 0)) 2))
  5896. (while (>= (setq n (1- n)) 0)
  5897. (org-demote))
  5898. (end-of-line 1))))))
  5899. (defun org-convert-to-oddeven-levels ()
  5900. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5901. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5902. section with an even level, conversion would destroy the structure of the file. An error
  5903. is signaled in this case."
  5904. (interactive)
  5905. (goto-char (point-min))
  5906. ;; First check if there are no even levels
  5907. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5908. (org-show-context t)
  5909. (error "Not all levels are odd in this file. Conversion not possible."))
  5910. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5911. (let ((org-odd-levels-only nil) n)
  5912. (save-excursion
  5913. (goto-char (point-min))
  5914. (while (re-search-forward "^\\*\\*+ " nil t)
  5915. (setq n (/ (1- (length (match-string 0))) 2))
  5916. (while (>= (setq n (1- n)) 0)
  5917. (org-promote))
  5918. (end-of-line 1))))))
  5919. (defun org-tr-level (n)
  5920. "Make N odd if required."
  5921. (if org-odd-levels-only (1+ (/ n 2)) n))
  5922. ;;; Vertical tree motion, cutting and pasting of subtrees
  5923. (defun org-move-subtree-up (&optional arg)
  5924. "Move the current subtree up past ARG headlines of the same level."
  5925. (interactive "p")
  5926. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5927. (defun org-move-subtree-down (&optional arg)
  5928. "Move the current subtree down past ARG headlines of the same level."
  5929. (interactive "p")
  5930. (setq arg (prefix-numeric-value arg))
  5931. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5932. 'outline-get-last-sibling))
  5933. (ins-point (make-marker))
  5934. (cnt (abs arg))
  5935. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5936. ;; Select the tree
  5937. (org-back-to-heading)
  5938. (setq beg0 (point))
  5939. (save-excursion
  5940. (setq ne-beg (org-back-over-empty-lines))
  5941. (setq beg (point)))
  5942. (save-match-data
  5943. (save-excursion (outline-end-of-heading)
  5944. (setq folded (org-invisible-p)))
  5945. (outline-end-of-subtree))
  5946. (outline-next-heading)
  5947. (setq ne-end (org-back-over-empty-lines))
  5948. (setq end (point))
  5949. (goto-char beg0)
  5950. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5951. ;; include less whitespace
  5952. (save-excursion
  5953. (goto-char beg)
  5954. (forward-line (- ne-beg ne-end))
  5955. (setq beg (point))))
  5956. ;; Find insertion point, with error handling
  5957. (while (> cnt 0)
  5958. (or (and (funcall movfunc) (looking-at outline-regexp))
  5959. (progn (goto-char beg0)
  5960. (error "Cannot move past superior level or buffer limit")))
  5961. (setq cnt (1- cnt)))
  5962. (if (> arg 0)
  5963. ;; Moving forward - still need to move over subtree
  5964. (progn (org-end-of-subtree t t)
  5965. (save-excursion
  5966. (org-back-over-empty-lines)
  5967. (or (bolp) (newline)))))
  5968. (setq ne-ins (org-back-over-empty-lines))
  5969. (move-marker ins-point (point))
  5970. (setq txt (buffer-substring beg end))
  5971. (delete-region beg end)
  5972. (outline-flag-region (1- beg) beg nil)
  5973. (outline-flag-region (1- (point)) (point) nil)
  5974. (insert txt)
  5975. (or (bolp) (insert "\n"))
  5976. (setq ins-end (point))
  5977. (goto-char ins-point)
  5978. (org-skip-whitespace)
  5979. (when (and (< arg 0)
  5980. (org-first-sibling-p)
  5981. (> ne-ins ne-beg))
  5982. ;; Move whitespace back to beginning
  5983. (save-excursion
  5984. (goto-char ins-end)
  5985. (let ((kill-whole-line t))
  5986. (kill-line (- ne-ins ne-beg)) (point)))
  5987. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5988. (move-marker ins-point nil)
  5989. (org-compact-display-after-subtree-move)
  5990. (unless folded
  5991. (org-show-entry)
  5992. (show-children)
  5993. (org-cycle-hide-drawers 'children))))
  5994. (defvar org-subtree-clip ""
  5995. "Clipboard for cut and paste of subtrees.
  5996. This is actually only a copy of the kill, because we use the normal kill
  5997. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5998. (defvar org-subtree-clip-folded nil
  5999. "Was the last copied subtree folded?
  6000. This is used to fold the tree back after pasting.")
  6001. (defun org-cut-subtree (&optional n)
  6002. "Cut the current subtree into the clipboard.
  6003. With prefix arg N, cut this many sequential subtrees.
  6004. This is a short-hand for marking the subtree and then cutting it."
  6005. (interactive "p")
  6006. (org-copy-subtree n 'cut))
  6007. (defun org-copy-subtree (&optional n cut)
  6008. "Cut the current subtree into the clipboard.
  6009. With prefix arg N, cut this many sequential subtrees.
  6010. This is a short-hand for marking the subtree and then copying it.
  6011. If CUT is non-nil, actually cut the subtree."
  6012. (interactive "p")
  6013. (let (beg end folded (beg0 (point)))
  6014. (if (interactive-p)
  6015. (org-back-to-heading nil) ; take what looks like a subtree
  6016. (org-back-to-heading t)) ; take what is really there
  6017. (org-back-over-empty-lines)
  6018. (setq beg (point))
  6019. (skip-chars-forward " \t\r\n")
  6020. (save-match-data
  6021. (save-excursion (outline-end-of-heading)
  6022. (setq folded (org-invisible-p)))
  6023. (condition-case nil
  6024. (outline-forward-same-level (1- n))
  6025. (error nil))
  6026. (org-end-of-subtree t t))
  6027. (org-back-over-empty-lines)
  6028. (setq end (point))
  6029. (goto-char beg0)
  6030. (when (> end beg)
  6031. (setq org-subtree-clip-folded folded)
  6032. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6033. (setq org-subtree-clip (current-kill 0))
  6034. (message "%s: Subtree(s) with %d characters"
  6035. (if cut "Cut" "Copied")
  6036. (length org-subtree-clip)))))
  6037. (defun org-paste-subtree (&optional level tree)
  6038. "Paste the clipboard as a subtree, with modification of headline level.
  6039. The entire subtree is promoted or demoted in order to match a new headline
  6040. level. By default, the new level is derived from the visible headings
  6041. before and after the insertion point, and taken to be the inferior headline
  6042. level of the two. So if the previous visible heading is level 3 and the
  6043. next is level 4 (or vice versa), level 4 will be used for insertion.
  6044. This makes sure that the subtree remains an independent subtree and does
  6045. not swallow low level entries.
  6046. You can also force a different level, either by using a numeric prefix
  6047. argument, or by inserting the heading marker by hand. For example, if the
  6048. cursor is after \"*****\", then the tree will be shifted to level 5.
  6049. If you want to insert the tree as is, just use \\[yank].
  6050. If optional TREE is given, use this text instead of the kill ring."
  6051. (interactive "P")
  6052. (unless (org-kill-is-subtree-p tree)
  6053. (error "%s"
  6054. (substitute-command-keys
  6055. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6056. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  6057. (^re (concat "^\\(" outline-regexp "\\)"))
  6058. (re (concat "\\(" outline-regexp "\\)"))
  6059. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6060. (old-level (if (string-match ^re txt)
  6061. (- (match-end 0) (match-beginning 0) 1)
  6062. -1))
  6063. (force-level (cond (level (prefix-numeric-value level))
  6064. ((string-match
  6065. ^re_ (buffer-substring (point-at-bol) (point)))
  6066. (- (match-end 1) (match-beginning 1)))
  6067. (t nil)))
  6068. (previous-level (save-excursion
  6069. (condition-case nil
  6070. (progn
  6071. (outline-previous-visible-heading 1)
  6072. (if (looking-at re)
  6073. (- (match-end 0) (match-beginning 0) 1)
  6074. 1))
  6075. (error 1))))
  6076. (next-level (save-excursion
  6077. (condition-case nil
  6078. (progn
  6079. (or (looking-at outline-regexp)
  6080. (outline-next-visible-heading 1))
  6081. (if (looking-at re)
  6082. (- (match-end 0) (match-beginning 0) 1)
  6083. 1))
  6084. (error 1))))
  6085. (new-level (or force-level (max previous-level next-level)))
  6086. (shift (if (or (= old-level -1)
  6087. (= new-level -1)
  6088. (= old-level new-level))
  6089. 0
  6090. (- new-level old-level)))
  6091. (delta (if (> shift 0) -1 1))
  6092. (func (if (> shift 0) 'org-demote 'org-promote))
  6093. (org-odd-levels-only nil)
  6094. beg end)
  6095. ;; Remove the forced level indicator
  6096. (if force-level
  6097. (delete-region (point-at-bol) (point)))
  6098. ;; Paste
  6099. (beginning-of-line 1)
  6100. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6101. (setq beg (point))
  6102. (insert-before-markers txt) ;; FIXME: correct fix????
  6103. (unless (string-match "\n\\'" txt) (insert "\n"))
  6104. (setq end (point))
  6105. (goto-char beg)
  6106. (skip-chars-forward " \t\n\r")
  6107. (setq beg (point))
  6108. ;; Shift if necessary
  6109. (unless (= shift 0)
  6110. (save-restriction
  6111. (narrow-to-region beg end)
  6112. (while (not (= shift 0))
  6113. (org-map-region func (point-min) (point-max))
  6114. (setq shift (+ delta shift)))
  6115. (goto-char (point-min))))
  6116. (when (interactive-p)
  6117. (message "Clipboard pasted as level %d subtree" new-level))
  6118. (if (and kill-ring
  6119. (eq org-subtree-clip (current-kill 0))
  6120. org-subtree-clip-folded)
  6121. ;; The tree was folded before it was killed/copied
  6122. (hide-subtree))))
  6123. (defun org-kill-is-subtree-p (&optional txt)
  6124. "Check if the current kill is an outline subtree, or a set of trees.
  6125. Returns nil if kill does not start with a headline, or if the first
  6126. headline level is not the largest headline level in the tree.
  6127. So this will actually accept several entries of equal levels as well,
  6128. which is OK for `org-paste-subtree'.
  6129. If optional TXT is given, check this string instead of the current kill."
  6130. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6131. (start-level (and kill
  6132. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6133. org-outline-regexp "\\)")
  6134. kill)
  6135. (- (match-end 2) (match-beginning 2) 1)))
  6136. (re (concat "^" org-outline-regexp))
  6137. (start (1+ (match-beginning 2))))
  6138. (if (not start-level)
  6139. (progn
  6140. nil) ;; does not even start with a heading
  6141. (catch 'exit
  6142. (while (setq start (string-match re kill (1+ start)))
  6143. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6144. (throw 'exit nil)))
  6145. t))))
  6146. (defun org-narrow-to-subtree ()
  6147. "Narrow buffer to the current subtree."
  6148. (interactive)
  6149. (save-excursion
  6150. (save-match-data
  6151. (narrow-to-region
  6152. (progn (org-back-to-heading) (point))
  6153. (progn (org-end-of-subtree t t) (point))))))
  6154. ;;; Outline Sorting
  6155. (defun org-sort (with-case)
  6156. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6157. Optional argument WITH-CASE means sort case-sensitively."
  6158. (interactive "P")
  6159. (if (org-at-table-p)
  6160. (org-call-with-arg 'org-table-sort-lines with-case)
  6161. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6162. (defvar org-priority-regexp) ; defined later in the file
  6163. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6164. "Sort entries on a certain level of an outline tree.
  6165. If there is an active region, the entries in the region are sorted.
  6166. Else, if the cursor is before the first entry, sort the top-level items.
  6167. Else, the children of the entry at point are sorted.
  6168. Sorting can be alphabetically, numerically, and by date/time as given by
  6169. the first time stamp in the entry. The command prompts for the sorting
  6170. type unless it has been given to the function through the SORTING-TYPE
  6171. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6172. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6173. called with point at the beginning of the record. It must return either
  6174. a string or a number that should serve as the sorting key for that record.
  6175. Comparing entries ignores case by default. However, with an optional argument
  6176. WITH-CASE, the sorting considers case as well."
  6177. (interactive "P")
  6178. (let ((case-func (if with-case 'identity 'downcase))
  6179. start beg end stars re re2
  6180. txt what tmp plain-list-p)
  6181. ;; Find beginning and end of region to sort
  6182. (cond
  6183. ((org-region-active-p)
  6184. ;; we will sort the region
  6185. (setq end (region-end)
  6186. what "region")
  6187. (goto-char (region-beginning))
  6188. (if (not (org-on-heading-p)) (outline-next-heading))
  6189. (setq start (point)))
  6190. ((org-at-item-p)
  6191. ;; we will sort this plain list
  6192. (org-beginning-of-item-list) (setq start (point))
  6193. (org-end-of-item-list) (setq end (point))
  6194. (goto-char start)
  6195. (setq plain-list-p t
  6196. what "plain list"))
  6197. ((or (org-on-heading-p)
  6198. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6199. ;; we will sort the children of the current headline
  6200. (org-back-to-heading)
  6201. (setq start (point)
  6202. end (progn (org-end-of-subtree t t)
  6203. (org-back-over-empty-lines)
  6204. (point))
  6205. what "children")
  6206. (goto-char start)
  6207. (show-subtree)
  6208. (outline-next-heading))
  6209. (t
  6210. ;; we will sort the top-level entries in this file
  6211. (goto-char (point-min))
  6212. (or (org-on-heading-p) (outline-next-heading))
  6213. (setq start (point) end (point-max) what "top-level")
  6214. (goto-char start)
  6215. (show-all)))
  6216. (setq beg (point))
  6217. (if (>= beg end) (error "Nothing to sort"))
  6218. (unless plain-list-p
  6219. (looking-at "\\(\\*+\\)")
  6220. (setq stars (match-string 1)
  6221. re (concat "^" (regexp-quote stars) " +")
  6222. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6223. txt (buffer-substring beg end))
  6224. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6225. (if (and (not (equal stars "*")) (string-match re2 txt))
  6226. (error "Region to sort contains a level above the first entry")))
  6227. (unless sorting-type
  6228. (message
  6229. (if plain-list-p
  6230. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6231. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6232. what)
  6233. (setq sorting-type (read-char-exclusive))
  6234. (and (= (downcase sorting-type) ?f)
  6235. (setq getkey-func
  6236. (completing-read "Sort using function: "
  6237. obarray 'fboundp t nil nil))
  6238. (setq getkey-func (intern getkey-func)))
  6239. (and (= (downcase sorting-type) ?r)
  6240. (setq property
  6241. (completing-read "Property: "
  6242. (mapcar 'list (org-buffer-property-keys t))
  6243. nil t))))
  6244. (message "Sorting entries...")
  6245. (save-restriction
  6246. (narrow-to-region start end)
  6247. (let ((dcst (downcase sorting-type))
  6248. (now (current-time)))
  6249. (sort-subr
  6250. (/= dcst sorting-type)
  6251. ;; This function moves to the beginning character of the "record" to
  6252. ;; be sorted.
  6253. (if plain-list-p
  6254. (lambda nil
  6255. (if (org-at-item-p) t (goto-char (point-max))))
  6256. (lambda nil
  6257. (if (re-search-forward re nil t)
  6258. (goto-char (match-beginning 0))
  6259. (goto-char (point-max)))))
  6260. ;; This function moves to the last character of the "record" being
  6261. ;; sorted.
  6262. (if plain-list-p
  6263. 'org-end-of-item
  6264. (lambda nil
  6265. (save-match-data
  6266. (condition-case nil
  6267. (outline-forward-same-level 1)
  6268. (error
  6269. (goto-char (point-max)))))))
  6270. ;; This function returns the value that gets sorted against.
  6271. (if plain-list-p
  6272. (lambda nil
  6273. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6274. (cond
  6275. ((= dcst ?n)
  6276. (string-to-number (buffer-substring (match-end 0)
  6277. (point-at-eol))))
  6278. ((= dcst ?a)
  6279. (buffer-substring (match-end 0) (point-at-eol)))
  6280. ((= dcst ?t)
  6281. (if (re-search-forward org-ts-regexp
  6282. (point-at-eol) t)
  6283. (org-time-string-to-time (match-string 0))
  6284. now))
  6285. ((= dcst ?f)
  6286. (if getkey-func
  6287. (progn
  6288. (setq tmp (funcall getkey-func))
  6289. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6290. tmp)
  6291. (error "Invalid key function `%s'" getkey-func)))
  6292. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6293. (lambda nil
  6294. (cond
  6295. ((= dcst ?n)
  6296. (if (looking-at outline-regexp)
  6297. (string-to-number (buffer-substring (match-end 0)
  6298. (point-at-eol)))
  6299. nil))
  6300. ((= dcst ?a)
  6301. (funcall case-func (buffer-substring (point-at-bol)
  6302. (point-at-eol))))
  6303. ((= dcst ?t)
  6304. (if (re-search-forward org-ts-regexp
  6305. (save-excursion
  6306. (forward-line 2)
  6307. (point)) t)
  6308. (org-time-string-to-time (match-string 0))
  6309. now))
  6310. ((= dcst ?p)
  6311. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6312. (string-to-char (match-string 2))
  6313. org-default-priority))
  6314. ((= dcst ?r)
  6315. (or (org-entry-get nil property) ""))
  6316. ((= dcst ?f)
  6317. (if getkey-func
  6318. (progn
  6319. (setq tmp (funcall getkey-func))
  6320. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6321. tmp)
  6322. (error "Invalid key function `%s'" getkey-func)))
  6323. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6324. nil
  6325. (cond
  6326. ((= dcst ?a) 'string<)
  6327. ((= dcst ?t) 'time-less-p)
  6328. (t nil)))))
  6329. (message "Sorting entries...done")))
  6330. (defun org-do-sort (table what &optional with-case sorting-type)
  6331. "Sort TABLE of WHAT according to SORTING-TYPE.
  6332. The user will be prompted for the SORTING-TYPE if the call to this
  6333. function does not specify it. WHAT is only for the prompt, to indicate
  6334. what is being sorted. The sorting key will be extracted from
  6335. the car of the elements of the table.
  6336. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6337. (unless sorting-type
  6338. (message
  6339. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6340. what)
  6341. (setq sorting-type (read-char-exclusive)))
  6342. (let ((dcst (downcase sorting-type))
  6343. extractfun comparefun)
  6344. ;; Define the appropriate functions
  6345. (cond
  6346. ((= dcst ?n)
  6347. (setq extractfun 'string-to-number
  6348. comparefun (if (= dcst sorting-type) '< '>)))
  6349. ((= dcst ?a)
  6350. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6351. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6352. comparefun (if (= dcst sorting-type)
  6353. 'string<
  6354. (lambda (a b) (and (not (string< a b))
  6355. (not (string= a b)))))))
  6356. ((= dcst ?t)
  6357. (setq extractfun
  6358. (lambda (x)
  6359. (if (string-match org-ts-regexp x)
  6360. (time-to-seconds
  6361. (org-time-string-to-time (match-string 0 x)))
  6362. 0))
  6363. comparefun (if (= dcst sorting-type) '< '>)))
  6364. (t (error "Invalid sorting type `%c'" sorting-type)))
  6365. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6366. table)
  6367. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6368. ;;;; Plain list items, including checkboxes
  6369. ;;; Plain list items
  6370. (defun org-at-item-p ()
  6371. "Is point in a line starting a hand-formatted item?"
  6372. (let ((llt org-plain-list-ordered-item-terminator))
  6373. (save-excursion
  6374. (goto-char (point-at-bol))
  6375. (looking-at
  6376. (cond
  6377. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6378. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6379. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6380. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6381. (defun org-in-item-p ()
  6382. "It the cursor inside a plain list item.
  6383. Does not have to be the first line."
  6384. (save-excursion
  6385. (condition-case nil
  6386. (progn
  6387. (org-beginning-of-item)
  6388. (org-at-item-p)
  6389. t)
  6390. (error nil))))
  6391. (defun org-insert-item (&optional checkbox)
  6392. "Insert a new item at the current level.
  6393. Return t when things worked, nil when we are not in an item."
  6394. (when (save-excursion
  6395. (condition-case nil
  6396. (progn
  6397. (org-beginning-of-item)
  6398. (org-at-item-p)
  6399. (if (org-invisible-p) (error "Invisible item"))
  6400. t)
  6401. (error nil)))
  6402. (let* ((bul (match-string 0))
  6403. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6404. (match-end 0)))
  6405. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6406. pos)
  6407. (cond
  6408. ((and (org-at-item-p) (<= (point) eow))
  6409. ;; before the bullet
  6410. (beginning-of-line 1)
  6411. (open-line (if blank 2 1)))
  6412. ((<= (point) eow)
  6413. (beginning-of-line 1))
  6414. (t
  6415. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6416. (end-of-line 1)
  6417. (delete-horizontal-space))
  6418. (newline (if blank 2 1))))
  6419. (insert bul (if checkbox "[ ]" ""))
  6420. (just-one-space)
  6421. (setq pos (point))
  6422. (end-of-line 1)
  6423. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6424. (org-maybe-renumber-ordered-list)
  6425. (and checkbox (org-update-checkbox-count-maybe))
  6426. t))
  6427. ;;; Checkboxes
  6428. (defun org-at-item-checkbox-p ()
  6429. "Is point at a line starting a plain-list item with a checklet?"
  6430. (and (org-at-item-p)
  6431. (save-excursion
  6432. (goto-char (match-end 0))
  6433. (skip-chars-forward " \t")
  6434. (looking-at "\\[[- X]\\]"))))
  6435. (defun org-toggle-checkbox (&optional arg)
  6436. "Toggle the checkbox in the current line."
  6437. (interactive "P")
  6438. (catch 'exit
  6439. (let (beg end status (firstnew 'unknown))
  6440. (cond
  6441. ((org-region-active-p)
  6442. (setq beg (region-beginning) end (region-end)))
  6443. ((org-on-heading-p)
  6444. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6445. ((org-at-item-checkbox-p)
  6446. (let ((pos (point)))
  6447. (replace-match
  6448. (cond (arg "[-]")
  6449. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6450. (t "[ ]"))
  6451. t t)
  6452. (goto-char pos))
  6453. (throw 'exit t))
  6454. (t (error "Not at a checkbox or heading, and no active region")))
  6455. (save-excursion
  6456. (goto-char beg)
  6457. (while (< (point) end)
  6458. (when (org-at-item-checkbox-p)
  6459. (setq status (equal (match-string 0) "[X]"))
  6460. (when (eq firstnew 'unknown)
  6461. (setq firstnew (not status)))
  6462. (replace-match
  6463. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6464. (beginning-of-line 2)))))
  6465. (org-update-checkbox-count-maybe))
  6466. (defun org-update-checkbox-count-maybe ()
  6467. "Update checkbox statistics unless turned off by user."
  6468. (when org-provide-checkbox-statistics
  6469. (org-update-checkbox-count)))
  6470. (defun org-update-checkbox-count (&optional all)
  6471. "Update the checkbox statistics in the current section.
  6472. This will find all statistic cookies like [57%] and [6/12] and update them
  6473. with the current numbers. With optional prefix argument ALL, do this for
  6474. the whole buffer."
  6475. (interactive "P")
  6476. (save-excursion
  6477. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6478. (beg (condition-case nil
  6479. (progn (outline-back-to-heading) (point))
  6480. (error (point-min))))
  6481. (end (move-marker (make-marker)
  6482. (progn (outline-next-heading) (point))))
  6483. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6484. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6485. (re-find (concat re "\\|" re-box))
  6486. beg-cookie end-cookie is-percent c-on c-off lim
  6487. eline curr-ind next-ind continue-from startsearch
  6488. (cstat 0)
  6489. )
  6490. (when all
  6491. (goto-char (point-min))
  6492. (outline-next-heading)
  6493. (setq beg (point) end (point-max)))
  6494. (goto-char end)
  6495. ;; find each statistic cookie
  6496. (while (re-search-backward re-find beg t)
  6497. (setq beg-cookie (match-beginning 1)
  6498. end-cookie (match-end 1)
  6499. cstat (+ cstat (if end-cookie 1 0))
  6500. startsearch (point-at-eol)
  6501. continue-from (point-at-bol)
  6502. is-percent (match-beginning 2)
  6503. lim (cond
  6504. ((org-on-heading-p) (outline-next-heading) (point))
  6505. ((org-at-item-p) (org-end-of-item) (point))
  6506. (t nil))
  6507. c-on 0
  6508. c-off 0)
  6509. (when lim
  6510. ;; find first checkbox for this cookie and gather
  6511. ;; statistics from all that are at this indentation level
  6512. (goto-char startsearch)
  6513. (if (re-search-forward re-box lim t)
  6514. (progn
  6515. (org-beginning-of-item)
  6516. (setq curr-ind (org-get-indentation))
  6517. (setq next-ind curr-ind)
  6518. (while (= curr-ind next-ind)
  6519. (save-excursion (end-of-line) (setq eline (point)))
  6520. (if (re-search-forward re-box eline t)
  6521. (if (member (match-string 2) '("[ ]" "[-]"))
  6522. (setq c-off (1+ c-off))
  6523. (setq c-on (1+ c-on))
  6524. )
  6525. )
  6526. (org-end-of-item)
  6527. (setq next-ind (org-get-indentation))
  6528. )))
  6529. (goto-char continue-from)
  6530. ;; update cookie
  6531. (when end-cookie
  6532. (delete-region beg-cookie end-cookie)
  6533. (goto-char beg-cookie)
  6534. (insert
  6535. (if is-percent
  6536. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6537. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6538. ;; update items checkbox if it has one
  6539. (when (org-at-item-p)
  6540. (org-beginning-of-item)
  6541. (when (and (> (+ c-on c-off) 0)
  6542. (re-search-forward re-box (point-at-eol) t))
  6543. (setq beg-cookie (match-beginning 2)
  6544. end-cookie (match-end 2))
  6545. (delete-region beg-cookie end-cookie)
  6546. (goto-char beg-cookie)
  6547. (cond ((= c-off 0) (insert "[X]"))
  6548. ((= c-on 0) (insert "[ ]"))
  6549. (t (insert "[-]")))
  6550. )))
  6551. (goto-char continue-from))
  6552. (when (interactive-p)
  6553. (message "Checkbox satistics updated %s (%d places)"
  6554. (if all "in entire file" "in current outline entry") cstat)))))
  6555. (defun org-get-checkbox-statistics-face ()
  6556. "Select the face for checkbox statistics.
  6557. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6558. it will be `org-todo'."
  6559. (if (match-end 1)
  6560. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6561. (if (and (> (match-end 2) (match-beginning 2))
  6562. (equal (match-string 2) (match-string 3)))
  6563. 'org-done
  6564. 'org-todo)))
  6565. (defun org-get-indentation (&optional line)
  6566. "Get the indentation of the current line, interpreting tabs.
  6567. When LINE is given, assume it represents a line and compute its indentation."
  6568. (if line
  6569. (if (string-match "^ *" (org-remove-tabs line))
  6570. (match-end 0))
  6571. (save-excursion
  6572. (beginning-of-line 1)
  6573. (skip-chars-forward " \t")
  6574. (current-column))))
  6575. (defun org-remove-tabs (s &optional width)
  6576. "Replace tabulators in S with spaces.
  6577. Assumes that s is a single line, starting in column 0."
  6578. (setq width (or width tab-width))
  6579. (while (string-match "\t" s)
  6580. (setq s (replace-match
  6581. (make-string
  6582. (- (* width (/ (+ (match-beginning 0) width) width))
  6583. (match-beginning 0)) ?\ )
  6584. t t s)))
  6585. s)
  6586. (defun org-fix-indentation (line ind)
  6587. "Fix indentation in LINE.
  6588. IND is a cons cell with target and minimum indentation.
  6589. If the current indenation in LINE is smaller than the minimum,
  6590. leave it alone. If it is larger than ind, set it to the target."
  6591. (let* ((l (org-remove-tabs line))
  6592. (i (org-get-indentation l))
  6593. (i1 (car ind)) (i2 (cdr ind)))
  6594. (if (>= i i2) (setq l (substring line i2)))
  6595. (if (> i1 0)
  6596. (concat (make-string i1 ?\ ) l)
  6597. l)))
  6598. (defcustom org-empty-line-terminates-plain-lists nil
  6599. "Non-nil means, an empty line ends all plain list levels.
  6600. When nil, empty lines are part of the preceeding item."
  6601. :group 'org-plain-lists
  6602. :type 'boolean)
  6603. (defun org-beginning-of-item ()
  6604. "Go to the beginning of the current hand-formatted item.
  6605. If the cursor is not in an item, throw an error."
  6606. (interactive)
  6607. (let ((pos (point))
  6608. (limit (save-excursion
  6609. (condition-case nil
  6610. (progn
  6611. (org-back-to-heading)
  6612. (beginning-of-line 2) (point))
  6613. (error (point-min)))))
  6614. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6615. ind ind1)
  6616. (if (org-at-item-p)
  6617. (beginning-of-line 1)
  6618. (beginning-of-line 1)
  6619. (skip-chars-forward " \t")
  6620. (setq ind (current-column))
  6621. (if (catch 'exit
  6622. (while t
  6623. (beginning-of-line 0)
  6624. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6625. (if (looking-at "[ \t]*$")
  6626. (setq ind1 ind-empty)
  6627. (skip-chars-forward " \t")
  6628. (setq ind1 (current-column)))
  6629. (if (< ind1 ind)
  6630. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6631. nil
  6632. (goto-char pos)
  6633. (error "Not in an item")))))
  6634. (defun org-end-of-item ()
  6635. "Go to the end of the current hand-formatted item.
  6636. If the cursor is not in an item, throw an error."
  6637. (interactive)
  6638. (let* ((pos (point))
  6639. ind1
  6640. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6641. (limit (save-excursion (outline-next-heading) (point)))
  6642. (ind (save-excursion
  6643. (org-beginning-of-item)
  6644. (skip-chars-forward " \t")
  6645. (current-column)))
  6646. (end (catch 'exit
  6647. (while t
  6648. (beginning-of-line 2)
  6649. (if (eobp) (throw 'exit (point)))
  6650. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6651. (if (looking-at "[ \t]*$")
  6652. (setq ind1 ind-empty)
  6653. (skip-chars-forward " \t")
  6654. (setq ind1 (current-column)))
  6655. (if (<= ind1 ind)
  6656. (throw 'exit (point-at-bol)))))))
  6657. (if end
  6658. (goto-char end)
  6659. (goto-char pos)
  6660. (error "Not in an item"))))
  6661. (defun org-next-item ()
  6662. "Move to the beginning of the next item in the current plain list.
  6663. Error if not at a plain list, or if this is the last item in the list."
  6664. (interactive)
  6665. (let (ind ind1 (pos (point)))
  6666. (org-beginning-of-item)
  6667. (setq ind (org-get-indentation))
  6668. (org-end-of-item)
  6669. (setq ind1 (org-get-indentation))
  6670. (unless (and (org-at-item-p) (= ind ind1))
  6671. (goto-char pos)
  6672. (error "On last item"))))
  6673. (defun org-previous-item ()
  6674. "Move to the beginning of the previous item in the current plain list.
  6675. Error if not at a plain list, or if this is the first item in the list."
  6676. (interactive)
  6677. (let (beg ind ind1 (pos (point)))
  6678. (org-beginning-of-item)
  6679. (setq beg (point))
  6680. (setq ind (org-get-indentation))
  6681. (goto-char beg)
  6682. (catch 'exit
  6683. (while t
  6684. (beginning-of-line 0)
  6685. (if (looking-at "[ \t]*$")
  6686. nil
  6687. (if (<= (setq ind1 (org-get-indentation)) ind)
  6688. (throw 'exit t)))))
  6689. (condition-case nil
  6690. (if (or (not (org-at-item-p))
  6691. (< ind1 (1- ind)))
  6692. (error "")
  6693. (org-beginning-of-item))
  6694. (error (goto-char pos)
  6695. (error "On first item")))))
  6696. (defun org-first-list-item-p ()
  6697. "Is this heading the item in a plain list?"
  6698. (unless (org-at-item-p)
  6699. (error "Not at a plain list item"))
  6700. (org-beginning-of-item)
  6701. (= (point) (save-excursion (org-beginning-of-item-list))))
  6702. (defun org-move-item-down ()
  6703. "Move the plain list item at point down, i.e. swap with following item.
  6704. Subitems (items with larger indentation) are considered part of the item,
  6705. so this really moves item trees."
  6706. (interactive)
  6707. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6708. (org-beginning-of-item)
  6709. (setq beg0 (point))
  6710. (save-excursion
  6711. (setq ne-beg (org-back-over-empty-lines))
  6712. (setq beg (point)))
  6713. (goto-char beg0)
  6714. (setq ind (org-get-indentation))
  6715. (org-end-of-item)
  6716. (setq end0 (point))
  6717. (setq ind1 (org-get-indentation))
  6718. (setq ne-end (org-back-over-empty-lines))
  6719. (setq end (point))
  6720. (goto-char beg0)
  6721. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6722. ;; include less whitespace
  6723. (save-excursion
  6724. (goto-char beg)
  6725. (forward-line (- ne-beg ne-end))
  6726. (setq beg (point))))
  6727. (goto-char end0)
  6728. (if (and (org-at-item-p) (= ind ind1))
  6729. (progn
  6730. (org-end-of-item)
  6731. (org-back-over-empty-lines)
  6732. (setq txt (buffer-substring beg end))
  6733. (save-excursion
  6734. (delete-region beg end))
  6735. (setq pos (point))
  6736. (insert txt)
  6737. (goto-char pos) (org-skip-whitespace)
  6738. (org-maybe-renumber-ordered-list))
  6739. (goto-char pos)
  6740. (error "Cannot move this item further down"))))
  6741. (defun org-move-item-up (arg)
  6742. "Move the plain list item at point up, i.e. swap with previous item.
  6743. Subitems (items with larger indentation) are considered part of the item,
  6744. so this really moves item trees."
  6745. (interactive "p")
  6746. (let (beg beg0 end ind ind1 (pos (point)) txt
  6747. ne-beg ne-ins ins-end)
  6748. (org-beginning-of-item)
  6749. (setq beg0 (point))
  6750. (setq ind (org-get-indentation))
  6751. (save-excursion
  6752. (setq ne-beg (org-back-over-empty-lines))
  6753. (setq beg (point)))
  6754. (goto-char beg0)
  6755. (org-end-of-item)
  6756. (setq end (point))
  6757. (goto-char beg0)
  6758. (catch 'exit
  6759. (while t
  6760. (beginning-of-line 0)
  6761. (if (looking-at "[ \t]*$")
  6762. (if org-empty-line-terminates-plain-lists
  6763. (progn
  6764. (goto-char pos)
  6765. (error "Cannot move this item further up"))
  6766. nil)
  6767. (if (<= (setq ind1 (org-get-indentation)) ind)
  6768. (throw 'exit t)))))
  6769. (condition-case nil
  6770. (org-beginning-of-item)
  6771. (error (goto-char beg)
  6772. (error "Cannot move this item further up")))
  6773. (setq ind1 (org-get-indentation))
  6774. (if (and (org-at-item-p) (= ind ind1))
  6775. (progn
  6776. (setq ne-ins (org-back-over-empty-lines))
  6777. (setq txt (buffer-substring beg end))
  6778. (save-excursion
  6779. (delete-region beg end))
  6780. (setq pos (point))
  6781. (insert txt)
  6782. (setq ins-end (point))
  6783. (goto-char pos) (org-skip-whitespace)
  6784. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6785. ;; Move whitespace back to beginning
  6786. (save-excursion
  6787. (goto-char ins-end)
  6788. (let ((kill-whole-line t))
  6789. (kill-line (- ne-ins ne-beg)) (point)))
  6790. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6791. (org-maybe-renumber-ordered-list))
  6792. (goto-char pos)
  6793. (error "Cannot move this item further up"))))
  6794. (defun org-maybe-renumber-ordered-list ()
  6795. "Renumber the ordered list at point if setup allows it.
  6796. This tests the user option `org-auto-renumber-ordered-lists' before
  6797. doing the renumbering."
  6798. (interactive)
  6799. (when (and org-auto-renumber-ordered-lists
  6800. (org-at-item-p))
  6801. (if (match-beginning 3)
  6802. (org-renumber-ordered-list 1)
  6803. (org-fix-bullet-type))))
  6804. (defun org-maybe-renumber-ordered-list-safe ()
  6805. (condition-case nil
  6806. (save-excursion
  6807. (org-maybe-renumber-ordered-list))
  6808. (error nil)))
  6809. (defun org-cycle-list-bullet (&optional which)
  6810. "Cycle through the different itemize/enumerate bullets.
  6811. This cycle the entire list level through the sequence:
  6812. `-' -> `+' -> `*' -> `1.' -> `1)'
  6813. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6814. 0 meand `-', 1 means `+' etc."
  6815. (interactive "P")
  6816. (org-preserve-lc
  6817. (org-beginning-of-item-list)
  6818. (org-at-item-p)
  6819. (beginning-of-line 1)
  6820. (let ((current (match-string 0))
  6821. (prevp (eq which 'previous))
  6822. new)
  6823. (setq new (cond
  6824. ((and (numberp which)
  6825. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6826. ((string-match "-" current) (if prevp "1)" "+"))
  6827. ((string-match "\\+" current)
  6828. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6829. ((string-match "\\*" current) (if prevp "+" "1."))
  6830. ((string-match "\\." current) (if prevp "*" "1)"))
  6831. ((string-match ")" current) (if prevp "1." "-"))
  6832. (t (error "This should not happen"))))
  6833. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6834. (org-fix-bullet-type)
  6835. (org-maybe-renumber-ordered-list))))
  6836. (defun org-get-string-indentation (s)
  6837. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6838. (let ((n -1) (i 0) (w tab-width) c)
  6839. (catch 'exit
  6840. (while (< (setq n (1+ n)) (length s))
  6841. (setq c (aref s n))
  6842. (cond ((= c ?\ ) (setq i (1+ i)))
  6843. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6844. (t (throw 'exit t)))))
  6845. i))
  6846. (defun org-renumber-ordered-list (arg)
  6847. "Renumber an ordered plain list.
  6848. Cursor needs to be in the first line of an item, the line that starts
  6849. with something like \"1.\" or \"2)\"."
  6850. (interactive "p")
  6851. (unless (and (org-at-item-p)
  6852. (match-beginning 3))
  6853. (error "This is not an ordered list"))
  6854. (let ((line (org-current-line))
  6855. (col (current-column))
  6856. (ind (org-get-string-indentation
  6857. (buffer-substring (point-at-bol) (match-beginning 3))))
  6858. ;; (term (substring (match-string 3) -1))
  6859. ind1 (n (1- arg))
  6860. fmt)
  6861. ;; find where this list begins
  6862. (org-beginning-of-item-list)
  6863. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6864. (setq fmt (concat "%d" (match-string 1)))
  6865. (beginning-of-line 0)
  6866. ;; walk forward and replace these numbers
  6867. (catch 'exit
  6868. (while t
  6869. (catch 'next
  6870. (beginning-of-line 2)
  6871. (if (eobp) (throw 'exit nil))
  6872. (if (looking-at "[ \t]*$") (throw 'next nil))
  6873. (skip-chars-forward " \t") (setq ind1 (current-column))
  6874. (if (> ind1 ind) (throw 'next t))
  6875. (if (< ind1 ind) (throw 'exit t))
  6876. (if (not (org-at-item-p)) (throw 'exit nil))
  6877. (delete-region (match-beginning 2) (match-end 2))
  6878. (goto-char (match-beginning 2))
  6879. (insert (format fmt (setq n (1+ n)))))))
  6880. (goto-line line)
  6881. (move-to-column col)))
  6882. (defun org-fix-bullet-type ()
  6883. "Make sure all items in this list have the same bullet as the firsst item."
  6884. (interactive)
  6885. (unless (org-at-item-p) (error "This is not a list"))
  6886. (let ((line (org-current-line))
  6887. (col (current-column))
  6888. (ind (current-indentation))
  6889. ind1 bullet)
  6890. ;; find where this list begins
  6891. (org-beginning-of-item-list)
  6892. (beginning-of-line 1)
  6893. ;; find out what the bullet type is
  6894. (looking-at "[ \t]*\\(\\S-+\\)")
  6895. (setq bullet (match-string 1))
  6896. ;; walk forward and replace these numbers
  6897. (beginning-of-line 0)
  6898. (catch 'exit
  6899. (while t
  6900. (catch 'next
  6901. (beginning-of-line 2)
  6902. (if (eobp) (throw 'exit nil))
  6903. (if (looking-at "[ \t]*$") (throw 'next nil))
  6904. (skip-chars-forward " \t") (setq ind1 (current-column))
  6905. (if (> ind1 ind) (throw 'next t))
  6906. (if (< ind1 ind) (throw 'exit t))
  6907. (if (not (org-at-item-p)) (throw 'exit nil))
  6908. (skip-chars-forward " \t")
  6909. (looking-at "\\S-+")
  6910. (replace-match bullet))))
  6911. (goto-line line)
  6912. (move-to-column col)
  6913. (if (string-match "[0-9]" bullet)
  6914. (org-renumber-ordered-list 1))))
  6915. (defun org-beginning-of-item-list ()
  6916. "Go to the beginning of the current item list.
  6917. I.e. to the first item in this list."
  6918. (interactive)
  6919. (org-beginning-of-item)
  6920. (let ((pos (point-at-bol))
  6921. (ind (org-get-indentation))
  6922. ind1)
  6923. ;; find where this list begins
  6924. (catch 'exit
  6925. (while t
  6926. (catch 'next
  6927. (beginning-of-line 0)
  6928. (if (looking-at "[ \t]*$")
  6929. (throw (if (bobp) 'exit 'next) t))
  6930. (skip-chars-forward " \t") (setq ind1 (current-column))
  6931. (if (or (< ind1 ind)
  6932. (and (= ind1 ind)
  6933. (not (org-at-item-p)))
  6934. (bobp))
  6935. (throw 'exit t)
  6936. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6937. (goto-char pos)))
  6938. (defun org-end-of-item-list ()
  6939. "Go to the end of the current item list.
  6940. I.e. to the text after the last item."
  6941. (interactive)
  6942. (org-beginning-of-item)
  6943. (let ((pos (point-at-bol))
  6944. (ind (org-get-indentation))
  6945. ind1)
  6946. ;; find where this list begins
  6947. (catch 'exit
  6948. (while t
  6949. (catch 'next
  6950. (beginning-of-line 2)
  6951. (if (looking-at "[ \t]*$")
  6952. (throw (if (eobp) 'exit 'next) t))
  6953. (skip-chars-forward " \t") (setq ind1 (current-column))
  6954. (if (or (< ind1 ind)
  6955. (and (= ind1 ind)
  6956. (not (org-at-item-p)))
  6957. (eobp))
  6958. (progn
  6959. (setq pos (point-at-bol))
  6960. (throw 'exit t))))))
  6961. (goto-char pos)))
  6962. (defvar org-last-indent-begin-marker (make-marker))
  6963. (defvar org-last-indent-end-marker (make-marker))
  6964. (defun org-outdent-item (arg)
  6965. "Outdent a local list item."
  6966. (interactive "p")
  6967. (org-indent-item (- arg)))
  6968. (defun org-indent-item (arg)
  6969. "Indent a local list item."
  6970. (interactive "p")
  6971. (unless (org-at-item-p)
  6972. (error "Not on an item"))
  6973. (save-excursion
  6974. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6975. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6976. (setq beg org-last-indent-begin-marker
  6977. end org-last-indent-end-marker)
  6978. (org-beginning-of-item)
  6979. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6980. (org-end-of-item)
  6981. (setq end (move-marker org-last-indent-end-marker (point))))
  6982. (goto-char beg)
  6983. (setq tmp (org-item-indent-positions)
  6984. ind (car tmp)
  6985. ind-down (nth 2 tmp)
  6986. ind-up (nth 1 tmp)
  6987. delta (if (> arg 0)
  6988. (if ind-down (- ind-down ind) 2)
  6989. (if ind-up (- ind-up ind) -2)))
  6990. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6991. (while (< (point) end)
  6992. (beginning-of-line 1)
  6993. (skip-chars-forward " \t") (setq ind1 (current-column))
  6994. (delete-region (point-at-bol) (point))
  6995. (or (eolp) (indent-to-column (+ ind1 delta)))
  6996. (beginning-of-line 2))))
  6997. (org-fix-bullet-type)
  6998. (org-maybe-renumber-ordered-list-safe)
  6999. (save-excursion
  7000. (beginning-of-line 0)
  7001. (condition-case nil (org-beginning-of-item) (error nil))
  7002. (org-maybe-renumber-ordered-list-safe)))
  7003. (defun org-item-indent-positions ()
  7004. "Return indentation for plain list items.
  7005. This returns a list with three values: The current indentation, the
  7006. parent indentation and the indentation a child should habe.
  7007. Assumes cursor in item line."
  7008. (let* ((bolpos (point-at-bol))
  7009. (ind (org-get-indentation))
  7010. ind-down ind-up pos)
  7011. (save-excursion
  7012. (org-beginning-of-item-list)
  7013. (skip-chars-backward "\n\r \t")
  7014. (when (org-in-item-p)
  7015. (org-beginning-of-item)
  7016. (setq ind-up (org-get-indentation))))
  7017. (setq pos (point))
  7018. (save-excursion
  7019. (cond
  7020. ((and (condition-case nil (progn (org-previous-item) t)
  7021. (error nil))
  7022. (or (forward-char 1) t)
  7023. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  7024. (setq ind-down (org-get-indentation)))
  7025. ((and (goto-char pos)
  7026. (org-at-item-p))
  7027. (goto-char (match-end 0))
  7028. (skip-chars-forward " \t")
  7029. (setq ind-down (current-column)))))
  7030. (list ind ind-up ind-down)))
  7031. ;;; The orgstruct minor mode
  7032. ;; Define a minor mode which can be used in other modes in order to
  7033. ;; integrate the org-mode structure editing commands.
  7034. ;; This is really a hack, because the org-mode structure commands use
  7035. ;; keys which normally belong to the major mode. Here is how it
  7036. ;; works: The minor mode defines all the keys necessary to operate the
  7037. ;; structure commands, but wraps the commands into a function which
  7038. ;; tests if the cursor is currently at a headline or a plain list
  7039. ;; item. If that is the case, the structure command is used,
  7040. ;; temporarily setting many Org-mode variables like regular
  7041. ;; expressions for filling etc. However, when any of those keys is
  7042. ;; used at a different location, function uses `key-binding' to look
  7043. ;; up if the key has an associated command in another currently active
  7044. ;; keymap (minor modes, major mode, global), and executes that
  7045. ;; command. There might be problems if any of the keys is otherwise
  7046. ;; used as a prefix key.
  7047. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7048. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7049. ;; addresses this by checking explicitly for both bindings.
  7050. (defvar orgstruct-mode-map (make-sparse-keymap)
  7051. "Keymap for the minor `orgstruct-mode'.")
  7052. (defvar org-local-vars nil
  7053. "List of local variables, for use by `orgstruct-mode'")
  7054. ;;;###autoload
  7055. (define-minor-mode orgstruct-mode
  7056. "Toggle the minor more `orgstruct-mode'.
  7057. This mode is for using Org-mode structure commands in other modes.
  7058. The following key behave as if Org-mode was active, if the cursor
  7059. is on a headline, or on a plain list item (both in the definition
  7060. of Org-mode).
  7061. M-up Move entry/item up
  7062. M-down Move entry/item down
  7063. M-left Promote
  7064. M-right Demote
  7065. M-S-up Move entry/item up
  7066. M-S-down Move entry/item down
  7067. M-S-left Promote subtree
  7068. M-S-right Demote subtree
  7069. M-q Fill paragraph and items like in Org-mode
  7070. C-c ^ Sort entries
  7071. C-c - Cycle list bullet
  7072. TAB Cycle item visibility
  7073. M-RET Insert new heading/item
  7074. S-M-RET Insert new TODO heading / Chekbox item
  7075. C-c C-c Set tags / toggle checkbox"
  7076. nil " OrgStruct" nil
  7077. (org-load-modules-maybe)
  7078. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7079. ;;;###autoload
  7080. (defun turn-on-orgstruct ()
  7081. "Unconditionally turn on `orgstruct-mode'."
  7082. (orgstruct-mode 1))
  7083. ;;;###autoload
  7084. (defun turn-on-orgstruct++ ()
  7085. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  7086. In addition to setting orgstruct-mode, this also exports all indentation and
  7087. autofilling variables from org-mode into the buffer. Note that turning
  7088. off orgstruct-mode will *not* remove these additional settings."
  7089. (orgstruct-mode 1)
  7090. (let (var val)
  7091. (mapc
  7092. (lambda (x)
  7093. (when (string-match
  7094. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7095. (symbol-name (car x)))
  7096. (setq var (car x) val (nth 1 x))
  7097. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7098. org-local-vars)))
  7099. (defun orgstruct-error ()
  7100. "Error when there is no default binding for a structure key."
  7101. (interactive)
  7102. (error "This key has no function outside structure elements"))
  7103. (defun orgstruct-setup ()
  7104. "Setup orgstruct keymaps."
  7105. (let ((nfunc 0)
  7106. (bindings
  7107. (list
  7108. '([(meta up)] org-metaup)
  7109. '([(meta down)] org-metadown)
  7110. '([(meta left)] org-metaleft)
  7111. '([(meta right)] org-metaright)
  7112. '([(meta shift up)] org-shiftmetaup)
  7113. '([(meta shift down)] org-shiftmetadown)
  7114. '([(meta shift left)] org-shiftmetaleft)
  7115. '([(meta shift right)] org-shiftmetaright)
  7116. '([(shift up)] org-shiftup)
  7117. '([(shift down)] org-shiftdown)
  7118. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7119. '("\M-q" fill-paragraph)
  7120. '("\C-c^" org-sort)
  7121. '("\C-c-" org-cycle-list-bullet)))
  7122. elt key fun cmd)
  7123. (while (setq elt (pop bindings))
  7124. (setq nfunc (1+ nfunc))
  7125. (setq key (org-key (car elt))
  7126. fun (nth 1 elt)
  7127. cmd (orgstruct-make-binding fun nfunc key))
  7128. (org-defkey orgstruct-mode-map key cmd))
  7129. ;; Special treatment needed for TAB and RET
  7130. (org-defkey orgstruct-mode-map [(tab)]
  7131. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7132. (org-defkey orgstruct-mode-map "\C-i"
  7133. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7134. (org-defkey orgstruct-mode-map "\M-\C-m"
  7135. (orgstruct-make-binding 'org-insert-heading 105
  7136. "\M-\C-m" [(meta return)]))
  7137. (org-defkey orgstruct-mode-map [(meta return)]
  7138. (orgstruct-make-binding 'org-insert-heading 106
  7139. [(meta return)] "\M-\C-m"))
  7140. (org-defkey orgstruct-mode-map [(shift meta return)]
  7141. (orgstruct-make-binding 'org-insert-todo-heading 107
  7142. [(meta return)] "\M-\C-m"))
  7143. (unless org-local-vars
  7144. (setq org-local-vars (org-get-local-variables)))
  7145. t))
  7146. (defun orgstruct-make-binding (fun n &rest keys)
  7147. "Create a function for binding in the structure minor mode.
  7148. FUN is the command to call inside a table. N is used to create a unique
  7149. command name. KEYS are keys that should be checked in for a command
  7150. to execute outside of tables."
  7151. (eval
  7152. (list 'defun
  7153. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7154. '(arg)
  7155. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7156. "Outside of structure, run the binding of `"
  7157. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7158. "'.")
  7159. '(interactive "p")
  7160. (list 'if
  7161. '(org-context-p 'headline 'item)
  7162. (list 'org-run-like-in-org-mode (list 'quote fun))
  7163. (list 'let '(orgstruct-mode)
  7164. (list 'call-interactively
  7165. (append '(or)
  7166. (mapcar (lambda (k)
  7167. (list 'key-binding k))
  7168. keys)
  7169. '('orgstruct-error))))))))
  7170. (defun org-context-p (&rest contexts)
  7171. "Check if local context is and of CONTEXTS.
  7172. Possible values in the list of contexts are `table', `headline', and `item'."
  7173. (let ((pos (point)))
  7174. (goto-char (point-at-bol))
  7175. (prog1 (or (and (memq 'table contexts)
  7176. (looking-at "[ \t]*|"))
  7177. (and (memq 'headline contexts)
  7178. (looking-at "\\*+"))
  7179. (and (memq 'item contexts)
  7180. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7181. (goto-char pos))))
  7182. (defun org-get-local-variables ()
  7183. "Return a list of all local variables in an org-mode buffer."
  7184. (let (varlist)
  7185. (with-current-buffer (get-buffer-create "*Org tmp*")
  7186. (erase-buffer)
  7187. (org-mode)
  7188. (setq varlist (buffer-local-variables)))
  7189. (kill-buffer "*Org tmp*")
  7190. (delq nil
  7191. (mapcar
  7192. (lambda (x)
  7193. (setq x
  7194. (if (symbolp x)
  7195. (list x)
  7196. (list (car x) (list 'quote (cdr x)))))
  7197. (if (string-match
  7198. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7199. (symbol-name (car x)))
  7200. x nil))
  7201. varlist))))
  7202. ;;;###autoload
  7203. (defun org-run-like-in-org-mode (cmd)
  7204. (org-load-modules-maybe)
  7205. (unless org-local-vars
  7206. (setq org-local-vars (org-get-local-variables)))
  7207. (eval (list 'let org-local-vars
  7208. (list 'call-interactively (list 'quote cmd)))))
  7209. ;;;; Archiving
  7210. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7211. (defun org-archive-subtree (&optional find-done)
  7212. "Move the current subtree to the archive.
  7213. The archive can be a certain top-level heading in the current file, or in
  7214. a different file. The tree will be moved to that location, the subtree
  7215. heading be marked DONE, and the current time will be added.
  7216. When called with prefix argument FIND-DONE, find whole trees without any
  7217. open TODO items and archive them (after getting confirmation from the user).
  7218. If the cursor is not at a headline when this comand is called, try all level
  7219. 1 trees. If the cursor is on a headline, only try the direct children of
  7220. this heading."
  7221. (interactive "P")
  7222. (if find-done
  7223. (org-archive-all-done)
  7224. ;; Save all relevant TODO keyword-relatex variables
  7225. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7226. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7227. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7228. (tr-org-done-keywords org-done-keywords)
  7229. (tr-org-todo-regexp org-todo-regexp)
  7230. (tr-org-todo-line-regexp org-todo-line-regexp)
  7231. (tr-org-odd-levels-only org-odd-levels-only)
  7232. (this-buffer (current-buffer))
  7233. (org-archive-location org-archive-location)
  7234. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7235. ;; start of variables that will be used for saving context
  7236. ;; The compiler complains about them - keep them anyway!
  7237. (file (abbreviate-file-name (buffer-file-name)))
  7238. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7239. (time (format-time-string
  7240. (substring (cdr org-time-stamp-formats) 1 -1)
  7241. (current-time)))
  7242. afile heading buffer level newfile-p
  7243. category todo priority
  7244. ;; start of variables that will be used for savind context
  7245. ltags itags prop)
  7246. ;; Try to find a local archive location
  7247. (save-excursion
  7248. (save-restriction
  7249. (widen)
  7250. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7251. (if (and prop (string-match "\\S-" prop))
  7252. (setq org-archive-location prop)
  7253. (if (or (re-search-backward re nil t)
  7254. (re-search-forward re nil t))
  7255. (setq org-archive-location (match-string 1))))))
  7256. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7257. (progn
  7258. (setq afile (format (match-string 1 org-archive-location)
  7259. (file-name-nondirectory buffer-file-name))
  7260. heading (match-string 2 org-archive-location)))
  7261. (error "Invalid `org-archive-location'"))
  7262. (if (> (length afile) 0)
  7263. (setq newfile-p (not (file-exists-p afile))
  7264. buffer (find-file-noselect afile))
  7265. (setq buffer (current-buffer)))
  7266. (unless buffer
  7267. (error "Cannot access file \"%s\"" afile))
  7268. (if (and (> (length heading) 0)
  7269. (string-match "^\\*+" heading))
  7270. (setq level (match-end 0))
  7271. (setq heading nil level 0))
  7272. (save-excursion
  7273. (org-back-to-heading t)
  7274. ;; Get context information that will be lost by moving the tree
  7275. (org-refresh-category-properties)
  7276. (setq category (org-get-category)
  7277. todo (and (looking-at org-todo-line-regexp)
  7278. (match-string 2))
  7279. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7280. ltags (org-get-tags)
  7281. itags (org-delete-all ltags (org-get-tags-at)))
  7282. (setq ltags (mapconcat 'identity ltags " ")
  7283. itags (mapconcat 'identity itags " "))
  7284. ;; We first only copy, in case something goes wrong
  7285. ;; we need to protect this-command, to avoid kill-region sets it,
  7286. ;; which would lead to duplication of subtrees
  7287. (let (this-command) (org-copy-subtree))
  7288. (set-buffer buffer)
  7289. ;; Enforce org-mode for the archive buffer
  7290. (if (not (org-mode-p))
  7291. ;; Force the mode for future visits.
  7292. (let ((org-insert-mode-line-in-empty-file t)
  7293. (org-inhibit-startup t))
  7294. (call-interactively 'org-mode)))
  7295. (when newfile-p
  7296. (goto-char (point-max))
  7297. (insert (format "\nArchived entries from file %s\n\n"
  7298. (buffer-file-name this-buffer))))
  7299. ;; Force the TODO keywords of the original buffer
  7300. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7301. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7302. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7303. (org-done-keywords tr-org-done-keywords)
  7304. (org-todo-regexp tr-org-todo-regexp)
  7305. (org-todo-line-regexp tr-org-todo-line-regexp)
  7306. (org-odd-levels-only
  7307. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7308. org-odd-levels-only
  7309. tr-org-odd-levels-only)))
  7310. (goto-char (point-min))
  7311. (show-all)
  7312. (if heading
  7313. (progn
  7314. (if (re-search-forward
  7315. (concat "^" (regexp-quote heading)
  7316. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7317. nil t)
  7318. (goto-char (match-end 0))
  7319. ;; Heading not found, just insert it at the end
  7320. (goto-char (point-max))
  7321. (or (bolp) (insert "\n"))
  7322. (insert "\n" heading "\n")
  7323. (end-of-line 0))
  7324. ;; Make the subtree visible
  7325. (show-subtree)
  7326. (org-end-of-subtree t)
  7327. (skip-chars-backward " \t\r\n")
  7328. (and (looking-at "[ \t\r\n]*")
  7329. (replace-match "\n\n")))
  7330. ;; No specific heading, just go to end of file.
  7331. (goto-char (point-max)) (insert "\n"))
  7332. ;; Paste
  7333. (org-paste-subtree (org-get-valid-level level 1))
  7334. ;; Mark the entry as done
  7335. (when (and org-archive-mark-done
  7336. (looking-at org-todo-line-regexp)
  7337. (or (not (match-end 2))
  7338. (not (member (match-string 2) org-done-keywords))))
  7339. (let (org-log-done org-todo-log-states)
  7340. (org-todo
  7341. (car (or (member org-archive-mark-done org-done-keywords)
  7342. org-done-keywords)))))
  7343. ;; Add the context info
  7344. (when org-archive-save-context-info
  7345. (let ((l org-archive-save-context-info) e n v)
  7346. (while (setq e (pop l))
  7347. (when (and (setq v (symbol-value e))
  7348. (stringp v) (string-match "\\S-" v))
  7349. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7350. (org-entry-put (point) n v)))))
  7351. ;; Save and kill the buffer, if it is not the same buffer.
  7352. (if (not (eq this-buffer buffer))
  7353. (progn (save-buffer) (kill-buffer buffer)))))
  7354. ;; Here we are back in the original buffer. Everything seems to have
  7355. ;; worked. So now cut the tree and finish up.
  7356. (let (this-command) (org-cut-subtree))
  7357. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7358. (message "Subtree archived %s"
  7359. (if (eq this-buffer buffer)
  7360. (concat "under heading: " heading)
  7361. (concat "in file: " (abbreviate-file-name afile)))))))
  7362. (defun org-refresh-category-properties ()
  7363. "Refresh category text properties in teh buffer."
  7364. (let ((def-cat (cond
  7365. ((null org-category)
  7366. (if buffer-file-name
  7367. (file-name-sans-extension
  7368. (file-name-nondirectory buffer-file-name))
  7369. "???"))
  7370. ((symbolp org-category) (symbol-name org-category))
  7371. (t org-category)))
  7372. beg end cat pos optionp)
  7373. (org-unmodified
  7374. (save-excursion
  7375. (save-restriction
  7376. (widen)
  7377. (goto-char (point-min))
  7378. (put-text-property (point) (point-max) 'org-category def-cat)
  7379. (while (re-search-forward
  7380. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7381. (setq pos (match-end 0)
  7382. optionp (equal (char-after (match-beginning 0)) ?#)
  7383. cat (org-trim (match-string 2)))
  7384. (if optionp
  7385. (setq beg (point-at-bol) end (point-max))
  7386. (org-back-to-heading t)
  7387. (setq beg (point) end (org-end-of-subtree t t)))
  7388. (put-text-property beg end 'org-category cat)
  7389. (goto-char pos)))))))
  7390. (defun org-archive-all-done (&optional tag)
  7391. "Archive sublevels of the current tree without open TODO items.
  7392. If the cursor is not on a headline, try all level 1 trees. If
  7393. it is on a headline, try all direct children.
  7394. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7395. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7396. (rea (concat ".*:" org-archive-tag ":"))
  7397. (begm (make-marker))
  7398. (endm (make-marker))
  7399. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7400. "Move subtree to archive (no open TODO items)? "))
  7401. beg end (cntarch 0))
  7402. (if (org-on-heading-p)
  7403. (progn
  7404. (setq re1 (concat "^" (regexp-quote
  7405. (make-string
  7406. (1+ (- (match-end 0) (match-beginning 0) 1))
  7407. ?*))
  7408. " "))
  7409. (move-marker begm (point))
  7410. (move-marker endm (org-end-of-subtree t)))
  7411. (setq re1 "^* ")
  7412. (move-marker begm (point-min))
  7413. (move-marker endm (point-max)))
  7414. (save-excursion
  7415. (goto-char begm)
  7416. (while (re-search-forward re1 endm t)
  7417. (setq beg (match-beginning 0)
  7418. end (save-excursion (org-end-of-subtree t) (point)))
  7419. (goto-char beg)
  7420. (if (re-search-forward re end t)
  7421. (goto-char end)
  7422. (goto-char beg)
  7423. (if (and (or (not tag) (not (looking-at rea)))
  7424. (y-or-n-p question))
  7425. (progn
  7426. (if tag
  7427. (org-toggle-tag org-archive-tag 'on)
  7428. (org-archive-subtree))
  7429. (setq cntarch (1+ cntarch)))
  7430. (goto-char end)))))
  7431. (message "%d trees archived" cntarch)))
  7432. (defun org-cycle-hide-drawers (state)
  7433. "Re-hide all drawers after a visibility state change."
  7434. (when (and (org-mode-p)
  7435. (not (memq state '(overview folded))))
  7436. (save-excursion
  7437. (let* ((globalp (memq state '(contents all)))
  7438. (beg (if globalp (point-min) (point)))
  7439. (end (if globalp (point-max) (org-end-of-subtree t))))
  7440. (goto-char beg)
  7441. (while (re-search-forward org-drawer-regexp end t)
  7442. (org-flag-drawer t))))))
  7443. (defun org-flag-drawer (flag)
  7444. (save-excursion
  7445. (beginning-of-line 1)
  7446. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7447. (let ((b (match-end 0))
  7448. (outline-regexp org-outline-regexp))
  7449. (if (re-search-forward
  7450. "^[ \t]*:END:"
  7451. (save-excursion (outline-next-heading) (point)) t)
  7452. (outline-flag-region b (point-at-eol) flag)
  7453. (error ":END: line missing"))))))
  7454. (defun org-cycle-hide-archived-subtrees (state)
  7455. "Re-hide all archived subtrees after a visibility state change."
  7456. (when (and (not org-cycle-open-archived-trees)
  7457. (not (memq state '(overview folded))))
  7458. (save-excursion
  7459. (let* ((globalp (memq state '(contents all)))
  7460. (beg (if globalp (point-min) (point)))
  7461. (end (if globalp (point-max) (org-end-of-subtree t))))
  7462. (org-hide-archived-subtrees beg end)
  7463. (goto-char beg)
  7464. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7465. (message "%s" (substitute-command-keys
  7466. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7467. (defun org-force-cycle-archived ()
  7468. "Cycle subtree even if it is archived."
  7469. (interactive)
  7470. (setq this-command 'org-cycle)
  7471. (let ((org-cycle-open-archived-trees t))
  7472. (call-interactively 'org-cycle)))
  7473. (defun org-hide-archived-subtrees (beg end)
  7474. "Re-hide all archived subtrees after a visibility state change."
  7475. (save-excursion
  7476. (let* ((re (concat ":" org-archive-tag ":")))
  7477. (goto-char beg)
  7478. (while (re-search-forward re end t)
  7479. (and (org-on-heading-p) (hide-subtree))
  7480. (org-end-of-subtree t)))))
  7481. (defun org-toggle-tag (tag &optional onoff)
  7482. "Toggle the tag TAG for the current line.
  7483. If ONOFF is `on' or `off', don't toggle but set to this state."
  7484. (unless (org-on-heading-p t) (error "Not on headling"))
  7485. (let (res current)
  7486. (save-excursion
  7487. (beginning-of-line)
  7488. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7489. (point-at-eol) t)
  7490. (progn
  7491. (setq current (match-string 1))
  7492. (replace-match ""))
  7493. (setq current ""))
  7494. (setq current (nreverse (org-split-string current ":")))
  7495. (cond
  7496. ((eq onoff 'on)
  7497. (setq res t)
  7498. (or (member tag current) (push tag current)))
  7499. ((eq onoff 'off)
  7500. (or (not (member tag current)) (setq current (delete tag current))))
  7501. (t (if (member tag current)
  7502. (setq current (delete tag current))
  7503. (setq res t)
  7504. (push tag current))))
  7505. (end-of-line 1)
  7506. (if current
  7507. (progn
  7508. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7509. (org-set-tags nil t))
  7510. (delete-horizontal-space))
  7511. (run-hooks 'org-after-tags-change-hook))
  7512. res))
  7513. (defun org-toggle-archive-tag (&optional arg)
  7514. "Toggle the archive tag for the current headline.
  7515. With prefix ARG, check all children of current headline and offer tagging
  7516. the children that do not contain any open TODO items."
  7517. (interactive "P")
  7518. (if arg
  7519. (org-archive-all-done 'tag)
  7520. (let (set)
  7521. (save-excursion
  7522. (org-back-to-heading t)
  7523. (setq set (org-toggle-tag org-archive-tag))
  7524. (when set (hide-subtree)))
  7525. (and set (beginning-of-line 1))
  7526. (message "Subtree %s" (if set "archived" "unarchived")))))
  7527. ;;;; Tables
  7528. ;;; The table editor
  7529. ;; Watch out: Here we are talking about two different kind of tables.
  7530. ;; Most of the code is for the tables created with the Org-mode table editor.
  7531. ;; Sometimes, we talk about tables created and edited with the table.el
  7532. ;; Emacs package. We call the former org-type tables, and the latter
  7533. ;; table.el-type tables.
  7534. (defun org-before-change-function (beg end)
  7535. "Every change indicates that a table might need an update."
  7536. (setq org-table-may-need-update t))
  7537. (defconst org-table-line-regexp "^[ \t]*|"
  7538. "Detects an org-type table line.")
  7539. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7540. "Detects an org-type table line.")
  7541. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7542. "Detects a table line marked for automatic recalculation.")
  7543. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7544. "Detects a table line marked for automatic recalculation.")
  7545. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7546. "Detects a table line marked for automatic recalculation.")
  7547. (defconst org-table-hline-regexp "^[ \t]*|-"
  7548. "Detects an org-type table hline.")
  7549. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7550. "Detects a table-type table hline.")
  7551. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7552. "Detects an org-type or table-type table.")
  7553. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7554. "Searching from within a table (any type) this finds the first line
  7555. outside the table.")
  7556. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7557. "Searching from within a table (any type) this finds the first line
  7558. outside the table.")
  7559. (defvar org-table-last-highlighted-reference nil)
  7560. (defvar org-table-formula-history nil)
  7561. (defvar org-table-column-names nil
  7562. "Alist with column names, derived from the `!' line.")
  7563. (defvar org-table-column-name-regexp nil
  7564. "Regular expression matching the current column names.")
  7565. (defvar org-table-local-parameters nil
  7566. "Alist with parameter names, derived from the `$' line.")
  7567. (defvar org-table-named-field-locations nil
  7568. "Alist with locations of named fields.")
  7569. (defvar org-table-current-line-types nil
  7570. "Table row types, non-nil only for the duration of a comand.")
  7571. (defvar org-table-current-begin-line nil
  7572. "Table begin line, non-nil only for the duration of a comand.")
  7573. (defvar org-table-current-begin-pos nil
  7574. "Table begin position, non-nil only for the duration of a comand.")
  7575. (defvar org-table-dlines nil
  7576. "Vector of data line line numbers in the current table.")
  7577. (defvar org-table-hlines nil
  7578. "Vector of hline line numbers in the current table.")
  7579. (defconst org-table-range-regexp
  7580. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7581. ;; 1 2 3 4 5
  7582. "Regular expression for matching ranges in formulas.")
  7583. (defconst org-table-range-regexp2
  7584. (concat
  7585. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7586. "\\.\\."
  7587. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7588. "Match a range for reference display.")
  7589. (defconst org-table-translate-regexp
  7590. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7591. "Match a reference that needs translation, for reference display.")
  7592. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7593. (defun org-table-create-with-table.el ()
  7594. "Use the table.el package to insert a new table.
  7595. If there is already a table at point, convert between Org-mode tables
  7596. and table.el tables."
  7597. (interactive)
  7598. (require 'table)
  7599. (cond
  7600. ((org-at-table.el-p)
  7601. (if (y-or-n-p "Convert table to Org-mode table? ")
  7602. (org-table-convert)))
  7603. ((org-at-table-p)
  7604. (if (y-or-n-p "Convert table to table.el table? ")
  7605. (org-table-convert)))
  7606. (t (call-interactively 'table-insert))))
  7607. (defun org-table-create-or-convert-from-region (arg)
  7608. "Convert region to table, or create an empty table.
  7609. If there is an active region, convert it to a table, using the function
  7610. `org-table-convert-region'. See the documentation of that function
  7611. to learn how the prefix argument is interpreted to determine the field
  7612. separator.
  7613. If there is no such region, create an empty table with `org-table-create'."
  7614. (interactive "P")
  7615. (if (org-region-active-p)
  7616. (org-table-convert-region (region-beginning) (region-end) arg)
  7617. (org-table-create arg)))
  7618. (defun org-table-create (&optional size)
  7619. "Query for a size and insert a table skeleton.
  7620. SIZE is a string Columns x Rows like for example \"3x2\"."
  7621. (interactive "P")
  7622. (unless size
  7623. (setq size (read-string
  7624. (concat "Table size Columns x Rows [e.g. "
  7625. org-table-default-size "]: ")
  7626. "" nil org-table-default-size)))
  7627. (let* ((pos (point))
  7628. (indent (make-string (current-column) ?\ ))
  7629. (split (org-split-string size " *x *"))
  7630. (rows (string-to-number (nth 1 split)))
  7631. (columns (string-to-number (car split)))
  7632. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7633. "\n")))
  7634. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7635. (point-at-bol) (point)))
  7636. (beginning-of-line 1)
  7637. (newline))
  7638. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7639. (dotimes (i rows) (insert line))
  7640. (goto-char pos)
  7641. (if (> rows 1)
  7642. ;; Insert a hline after the first row.
  7643. (progn
  7644. (end-of-line 1)
  7645. (insert "\n|-")
  7646. (goto-char pos)))
  7647. (org-table-align)))
  7648. (defun org-table-convert-region (beg0 end0 &optional separator)
  7649. "Convert region to a table.
  7650. The region goes from BEG0 to END0, but these borders will be moved
  7651. slightly, to make sure a beginning of line in the first line is included.
  7652. SEPARATOR specifies the field separator in the lines. It can have the
  7653. following values:
  7654. '(4) Use the comma as a field separator
  7655. '(16) Use a TAB as field separator
  7656. integer When a number, use that many spaces as field separator
  7657. nil When nil, the command tries to be smart and figure out the
  7658. separator in the following way:
  7659. - when each line contains a TAB, assume TAB-separated material
  7660. - when each line contains a comme, assume CSV material
  7661. - else, assume one or more SPACE charcters as separator."
  7662. (interactive "rP")
  7663. (let* ((beg (min beg0 end0))
  7664. (end (max beg0 end0))
  7665. re)
  7666. (goto-char beg)
  7667. (beginning-of-line 1)
  7668. (setq beg (move-marker (make-marker) (point)))
  7669. (goto-char end)
  7670. (if (bolp) (backward-char 1) (end-of-line 1))
  7671. (setq end (move-marker (make-marker) (point)))
  7672. ;; Get the right field separator
  7673. (unless separator
  7674. (goto-char beg)
  7675. (setq separator
  7676. (cond
  7677. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7678. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7679. (t 1))))
  7680. (setq re (cond
  7681. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7682. ((equal separator '(16)) "^\\|\t")
  7683. ((integerp separator)
  7684. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7685. (t (error "This should not happen"))))
  7686. (goto-char beg)
  7687. (while (re-search-forward re end t)
  7688. (replace-match "| " t t))
  7689. (goto-char beg)
  7690. (insert " ")
  7691. (org-table-align)))
  7692. (defun org-table-import (file arg)
  7693. "Import FILE as a table.
  7694. The file is assumed to be tab-separated. Such files can be produced by most
  7695. spreadsheet and database applications. If no tabs (at least one per line)
  7696. are found, lines will be split on whitespace into fields."
  7697. (interactive "f\nP")
  7698. (or (bolp) (newline))
  7699. (let ((beg (point))
  7700. (pm (point-max)))
  7701. (insert-file-contents file)
  7702. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7703. (defun org-table-export ()
  7704. "Export table as a tab-separated file.
  7705. Such a file can be imported into a spreadsheet program like Excel."
  7706. (interactive)
  7707. (let* ((beg (org-table-begin))
  7708. (end (org-table-end))
  7709. (table (buffer-substring beg end))
  7710. (file (read-file-name "Export table to: "))
  7711. buf)
  7712. (unless (or (not (file-exists-p file))
  7713. (y-or-n-p (format "Overwrite file %s? " file)))
  7714. (error "Abort"))
  7715. (with-current-buffer (find-file-noselect file)
  7716. (setq buf (current-buffer))
  7717. (erase-buffer)
  7718. (fundamental-mode)
  7719. (insert table)
  7720. (goto-char (point-min))
  7721. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7722. (replace-match "" t t)
  7723. (end-of-line 1))
  7724. (goto-char (point-min))
  7725. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7726. (replace-match "" t t)
  7727. (goto-char (min (1+ (point)) (point-max))))
  7728. (goto-char (point-min))
  7729. (while (re-search-forward "^-[-+]*$" nil t)
  7730. (replace-match "")
  7731. (if (looking-at "\n")
  7732. (delete-char 1)))
  7733. (goto-char (point-min))
  7734. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7735. (replace-match "\t" t t))
  7736. (save-buffer))
  7737. (kill-buffer buf)))
  7738. (defvar org-table-aligned-begin-marker (make-marker)
  7739. "Marker at the beginning of the table last aligned.
  7740. Used to check if cursor still is in that table, to minimize realignment.")
  7741. (defvar org-table-aligned-end-marker (make-marker)
  7742. "Marker at the end of the table last aligned.
  7743. Used to check if cursor still is in that table, to minimize realignment.")
  7744. (defvar org-table-last-alignment nil
  7745. "List of flags for flushright alignment, from the last re-alignment.
  7746. This is being used to correctly align a single field after TAB or RET.")
  7747. (defvar org-table-last-column-widths nil
  7748. "List of max width of fields in each column.
  7749. This is being used to correctly align a single field after TAB or RET.")
  7750. (defvar org-table-overlay-coordinates nil
  7751. "Overlay coordinates after each align of a table.")
  7752. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7753. (defvar org-last-recalc-line nil)
  7754. (defconst org-narrow-column-arrow "=>"
  7755. "Used as display property in narrowed table columns.")
  7756. (defun org-table-align ()
  7757. "Align the table at point by aligning all vertical bars."
  7758. (interactive)
  7759. (let* (
  7760. ;; Limits of table
  7761. (beg (org-table-begin))
  7762. (end (org-table-end))
  7763. ;; Current cursor position
  7764. (linepos (org-current-line))
  7765. (colpos (org-table-current-column))
  7766. (winstart (window-start))
  7767. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7768. lines (new "") lengths l typenums ty fields maxfields i
  7769. column
  7770. (indent "") cnt frac
  7771. rfmt hfmt
  7772. (spaces '(1 . 1))
  7773. (sp1 (car spaces))
  7774. (sp2 (cdr spaces))
  7775. (rfmt1 (concat
  7776. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7777. (hfmt1 (concat
  7778. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7779. emptystrings links dates emph narrow fmax f1 len c e)
  7780. (untabify beg end)
  7781. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7782. ;; Check if we have links or dates
  7783. (goto-char beg)
  7784. (setq links (re-search-forward org-bracket-link-regexp end t))
  7785. (goto-char beg)
  7786. (setq emph (and org-hide-emphasis-markers
  7787. (re-search-forward org-emph-re end t)))
  7788. (goto-char beg)
  7789. (setq dates (and org-display-custom-times
  7790. (re-search-forward org-ts-regexp-both end t)))
  7791. ;; Make sure the link properties are right
  7792. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7793. ;; Make sure the date properties are right
  7794. (when dates (goto-char beg) (while (org-activate-dates end)))
  7795. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7796. ;; Check if we are narrowing any columns
  7797. (goto-char beg)
  7798. (setq narrow (and org-format-transports-properties-p
  7799. (re-search-forward "<[0-9]+>" end t)))
  7800. ;; Get the rows
  7801. (setq lines (org-split-string
  7802. (buffer-substring beg end) "\n"))
  7803. ;; Store the indentation of the first line
  7804. (if (string-match "^ *" (car lines))
  7805. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7806. ;; Mark the hlines by setting the corresponding element to nil
  7807. ;; At the same time, we remove trailing space.
  7808. (setq lines (mapcar (lambda (l)
  7809. (if (string-match "^ *|-" l)
  7810. nil
  7811. (if (string-match "[ \t]+$" l)
  7812. (substring l 0 (match-beginning 0))
  7813. l)))
  7814. lines))
  7815. ;; Get the data fields by splitting the lines.
  7816. (setq fields (mapcar
  7817. (lambda (l)
  7818. (org-split-string l " *| *"))
  7819. (delq nil (copy-sequence lines))))
  7820. ;; How many fields in the longest line?
  7821. (condition-case nil
  7822. (setq maxfields (apply 'max (mapcar 'length fields)))
  7823. (error
  7824. (kill-region beg end)
  7825. (org-table-create org-table-default-size)
  7826. (error "Empty table - created default table")))
  7827. ;; A list of empty strings to fill any short rows on output
  7828. (setq emptystrings (make-list maxfields ""))
  7829. ;; Check for special formatting.
  7830. (setq i -1)
  7831. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7832. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7833. ;; Check if there is an explicit width specified
  7834. (when narrow
  7835. (setq c column fmax nil)
  7836. (while c
  7837. (setq e (pop c))
  7838. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7839. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7840. ;; Find fields that are wider than fmax, and shorten them
  7841. (when fmax
  7842. (loop for xx in column do
  7843. (when (and (stringp xx)
  7844. (> (org-string-width xx) fmax))
  7845. (org-add-props xx nil
  7846. 'help-echo
  7847. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7848. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7849. (unless (> f1 1)
  7850. (error "Cannot narrow field starting with wide link \"%s\""
  7851. (match-string 0 xx)))
  7852. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7853. (add-text-properties (- f1 2) f1
  7854. (list 'display org-narrow-column-arrow)
  7855. xx)))))
  7856. ;; Get the maximum width for each column
  7857. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7858. ;; Get the fraction of numbers, to decide about alignment of the column
  7859. (setq cnt 0 frac 0.0)
  7860. (loop for x in column do
  7861. (if (equal x "")
  7862. nil
  7863. (setq frac ( / (+ (* frac cnt)
  7864. (if (string-match org-table-number-regexp x) 1 0))
  7865. (setq cnt (1+ cnt))))))
  7866. (push (>= frac org-table-number-fraction) typenums))
  7867. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7868. ;; Store the alignment of this table, for later editing of single fields
  7869. (setq org-table-last-alignment typenums
  7870. org-table-last-column-widths lengths)
  7871. ;; With invisible characters, `format' does not get the field width right
  7872. ;; So we need to make these fields wide by hand.
  7873. (when (or links emph)
  7874. (loop for i from 0 upto (1- maxfields) do
  7875. (setq len (nth i lengths))
  7876. (loop for j from 0 upto (1- (length fields)) do
  7877. (setq c (nthcdr i (car (nthcdr j fields))))
  7878. (if (and (stringp (car c))
  7879. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7880. ; (string-match org-bracket-link-regexp (car c))
  7881. (< (org-string-width (car c)) len))
  7882. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7883. ;; Compute the formats needed for output of the table
  7884. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7885. (while (setq l (pop lengths))
  7886. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7887. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7888. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7889. (setq rfmt (concat rfmt "\n")
  7890. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7891. (setq new (mapconcat
  7892. (lambda (l)
  7893. (if l (apply 'format rfmt
  7894. (append (pop fields) emptystrings))
  7895. hfmt))
  7896. lines ""))
  7897. ;; Replace the old one
  7898. (delete-region beg end)
  7899. (move-marker end nil)
  7900. (move-marker org-table-aligned-begin-marker (point))
  7901. (insert new)
  7902. (move-marker org-table-aligned-end-marker (point))
  7903. (when (and orgtbl-mode (not (org-mode-p)))
  7904. (goto-char org-table-aligned-begin-marker)
  7905. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7906. ;; Try to move to the old location
  7907. (goto-line winstartline)
  7908. (setq winstart (point-at-bol))
  7909. (goto-line linepos)
  7910. (set-window-start (selected-window) winstart 'noforce)
  7911. (org-table-goto-column colpos)
  7912. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7913. (setq org-table-may-need-update nil)
  7914. ))
  7915. (defun org-string-width (s)
  7916. "Compute width of string, ignoring invisible characters.
  7917. This ignores character with invisibility property `org-link', and also
  7918. characters with property `org-cwidth', because these will become invisible
  7919. upon the next fontification round."
  7920. (let (b l)
  7921. (when (or (eq t buffer-invisibility-spec)
  7922. (assq 'org-link buffer-invisibility-spec))
  7923. (while (setq b (text-property-any 0 (length s)
  7924. 'invisible 'org-link s))
  7925. (setq s (concat (substring s 0 b)
  7926. (substring s (or (next-single-property-change
  7927. b 'invisible s) (length s)))))))
  7928. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7929. (setq s (concat (substring s 0 b)
  7930. (substring s (or (next-single-property-change
  7931. b 'org-cwidth s) (length s))))))
  7932. (setq l (string-width s) b -1)
  7933. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7934. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7935. l))
  7936. (defun org-table-begin (&optional table-type)
  7937. "Find the beginning of the table and return its position.
  7938. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7939. (save-excursion
  7940. (if (not (re-search-backward
  7941. (if table-type org-table-any-border-regexp
  7942. org-table-border-regexp)
  7943. nil t))
  7944. (progn (goto-char (point-min)) (point))
  7945. (goto-char (match-beginning 0))
  7946. (beginning-of-line 2)
  7947. (point))))
  7948. (defun org-table-end (&optional table-type)
  7949. "Find the end of the table and return its position.
  7950. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7951. (save-excursion
  7952. (if (not (re-search-forward
  7953. (if table-type org-table-any-border-regexp
  7954. org-table-border-regexp)
  7955. nil t))
  7956. (goto-char (point-max))
  7957. (goto-char (match-beginning 0)))
  7958. (point-marker)))
  7959. (defun org-table-justify-field-maybe (&optional new)
  7960. "Justify the current field, text to left, number to right.
  7961. Optional argument NEW may specify text to replace the current field content."
  7962. (cond
  7963. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7964. ((org-at-table-hline-p))
  7965. ((and (not new)
  7966. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7967. (current-buffer)))
  7968. (< (point) org-table-aligned-begin-marker)
  7969. (>= (point) org-table-aligned-end-marker)))
  7970. ;; This is not the same table, force a full re-align
  7971. (setq org-table-may-need-update t))
  7972. (t ;; realign the current field, based on previous full realign
  7973. (let* ((pos (point)) s
  7974. (col (org-table-current-column))
  7975. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7976. l f n o e)
  7977. (when (> col 0)
  7978. (skip-chars-backward "^|\n")
  7979. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7980. (progn
  7981. (setq s (match-string 1)
  7982. o (match-string 0)
  7983. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7984. e (not (= (match-beginning 2) (match-end 2))))
  7985. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7986. l (if e "|" (setq org-table-may-need-update t) ""))
  7987. n (format f s))
  7988. (if new
  7989. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7990. (setq n (format f new))
  7991. (setq n (concat new "|") org-table-may-need-update t)))
  7992. (or (equal n o)
  7993. (let (org-table-may-need-update)
  7994. (replace-match n t t))))
  7995. (setq org-table-may-need-update t))
  7996. (goto-char pos))))))
  7997. (defun org-table-next-field ()
  7998. "Go to the next field in the current table, creating new lines as needed.
  7999. Before doing so, re-align the table if necessary."
  8000. (interactive)
  8001. (org-table-maybe-eval-formula)
  8002. (org-table-maybe-recalculate-line)
  8003. (if (and org-table-automatic-realign
  8004. org-table-may-need-update)
  8005. (org-table-align))
  8006. (let ((end (org-table-end)))
  8007. (if (org-at-table-hline-p)
  8008. (end-of-line 1))
  8009. (condition-case nil
  8010. (progn
  8011. (re-search-forward "|" end)
  8012. (if (looking-at "[ \t]*$")
  8013. (re-search-forward "|" end))
  8014. (if (and (looking-at "-")
  8015. org-table-tab-jumps-over-hlines
  8016. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  8017. (goto-char (match-beginning 1)))
  8018. (if (looking-at "-")
  8019. (progn
  8020. (beginning-of-line 0)
  8021. (org-table-insert-row 'below))
  8022. (if (looking-at " ") (forward-char 1))))
  8023. (error
  8024. (org-table-insert-row 'below)))))
  8025. (defun org-table-previous-field ()
  8026. "Go to the previous field in the table.
  8027. Before doing so, re-align the table if necessary."
  8028. (interactive)
  8029. (org-table-justify-field-maybe)
  8030. (org-table-maybe-recalculate-line)
  8031. (if (and org-table-automatic-realign
  8032. org-table-may-need-update)
  8033. (org-table-align))
  8034. (if (org-at-table-hline-p)
  8035. (end-of-line 1))
  8036. (re-search-backward "|" (org-table-begin))
  8037. (re-search-backward "|" (org-table-begin))
  8038. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  8039. (re-search-backward "|" (org-table-begin)))
  8040. (if (looking-at "| ?")
  8041. (goto-char (match-end 0))))
  8042. (defun org-table-next-row ()
  8043. "Go to the next row (same column) in the current table.
  8044. Before doing so, re-align the table if necessary."
  8045. (interactive)
  8046. (org-table-maybe-eval-formula)
  8047. (org-table-maybe-recalculate-line)
  8048. (if (or (looking-at "[ \t]*$")
  8049. (save-excursion (skip-chars-backward " \t") (bolp)))
  8050. (newline)
  8051. (if (and org-table-automatic-realign
  8052. org-table-may-need-update)
  8053. (org-table-align))
  8054. (let ((col (org-table-current-column)))
  8055. (beginning-of-line 2)
  8056. (if (or (not (org-at-table-p))
  8057. (org-at-table-hline-p))
  8058. (progn
  8059. (beginning-of-line 0)
  8060. (org-table-insert-row 'below)))
  8061. (org-table-goto-column col)
  8062. (skip-chars-backward "^|\n\r")
  8063. (if (looking-at " ") (forward-char 1)))))
  8064. (defun org-table-copy-down (n)
  8065. "Copy a field down in the current column.
  8066. If the field at the cursor is empty, copy into it the content of the nearest
  8067. non-empty field above. With argument N, use the Nth non-empty field.
  8068. If the current field is not empty, it is copied down to the next row, and
  8069. the cursor is moved with it. Therefore, repeating this command causes the
  8070. column to be filled row-by-row.
  8071. If the variable `org-table-copy-increment' is non-nil and the field is an
  8072. integer or a timestamp, it will be incremented while copying. In the case of
  8073. a timestamp, if the cursor is on the year, change the year. If it is on the
  8074. month or the day, change that. Point will stay on the current date field
  8075. in order to easily repeat the interval."
  8076. (interactive "p")
  8077. (let* ((colpos (org-table-current-column))
  8078. (col (current-column))
  8079. (field (org-table-get-field))
  8080. (non-empty (string-match "[^ \t]" field))
  8081. (beg (org-table-begin))
  8082. txt)
  8083. (org-table-check-inside-data-field)
  8084. (if non-empty
  8085. (progn
  8086. (setq txt (org-trim field))
  8087. (org-table-next-row)
  8088. (org-table-blank-field))
  8089. (save-excursion
  8090. (setq txt
  8091. (catch 'exit
  8092. (while (progn (beginning-of-line 1)
  8093. (re-search-backward org-table-dataline-regexp
  8094. beg t))
  8095. (org-table-goto-column colpos t)
  8096. (if (and (looking-at
  8097. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8098. (= (setq n (1- n)) 0))
  8099. (throw 'exit (match-string 1))))))))
  8100. (if txt
  8101. (progn
  8102. (if (and org-table-copy-increment
  8103. (string-match "^[0-9]+$" txt))
  8104. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8105. (insert txt)
  8106. (move-to-column col)
  8107. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8108. (org-timestamp-up 1)
  8109. (org-table-maybe-recalculate-line))
  8110. (org-table-align)
  8111. (move-to-column col))
  8112. (error "No non-empty field found"))))
  8113. (defun org-table-check-inside-data-field ()
  8114. "Is point inside a table data field?
  8115. I.e. not on a hline or before the first or after the last column?
  8116. This actually throws an error, so it aborts the current command."
  8117. (if (or (not (org-at-table-p))
  8118. (= (org-table-current-column) 0)
  8119. (org-at-table-hline-p)
  8120. (looking-at "[ \t]*$"))
  8121. (error "Not in table data field")))
  8122. (defvar org-table-clip nil
  8123. "Clipboard for table regions.")
  8124. (defun org-table-blank-field ()
  8125. "Blank the current table field or active region."
  8126. (interactive)
  8127. (org-table-check-inside-data-field)
  8128. (if (and (interactive-p) (org-region-active-p))
  8129. (let (org-table-clip)
  8130. (org-table-cut-region (region-beginning) (region-end)))
  8131. (skip-chars-backward "^|")
  8132. (backward-char 1)
  8133. (if (looking-at "|[^|\n]+")
  8134. (let* ((pos (match-beginning 0))
  8135. (match (match-string 0))
  8136. (len (org-string-width match)))
  8137. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8138. (goto-char (+ 2 pos))
  8139. (substring match 1)))))
  8140. (defun org-table-get-field (&optional n replace)
  8141. "Return the value of the field in column N of current row.
  8142. N defaults to current field.
  8143. If REPLACE is a string, replace field with this value. The return value
  8144. is always the old value."
  8145. (and n (org-table-goto-column n))
  8146. (skip-chars-backward "^|\n")
  8147. (backward-char 1)
  8148. (if (looking-at "|[^|\r\n]*")
  8149. (let* ((pos (match-beginning 0))
  8150. (val (buffer-substring (1+ pos) (match-end 0))))
  8151. (if replace
  8152. (replace-match (concat "|" replace) t t))
  8153. (goto-char (min (point-at-eol) (+ 2 pos)))
  8154. val)
  8155. (forward-char 1) ""))
  8156. (defun org-table-field-info (arg)
  8157. "Show info about the current field, and highlight any reference at point."
  8158. (interactive "P")
  8159. (org-table-get-specials)
  8160. (save-excursion
  8161. (let* ((pos (point))
  8162. (col (org-table-current-column))
  8163. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8164. (name (car (rassoc (list (org-current-line) col)
  8165. org-table-named-field-locations)))
  8166. (eql (org-table-get-stored-formulas))
  8167. (dline (org-table-current-dline))
  8168. (ref (format "@%d$%d" dline col))
  8169. (ref1 (org-table-convert-refs-to-an ref))
  8170. (fequation (or (assoc name eql) (assoc ref eql)))
  8171. (cequation (assoc (int-to-string col) eql))
  8172. (eqn (or fequation cequation)))
  8173. (goto-char pos)
  8174. (condition-case nil
  8175. (org-table-show-reference 'local)
  8176. (error nil))
  8177. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8178. dline col
  8179. (if cname (concat " or $" cname) "")
  8180. dline col ref1
  8181. (if name (concat " or $" name) "")
  8182. ;; FIXME: formula info not correct if special table line
  8183. (if eqn
  8184. (concat ", formula: "
  8185. (org-table-formula-to-user
  8186. (concat
  8187. (if (string-match "^[$@]"(car eqn)) "" "$")
  8188. (car eqn) "=" (cdr eqn))))
  8189. "")))))
  8190. (defun org-table-current-column ()
  8191. "Find out which column we are in.
  8192. When called interactively, column is also displayed in echo area."
  8193. (interactive)
  8194. (if (interactive-p) (org-table-check-inside-data-field))
  8195. (save-excursion
  8196. (let ((cnt 0) (pos (point)))
  8197. (beginning-of-line 1)
  8198. (while (search-forward "|" pos t)
  8199. (setq cnt (1+ cnt)))
  8200. (if (interactive-p) (message "This is table column %d" cnt))
  8201. cnt)))
  8202. (defun org-table-current-dline ()
  8203. "Find out what table data line we are in.
  8204. Only datalins count for this."
  8205. (interactive)
  8206. (if (interactive-p) (org-table-check-inside-data-field))
  8207. (save-excursion
  8208. (let ((cnt 0) (pos (point)))
  8209. (goto-char (org-table-begin))
  8210. (while (<= (point) pos)
  8211. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8212. (beginning-of-line 2))
  8213. (if (interactive-p) (message "This is table line %d" cnt))
  8214. cnt)))
  8215. (defun org-table-goto-column (n &optional on-delim force)
  8216. "Move the cursor to the Nth column in the current table line.
  8217. With optional argument ON-DELIM, stop with point before the left delimiter
  8218. of the field.
  8219. If there are less than N fields, just go to after the last delimiter.
  8220. However, when FORCE is non-nil, create new columns if necessary."
  8221. (interactive "p")
  8222. (let ((pos (point-at-eol)))
  8223. (beginning-of-line 1)
  8224. (when (> n 0)
  8225. (while (and (> (setq n (1- n)) -1)
  8226. (or (search-forward "|" pos t)
  8227. (and force
  8228. (progn (end-of-line 1)
  8229. (skip-chars-backward "^|")
  8230. (insert " | "))))))
  8231. ; (backward-char 2) t)))))
  8232. (when (and force (not (looking-at ".*|")))
  8233. (save-excursion (end-of-line 1) (insert " | ")))
  8234. (if on-delim
  8235. (backward-char 1)
  8236. (if (looking-at " ") (forward-char 1))))))
  8237. (defun org-at-table-p (&optional table-type)
  8238. "Return t if the cursor is inside an org-type table.
  8239. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8240. (if org-enable-table-editor
  8241. (save-excursion
  8242. (beginning-of-line 1)
  8243. (looking-at (if table-type org-table-any-line-regexp
  8244. org-table-line-regexp)))
  8245. nil))
  8246. (defun org-at-table.el-p ()
  8247. "Return t if and only if we are at a table.el table."
  8248. (and (org-at-table-p 'any)
  8249. (save-excursion
  8250. (goto-char (org-table-begin 'any))
  8251. (looking-at org-table1-hline-regexp))))
  8252. (defun org-table-recognize-table.el ()
  8253. "If there is a table.el table nearby, recognize it and move into it."
  8254. (if org-table-tab-recognizes-table.el
  8255. (if (org-at-table.el-p)
  8256. (progn
  8257. (beginning-of-line 1)
  8258. (if (looking-at org-table-dataline-regexp)
  8259. nil
  8260. (if (looking-at org-table1-hline-regexp)
  8261. (progn
  8262. (beginning-of-line 2)
  8263. (if (looking-at org-table-any-border-regexp)
  8264. (beginning-of-line -1)))))
  8265. (if (re-search-forward "|" (org-table-end t) t)
  8266. (progn
  8267. (require 'table)
  8268. (if (table--at-cell-p (point))
  8269. t
  8270. (message "recognizing table.el table...")
  8271. (table-recognize-table)
  8272. (message "recognizing table.el table...done")))
  8273. (error "This should not happen..."))
  8274. t)
  8275. nil)
  8276. nil))
  8277. (defun org-at-table-hline-p ()
  8278. "Return t if the cursor is inside a hline in a table."
  8279. (if org-enable-table-editor
  8280. (save-excursion
  8281. (beginning-of-line 1)
  8282. (looking-at org-table-hline-regexp))
  8283. nil))
  8284. (defun org-table-insert-column ()
  8285. "Insert a new column into the table."
  8286. (interactive)
  8287. (if (not (org-at-table-p))
  8288. (error "Not at a table"))
  8289. (org-table-find-dataline)
  8290. (let* ((col (max 1 (org-table-current-column)))
  8291. (beg (org-table-begin))
  8292. (end (org-table-end))
  8293. ;; Current cursor position
  8294. (linepos (org-current-line))
  8295. (colpos col))
  8296. (goto-char beg)
  8297. (while (< (point) end)
  8298. (if (org-at-table-hline-p)
  8299. nil
  8300. (org-table-goto-column col t)
  8301. (insert "| "))
  8302. (beginning-of-line 2))
  8303. (move-marker end nil)
  8304. (goto-line linepos)
  8305. (org-table-goto-column colpos)
  8306. (org-table-align)
  8307. (org-table-fix-formulas "$" nil (1- col) 1)))
  8308. (defun org-table-find-dataline ()
  8309. "Find a dataline in the current table, which is needed for column commands."
  8310. (if (and (org-at-table-p)
  8311. (not (org-at-table-hline-p)))
  8312. t
  8313. (let ((col (current-column))
  8314. (end (org-table-end)))
  8315. (move-to-column col)
  8316. (while (and (< (point) end)
  8317. (or (not (= (current-column) col))
  8318. (org-at-table-hline-p)))
  8319. (beginning-of-line 2)
  8320. (move-to-column col))
  8321. (if (and (org-at-table-p)
  8322. (not (org-at-table-hline-p)))
  8323. t
  8324. (error
  8325. "Please position cursor in a data line for column operations")))))
  8326. (defun org-table-delete-column ()
  8327. "Delete a column from the table."
  8328. (interactive)
  8329. (if (not (org-at-table-p))
  8330. (error "Not at a table"))
  8331. (org-table-find-dataline)
  8332. (org-table-check-inside-data-field)
  8333. (let* ((col (org-table-current-column))
  8334. (beg (org-table-begin))
  8335. (end (org-table-end))
  8336. ;; Current cursor position
  8337. (linepos (org-current-line))
  8338. (colpos col))
  8339. (goto-char beg)
  8340. (while (< (point) end)
  8341. (if (org-at-table-hline-p)
  8342. nil
  8343. (org-table-goto-column col t)
  8344. (and (looking-at "|[^|\n]+|")
  8345. (replace-match "|")))
  8346. (beginning-of-line 2))
  8347. (move-marker end nil)
  8348. (goto-line linepos)
  8349. (org-table-goto-column colpos)
  8350. (org-table-align)
  8351. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8352. col -1 col)))
  8353. (defun org-table-move-column-right ()
  8354. "Move column to the right."
  8355. (interactive)
  8356. (org-table-move-column nil))
  8357. (defun org-table-move-column-left ()
  8358. "Move column to the left."
  8359. (interactive)
  8360. (org-table-move-column 'left))
  8361. (defun org-table-move-column (&optional left)
  8362. "Move the current column to the right. With arg LEFT, move to the left."
  8363. (interactive "P")
  8364. (if (not (org-at-table-p))
  8365. (error "Not at a table"))
  8366. (org-table-find-dataline)
  8367. (org-table-check-inside-data-field)
  8368. (let* ((col (org-table-current-column))
  8369. (col1 (if left (1- col) col))
  8370. (beg (org-table-begin))
  8371. (end (org-table-end))
  8372. ;; Current cursor position
  8373. (linepos (org-current-line))
  8374. (colpos (if left (1- col) (1+ col))))
  8375. (if (and left (= col 1))
  8376. (error "Cannot move column further left"))
  8377. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8378. (error "Cannot move column further right"))
  8379. (goto-char beg)
  8380. (while (< (point) end)
  8381. (if (org-at-table-hline-p)
  8382. nil
  8383. (org-table-goto-column col1 t)
  8384. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8385. (replace-match "|\\2|\\1|")))
  8386. (beginning-of-line 2))
  8387. (move-marker end nil)
  8388. (goto-line linepos)
  8389. (org-table-goto-column colpos)
  8390. (org-table-align)
  8391. (org-table-fix-formulas
  8392. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8393. (cons (number-to-string colpos) (number-to-string col))))))
  8394. (defun org-table-move-row-down ()
  8395. "Move table row down."
  8396. (interactive)
  8397. (org-table-move-row nil))
  8398. (defun org-table-move-row-up ()
  8399. "Move table row up."
  8400. (interactive)
  8401. (org-table-move-row 'up))
  8402. (defun org-table-move-row (&optional up)
  8403. "Move the current table line down. With arg UP, move it up."
  8404. (interactive "P")
  8405. (let* ((col (current-column))
  8406. (pos (point))
  8407. (hline1p (save-excursion (beginning-of-line 1)
  8408. (looking-at org-table-hline-regexp)))
  8409. (dline1 (org-table-current-dline))
  8410. (dline2 (+ dline1 (if up -1 1)))
  8411. (tonew (if up 0 2))
  8412. txt hline2p)
  8413. (beginning-of-line tonew)
  8414. (unless (org-at-table-p)
  8415. (goto-char pos)
  8416. (error "Cannot move row further"))
  8417. (setq hline2p (looking-at org-table-hline-regexp))
  8418. (goto-char pos)
  8419. (beginning-of-line 1)
  8420. (setq pos (point))
  8421. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8422. (delete-region (point) (1+ (point-at-eol)))
  8423. (beginning-of-line tonew)
  8424. (insert txt)
  8425. (beginning-of-line 0)
  8426. (move-to-column col)
  8427. (unless (or hline1p hline2p)
  8428. (org-table-fix-formulas
  8429. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8430. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8431. (defun org-table-insert-row (&optional arg)
  8432. "Insert a new row above the current line into the table.
  8433. With prefix ARG, insert below the current line."
  8434. (interactive "P")
  8435. (if (not (org-at-table-p))
  8436. (error "Not at a table"))
  8437. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8438. (new (org-table-clean-line line)))
  8439. ;; Fix the first field if necessary
  8440. (if (string-match "^[ \t]*| *[#$] *|" line)
  8441. (setq new (replace-match (match-string 0 line) t t new)))
  8442. (beginning-of-line (if arg 2 1))
  8443. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8444. (beginning-of-line 0)
  8445. (re-search-forward "| ?" (point-at-eol) t)
  8446. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8447. (org-table-align))
  8448. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8449. (defun org-table-insert-hline (&optional above)
  8450. "Insert a horizontal-line below the current line into the table.
  8451. With prefix ABOVE, insert above the current line."
  8452. (interactive "P")
  8453. (if (not (org-at-table-p))
  8454. (error "Not at a table"))
  8455. (let ((line (org-table-clean-line
  8456. (buffer-substring (point-at-bol) (point-at-eol))))
  8457. (col (current-column)))
  8458. (while (string-match "|\\( +\\)|" line)
  8459. (setq line (replace-match
  8460. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8461. ?-) "|") t t line)))
  8462. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8463. (beginning-of-line (if above 1 2))
  8464. (insert line "\n")
  8465. (beginning-of-line (if above 1 -1))
  8466. (move-to-column col)
  8467. (and org-table-overlay-coordinates (org-table-align))))
  8468. (defun org-table-hline-and-move (&optional same-column)
  8469. "Insert a hline and move to the row below that line."
  8470. (interactive "P")
  8471. (let ((col (org-table-current-column)))
  8472. (org-table-maybe-eval-formula)
  8473. (org-table-maybe-recalculate-line)
  8474. (org-table-insert-hline)
  8475. (end-of-line 2)
  8476. (if (looking-at "\n[ \t]*|-")
  8477. (progn (insert "\n|") (org-table-align))
  8478. (org-table-next-field))
  8479. (if same-column (org-table-goto-column col))))
  8480. (defun org-table-clean-line (s)
  8481. "Convert a table line S into a string with only \"|\" and space.
  8482. In particular, this does handle wide and invisible characters."
  8483. (if (string-match "^[ \t]*|-" s)
  8484. ;; It's a hline, just map the characters
  8485. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8486. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8487. (setq s (replace-match
  8488. (concat "|" (make-string (org-string-width (match-string 1 s))
  8489. ?\ ) "|")
  8490. t t s)))
  8491. s))
  8492. (defun org-table-kill-row ()
  8493. "Delete the current row or horizontal line from the table."
  8494. (interactive)
  8495. (if (not (org-at-table-p))
  8496. (error "Not at a table"))
  8497. (let ((col (current-column))
  8498. (dline (org-table-current-dline)))
  8499. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8500. (if (not (org-at-table-p)) (beginning-of-line 0))
  8501. (move-to-column col)
  8502. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8503. dline -1 dline)))
  8504. (defun org-table-sort-lines (with-case &optional sorting-type)
  8505. "Sort table lines according to the column at point.
  8506. The position of point indicates the column to be used for
  8507. sorting, and the range of lines is the range between the nearest
  8508. horizontal separator lines, or the entire table of no such lines
  8509. exist. If point is before the first column, you will be prompted
  8510. for the sorting column. If there is an active region, the mark
  8511. specifies the first line and the sorting column, while point
  8512. should be in the last line to be included into the sorting.
  8513. The command then prompts for the sorting type which can be
  8514. alphabetically, numerically, or by time (as given in a time stamp
  8515. in the field). Sorting in reverse order is also possible.
  8516. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8517. If SORTING-TYPE is specified when this function is called from a Lisp
  8518. program, no prompting will take place. SORTING-TYPE must be a character,
  8519. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8520. should be done in reverse order."
  8521. (interactive "P")
  8522. (let* ((thisline (org-current-line))
  8523. (thiscol (org-table-current-column))
  8524. beg end bcol ecol tend tbeg column lns pos)
  8525. (when (equal thiscol 0)
  8526. (if (interactive-p)
  8527. (setq thiscol
  8528. (string-to-number
  8529. (read-string "Use column N for sorting: ")))
  8530. (setq thiscol 1))
  8531. (org-table-goto-column thiscol))
  8532. (org-table-check-inside-data-field)
  8533. (if (org-region-active-p)
  8534. (progn
  8535. (setq beg (region-beginning) end (region-end))
  8536. (goto-char beg)
  8537. (setq column (org-table-current-column)
  8538. beg (point-at-bol))
  8539. (goto-char end)
  8540. (setq end (point-at-bol 2)))
  8541. (setq column (org-table-current-column)
  8542. pos (point)
  8543. tbeg (org-table-begin)
  8544. tend (org-table-end))
  8545. (if (re-search-backward org-table-hline-regexp tbeg t)
  8546. (setq beg (point-at-bol 2))
  8547. (goto-char tbeg)
  8548. (setq beg (point-at-bol 1)))
  8549. (goto-char pos)
  8550. (if (re-search-forward org-table-hline-regexp tend t)
  8551. (setq end (point-at-bol 1))
  8552. (goto-char tend)
  8553. (setq end (point-at-bol))))
  8554. (setq beg (move-marker (make-marker) beg)
  8555. end (move-marker (make-marker) end))
  8556. (untabify beg end)
  8557. (goto-char beg)
  8558. (org-table-goto-column column)
  8559. (skip-chars-backward "^|")
  8560. (setq bcol (current-column))
  8561. (org-table-goto-column (1+ column))
  8562. (skip-chars-backward "^|")
  8563. (setq ecol (1- (current-column)))
  8564. (org-table-goto-column column)
  8565. (setq lns (mapcar (lambda(x) (cons
  8566. (org-sort-remove-invisible
  8567. (nth (1- column)
  8568. (org-split-string x "[ \t]*|[ \t]*")))
  8569. x))
  8570. (org-split-string (buffer-substring beg end) "\n")))
  8571. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8572. (delete-region beg end)
  8573. (move-marker beg nil)
  8574. (move-marker end nil)
  8575. (insert (mapconcat 'cdr lns "\n") "\n")
  8576. (goto-line thisline)
  8577. (org-table-goto-column thiscol)
  8578. (message "%d lines sorted, based on column %d" (length lns) column)))
  8579. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8580. (defun org-sort-remove-invisible (s)
  8581. (remove-text-properties 0 (length s) org-rm-props s)
  8582. (while (string-match org-bracket-link-regexp s)
  8583. (setq s (replace-match (if (match-end 2)
  8584. (match-string 3 s)
  8585. (match-string 1 s)) t t s)))
  8586. s)
  8587. (defun org-table-cut-region (beg end)
  8588. "Copy region in table to the clipboard and blank all relevant fields."
  8589. (interactive "r")
  8590. (org-table-copy-region beg end 'cut))
  8591. (defun org-table-copy-region (beg end &optional cut)
  8592. "Copy rectangular region in table to clipboard.
  8593. A special clipboard is used which can only be accessed
  8594. with `org-table-paste-rectangle'."
  8595. (interactive "rP")
  8596. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8597. region cols
  8598. (rpl (if cut " " nil)))
  8599. (goto-char beg)
  8600. (org-table-check-inside-data-field)
  8601. (setq l01 (org-current-line)
  8602. c01 (org-table-current-column))
  8603. (goto-char end)
  8604. (org-table-check-inside-data-field)
  8605. (setq l02 (org-current-line)
  8606. c02 (org-table-current-column))
  8607. (setq l1 (min l01 l02) l2 (max l01 l02)
  8608. c1 (min c01 c02) c2 (max c01 c02))
  8609. (catch 'exit
  8610. (while t
  8611. (catch 'nextline
  8612. (if (> l1 l2) (throw 'exit t))
  8613. (goto-line l1)
  8614. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8615. (setq cols nil ic1 c1 ic2 c2)
  8616. (while (< ic1 (1+ ic2))
  8617. (push (org-table-get-field ic1 rpl) cols)
  8618. (setq ic1 (1+ ic1)))
  8619. (push (nreverse cols) region)
  8620. (setq l1 (1+ l1)))))
  8621. (setq org-table-clip (nreverse region))
  8622. (if cut (org-table-align))
  8623. org-table-clip))
  8624. (defun org-table-paste-rectangle ()
  8625. "Paste a rectangular region into a table.
  8626. The upper right corner ends up in the current field. All involved fields
  8627. will be overwritten. If the rectangle does not fit into the present table,
  8628. the table is enlarged as needed. The process ignores horizontal separator
  8629. lines."
  8630. (interactive)
  8631. (unless (and org-table-clip (listp org-table-clip))
  8632. (error "First cut/copy a region to paste!"))
  8633. (org-table-check-inside-data-field)
  8634. (let* ((clip org-table-clip)
  8635. (line (org-current-line))
  8636. (col (org-table-current-column))
  8637. (org-enable-table-editor t)
  8638. (org-table-automatic-realign nil)
  8639. c cols field)
  8640. (while (setq cols (pop clip))
  8641. (while (org-at-table-hline-p) (beginning-of-line 2))
  8642. (if (not (org-at-table-p))
  8643. (progn (end-of-line 0) (org-table-next-field)))
  8644. (setq c col)
  8645. (while (setq field (pop cols))
  8646. (org-table-goto-column c nil 'force)
  8647. (org-table-get-field nil field)
  8648. (setq c (1+ c)))
  8649. (beginning-of-line 2))
  8650. (goto-line line)
  8651. (org-table-goto-column col)
  8652. (org-table-align)))
  8653. (defun org-table-convert ()
  8654. "Convert from `org-mode' table to table.el and back.
  8655. Obviously, this only works within limits. When an Org-mode table is
  8656. converted to table.el, all horizontal separator lines get lost, because
  8657. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8658. A table.el table can be converted to an Org-mode table only if it does not
  8659. do row or column spanning. Multiline cells will become multiple cells.
  8660. Beware, Org-mode does not test if the table can be successfully converted - it
  8661. blindly applies a recipe that works for simple tables."
  8662. (interactive)
  8663. (require 'table)
  8664. (if (org-at-table.el-p)
  8665. ;; convert to Org-mode table
  8666. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8667. (end (move-marker (make-marker) (org-table-end t))))
  8668. (table-unrecognize-region beg end)
  8669. (goto-char beg)
  8670. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8671. (replace-match ""))
  8672. (goto-char beg))
  8673. (if (org-at-table-p)
  8674. ;; convert to table.el table
  8675. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8676. (end (move-marker (make-marker) (org-table-end))))
  8677. ;; first, get rid of all horizontal lines
  8678. (goto-char beg)
  8679. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8680. (replace-match ""))
  8681. ;; insert a hline before first
  8682. (goto-char beg)
  8683. (org-table-insert-hline 'above)
  8684. (beginning-of-line -1)
  8685. ;; insert a hline after each line
  8686. (while (progn (beginning-of-line 3) (< (point) end))
  8687. (org-table-insert-hline))
  8688. (goto-char beg)
  8689. (setq end (move-marker end (org-table-end)))
  8690. ;; replace "+" at beginning and ending of hlines
  8691. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8692. (replace-match "\\1+-"))
  8693. (goto-char beg)
  8694. (while (re-search-forward "-|[ \t]*$" end t)
  8695. (replace-match "-+"))
  8696. (goto-char beg)))))
  8697. (defun org-table-wrap-region (arg)
  8698. "Wrap several fields in a column like a paragraph.
  8699. This is useful if you'd like to spread the contents of a field over several
  8700. lines, in order to keep the table compact.
  8701. If there is an active region, and both point and mark are in the same column,
  8702. the text in the column is wrapped to minimum width for the given number of
  8703. lines. Generally, this makes the table more compact. A prefix ARG may be
  8704. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8705. formats the selected text to two lines. If the region was longer than two
  8706. lines, the remaining lines remain empty. A negative prefix argument reduces
  8707. the current number of lines by that amount. The wrapped text is pasted back
  8708. into the table. If you formatted it to more lines than it was before, fields
  8709. further down in the table get overwritten - so you might need to make space in
  8710. the table first.
  8711. If there is no region, the current field is split at the cursor position and
  8712. the text fragment to the right of the cursor is prepended to the field one
  8713. line down.
  8714. If there is no region, but you specify a prefix ARG, the current field gets
  8715. blank, and the content is appended to the field above."
  8716. (interactive "P")
  8717. (org-table-check-inside-data-field)
  8718. (if (org-region-active-p)
  8719. ;; There is a region: fill as a paragraph
  8720. (let* ((beg (region-beginning))
  8721. (cline (save-excursion (goto-char beg) (org-current-line)))
  8722. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8723. nlines)
  8724. (org-table-cut-region (region-beginning) (region-end))
  8725. (if (> (length (car org-table-clip)) 1)
  8726. (error "Region must be limited to single column"))
  8727. (setq nlines (if arg
  8728. (if (< arg 1)
  8729. (+ (length org-table-clip) arg)
  8730. arg)
  8731. (length org-table-clip)))
  8732. (setq org-table-clip
  8733. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8734. nil nlines)))
  8735. (goto-line cline)
  8736. (org-table-goto-column ccol)
  8737. (org-table-paste-rectangle))
  8738. ;; No region, split the current field at point
  8739. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8740. (skip-chars-forward "^\r\n|"))
  8741. (if arg
  8742. ;; combine with field above
  8743. (let ((s (org-table-blank-field))
  8744. (col (org-table-current-column)))
  8745. (beginning-of-line 0)
  8746. (while (org-at-table-hline-p) (beginning-of-line 0))
  8747. (org-table-goto-column col)
  8748. (skip-chars-forward "^|")
  8749. (skip-chars-backward " ")
  8750. (insert " " (org-trim s))
  8751. (org-table-align))
  8752. ;; split field
  8753. (if (looking-at "\\([^|]+\\)+|")
  8754. (let ((s (match-string 1)))
  8755. (replace-match " |")
  8756. (goto-char (match-beginning 0))
  8757. (org-table-next-row)
  8758. (insert (org-trim s) " ")
  8759. (org-table-align))
  8760. (org-table-next-row)))))
  8761. (defvar org-field-marker nil)
  8762. (defun org-table-edit-field (arg)
  8763. "Edit table field in a different window.
  8764. This is mainly useful for fields that contain hidden parts.
  8765. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8766. it can be edited in place."
  8767. (interactive "P")
  8768. (if arg
  8769. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8770. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8771. (remove-text-properties b e '(org-cwidth t invisible t
  8772. display t intangible t))
  8773. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8774. (font-lock-fontify-block)))
  8775. (let ((pos (move-marker (make-marker) (point)))
  8776. (field (org-table-get-field))
  8777. (cw (current-window-configuration))
  8778. p)
  8779. (org-switch-to-buffer-other-window "*Org tmp*")
  8780. (erase-buffer)
  8781. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8782. (let ((org-inhibit-startup t)) (org-mode))
  8783. (goto-char (setq p (point-max)))
  8784. (insert (org-trim field))
  8785. (remove-text-properties p (point-max)
  8786. '(invisible t org-cwidth t display t
  8787. intangible t))
  8788. (goto-char p)
  8789. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8790. (org-set-local 'org-window-configuration cw)
  8791. (org-set-local 'org-field-marker pos)
  8792. (message "Edit and finish with C-c C-c"))))
  8793. (defun org-table-finish-edit-field ()
  8794. "Finish editing a table data field.
  8795. Remove all newline characters, insert the result into the table, realign
  8796. the table and kill the editing buffer."
  8797. (let ((pos org-field-marker)
  8798. (cw org-window-configuration)
  8799. (cb (current-buffer))
  8800. text)
  8801. (goto-char (point-min))
  8802. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8803. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8804. (replace-match " "))
  8805. (setq text (org-trim (buffer-string)))
  8806. (set-window-configuration cw)
  8807. (kill-buffer cb)
  8808. (select-window (get-buffer-window (marker-buffer pos)))
  8809. (goto-char pos)
  8810. (move-marker pos nil)
  8811. (org-table-check-inside-data-field)
  8812. (org-table-get-field nil text)
  8813. (org-table-align)
  8814. (message "New field value inserted")))
  8815. (defun org-trim (s)
  8816. "Remove whitespace at beginning and end of string."
  8817. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8818. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8819. s)
  8820. (defun org-wrap (string &optional width lines)
  8821. "Wrap string to either a number of lines, or a width in characters.
  8822. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8823. that costs. If there is a word longer than WIDTH, the text is actually
  8824. wrapped to the length of that word.
  8825. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8826. many lines, whatever width that takes.
  8827. The return value is a list of lines, without newlines at the end."
  8828. (let* ((words (org-split-string string "[ \t\n]+"))
  8829. (maxword (apply 'max (mapcar 'org-string-width words)))
  8830. w ll)
  8831. (cond (width
  8832. (org-do-wrap words (max maxword width)))
  8833. (lines
  8834. (setq w maxword)
  8835. (setq ll (org-do-wrap words maxword))
  8836. (if (<= (length ll) lines)
  8837. ll
  8838. (setq ll words)
  8839. (while (> (length ll) lines)
  8840. (setq w (1+ w))
  8841. (setq ll (org-do-wrap words w)))
  8842. ll))
  8843. (t (error "Cannot wrap this")))))
  8844. (defun org-do-wrap (words width)
  8845. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8846. (let (lines line)
  8847. (while words
  8848. (setq line (pop words))
  8849. (while (and words (< (+ (length line) (length (car words))) width))
  8850. (setq line (concat line " " (pop words))))
  8851. (setq lines (push line lines)))
  8852. (nreverse lines)))
  8853. (defun org-split-string (string &optional separators)
  8854. "Splits STRING into substrings at SEPARATORS.
  8855. No empty strings are returned if there are matches at the beginning
  8856. and end of string."
  8857. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8858. (start 0)
  8859. notfirst
  8860. (list nil))
  8861. (while (and (string-match rexp string
  8862. (if (and notfirst
  8863. (= start (match-beginning 0))
  8864. (< start (length string)))
  8865. (1+ start) start))
  8866. (< (match-beginning 0) (length string)))
  8867. (setq notfirst t)
  8868. (or (eq (match-beginning 0) 0)
  8869. (and (eq (match-beginning 0) (match-end 0))
  8870. (eq (match-beginning 0) start))
  8871. (setq list
  8872. (cons (substring string start (match-beginning 0))
  8873. list)))
  8874. (setq start (match-end 0)))
  8875. (or (eq start (length string))
  8876. (setq list
  8877. (cons (substring string start)
  8878. list)))
  8879. (nreverse list)))
  8880. (defun org-table-map-tables (function)
  8881. "Apply FUNCTION to the start of all tables in the buffer."
  8882. (save-excursion
  8883. (save-restriction
  8884. (widen)
  8885. (goto-char (point-min))
  8886. (while (re-search-forward org-table-any-line-regexp nil t)
  8887. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8888. (beginning-of-line 1)
  8889. (if (looking-at org-table-line-regexp)
  8890. (save-excursion (funcall function)))
  8891. (re-search-forward org-table-any-border-regexp nil 1))))
  8892. (message "Mapping tables: done"))
  8893. (defvar org-timecnt) ; dynamically scoped parameter
  8894. (defun org-table-sum (&optional beg end nlast)
  8895. "Sum numbers in region of current table column.
  8896. The result will be displayed in the echo area, and will be available
  8897. as kill to be inserted with \\[yank].
  8898. If there is an active region, it is interpreted as a rectangle and all
  8899. numbers in that rectangle will be summed. If there is no active
  8900. region and point is located in a table column, sum all numbers in that
  8901. column.
  8902. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8903. numbers are assumed to be times as well (in decimal hours) and the
  8904. numbers are added as such.
  8905. If NLAST is a number, only the NLAST fields will actually be summed."
  8906. (interactive)
  8907. (save-excursion
  8908. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8909. (cond
  8910. ((and beg end)) ; beg and end given explicitly
  8911. ((org-region-active-p)
  8912. (setq beg (region-beginning) end (region-end)))
  8913. (t
  8914. (setq col (org-table-current-column))
  8915. (goto-char (org-table-begin))
  8916. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8917. (error "No table data"))
  8918. (org-table-goto-column col)
  8919. (setq beg (point))
  8920. (goto-char (org-table-end))
  8921. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8922. (error "No table data"))
  8923. (org-table-goto-column col)
  8924. (setq end (point))))
  8925. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8926. (items1 (cond ((not nlast) items)
  8927. ((>= nlast (length items)) items)
  8928. (t (setq items (reverse items))
  8929. (setcdr (nthcdr (1- nlast) items) nil)
  8930. (nreverse items))))
  8931. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8932. items1)))
  8933. (res (apply '+ numbers))
  8934. (sres (if (= org-timecnt 0)
  8935. (format "%g" res)
  8936. (setq diff (* 3600 res)
  8937. h (floor (/ diff 3600)) diff (mod diff 3600)
  8938. m (floor (/ diff 60)) diff (mod diff 60)
  8939. s diff)
  8940. (format "%d:%02d:%02d" h m s))))
  8941. (kill-new sres)
  8942. (if (interactive-p)
  8943. (message "%s"
  8944. (substitute-command-keys
  8945. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8946. (length numbers) sres))))
  8947. sres))))
  8948. (defun org-table-get-number-for-summing (s)
  8949. (let (n)
  8950. (if (string-match "^ *|? *" s)
  8951. (setq s (replace-match "" nil nil s)))
  8952. (if (string-match " *|? *$" s)
  8953. (setq s (replace-match "" nil nil s)))
  8954. (setq n (string-to-number s))
  8955. (cond
  8956. ((and (string-match "0" s)
  8957. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8958. ((string-match "\\`[ \t]+\\'" s) nil)
  8959. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8960. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8961. (m (string-to-number (or (match-string 2 s) "0")))
  8962. (s (string-to-number (or (match-string 4 s) "0"))))
  8963. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8964. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8965. ((equal n 0) nil)
  8966. (t n))))
  8967. (defun org-table-current-field-formula (&optional key noerror)
  8968. "Return the formula active for the current field.
  8969. Assumes that specials are in place.
  8970. If KEY is given, return the key to this formula.
  8971. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8972. (let* ((name (car (rassoc (list (org-current-line)
  8973. (org-table-current-column))
  8974. org-table-named-field-locations)))
  8975. (col (org-table-current-column))
  8976. (scol (int-to-string col))
  8977. (ref (format "@%d$%d" (org-table-current-dline) col))
  8978. (stored-list (org-table-get-stored-formulas noerror))
  8979. (ass (or (assoc name stored-list)
  8980. (assoc ref stored-list)
  8981. (assoc scol stored-list))))
  8982. (if key
  8983. (car ass)
  8984. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8985. (cdr ass))))))
  8986. (defun org-table-get-formula (&optional equation named)
  8987. "Read a formula from the minibuffer, offer stored formula as default.
  8988. When NAMED is non-nil, look for a named equation."
  8989. (let* ((stored-list (org-table-get-stored-formulas))
  8990. (name (car (rassoc (list (org-current-line)
  8991. (org-table-current-column))
  8992. org-table-named-field-locations)))
  8993. (ref (format "@%d$%d" (org-table-current-dline)
  8994. (org-table-current-column)))
  8995. (refass (assoc ref stored-list))
  8996. (scol (if named
  8997. (if name name ref)
  8998. (int-to-string (org-table-current-column))))
  8999. (dummy (and (or name refass) (not named)
  9000. (not (y-or-n-p "Replace field formula with column formula? " ))
  9001. (error "Abort")))
  9002. (name (or name ref))
  9003. (org-table-may-need-update nil)
  9004. (stored (cdr (assoc scol stored-list)))
  9005. (eq (cond
  9006. ((and stored equation (string-match "^ *=? *$" equation))
  9007. stored)
  9008. ((stringp equation)
  9009. equation)
  9010. (t (org-table-formula-from-user
  9011. (read-string
  9012. (org-table-formula-to-user
  9013. (format "%s formula %s%s="
  9014. (if named "Field" "Column")
  9015. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  9016. scol))
  9017. (if stored (org-table-formula-to-user stored) "")
  9018. 'org-table-formula-history
  9019. )))))
  9020. mustsave)
  9021. (when (not (string-match "\\S-" eq))
  9022. ;; remove formula
  9023. (setq stored-list (delq (assoc scol stored-list) stored-list))
  9024. (org-table-store-formulas stored-list)
  9025. (error "Formula removed"))
  9026. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  9027. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  9028. (if (and name (not named))
  9029. ;; We set the column equation, delete the named one.
  9030. (setq stored-list (delq (assoc name stored-list) stored-list)
  9031. mustsave t))
  9032. (if stored
  9033. (setcdr (assoc scol stored-list) eq)
  9034. (setq stored-list (cons (cons scol eq) stored-list)))
  9035. (if (or mustsave (not (equal stored eq)))
  9036. (org-table-store-formulas stored-list))
  9037. eq))
  9038. (defun org-table-store-formulas (alist)
  9039. "Store the list of formulas below the current table."
  9040. (setq alist (sort alist 'org-table-formula-less-p))
  9041. (save-excursion
  9042. (goto-char (org-table-end))
  9043. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  9044. (progn
  9045. ;; don't overwrite TBLFM, we might use text properties to store stuff
  9046. (goto-char (match-beginning 2))
  9047. (delete-region (match-beginning 2) (match-end 0)))
  9048. (insert "#+TBLFM:"))
  9049. (insert " "
  9050. (mapconcat (lambda (x)
  9051. (concat
  9052. (if (equal (string-to-char (car x)) ?@) "" "$")
  9053. (car x) "=" (cdr x)))
  9054. alist "::")
  9055. "\n")))
  9056. (defsubst org-table-formula-make-cmp-string (a)
  9057. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  9058. (concat
  9059. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  9060. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  9061. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  9062. (defun org-table-formula-less-p (a b)
  9063. "Compare two formulas for sorting."
  9064. (let ((as (org-table-formula-make-cmp-string (car a)))
  9065. (bs (org-table-formula-make-cmp-string (car b))))
  9066. (and as bs (string< as bs))))
  9067. (defun org-table-get-stored-formulas (&optional noerror)
  9068. "Return an alist with the stored formulas directly after current table."
  9069. (interactive)
  9070. (let (scol eq eq-alist strings string seen)
  9071. (save-excursion
  9072. (goto-char (org-table-end))
  9073. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9074. (setq strings (org-split-string (match-string 2) " *:: *"))
  9075. (while (setq string (pop strings))
  9076. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  9077. (setq scol (if (match-end 2)
  9078. (match-string 2 string)
  9079. (match-string 1 string))
  9080. eq (match-string 3 string)
  9081. eq-alist (cons (cons scol eq) eq-alist))
  9082. (if (member scol seen)
  9083. (if noerror
  9084. (progn
  9085. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9086. (ding)
  9087. (sit-for 2))
  9088. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  9089. (push scol seen))))))
  9090. (nreverse eq-alist)))
  9091. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  9092. "Modify the equations after the table structure has been edited.
  9093. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  9094. For all numbers larger than LIMIT, shift them by DELTA."
  9095. (save-excursion
  9096. (goto-char (org-table-end))
  9097. (when (looking-at "#\\+TBLFM:")
  9098. (let ((re (concat key "\\([0-9]+\\)"))
  9099. (re2
  9100. (when remove
  9101. (if (equal key "$")
  9102. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9103. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9104. s n a)
  9105. (when remove
  9106. (while (re-search-forward re2 (point-at-eol) t)
  9107. (replace-match "")))
  9108. (while (re-search-forward re (point-at-eol) t)
  9109. (setq s (match-string 1) n (string-to-number s))
  9110. (cond
  9111. ((setq a (assoc s replace))
  9112. (replace-match (concat key (cdr a)) t t))
  9113. ((and limit (> n limit))
  9114. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9115. (defun org-table-get-specials ()
  9116. "Get the column names and local parameters for this table."
  9117. (save-excursion
  9118. (let ((beg (org-table-begin)) (end (org-table-end))
  9119. names name fields fields1 field cnt
  9120. c v l line col types dlines hlines)
  9121. (setq org-table-column-names nil
  9122. org-table-local-parameters nil
  9123. org-table-named-field-locations nil
  9124. org-table-current-begin-line nil
  9125. org-table-current-begin-pos nil
  9126. org-table-current-line-types nil)
  9127. (goto-char beg)
  9128. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9129. (setq names (org-split-string (match-string 1) " *| *")
  9130. cnt 1)
  9131. (while (setq name (pop names))
  9132. (setq cnt (1+ cnt))
  9133. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9134. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9135. (setq org-table-column-names (nreverse org-table-column-names))
  9136. (setq org-table-column-name-regexp
  9137. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9138. (goto-char beg)
  9139. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9140. (setq fields (org-split-string (match-string 1) " *| *"))
  9141. (while (setq field (pop fields))
  9142. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9143. (push (cons (match-string 1 field) (match-string 2 field))
  9144. org-table-local-parameters))))
  9145. (goto-char beg)
  9146. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9147. (setq c (match-string 1)
  9148. fields (org-split-string (match-string 2) " *| *"))
  9149. (save-excursion
  9150. (beginning-of-line (if (equal c "_") 2 0))
  9151. (setq line (org-current-line) col 1)
  9152. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9153. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9154. (while (and fields1 (setq field (pop fields)))
  9155. (setq v (pop fields1) col (1+ col))
  9156. (when (and (stringp field) (stringp v)
  9157. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9158. (push (cons field v) org-table-local-parameters)
  9159. (push (list field line col) org-table-named-field-locations))))
  9160. ;; Analyse the line types
  9161. (goto-char beg)
  9162. (setq org-table-current-begin-line (org-current-line)
  9163. org-table-current-begin-pos (point)
  9164. l org-table-current-begin-line)
  9165. (while (looking-at "[ \t]*|\\(-\\)?")
  9166. (push (if (match-end 1) 'hline 'dline) types)
  9167. (if (match-end 1) (push l hlines) (push l dlines))
  9168. (beginning-of-line 2)
  9169. (setq l (1+ l)))
  9170. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9171. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9172. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9173. (defun org-table-maybe-eval-formula ()
  9174. "Check if the current field starts with \"=\" or \":=\".
  9175. If yes, store the formula and apply it."
  9176. ;; We already know we are in a table. Get field will only return a formula
  9177. ;; when appropriate. It might return a separator line, but no problem.
  9178. (when org-table-formula-evaluate-inline
  9179. (let* ((field (org-trim (or (org-table-get-field) "")))
  9180. named eq)
  9181. (when (string-match "^:?=\\(.*\\)" field)
  9182. (setq named (equal (string-to-char field) ?:)
  9183. eq (match-string 1 field))
  9184. (if (or (fboundp 'calc-eval)
  9185. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9186. (org-table-eval-formula (if named '(4) nil)
  9187. (org-table-formula-from-user eq))
  9188. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9189. (defvar org-recalc-commands nil
  9190. "List of commands triggering the recalculation of a line.
  9191. Will be filled automatically during use.")
  9192. (defvar org-recalc-marks
  9193. '((" " . "Unmarked: no special line, no automatic recalculation")
  9194. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9195. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9196. ("!" . "Column name definition line. Reference in formula as $name.")
  9197. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9198. ("_" . "Names for values in row below this one.")
  9199. ("^" . "Names for values in row above this one.")))
  9200. (defun org-table-rotate-recalc-marks (&optional newchar)
  9201. "Rotate the recalculation mark in the first column.
  9202. If in any row, the first field is not consistent with a mark,
  9203. insert a new column for the markers.
  9204. When there is an active region, change all the lines in the region,
  9205. after prompting for the marking character.
  9206. After each change, a message will be displayed indicating the meaning
  9207. of the new mark."
  9208. (interactive)
  9209. (unless (org-at-table-p) (error "Not at a table"))
  9210. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9211. (beg (org-table-begin))
  9212. (end (org-table-end))
  9213. (l (org-current-line))
  9214. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9215. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9216. (have-col
  9217. (save-excursion
  9218. (goto-char beg)
  9219. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9220. (col (org-table-current-column))
  9221. (forcenew (car (assoc newchar org-recalc-marks)))
  9222. epos new)
  9223. (when l1
  9224. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9225. (setq newchar (char-to-string (read-char-exclusive))
  9226. forcenew (car (assoc newchar org-recalc-marks))))
  9227. (if (and newchar (not forcenew))
  9228. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9229. newchar))
  9230. (if l1 (goto-line l1))
  9231. (save-excursion
  9232. (beginning-of-line 1)
  9233. (unless (looking-at org-table-dataline-regexp)
  9234. (error "Not at a table data line")))
  9235. (unless have-col
  9236. (org-table-goto-column 1)
  9237. (org-table-insert-column)
  9238. (org-table-goto-column (1+ col)))
  9239. (setq epos (point-at-eol))
  9240. (save-excursion
  9241. (beginning-of-line 1)
  9242. (org-table-get-field
  9243. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9244. (concat " "
  9245. (setq new (or forcenew
  9246. (cadr (member (match-string 1) marks))))
  9247. " ")
  9248. " # ")))
  9249. (if (and l1 l2)
  9250. (progn
  9251. (goto-line l1)
  9252. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9253. (and (looking-at org-table-dataline-regexp)
  9254. (org-table-get-field 1 (concat " " new " "))))
  9255. (goto-line l1)))
  9256. (if (not (= epos (point-at-eol))) (org-table-align))
  9257. (goto-line l)
  9258. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9259. (defun org-table-maybe-recalculate-line ()
  9260. "Recompute the current line if marked for it, and if we haven't just done it."
  9261. (interactive)
  9262. (and org-table-allow-automatic-line-recalculation
  9263. (not (and (memq last-command org-recalc-commands)
  9264. (equal org-last-recalc-line (org-current-line))))
  9265. (save-excursion (beginning-of-line 1)
  9266. (looking-at org-table-auto-recalculate-regexp))
  9267. (org-table-recalculate) t))
  9268. (defvar org-table-formula-debug nil
  9269. "Non-nil means, debug table formulas.
  9270. When nil, simply write \"#ERROR\" in corrupted fields.")
  9271. (make-variable-buffer-local 'org-table-formula-debug)
  9272. (defvar modes)
  9273. (defsubst org-set-calc-mode (var &optional value)
  9274. (if (stringp var)
  9275. (setq var (assoc var '(("D" calc-angle-mode deg)
  9276. ("R" calc-angle-mode rad)
  9277. ("F" calc-prefer-frac t)
  9278. ("S" calc-symbolic-mode t)))
  9279. value (nth 2 var) var (nth 1 var)))
  9280. (if (memq var modes)
  9281. (setcar (cdr (memq var modes)) value)
  9282. (cons var (cons value modes)))
  9283. modes)
  9284. (defun org-table-eval-formula (&optional arg equation
  9285. suppress-align suppress-const
  9286. suppress-store suppress-analysis)
  9287. "Replace the table field value at the cursor by the result of a calculation.
  9288. This function makes use of Dave Gillespie's Calc package, in my view the
  9289. most exciting program ever written for GNU Emacs. So you need to have Calc
  9290. installed in order to use this function.
  9291. In a table, this command replaces the value in the current field with the
  9292. result of a formula. It also installs the formula as the \"current\" column
  9293. formula, by storing it in a special line below the table. When called
  9294. with a `C-u' prefix, the current field must ba a named field, and the
  9295. formula is installed as valid in only this specific field.
  9296. When called with two `C-u' prefixes, insert the active equation
  9297. for the field back into the current field, so that it can be
  9298. edited there. This is useful in order to use \\[org-table-show-reference]
  9299. to check the referenced fields.
  9300. When called, the command first prompts for a formula, which is read in
  9301. the minibuffer. Previously entered formulas are available through the
  9302. history list, and the last used formula is offered as a default.
  9303. These stored formulas are adapted correctly when moving, inserting, or
  9304. deleting columns with the corresponding commands.
  9305. The formula can be any algebraic expression understood by the Calc package.
  9306. For details, see the Org-mode manual.
  9307. This function can also be called from Lisp programs and offers
  9308. additional arguments: EQUATION can be the formula to apply. If this
  9309. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9310. used to speed-up recursive calls by by-passing unnecessary aligns.
  9311. SUPPRESS-CONST suppresses the interpretation of constants in the
  9312. formula, assuming that this has been done already outside the function.
  9313. SUPPRESS-STORE means the formula should not be stored, either because
  9314. it is already stored, or because it is a modified equation that should
  9315. not overwrite the stored one."
  9316. (interactive "P")
  9317. (org-table-check-inside-data-field)
  9318. (or suppress-analysis (org-table-get-specials))
  9319. (if (equal arg '(16))
  9320. (let ((eq (org-table-current-field-formula)))
  9321. (or eq (error "No equation active for current field"))
  9322. (org-table-get-field nil eq)
  9323. (org-table-align)
  9324. (setq org-table-may-need-update t))
  9325. (let* (fields
  9326. (ndown (if (integerp arg) arg 1))
  9327. (org-table-automatic-realign nil)
  9328. (case-fold-search nil)
  9329. (down (> ndown 1))
  9330. (formula (if (and equation suppress-store)
  9331. equation
  9332. (org-table-get-formula equation (equal arg '(4)))))
  9333. (n0 (org-table-current-column))
  9334. (modes (copy-sequence org-calc-default-modes))
  9335. (numbers nil) ; was a variable, now fixed default
  9336. (keep-empty nil)
  9337. n form form0 bw fmt x ev orig c lispp literal)
  9338. ;; Parse the format string. Since we have a lot of modes, this is
  9339. ;; a lot of work. However, I think calc still uses most of the time.
  9340. (if (string-match ";" formula)
  9341. (let ((tmp (org-split-string formula ";")))
  9342. (setq formula (car tmp)
  9343. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9344. (nth 1 tmp)))
  9345. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9346. (setq c (string-to-char (match-string 1 fmt))
  9347. n (string-to-number (match-string 2 fmt)))
  9348. (if (= c ?p)
  9349. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9350. (setq modes (org-set-calc-mode
  9351. 'calc-float-format
  9352. (list (cdr (assoc c '((?n . float) (?f . fix)
  9353. (?s . sci) (?e . eng))))
  9354. n))))
  9355. (setq fmt (replace-match "" t t fmt)))
  9356. (if (string-match "[NT]" fmt)
  9357. (setq numbers (equal (match-string 0 fmt) "N")
  9358. fmt (replace-match "" t t fmt)))
  9359. (if (string-match "L" fmt)
  9360. (setq literal t
  9361. fmt (replace-match "" t t fmt)))
  9362. (if (string-match "E" fmt)
  9363. (setq keep-empty t
  9364. fmt (replace-match "" t t fmt)))
  9365. (while (string-match "[DRFS]" fmt)
  9366. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9367. (setq fmt (replace-match "" t t fmt)))
  9368. (unless (string-match "\\S-" fmt)
  9369. (setq fmt nil))))
  9370. (if (and (not suppress-const) org-table-formula-use-constants)
  9371. (setq formula (org-table-formula-substitute-names formula)))
  9372. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9373. (while (> ndown 0)
  9374. (setq fields (org-split-string
  9375. (org-no-properties
  9376. (buffer-substring (point-at-bol) (point-at-eol)))
  9377. " *| *"))
  9378. (if (eq numbers t)
  9379. (setq fields (mapcar
  9380. (lambda (x) (number-to-string (string-to-number x)))
  9381. fields)))
  9382. (setq ndown (1- ndown))
  9383. (setq form (copy-sequence formula)
  9384. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9385. (if (and lispp literal) (setq lispp 'literal))
  9386. ;; Check for old vertical references
  9387. (setq form (org-rewrite-old-row-references form))
  9388. ;; Insert complex ranges
  9389. (while (string-match org-table-range-regexp form)
  9390. (setq form
  9391. (replace-match
  9392. (save-match-data
  9393. (org-table-make-reference
  9394. (org-table-get-range (match-string 0 form) nil n0)
  9395. keep-empty numbers lispp))
  9396. t t form)))
  9397. ;; Insert simple ranges
  9398. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9399. (setq form
  9400. (replace-match
  9401. (save-match-data
  9402. (org-table-make-reference
  9403. (org-sublist
  9404. fields (string-to-number (match-string 1 form))
  9405. (string-to-number (match-string 2 form)))
  9406. keep-empty numbers lispp))
  9407. t t form)))
  9408. (setq form0 form)
  9409. ;; Insert the references to fields in same row
  9410. (while (string-match "\\$\\([0-9]+\\)" form)
  9411. (setq n (string-to-number (match-string 1 form))
  9412. x (nth (1- (if (= n 0) n0 n)) fields))
  9413. (unless x (error "Invalid field specifier \"%s\""
  9414. (match-string 0 form)))
  9415. (setq form (replace-match
  9416. (save-match-data
  9417. (org-table-make-reference x nil numbers lispp))
  9418. t t form)))
  9419. (if lispp
  9420. (setq ev (condition-case nil
  9421. (eval (eval (read form)))
  9422. (error "#ERROR"))
  9423. ev (if (numberp ev) (number-to-string ev) ev))
  9424. (or (fboundp 'calc-eval)
  9425. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9426. (setq ev (calc-eval (cons form modes)
  9427. (if numbers 'num))))
  9428. (when org-table-formula-debug
  9429. (with-output-to-temp-buffer "*Substitution History*"
  9430. (princ (format "Substitution history of formula
  9431. Orig: %s
  9432. $xyz-> %s
  9433. @r$c-> %s
  9434. $1-> %s\n" orig formula form0 form))
  9435. (if (listp ev)
  9436. (princ (format " %s^\nError: %s"
  9437. (make-string (car ev) ?\-) (nth 1 ev)))
  9438. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9439. ev (or fmt "NONE")
  9440. (if fmt (format fmt (string-to-number ev)) ev)))))
  9441. (setq bw (get-buffer-window "*Substitution History*"))
  9442. (shrink-window-if-larger-than-buffer bw)
  9443. (unless (and (interactive-p) (not ndown))
  9444. (unless (let (inhibit-redisplay)
  9445. (y-or-n-p "Debugging Formula. Continue to next? "))
  9446. (org-table-align)
  9447. (error "Abort"))
  9448. (delete-window bw)
  9449. (message "")))
  9450. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9451. (org-table-justify-field-maybe
  9452. (if fmt (format fmt (string-to-number ev)) ev))
  9453. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9454. (call-interactively 'org-return)
  9455. (setq ndown 0)))
  9456. (and down (org-table-maybe-recalculate-line))
  9457. (or suppress-align (and org-table-may-need-update
  9458. (org-table-align))))))
  9459. (defun org-table-put-field-property (prop value)
  9460. (save-excursion
  9461. (put-text-property (progn (skip-chars-backward "^|") (point))
  9462. (progn (skip-chars-forward "^|") (point))
  9463. prop value)))
  9464. (defun org-table-get-range (desc &optional tbeg col highlight)
  9465. "Get a calc vector from a column, accorting to descriptor DESC.
  9466. Optional arguments TBEG and COL can give the beginning of the table and
  9467. the current column, to avoid unnecessary parsing.
  9468. HIGHLIGHT means, just highlight the range."
  9469. (if (not (equal (string-to-char desc) ?@))
  9470. (setq desc (concat "@" desc)))
  9471. (save-excursion
  9472. (or tbeg (setq tbeg (org-table-begin)))
  9473. (or col (setq col (org-table-current-column)))
  9474. (let ((thisline (org-current-line))
  9475. beg end c1 c2 r1 r2 rangep tmp)
  9476. (unless (string-match org-table-range-regexp desc)
  9477. (error "Invalid table range specifier `%s'" desc))
  9478. (setq rangep (match-end 3)
  9479. r1 (and (match-end 1) (match-string 1 desc))
  9480. r2 (and (match-end 4) (match-string 4 desc))
  9481. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9482. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9483. (and c1 (setq c1 (+ (string-to-number c1)
  9484. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9485. (and c2 (setq c2 (+ (string-to-number c2)
  9486. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9487. (if (equal r1 "") (setq r1 nil))
  9488. (if (equal r2 "") (setq r2 nil))
  9489. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9490. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9491. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9492. (if (not r1) (setq r1 thisline))
  9493. (if (not r2) (setq r2 thisline))
  9494. (if (not c1) (setq c1 col))
  9495. (if (not c2) (setq c2 col))
  9496. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9497. ;; just one field
  9498. (progn
  9499. (goto-line r1)
  9500. (while (not (looking-at org-table-dataline-regexp))
  9501. (beginning-of-line 2))
  9502. (prog1 (org-trim (org-table-get-field c1))
  9503. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9504. ;; A range, return a vector
  9505. ;; First sort the numbers to get a regular ractangle
  9506. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9507. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9508. (goto-line r1)
  9509. (while (not (looking-at org-table-dataline-regexp))
  9510. (beginning-of-line 2))
  9511. (org-table-goto-column c1)
  9512. (setq beg (point))
  9513. (goto-line r2)
  9514. (while (not (looking-at org-table-dataline-regexp))
  9515. (beginning-of-line 0))
  9516. (org-table-goto-column c2)
  9517. (setq end (point))
  9518. (if highlight
  9519. (org-table-highlight-rectangle
  9520. beg (progn (skip-chars-forward "^|\n") (point))))
  9521. ;; return string representation of calc vector
  9522. (mapcar 'org-trim
  9523. (apply 'append (org-table-copy-region beg end)))))))
  9524. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9525. "Analyze descriptor DESC and retrieve the corresponding line number.
  9526. The cursor is currently in line CLINE, the table begins in line BLINE,
  9527. and TABLE is a vector with line types."
  9528. (if (string-match "^[0-9]+$" desc)
  9529. (aref org-table-dlines (string-to-number desc))
  9530. (setq cline (or cline (org-current-line))
  9531. bline (or bline org-table-current-begin-line)
  9532. table (or table org-table-current-line-types))
  9533. (if (or
  9534. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9535. ;; 1 2 3 4 5 6
  9536. (and (not (match-end 3)) (not (match-end 6)))
  9537. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9538. (error "invalid row descriptor `%s'" desc))
  9539. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9540. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9541. (odir (and (match-end 5) (match-string 5 desc)))
  9542. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9543. (i (- cline bline))
  9544. (rel (and (match-end 6)
  9545. (or (and (match-end 1) (not (match-end 3)))
  9546. (match-end 5)))))
  9547. (if (and hn (not hdir))
  9548. (progn
  9549. (setq i 0 hdir "+")
  9550. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9551. (if (and (not hn) on (not odir))
  9552. (error "should never happen");;(aref org-table-dlines on)
  9553. (if (and hn (> hn 0))
  9554. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9555. (if on
  9556. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9557. (+ bline i)))))
  9558. (defun org-find-row-type (table i type backwards relative n)
  9559. (let ((l (length table)))
  9560. (while (> n 0)
  9561. (while (and (setq i (+ i (if backwards -1 1)))
  9562. (>= i 0) (< i l)
  9563. (not (eq (aref table i) type))
  9564. (if (and relative (eq (aref table i) 'hline))
  9565. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9566. t)))
  9567. (setq n (1- n)))
  9568. (if (or (< i 0) (>= i l))
  9569. (error "Row descriptior leads outside table")
  9570. i)))
  9571. (defun org-rewrite-old-row-references (s)
  9572. (if (string-match "&[-+0-9I]" s)
  9573. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9574. s))
  9575. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9576. "Convert list ELEMENTS to something appropriate to insert into formula.
  9577. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9578. NUMBERS indicates that everything should be converted to numbers.
  9579. LISPP means to return something appropriate for a Lisp list."
  9580. (if (stringp elements) ; just a single val
  9581. (if lispp
  9582. (if (eq lispp 'literal)
  9583. elements
  9584. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9585. (if (equal elements "") (setq elements "0"))
  9586. (if numbers (number-to-string (string-to-number elements)) elements))
  9587. (unless keep-empty
  9588. (setq elements
  9589. (delq nil
  9590. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9591. elements))))
  9592. (setq elements (or elements '("0")))
  9593. (if lispp
  9594. (mapconcat
  9595. (lambda (x)
  9596. (if (eq lispp 'literal)
  9597. x
  9598. (prin1-to-string (if numbers (string-to-number x) x))))
  9599. elements " ")
  9600. (concat "[" (mapconcat
  9601. (lambda (x)
  9602. (if numbers (number-to-string (string-to-number x)) x))
  9603. elements
  9604. ",") "]"))))
  9605. (defun org-table-recalculate (&optional all noalign)
  9606. "Recalculate the current table line by applying all stored formulas.
  9607. With prefix arg ALL, do this for all lines in the table."
  9608. (interactive "P")
  9609. (or (memq this-command org-recalc-commands)
  9610. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9611. (unless (org-at-table-p) (error "Not at a table"))
  9612. (if (equal all '(16))
  9613. (org-table-iterate)
  9614. (org-table-get-specials)
  9615. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9616. (lambda (a b) (string< (car a) (car b)))))
  9617. (inhibit-redisplay (not debug-on-error))
  9618. (line-re org-table-dataline-regexp)
  9619. (thisline (org-current-line))
  9620. (thiscol (org-table-current-column))
  9621. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9622. ;; Insert constants in all formulas
  9623. (setq eqlist
  9624. (mapcar (lambda (x)
  9625. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9626. x)
  9627. eqlist))
  9628. ;; Split the equation list
  9629. (while (setq eq (pop eqlist))
  9630. (if (<= (string-to-char (car eq)) ?9)
  9631. (push eq eqlnum)
  9632. (push eq eqlname)))
  9633. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9634. (if all
  9635. (progn
  9636. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9637. (goto-char (setq beg (org-table-begin)))
  9638. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9639. ;; This is a table with marked lines, compute selected lines
  9640. (setq line-re org-table-recalculate-regexp)
  9641. ;; Move forward to the first non-header line
  9642. (if (and (re-search-forward org-table-dataline-regexp end t)
  9643. (re-search-forward org-table-hline-regexp end t)
  9644. (re-search-forward org-table-dataline-regexp end t))
  9645. (setq beg (match-beginning 0))
  9646. nil))) ;; just leave beg where it is
  9647. (setq beg (point-at-bol)
  9648. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9649. (goto-char beg)
  9650. (and all (message "Re-applying formulas to full table..."))
  9651. ;; First find the named fields, and mark them untouchanble
  9652. (remove-text-properties beg end '(org-untouchable t))
  9653. (while (setq eq (pop eqlname))
  9654. (setq name (car eq)
  9655. a (assoc name org-table-named-field-locations))
  9656. (and (not a)
  9657. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9658. (setq a (list name
  9659. (aref org-table-dlines
  9660. (string-to-number (match-string 1 name)))
  9661. (string-to-number (match-string 2 name)))))
  9662. (when (and a (or all (equal (nth 1 a) thisline)))
  9663. (message "Re-applying formula to field: %s" name)
  9664. (goto-line (nth 1 a))
  9665. (org-table-goto-column (nth 2 a))
  9666. (push (append a (list (cdr eq))) eqlname1)
  9667. (org-table-put-field-property :org-untouchable t)))
  9668. ;; Now evauluate the column formulas, but skip fields covered by
  9669. ;; field formulas
  9670. (goto-char beg)
  9671. (while (re-search-forward line-re end t)
  9672. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9673. ;; Unprotected line, recalculate
  9674. (and all (message "Re-applying formulas to full table...(line %d)"
  9675. (setq cnt (1+ cnt))))
  9676. (setq org-last-recalc-line (org-current-line))
  9677. (setq eql eqlnum)
  9678. (while (setq entry (pop eql))
  9679. (goto-line org-last-recalc-line)
  9680. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9681. (unless (get-text-property (point) :org-untouchable)
  9682. (org-table-eval-formula nil (cdr entry)
  9683. 'noalign 'nocst 'nostore 'noanalysis)))))
  9684. ;; Now evaluate the field formulas
  9685. (while (setq eq (pop eqlname1))
  9686. (message "Re-applying formula to field: %s" (car eq))
  9687. (goto-line (nth 1 eq))
  9688. (org-table-goto-column (nth 2 eq))
  9689. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9690. 'nostore 'noanalysis))
  9691. (goto-line thisline)
  9692. (org-table-goto-column thiscol)
  9693. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9694. (or noalign (and org-table-may-need-update (org-table-align))
  9695. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9696. ;; back to initial position
  9697. (message "Re-applying formulas...done")
  9698. (goto-line thisline)
  9699. (org-table-goto-column thiscol)
  9700. (or noalign (and org-table-may-need-update (org-table-align))
  9701. (and all (message "Re-applying formulas...done"))))))
  9702. (defun org-table-iterate (&optional arg)
  9703. "Recalculate the table until it does not change anymore."
  9704. (interactive "P")
  9705. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9706. (i 0)
  9707. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9708. thistbl)
  9709. (catch 'exit
  9710. (while (< i imax)
  9711. (setq i (1+ i))
  9712. (org-table-recalculate 'all)
  9713. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9714. (if (not (string= lasttbl thistbl))
  9715. (setq lasttbl thistbl)
  9716. (if (> i 1)
  9717. (message "Convergence after %d iterations" i)
  9718. (message "Table was already stable"))
  9719. (throw 'exit t)))
  9720. (error "No convergence after %d iterations" i))))
  9721. (defun org-table-formula-substitute-names (f)
  9722. "Replace $const with values in string F."
  9723. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9724. ;; First, check for column names
  9725. (while (setq start (string-match org-table-column-name-regexp f start))
  9726. (setq start (1+ start))
  9727. (setq a (assoc (match-string 1 f) org-table-column-names))
  9728. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9729. ;; Parameters and constants
  9730. (setq start 0)
  9731. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9732. (setq start (1+ start))
  9733. (if (setq a (save-match-data
  9734. (org-table-get-constant (match-string 1 f))))
  9735. (setq f (replace-match
  9736. (concat (if pp "(") a (if pp ")")) t t f))))
  9737. (if org-table-formula-debug
  9738. (put-text-property 0 (length f) :orig-formula f1 f))
  9739. f))
  9740. (defun org-table-get-constant (const)
  9741. "Find the value for a parameter or constant in a formula.
  9742. Parameters get priority."
  9743. (or (cdr (assoc const org-table-local-parameters))
  9744. (cdr (assoc const org-table-formula-constants-local))
  9745. (cdr (assoc const org-table-formula-constants))
  9746. (and (fboundp 'constants-get) (constants-get const))
  9747. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9748. (org-entry-get nil (substring const 5) 'inherit))
  9749. "#UNDEFINED_NAME"))
  9750. (defvar org-table-fedit-map
  9751. (let ((map (make-sparse-keymap)))
  9752. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9753. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9754. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9755. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9756. (org-defkey map "\C-c?" 'org-table-show-reference)
  9757. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9758. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9759. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9760. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9761. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9762. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9763. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9764. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9765. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9766. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9767. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9768. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9769. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9770. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9771. map))
  9772. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9773. '("Edit-Formulas"
  9774. ["Finish and Install" org-table-fedit-finish t]
  9775. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9776. ["Abort" org-table-fedit-abort t]
  9777. "--"
  9778. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9779. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9780. "--"
  9781. "Shift Reference at Point"
  9782. ["Up" org-table-fedit-ref-up t]
  9783. ["Down" org-table-fedit-ref-down t]
  9784. ["Left" org-table-fedit-ref-left t]
  9785. ["Right" org-table-fedit-ref-right t]
  9786. "-"
  9787. "Change Test Row for Column Formulas"
  9788. ["Up" org-table-fedit-line-up t]
  9789. ["Down" org-table-fedit-line-down t]
  9790. "--"
  9791. ["Scroll Table Window" org-table-fedit-scroll t]
  9792. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9793. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9794. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9795. org-table-overlay-coordinates)]
  9796. "--"
  9797. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9798. :style toggle :selected org-table-buffer-is-an]))
  9799. (defvar org-pos)
  9800. (defun org-table-edit-formulas ()
  9801. "Edit the formulas of the current table in a separate buffer."
  9802. (interactive)
  9803. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9804. (beginning-of-line 0))
  9805. (unless (org-at-table-p) (error "Not at a table"))
  9806. (org-table-get-specials)
  9807. (let ((key (org-table-current-field-formula 'key 'noerror))
  9808. (eql (sort (org-table-get-stored-formulas 'noerror)
  9809. 'org-table-formula-less-p))
  9810. (pos (move-marker (make-marker) (point)))
  9811. (startline 1)
  9812. (wc (current-window-configuration))
  9813. (titles '((column . "# Column Formulas\n")
  9814. (field . "# Field Formulas\n")
  9815. (named . "# Named Field Formulas\n")))
  9816. entry s type title)
  9817. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9818. (erase-buffer)
  9819. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9820. (let ((font-lock-global-modes '(not fundamental-mode)))
  9821. (fundamental-mode))
  9822. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9823. (org-set-local 'org-pos pos)
  9824. (org-set-local 'org-window-configuration wc)
  9825. (use-local-map org-table-fedit-map)
  9826. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9827. (easy-menu-add org-table-fedit-menu)
  9828. (setq startline (org-current-line))
  9829. (while (setq entry (pop eql))
  9830. (setq type (cond
  9831. ((equal (string-to-char (car entry)) ?@) 'field)
  9832. ((string-match "^[0-9]" (car entry)) 'column)
  9833. (t 'named)))
  9834. (when (setq title (assq type titles))
  9835. (or (bobp) (insert "\n"))
  9836. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9837. (setq titles (delq title titles)))
  9838. (if (equal key (car entry)) (setq startline (org-current-line)))
  9839. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9840. (car entry) " = " (cdr entry) "\n"))
  9841. (remove-text-properties 0 (length s) '(face nil) s)
  9842. (insert s))
  9843. (if (eq org-table-use-standard-references t)
  9844. (org-table-fedit-toggle-ref-type))
  9845. (goto-line startline)
  9846. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9847. (defun org-table-fedit-post-command ()
  9848. (when (not (memq this-command '(lisp-complete-symbol)))
  9849. (let ((win (selected-window)))
  9850. (save-excursion
  9851. (condition-case nil
  9852. (org-table-show-reference)
  9853. (error nil))
  9854. (select-window win)))))
  9855. (defun org-table-formula-to-user (s)
  9856. "Convert a formula from internal to user representation."
  9857. (if (eq org-table-use-standard-references t)
  9858. (org-table-convert-refs-to-an s)
  9859. s))
  9860. (defun org-table-formula-from-user (s)
  9861. "Convert a formula from user to internal representation."
  9862. (if org-table-use-standard-references
  9863. (org-table-convert-refs-to-rc s)
  9864. s))
  9865. (defun org-table-convert-refs-to-rc (s)
  9866. "Convert spreadsheet references from AB7 to @7$28.
  9867. Works for single references, but also for entire formulas and even the
  9868. full TBLFM line."
  9869. (let ((start 0))
  9870. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9871. (cond
  9872. ((match-end 3)
  9873. ;; format match, just advance
  9874. (setq start (match-end 0)))
  9875. ((and (> (match-beginning 0) 0)
  9876. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9877. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9878. ;; 3.e5 or something like this.
  9879. (setq start (match-end 0)))
  9880. (t
  9881. (setq start (match-beginning 0)
  9882. s (replace-match
  9883. (if (equal (match-string 2 s) "&")
  9884. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9885. (format "@%d$%d"
  9886. (string-to-number (match-string 2 s))
  9887. (org-letters-to-number (match-string 1 s))))
  9888. t t s)))))
  9889. s))
  9890. (defun org-table-convert-refs-to-an (s)
  9891. "Convert spreadsheet references from to @7$28 to AB7.
  9892. Works for single references, but also for entire formulas and even the
  9893. full TBLFM line."
  9894. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9895. (setq s (replace-match
  9896. (format "%s%d"
  9897. (org-number-to-letters
  9898. (string-to-number (match-string 2 s)))
  9899. (string-to-number (match-string 1 s)))
  9900. t t s)))
  9901. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9902. (setq s (replace-match (concat "\\1"
  9903. (org-number-to-letters
  9904. (string-to-number (match-string 2 s))) "&")
  9905. t nil s)))
  9906. s)
  9907. (defun org-letters-to-number (s)
  9908. "Convert a base 26 number represented by letters into an integer.
  9909. For example: AB -> 28."
  9910. (let ((n 0))
  9911. (setq s (upcase s))
  9912. (while (> (length s) 0)
  9913. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9914. s (substring s 1)))
  9915. n))
  9916. (defun org-number-to-letters (n)
  9917. "Convert an integer into a base 26 number represented by letters.
  9918. For example: 28 -> AB."
  9919. (let ((s ""))
  9920. (while (> n 0)
  9921. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9922. n (/ (1- n) 26)))
  9923. s))
  9924. (defun org-table-fedit-convert-buffer (function)
  9925. "Convert all references in this buffer, using FUNTION."
  9926. (let ((line (org-current-line)))
  9927. (goto-char (point-min))
  9928. (while (not (eobp))
  9929. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9930. (delete-region (point) (point-at-eol))
  9931. (or (eobp) (forward-char 1)))
  9932. (goto-line line)))
  9933. (defun org-table-fedit-toggle-ref-type ()
  9934. "Convert all references in the buffer from B3 to @3$2 and back."
  9935. (interactive)
  9936. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9937. (org-table-fedit-convert-buffer
  9938. (if org-table-buffer-is-an
  9939. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9940. (message "Reference type switched to %s"
  9941. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9942. (defun org-table-fedit-ref-up ()
  9943. "Shift the reference at point one row/hline up."
  9944. (interactive)
  9945. (org-table-fedit-shift-reference 'up))
  9946. (defun org-table-fedit-ref-down ()
  9947. "Shift the reference at point one row/hline down."
  9948. (interactive)
  9949. (org-table-fedit-shift-reference 'down))
  9950. (defun org-table-fedit-ref-left ()
  9951. "Shift the reference at point one field to the left."
  9952. (interactive)
  9953. (org-table-fedit-shift-reference 'left))
  9954. (defun org-table-fedit-ref-right ()
  9955. "Shift the reference at point one field to the right."
  9956. (interactive)
  9957. (org-table-fedit-shift-reference 'right))
  9958. (defun org-table-fedit-shift-reference (dir)
  9959. (cond
  9960. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9961. (if (memq dir '(left right))
  9962. (org-rematch-and-replace 1 (eq dir 'left))
  9963. (error "Cannot shift reference in this direction")))
  9964. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9965. ;; A B3-like reference
  9966. (if (memq dir '(up down))
  9967. (org-rematch-and-replace 2 (eq dir 'up))
  9968. (org-rematch-and-replace 1 (eq dir 'left))))
  9969. ((org-at-regexp-p
  9970. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9971. ;; An internal reference
  9972. (if (memq dir '(up down))
  9973. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9974. (org-rematch-and-replace 5 (eq dir 'left))))))
  9975. (defun org-rematch-and-replace (n &optional decr hline)
  9976. "Re-match the group N, and replace it with the shifted refrence."
  9977. (or (match-end n) (error "Cannot shift reference in this direction"))
  9978. (goto-char (match-beginning n))
  9979. (and (looking-at (regexp-quote (match-string n)))
  9980. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9981. t t)))
  9982. (defun org-shift-refpart (ref &optional decr hline)
  9983. "Shift a refrence part REF.
  9984. If DECR is set, decrease the references row/column, else increase.
  9985. If HLINE is set, this may be a hline reference, it certainly is not
  9986. a translation reference."
  9987. (save-match-data
  9988. (let* ((sign (string-match "^[-+]" ref)) n)
  9989. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9990. (cond
  9991. ((and hline (string-match "^I+" ref))
  9992. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9993. (setq n (+ n (if decr -1 1)))
  9994. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9995. (if sign
  9996. (setq sign (if (< n 0) "-" "+") n (abs n))
  9997. (setq n (max 1 n)))
  9998. (concat sign (make-string n ?I)))
  9999. ((string-match "^[0-9]+" ref)
  10000. (setq n (string-to-number (concat sign ref)))
  10001. (setq n (+ n (if decr -1 1)))
  10002. (if sign
  10003. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  10004. (number-to-string (max 1 n))))
  10005. ((string-match "^[a-zA-Z]+" ref)
  10006. (org-number-to-letters
  10007. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  10008. (t (error "Cannot shift reference"))))))
  10009. (defun org-table-fedit-toggle-coordinates ()
  10010. "Toggle the display of coordinates in the refrenced table."
  10011. (interactive)
  10012. (let ((pos (marker-position org-pos)))
  10013. (with-current-buffer (marker-buffer org-pos)
  10014. (save-excursion
  10015. (goto-char pos)
  10016. (org-table-toggle-coordinate-overlays)))))
  10017. (defun org-table-fedit-finish (&optional arg)
  10018. "Parse the buffer for formula definitions and install them.
  10019. With prefix ARG, apply the new formulas to the table."
  10020. (interactive "P")
  10021. (org-table-remove-rectangle-highlight)
  10022. (if org-table-use-standard-references
  10023. (progn
  10024. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  10025. (setq org-table-buffer-is-an nil)))
  10026. (let ((pos org-pos) eql var form)
  10027. (goto-char (point-min))
  10028. (while (re-search-forward
  10029. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  10030. nil t)
  10031. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  10032. form (match-string 3))
  10033. (setq form (org-trim form))
  10034. (when (not (equal form ""))
  10035. (while (string-match "[ \t]*\n[ \t]*" form)
  10036. (setq form (replace-match " " t t form)))
  10037. (when (assoc var eql)
  10038. (error "Double formulas for %s" var))
  10039. (push (cons var form) eql)))
  10040. (setq org-pos nil)
  10041. (set-window-configuration org-window-configuration)
  10042. (select-window (get-buffer-window (marker-buffer pos)))
  10043. (goto-char pos)
  10044. (unless (org-at-table-p)
  10045. (error "Lost table position - cannot install formulae"))
  10046. (org-table-store-formulas eql)
  10047. (move-marker pos nil)
  10048. (kill-buffer "*Edit Formulas*")
  10049. (if arg
  10050. (org-table-recalculate 'all)
  10051. (message "New formulas installed - press C-u C-c C-c to apply."))))
  10052. (defun org-table-fedit-abort ()
  10053. "Abort editing formulas, without installing the changes."
  10054. (interactive)
  10055. (org-table-remove-rectangle-highlight)
  10056. (let ((pos org-pos))
  10057. (set-window-configuration org-window-configuration)
  10058. (select-window (get-buffer-window (marker-buffer pos)))
  10059. (goto-char pos)
  10060. (move-marker pos nil)
  10061. (message "Formula editing aborted without installing changes")))
  10062. (defun org-table-fedit-lisp-indent ()
  10063. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  10064. (interactive)
  10065. (let ((pos (point)) beg end ind)
  10066. (beginning-of-line 1)
  10067. (cond
  10068. ((looking-at "[ \t]")
  10069. (goto-char pos)
  10070. (call-interactively 'lisp-indent-line))
  10071. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  10072. ((not (fboundp 'pp-buffer))
  10073. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  10074. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  10075. (goto-char (- (match-end 0) 2))
  10076. (setq beg (point))
  10077. (setq ind (make-string (current-column) ?\ ))
  10078. (condition-case nil (forward-sexp 1)
  10079. (error
  10080. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  10081. (setq end (point))
  10082. (save-restriction
  10083. (narrow-to-region beg end)
  10084. (if (eq last-command this-command)
  10085. (progn
  10086. (goto-char (point-min))
  10087. (setq this-command nil)
  10088. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  10089. (replace-match " ")))
  10090. (pp-buffer)
  10091. (untabify (point-min) (point-max))
  10092. (goto-char (1+ (point-min)))
  10093. (while (re-search-forward "^." nil t)
  10094. (beginning-of-line 1)
  10095. (insert ind))
  10096. (goto-char (point-max))
  10097. (backward-delete-char 1)))
  10098. (goto-char beg))
  10099. (t nil))))
  10100. (defvar org-show-positions nil)
  10101. (defun org-table-show-reference (&optional local)
  10102. "Show the location/value of the $ expression at point."
  10103. (interactive)
  10104. (org-table-remove-rectangle-highlight)
  10105. (catch 'exit
  10106. (let ((pos (if local (point) org-pos))
  10107. (face2 'highlight)
  10108. (org-inhibit-highlight-removal t)
  10109. (win (selected-window))
  10110. (org-show-positions nil)
  10111. var name e what match dest)
  10112. (if local (org-table-get-specials))
  10113. (setq what (cond
  10114. ((or (org-at-regexp-p org-table-range-regexp2)
  10115. (org-at-regexp-p org-table-translate-regexp)
  10116. (org-at-regexp-p org-table-range-regexp))
  10117. (setq match
  10118. (save-match-data
  10119. (org-table-convert-refs-to-rc (match-string 0))))
  10120. 'range)
  10121. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10122. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10123. ((not local) nil)
  10124. (t (error "No reference at point")))
  10125. match (and what (or match (match-string 0))))
  10126. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10127. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10128. 'secondary-selection))
  10129. (org-add-hook 'before-change-functions
  10130. 'org-table-remove-rectangle-highlight)
  10131. (if (eq what 'name) (setq var (substring match 1)))
  10132. (when (eq what 'range)
  10133. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10134. (setq match (org-table-formula-substitute-names match)))
  10135. (unless local
  10136. (save-excursion
  10137. (end-of-line 1)
  10138. (re-search-backward "^\\S-" nil t)
  10139. (beginning-of-line 1)
  10140. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10141. (setq dest
  10142. (save-match-data
  10143. (org-table-convert-refs-to-rc (match-string 1))))
  10144. (org-table-add-rectangle-overlay
  10145. (match-beginning 1) (match-end 1) face2))))
  10146. (if (and (markerp pos) (marker-buffer pos))
  10147. (if (get-buffer-window (marker-buffer pos))
  10148. (select-window (get-buffer-window (marker-buffer pos)))
  10149. (org-switch-to-buffer-other-window (get-buffer-window
  10150. (marker-buffer pos)))))
  10151. (goto-char pos)
  10152. (org-table-force-dataline)
  10153. (when dest
  10154. (setq name (substring dest 1))
  10155. (cond
  10156. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10157. (setq e (assoc name org-table-named-field-locations))
  10158. (goto-line (nth 1 e))
  10159. (org-table-goto-column (nth 2 e)))
  10160. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10161. (let ((l (string-to-number (match-string 1 dest)))
  10162. (c (string-to-number (match-string 2 dest))))
  10163. (goto-line (aref org-table-dlines l))
  10164. (org-table-goto-column c)))
  10165. (t (org-table-goto-column (string-to-number name))))
  10166. (move-marker pos (point))
  10167. (org-table-highlight-rectangle nil nil face2))
  10168. (cond
  10169. ((equal dest match))
  10170. ((not match))
  10171. ((eq what 'range)
  10172. (condition-case nil
  10173. (save-excursion
  10174. (org-table-get-range match nil nil 'highlight))
  10175. (error nil)))
  10176. ((setq e (assoc var org-table-named-field-locations))
  10177. (goto-line (nth 1 e))
  10178. (org-table-goto-column (nth 2 e))
  10179. (org-table-highlight-rectangle (point) (point))
  10180. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10181. ((setq e (assoc var org-table-column-names))
  10182. (org-table-goto-column (string-to-number (cdr e)))
  10183. (org-table-highlight-rectangle (point) (point))
  10184. (goto-char (org-table-begin))
  10185. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10186. (org-table-end) t)
  10187. (progn
  10188. (goto-char (match-beginning 1))
  10189. (org-table-highlight-rectangle)
  10190. (message "Named column (column %s)" (cdr e)))
  10191. (error "Column name not found")))
  10192. ((eq what 'column)
  10193. ;; column number
  10194. (org-table-goto-column (string-to-number (substring match 1)))
  10195. (org-table-highlight-rectangle (point) (point))
  10196. (message "Column %s" (substring match 1)))
  10197. ((setq e (assoc var org-table-local-parameters))
  10198. (goto-char (org-table-begin))
  10199. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10200. (progn
  10201. (goto-char (match-beginning 1))
  10202. (org-table-highlight-rectangle)
  10203. (message "Local parameter."))
  10204. (error "Parameter not found")))
  10205. (t
  10206. (cond
  10207. ((not var) (error "No reference at point"))
  10208. ((setq e (assoc var org-table-formula-constants-local))
  10209. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10210. var (cdr e)))
  10211. ((setq e (assoc var org-table-formula-constants))
  10212. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10213. var (cdr e)))
  10214. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10215. (message "Constant: $%s=%s, from `constants.el'%s."
  10216. var e (format " (%s units)" constants-unit-system)))
  10217. (t (error "Undefined name $%s" var)))))
  10218. (goto-char pos)
  10219. (when (and org-show-positions
  10220. (not (memq this-command '(org-table-fedit-scroll
  10221. org-table-fedit-scroll-down))))
  10222. (push pos org-show-positions)
  10223. (push org-table-current-begin-pos org-show-positions)
  10224. (let ((min (apply 'min org-show-positions))
  10225. (max (apply 'max org-show-positions)))
  10226. (goto-char min) (recenter 0)
  10227. (goto-char max)
  10228. (or (pos-visible-in-window-p max) (recenter -1))))
  10229. (select-window win))))
  10230. (defun org-table-force-dataline ()
  10231. "Make sure the cursor is in a dataline in a table."
  10232. (unless (save-excursion
  10233. (beginning-of-line 1)
  10234. (looking-at org-table-dataline-regexp))
  10235. (let* ((re org-table-dataline-regexp)
  10236. (p1 (save-excursion (re-search-forward re nil 'move)))
  10237. (p2 (save-excursion (re-search-backward re nil 'move))))
  10238. (cond ((and p1 p2)
  10239. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10240. p1 p2)))
  10241. ((or p1 p2) (goto-char (or p1 p2)))
  10242. (t (error "No table dataline around here"))))))
  10243. (defun org-table-fedit-line-up ()
  10244. "Move cursor one line up in the window showing the table."
  10245. (interactive)
  10246. (org-table-fedit-move 'previous-line))
  10247. (defun org-table-fedit-line-down ()
  10248. "Move cursor one line down in the window showing the table."
  10249. (interactive)
  10250. (org-table-fedit-move 'next-line))
  10251. (defun org-table-fedit-move (command)
  10252. "Move the cursor in the window shoinw the table.
  10253. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10254. (let ((org-table-allow-automatic-line-recalculation nil)
  10255. (pos org-pos) (win (selected-window)) p)
  10256. (select-window (get-buffer-window (marker-buffer org-pos)))
  10257. (setq p (point))
  10258. (call-interactively command)
  10259. (while (and (org-at-table-p)
  10260. (org-at-table-hline-p))
  10261. (call-interactively command))
  10262. (or (org-at-table-p) (goto-char p))
  10263. (move-marker pos (point))
  10264. (select-window win)))
  10265. (defun org-table-fedit-scroll (N)
  10266. (interactive "p")
  10267. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10268. (scroll-other-window N)))
  10269. (defun org-table-fedit-scroll-down (N)
  10270. (interactive "p")
  10271. (org-table-fedit-scroll (- N)))
  10272. (defvar org-table-rectangle-overlays nil)
  10273. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10274. "Add a new overlay."
  10275. (let ((ov (org-make-overlay beg end)))
  10276. (org-overlay-put ov 'face (or face 'secondary-selection))
  10277. (push ov org-table-rectangle-overlays)))
  10278. (defun org-table-highlight-rectangle (&optional beg end face)
  10279. "Highlight rectangular region in a table."
  10280. (setq beg (or beg (point)) end (or end (point)))
  10281. (let ((b (min beg end))
  10282. (e (max beg end))
  10283. l1 c1 l2 c2 tmp)
  10284. (and (boundp 'org-show-positions)
  10285. (setq org-show-positions (cons b (cons e org-show-positions))))
  10286. (goto-char (min beg end))
  10287. (setq l1 (org-current-line)
  10288. c1 (org-table-current-column))
  10289. (goto-char (max beg end))
  10290. (setq l2 (org-current-line)
  10291. c2 (org-table-current-column))
  10292. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10293. (goto-line l1)
  10294. (beginning-of-line 1)
  10295. (loop for line from l1 to l2 do
  10296. (when (looking-at org-table-dataline-regexp)
  10297. (org-table-goto-column c1)
  10298. (skip-chars-backward "^|\n") (setq beg (point))
  10299. (org-table-goto-column c2)
  10300. (skip-chars-forward "^|\n") (setq end (point))
  10301. (org-table-add-rectangle-overlay beg end face))
  10302. (beginning-of-line 2))
  10303. (goto-char b))
  10304. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10305. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10306. "Remove the rectangle overlays."
  10307. (unless org-inhibit-highlight-removal
  10308. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10309. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10310. (setq org-table-rectangle-overlays nil)))
  10311. (defvar org-table-coordinate-overlays nil
  10312. "Collects the cooordinate grid overlays, so that they can be removed.")
  10313. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10314. (defun org-table-overlay-coordinates ()
  10315. "Add overlays to the table at point, to show row/column coordinates."
  10316. (interactive)
  10317. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10318. (setq org-table-coordinate-overlays nil)
  10319. (save-excursion
  10320. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10321. (goto-char (org-table-begin))
  10322. (while (org-at-table-p)
  10323. (setq eol (point-at-eol))
  10324. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10325. (push ov org-table-coordinate-overlays)
  10326. (setq hline (looking-at org-table-hline-regexp))
  10327. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10328. (format "%4d" (setq id (1+ id)))))
  10329. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10330. (when hline
  10331. (setq ic 0)
  10332. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10333. (setq beg (1+ (match-beginning 0))
  10334. ic (1+ ic)
  10335. s1 (concat "$" (int-to-string ic))
  10336. s2 (org-number-to-letters ic)
  10337. str (if (eq org-table-use-standard-references t) s2 s1))
  10338. (setq ov (org-make-overlay beg (+ beg (length str))))
  10339. (push ov org-table-coordinate-overlays)
  10340. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10341. (beginning-of-line 2)))))
  10342. (defun org-table-toggle-coordinate-overlays ()
  10343. "Toggle the display of Row/Column numbers in tables."
  10344. (interactive)
  10345. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10346. (message "Row/Column number display turned %s"
  10347. (if org-table-overlay-coordinates "on" "off"))
  10348. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10349. (org-table-align))
  10350. (unless org-table-overlay-coordinates
  10351. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10352. (setq org-table-coordinate-overlays nil)))
  10353. (defun org-table-toggle-formula-debugger ()
  10354. "Toggle the formula debugger in tables."
  10355. (interactive)
  10356. (setq org-table-formula-debug (not org-table-formula-debug))
  10357. (message "Formula debugging has been turned %s"
  10358. (if org-table-formula-debug "on" "off")))
  10359. ;;; The orgtbl minor mode
  10360. ;; Define a minor mode which can be used in other modes in order to
  10361. ;; integrate the org-mode table editor.
  10362. ;; This is really a hack, because the org-mode table editor uses several
  10363. ;; keys which normally belong to the major mode, for example the TAB and
  10364. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10365. ;; necessary to operate the table editor, but wraps the commands into a
  10366. ;; function which tests if the cursor is currently inside a table. If that
  10367. ;; is the case, the table editor command is executed. However, when any of
  10368. ;; those keys is used outside a table, the function uses `key-binding' to
  10369. ;; look up if the key has an associated command in another currently active
  10370. ;; keymap (minor modes, major mode, global), and executes that command.
  10371. ;; There might be problems if any of the keys used by the table editor is
  10372. ;; otherwise used as a prefix key.
  10373. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10374. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10375. ;; addresses this by checking explicitly for both bindings.
  10376. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10377. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10378. ;; Some modes bind other commands to simple characters, for example
  10379. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10380. ;; active, this binding is ignored inside tables and replaced with a
  10381. ;; modified self-insert.
  10382. (defvar orgtbl-mode nil
  10383. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10384. table editor in arbitrary modes.")
  10385. (make-variable-buffer-local 'orgtbl-mode)
  10386. (defvar orgtbl-mode-map (make-keymap)
  10387. "Keymap for `orgtbl-mode'.")
  10388. ;;;###autoload
  10389. (defun turn-on-orgtbl ()
  10390. "Unconditionally turn on `orgtbl-mode'."
  10391. (orgtbl-mode 1))
  10392. (defvar org-old-auto-fill-inhibit-regexp nil
  10393. "Local variable used by `orgtbl-mode'")
  10394. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10395. "Matches a line belonging to an orgtbl.")
  10396. (defconst orgtbl-extra-font-lock-keywords
  10397. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10398. 0 (quote 'org-table) 'prepend))
  10399. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10400. ;;;###autoload
  10401. (defun orgtbl-mode (&optional arg)
  10402. "The `org-mode' table editor as a minor mode for use in other modes."
  10403. (interactive)
  10404. (org-load-modules-maybe)
  10405. (if (org-mode-p)
  10406. ;; Exit without error, in case some hook functions calls this
  10407. ;; by accident in org-mode.
  10408. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10409. (setq orgtbl-mode
  10410. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10411. (if orgtbl-mode
  10412. (progn
  10413. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10414. ;; Make sure we are first in minor-mode-map-alist
  10415. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10416. (and c (setq minor-mode-map-alist
  10417. (cons c (delq c minor-mode-map-alist)))))
  10418. (org-set-local (quote org-table-may-need-update) t)
  10419. (org-add-hook 'before-change-functions 'org-before-change-function
  10420. nil 'local)
  10421. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10422. auto-fill-inhibit-regexp)
  10423. (org-set-local 'auto-fill-inhibit-regexp
  10424. (if auto-fill-inhibit-regexp
  10425. (concat orgtbl-line-start-regexp "\\|"
  10426. auto-fill-inhibit-regexp)
  10427. orgtbl-line-start-regexp))
  10428. (org-add-to-invisibility-spec '(org-cwidth))
  10429. (when (fboundp 'font-lock-add-keywords)
  10430. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10431. (org-restart-font-lock))
  10432. (easy-menu-add orgtbl-mode-menu)
  10433. (run-hooks 'orgtbl-mode-hook))
  10434. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10435. (org-cleanup-narrow-column-properties)
  10436. (org-remove-from-invisibility-spec '(org-cwidth))
  10437. (remove-hook 'before-change-functions 'org-before-change-function t)
  10438. (when (fboundp 'font-lock-remove-keywords)
  10439. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10440. (org-restart-font-lock))
  10441. (easy-menu-remove orgtbl-mode-menu)
  10442. (force-mode-line-update 'all))))
  10443. (defun org-cleanup-narrow-column-properties ()
  10444. "Remove all properties related to narrow-column invisibility."
  10445. (let ((s 1))
  10446. (while (setq s (text-property-any s (point-max)
  10447. 'display org-narrow-column-arrow))
  10448. (remove-text-properties s (1+ s) '(display t)))
  10449. (setq s 1)
  10450. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10451. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10452. (setq s 1)
  10453. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10454. (remove-text-properties s (1+ s) '(invisible t)))))
  10455. ;; Install it as a minor mode.
  10456. (put 'orgtbl-mode :included t)
  10457. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10458. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10459. (defun orgtbl-make-binding (fun n &rest keys)
  10460. "Create a function for binding in the table minor mode.
  10461. FUN is the command to call inside a table. N is used to create a unique
  10462. command name. KEYS are keys that should be checked in for a command
  10463. to execute outside of tables."
  10464. (eval
  10465. (list 'defun
  10466. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10467. '(arg)
  10468. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10469. "Outside of tables, run the binding of `"
  10470. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10471. "'.")
  10472. '(interactive "p")
  10473. (list 'if
  10474. '(org-at-table-p)
  10475. (list 'call-interactively (list 'quote fun))
  10476. (list 'let '(orgtbl-mode)
  10477. (list 'call-interactively
  10478. (append '(or)
  10479. (mapcar (lambda (k)
  10480. (list 'key-binding k))
  10481. keys)
  10482. '('orgtbl-error))))))))
  10483. (defun orgtbl-error ()
  10484. "Error when there is no default binding for a table key."
  10485. (interactive)
  10486. (error "This key has no function outside tables"))
  10487. (defun orgtbl-setup ()
  10488. "Setup orgtbl keymaps."
  10489. (let ((nfunc 0)
  10490. (bindings
  10491. (list
  10492. '([(meta shift left)] org-table-delete-column)
  10493. '([(meta left)] org-table-move-column-left)
  10494. '([(meta right)] org-table-move-column-right)
  10495. '([(meta shift right)] org-table-insert-column)
  10496. '([(meta shift up)] org-table-kill-row)
  10497. '([(meta shift down)] org-table-insert-row)
  10498. '([(meta up)] org-table-move-row-up)
  10499. '([(meta down)] org-table-move-row-down)
  10500. '("\C-c\C-w" org-table-cut-region)
  10501. '("\C-c\M-w" org-table-copy-region)
  10502. '("\C-c\C-y" org-table-paste-rectangle)
  10503. '("\C-c-" org-table-insert-hline)
  10504. '("\C-c}" org-table-toggle-coordinate-overlays)
  10505. '("\C-c{" org-table-toggle-formula-debugger)
  10506. '("\C-m" org-table-next-row)
  10507. '([(shift return)] org-table-copy-down)
  10508. '("\C-c\C-q" org-table-wrap-region)
  10509. '("\C-c?" org-table-field-info)
  10510. '("\C-c " org-table-blank-field)
  10511. '("\C-c+" org-table-sum)
  10512. '("\C-c=" org-table-eval-formula)
  10513. '("\C-c'" org-table-edit-formulas)
  10514. '("\C-c`" org-table-edit-field)
  10515. '("\C-c*" org-table-recalculate)
  10516. '("\C-c|" org-table-create-or-convert-from-region)
  10517. '("\C-c^" org-table-sort-lines)
  10518. '([(control ?#)] org-table-rotate-recalc-marks)))
  10519. elt key fun cmd)
  10520. (while (setq elt (pop bindings))
  10521. (setq nfunc (1+ nfunc))
  10522. (setq key (org-key (car elt))
  10523. fun (nth 1 elt)
  10524. cmd (orgtbl-make-binding fun nfunc key))
  10525. (org-defkey orgtbl-mode-map key cmd))
  10526. ;; Special treatment needed for TAB and RET
  10527. (org-defkey orgtbl-mode-map [(return)]
  10528. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10529. (org-defkey orgtbl-mode-map "\C-m"
  10530. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10531. (org-defkey orgtbl-mode-map [(tab)]
  10532. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10533. (org-defkey orgtbl-mode-map "\C-i"
  10534. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10535. (org-defkey orgtbl-mode-map [(shift tab)]
  10536. (orgtbl-make-binding 'org-table-previous-field 104
  10537. [(shift tab)] [(tab)] "\C-i"))
  10538. (org-defkey orgtbl-mode-map "\M-\C-m"
  10539. (orgtbl-make-binding 'org-table-wrap-region 105
  10540. "\M-\C-m" [(meta return)]))
  10541. (org-defkey orgtbl-mode-map [(meta return)]
  10542. (orgtbl-make-binding 'org-table-wrap-region 106
  10543. [(meta return)] "\M-\C-m"))
  10544. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10545. (when orgtbl-optimized
  10546. ;; If the user wants maximum table support, we need to hijack
  10547. ;; some standard editing functions
  10548. (org-remap orgtbl-mode-map
  10549. 'self-insert-command 'orgtbl-self-insert-command
  10550. 'delete-char 'org-delete-char
  10551. 'delete-backward-char 'org-delete-backward-char)
  10552. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10553. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10554. '("OrgTbl"
  10555. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10556. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10557. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10558. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10559. "--"
  10560. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10561. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10562. ["Copy Field from Above"
  10563. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10564. "--"
  10565. ("Column"
  10566. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10567. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10568. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10569. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10570. ("Row"
  10571. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10572. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10573. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10574. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10575. ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
  10576. "--"
  10577. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10578. ("Rectangle"
  10579. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10580. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10581. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10582. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10583. "--"
  10584. ("Radio tables"
  10585. ["Insert table template" orgtbl-insert-radio-table
  10586. (assq major-mode orgtbl-radio-table-templates)]
  10587. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10588. "--"
  10589. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10590. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10591. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10592. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10593. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10594. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10595. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10596. ["Sum Column/Rectangle" org-table-sum
  10597. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10598. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10599. ["Debug Formulas"
  10600. org-table-toggle-formula-debugger :active (org-at-table-p)
  10601. :keys "C-c {"
  10602. :style toggle :selected org-table-formula-debug]
  10603. ["Show Col/Row Numbers"
  10604. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10605. :keys "C-c }"
  10606. :style toggle :selected org-table-overlay-coordinates]
  10607. ))
  10608. t))
  10609. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10610. "If the cursor is inside a table, realign the table.
  10611. It it is a table to be sent away to a receiver, do it.
  10612. With prefix arg, also recompute table."
  10613. (interactive "P")
  10614. (let ((pos (point)) action)
  10615. (save-excursion
  10616. (beginning-of-line 1)
  10617. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10618. ((looking-at "[ \t]*|") pos)
  10619. ((looking-at "#\\+TBLFM:") 'recalc))))
  10620. (cond
  10621. ((integerp action)
  10622. (goto-char action)
  10623. (org-table-maybe-eval-formula)
  10624. (if arg
  10625. (call-interactively 'org-table-recalculate)
  10626. (org-table-maybe-recalculate-line))
  10627. (call-interactively 'org-table-align)
  10628. (orgtbl-send-table 'maybe))
  10629. ((eq action 'recalc)
  10630. (save-excursion
  10631. (beginning-of-line 1)
  10632. (skip-chars-backward " \r\n\t")
  10633. (if (org-at-table-p)
  10634. (org-call-with-arg 'org-table-recalculate t))))
  10635. (t (let (orgtbl-mode)
  10636. (call-interactively (key-binding "\C-c\C-c")))))))
  10637. (defun orgtbl-tab (arg)
  10638. "Justification and field motion for `orgtbl-mode'."
  10639. (interactive "P")
  10640. (if arg (org-table-edit-field t)
  10641. (org-table-justify-field-maybe)
  10642. (org-table-next-field)))
  10643. (defun orgtbl-ret ()
  10644. "Justification and field motion for `orgtbl-mode'."
  10645. (interactive)
  10646. (org-table-justify-field-maybe)
  10647. (org-table-next-row))
  10648. (defun orgtbl-self-insert-command (N)
  10649. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10650. If the cursor is in a table looking at whitespace, the whitespace is
  10651. overwritten, and the table is not marked as requiring realignment."
  10652. (interactive "p")
  10653. (if (and (org-at-table-p)
  10654. (or
  10655. (and org-table-auto-blank-field
  10656. (member last-command
  10657. '(orgtbl-hijacker-command-100
  10658. orgtbl-hijacker-command-101
  10659. orgtbl-hijacker-command-102
  10660. orgtbl-hijacker-command-103
  10661. orgtbl-hijacker-command-104
  10662. orgtbl-hijacker-command-105))
  10663. (org-table-blank-field))
  10664. t)
  10665. (eq N 1)
  10666. (looking-at "[^|\n]* +|"))
  10667. (let (org-table-may-need-update)
  10668. (goto-char (1- (match-end 0)))
  10669. (delete-backward-char 1)
  10670. (goto-char (match-beginning 0))
  10671. (self-insert-command N))
  10672. (setq org-table-may-need-update t)
  10673. (let (orgtbl-mode)
  10674. (call-interactively (key-binding (vector last-input-event))))))
  10675. (defun org-force-self-insert (N)
  10676. "Needed to enforce self-insert under remapping."
  10677. (interactive "p")
  10678. (self-insert-command N))
  10679. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10680. "Regula expression matching exponentials as produced by calc.")
  10681. (defvar org-table-clean-did-remove-column nil)
  10682. (defun orgtbl-export (table target)
  10683. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10684. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10685. org-table-last-alignment org-table-last-column-widths
  10686. maxcol column)
  10687. (if (not (fboundp func))
  10688. (error "Cannot export orgtbl table to %s" target))
  10689. (setq lines (org-table-clean-before-export lines))
  10690. (setq table
  10691. (mapcar
  10692. (lambda (x)
  10693. (if (string-match org-table-hline-regexp x)
  10694. 'hline
  10695. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10696. lines))
  10697. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10698. table)))
  10699. (loop for i from (1- maxcol) downto 0 do
  10700. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10701. (setq column (delq nil column))
  10702. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10703. (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))
  10704. (funcall func table nil)))
  10705. (defun orgtbl-send-table (&optional maybe)
  10706. "Send a tranformed version of this table to the receiver position.
  10707. With argument MAYBE, fail quietly if no transformation is defined for
  10708. this table."
  10709. (interactive)
  10710. (catch 'exit
  10711. (unless (org-at-table-p) (error "Not at a table"))
  10712. ;; when non-interactive, we assume align has just happened.
  10713. (when (interactive-p) (org-table-align))
  10714. (save-excursion
  10715. (goto-char (org-table-begin))
  10716. (beginning-of-line 0)
  10717. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10718. (if maybe
  10719. (throw 'exit nil)
  10720. (error "Don't know how to transform this table."))))
  10721. (let* ((name (match-string 1))
  10722. beg
  10723. (transform (intern (match-string 2)))
  10724. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10725. (skip (plist-get params :skip))
  10726. (skipcols (plist-get params :skipcols))
  10727. (txt (buffer-substring-no-properties
  10728. (org-table-begin) (org-table-end)))
  10729. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10730. (lines (org-table-clean-before-export lines))
  10731. (i0 (if org-table-clean-did-remove-column 2 1))
  10732. (table (mapcar
  10733. (lambda (x)
  10734. (if (string-match org-table-hline-regexp x)
  10735. 'hline
  10736. (org-remove-by-index
  10737. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10738. skipcols i0)))
  10739. lines))
  10740. (fun (if (= i0 2) 'cdr 'identity))
  10741. (org-table-last-alignment
  10742. (org-remove-by-index (funcall fun org-table-last-alignment)
  10743. skipcols i0))
  10744. (org-table-last-column-widths
  10745. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10746. skipcols i0)))
  10747. (unless (fboundp transform)
  10748. (error "No such transformation function %s" transform))
  10749. (setq txt (funcall transform table params))
  10750. ;; Find the insertion place
  10751. (save-excursion
  10752. (goto-char (point-min))
  10753. (unless (re-search-forward
  10754. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10755. (error "Don't know where to insert translated table"))
  10756. (goto-char (match-beginning 0))
  10757. (beginning-of-line 2)
  10758. (setq beg (point))
  10759. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10760. (error "Cannot find end of insertion region"))
  10761. (beginning-of-line 1)
  10762. (delete-region beg (point))
  10763. (goto-char beg)
  10764. (insert txt "\n"))
  10765. (message "Table converted and installed at receiver location"))))
  10766. (defun org-remove-by-index (list indices &optional i0)
  10767. "Remove the elements in LIST with indices in INDICES.
  10768. First element has index 0, or I0 if given."
  10769. (if (not indices)
  10770. list
  10771. (if (integerp indices) (setq indices (list indices)))
  10772. (setq i0 (1- (or i0 0)))
  10773. (delq :rm (mapcar (lambda (x)
  10774. (setq i0 (1+ i0))
  10775. (if (memq i0 indices) :rm x))
  10776. list))))
  10777. (defun orgtbl-toggle-comment ()
  10778. "Comment or uncomment the orgtbl at point."
  10779. (interactive)
  10780. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10781. (re2 (concat "^" orgtbl-line-start-regexp))
  10782. (commented (save-excursion (beginning-of-line 1)
  10783. (cond ((looking-at re1) t)
  10784. ((looking-at re2) nil)
  10785. (t (error "Not at an org table")))))
  10786. (re (if commented re1 re2))
  10787. beg end)
  10788. (save-excursion
  10789. (beginning-of-line 1)
  10790. (while (looking-at re) (beginning-of-line 0))
  10791. (beginning-of-line 2)
  10792. (setq beg (point))
  10793. (while (looking-at re) (beginning-of-line 2))
  10794. (setq end (point)))
  10795. (comment-region beg end (if commented '(4) nil))))
  10796. (defun orgtbl-insert-radio-table ()
  10797. "Insert a radio table template appropriate for this major mode."
  10798. (interactive)
  10799. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10800. (txt (nth 1 e))
  10801. name pos)
  10802. (unless e (error "No radio table setup defined for %s" major-mode))
  10803. (setq name (read-string "Table name: "))
  10804. (while (string-match "%n" txt)
  10805. (setq txt (replace-match name t t txt)))
  10806. (or (bolp) (insert "\n"))
  10807. (setq pos (point))
  10808. (insert txt)
  10809. (goto-char pos)))
  10810. (defun org-get-param (params header i sym &optional hsym)
  10811. "Get parameter value for symbol SYM.
  10812. If this is a header line, actually get the value for the symbol with an
  10813. additional \"h\" inserted after the colon.
  10814. If the value is a protperty list, get the element for the current column.
  10815. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10816. (let ((val (plist-get params sym)))
  10817. (and hsym header (setq val (or (plist-get params hsym) val)))
  10818. (if (consp val) (plist-get val i) val)))
  10819. (defun orgtbl-to-generic (table params)
  10820. "Convert the orgtbl-mode TABLE to some other format.
  10821. This generic routine can be used for many standard cases.
  10822. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10823. separator line, or a list of fields for that line.
  10824. PARAMS is a property list of parameters that can influence the conversion.
  10825. For the generic converter, some parameters are obligatory: You need to
  10826. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10827. :splice, you must have :tstart and :tend.
  10828. Valid parameters are
  10829. :tstart String to start the table. Ignored when :splice is t.
  10830. :tend String to end the table. Ignored when :splice is t.
  10831. :splice When set to t, return only table body lines, don't wrap
  10832. them into :tstart and :tend. Default is nil.
  10833. :hline String to be inserted on horizontal separation lines.
  10834. May be nil to ignore hlines.
  10835. :lstart String to start a new table line.
  10836. :lend String to end a table line
  10837. :sep Separator between two fields
  10838. :lfmt Format for entire line, with enough %s to capture all fields.
  10839. If this is present, :lstart, :lend, and :sep are ignored.
  10840. :fmt A format to be used to wrap the field, should contain
  10841. %s for the original field value. For example, to wrap
  10842. everything in dollars, you could use :fmt \"$%s$\".
  10843. This may also be a property list with column numbers and
  10844. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10845. :hlstart :hlend :hlsep :hlfmt :hfmt
  10846. Same as above, specific for the header lines in the table.
  10847. All lines before the first hline are treated as header.
  10848. If any of these is not present, the data line value is used.
  10849. :efmt Use this format to print numbers with exponentials.
  10850. The format should have %s twice for inserting mantissa
  10851. and exponent, for example \"%s\\\\times10^{%s}\". This
  10852. may also be a property list with column numbers and
  10853. formats. :fmt will still be applied after :efmt.
  10854. In addition to this, the parameters :skip and :skipcols are always handled
  10855. directly by `orgtbl-send-table'. See manual."
  10856. (interactive)
  10857. (let* ((p params)
  10858. (splicep (plist-get p :splice))
  10859. (hline (plist-get p :hline))
  10860. rtn line i fm efm lfmt h)
  10861. ;; Do we have a header?
  10862. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10863. (setq h t))
  10864. ;; Put header
  10865. (unless splicep
  10866. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10867. ;; Now loop over all lines
  10868. (while (setq line (pop table))
  10869. (if (eq line 'hline)
  10870. ;; A horizontal separator line
  10871. (progn (if hline (push hline rtn))
  10872. (setq h nil)) ; no longer in header
  10873. ;; A normal line. Convert the fields, push line onto the result list
  10874. (setq i 0)
  10875. (setq line
  10876. (mapcar
  10877. (lambda (f)
  10878. (setq i (1+ i)
  10879. fm (org-get-param p h i :fmt :hfmt)
  10880. efm (org-get-param p h i :efmt))
  10881. (if (and efm (string-match orgtbl-exp-regexp f))
  10882. (setq f (format
  10883. efm (match-string 1 f) (match-string 2 f))))
  10884. (if fm (setq f (format fm f)))
  10885. f)
  10886. line))
  10887. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10888. (push (apply 'format lfmt line) rtn)
  10889. (push (concat
  10890. (org-get-param p h i :lstart :hlstart)
  10891. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10892. (org-get-param p h i :lend :hlend))
  10893. rtn))))
  10894. (unless splicep
  10895. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10896. (mapconcat 'identity (nreverse rtn) "\n")))
  10897. (defun orgtbl-to-latex (table params)
  10898. "Convert the orgtbl-mode TABLE to LaTeX.
  10899. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10900. separator line, or a list of fields for that line.
  10901. PARAMS is a property list of parameters that can influence the conversion.
  10902. Supports all parameters from `orgtbl-to-generic'. Most important for
  10903. LaTeX are:
  10904. :splice When set to t, return only table body lines, don't wrap
  10905. them into a tabular environment. Default is nil.
  10906. :fmt A format to be used to wrap the field, should contain %s for the
  10907. original field value. For example, to wrap everything in dollars,
  10908. use :fmt \"$%s$\". This may also be a property list with column
  10909. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10910. :efmt Format for transforming numbers with exponentials. The format
  10911. should have %s twice for inserting mantissa and exponent, for
  10912. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10913. This may also be a property list with column numbers and formats.
  10914. The general parameters :skip and :skipcols have already been applied when
  10915. this function is called."
  10916. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10917. org-table-last-alignment ""))
  10918. (params2
  10919. (list
  10920. :tstart (concat "\\begin{tabular}{" alignment "}")
  10921. :tend "\\end{tabular}"
  10922. :lstart "" :lend " \\\\" :sep " & "
  10923. :efmt "%s\\,(%s)" :hline "\\hline")))
  10924. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10925. (defun orgtbl-to-html (table params)
  10926. "Convert the orgtbl-mode TABLE to LaTeX.
  10927. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10928. separator line, or a list of fields for that line.
  10929. PARAMS is a property list of parameters that can influence the conversion.
  10930. Currently this function recognizes the following parameters:
  10931. :splice When set to t, return only table body lines, don't wrap
  10932. them into a <table> environment. Default is nil.
  10933. The general parameters :skip and :skipcols have already been applied when
  10934. this function is called. The function does *not* use `orgtbl-to-generic',
  10935. so you cannot specify parameters for it."
  10936. (let* ((splicep (plist-get params :splice))
  10937. html)
  10938. ;; Just call the formatter we already have
  10939. ;; We need to make text lines for it, so put the fields back together.
  10940. (setq html (org-format-org-table-html
  10941. (mapcar
  10942. (lambda (x)
  10943. (if (eq x 'hline)
  10944. "|----+----|"
  10945. (concat "| " (mapconcat 'identity x " | ") " |")))
  10946. table)
  10947. splicep))
  10948. (if (string-match "\n+\\'" html)
  10949. (setq html (replace-match "" t t html)))
  10950. html))
  10951. (defun orgtbl-to-texinfo (table params)
  10952. "Convert the orgtbl-mode TABLE to TeXInfo.
  10953. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10954. separator line, or a list of fields for that line.
  10955. PARAMS is a property list of parameters that can influence the conversion.
  10956. Supports all parameters from `orgtbl-to-generic'. Most important for
  10957. TeXInfo are:
  10958. :splice nil/t When set to t, return only table body lines, don't wrap
  10959. them into a multitable environment. Default is nil.
  10960. :fmt fmt A format to be used to wrap the field, should contain
  10961. %s for the original field value. For example, to wrap
  10962. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10963. This may also be a property list with column numbers and
  10964. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10965. :cf \"f1 f2..\" The column fractions for the table. By default these
  10966. are computed automatically from the width of the columns
  10967. under org-mode.
  10968. The general parameters :skip and :skipcols have already been applied when
  10969. this function is called."
  10970. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10971. (colfrac (or (plist-get params :cf)
  10972. (mapconcat
  10973. (lambda (x) (format "%.3f" (/ (float x) total)))
  10974. org-table-last-column-widths " ")))
  10975. (params2
  10976. (list
  10977. :tstart (concat "@multitable @columnfractions " colfrac)
  10978. :tend "@end multitable"
  10979. :lstart "@item " :lend "" :sep " @tab "
  10980. :hlstart "@headitem ")))
  10981. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10982. ;;;; Link Stuff
  10983. ;;; Link abbreviations
  10984. (defun org-link-expand-abbrev (link)
  10985. "Apply replacements as defined in `org-link-abbrev-alist."
  10986. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10987. (let* ((key (match-string 1 link))
  10988. (as (or (assoc key org-link-abbrev-alist-local)
  10989. (assoc key org-link-abbrev-alist)))
  10990. (tag (and (match-end 2) (match-string 3 link)))
  10991. rpl)
  10992. (if (not as)
  10993. link
  10994. (setq rpl (cdr as))
  10995. (cond
  10996. ((symbolp rpl) (funcall rpl tag))
  10997. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10998. (t (concat rpl tag)))))
  10999. link))
  11000. ;;; Storing and inserting links
  11001. (defvar org-insert-link-history nil
  11002. "Minibuffer history for links inserted with `org-insert-link'.")
  11003. (defvar org-stored-links nil
  11004. "Contains the links stored with `org-store-link'.")
  11005. (defvar org-store-link-plist nil
  11006. "Plist with info about the most recently link created with `org-store-link'.")
  11007. (defvar org-link-protocols nil
  11008. "Link protocols added to Org-mode using `org-add-link-type'.")
  11009. (defvar org-store-link-functions nil
  11010. "List of functions that are called to create and store a link.
  11011. Each function will be called in turn until one returns a non-nil
  11012. value. Each function should check if it is responsible for creating
  11013. this link (for example by looking at the major mode).
  11014. If not, it must exit and return nil.
  11015. If yes, it should return a non-nil value after a calling
  11016. `org-store-link-props' with a list of properties and values.
  11017. Special properties are:
  11018. :type The link prefix. like \"http\". This must be given.
  11019. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  11020. This is obligatory as well.
  11021. :description Optional default description for the second pair
  11022. of brackets in an Org-mode link. The user can still change
  11023. this when inserting this link into an Org-mode buffer.
  11024. In addition to these, any additional properties can be specified
  11025. and then used in remember templates.")
  11026. (defun org-add-link-type (type &optional follow export)
  11027. "Add TYPE to the list of `org-link-types'.
  11028. Re-compute all regular expressions depending on `org-link-types'
  11029. FOLLOW and EXPORT are two functions.
  11030. FOLLOW should take the link path as the single argument and do whatever
  11031. is necessary to follow the link, for example find a file or display
  11032. a mail message.
  11033. EXPORT should format the link path for export to one of the export formats.
  11034. It should be a function accepting three arguments:
  11035. path the path of the link, the text after the prefix (like \"http:\")
  11036. desc the description of the link, if any, nil if there was no descripton
  11037. format the export format, a symbol like `html' or `latex'.
  11038. The function may use the FORMAT information to return different values
  11039. depending on the format. The return value will be put literally into
  11040. the exported file.
  11041. Org-mode has a built-in default for exporting links. If you are happy with
  11042. this default, there is no need to define an export function for the link
  11043. type. For a simple example of an export function, see `org-bbdb.el'."
  11044. (add-to-list 'org-link-types type t)
  11045. (org-make-link-regexps)
  11046. (if (assoc type org-link-protocols)
  11047. (setcdr (assoc type org-link-protocols) (list follow export))
  11048. (push (list type follow export) org-link-protocols)))
  11049. (defun org-add-agenda-custom-command (entry)
  11050. "Replace or add a command in `org-agenda-custom-commands'.
  11051. This is mostly for hacking and trying a new command - once the command
  11052. works you probably want to add it to `org-agenda-custom-commands' for good."
  11053. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  11054. (if ass
  11055. (setcdr ass (cdr entry))
  11056. (push entry org-agenda-custom-commands))))
  11057. ;;;###autoload
  11058. (defun org-store-link (arg)
  11059. "\\<org-mode-map>Store an org-link to the current location.
  11060. This link is added to `org-stored-links' and can later be inserted
  11061. into an org-buffer with \\[org-insert-link].
  11062. For some link types, a prefix arg is interpreted:
  11063. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  11064. For file links, arg negates `org-context-in-file-links'."
  11065. (interactive "P")
  11066. (org-load-modules-maybe)
  11067. (setq org-store-link-plist nil) ; reset
  11068. (let (link cpltxt desc description search txt)
  11069. (cond
  11070. ((run-hook-with-args-until-success 'org-store-link-functions)
  11071. (setq link (plist-get org-store-link-plist :link)
  11072. desc (or (plist-get org-store-link-plist :description) link)))
  11073. ((eq major-mode 'calendar-mode)
  11074. (let ((cd (calendar-cursor-to-date)))
  11075. (setq link
  11076. (format-time-string
  11077. (car org-time-stamp-formats)
  11078. (apply 'encode-time
  11079. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  11080. nil nil nil))))
  11081. (org-store-link-props :type "calendar" :date cd)))
  11082. ((eq major-mode 'w3-mode)
  11083. (setq cpltxt (url-view-url t)
  11084. link (org-make-link cpltxt))
  11085. (org-store-link-props :type "w3" :url (url-view-url t)))
  11086. ((eq major-mode 'w3m-mode)
  11087. (setq cpltxt (or w3m-current-title w3m-current-url)
  11088. link (org-make-link w3m-current-url))
  11089. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11090. ((setq search (run-hook-with-args-until-success
  11091. 'org-create-file-search-functions))
  11092. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11093. "::" search))
  11094. (setq cpltxt (or description link)))
  11095. ((eq major-mode 'image-mode)
  11096. (setq cpltxt (concat "file:"
  11097. (abbreviate-file-name buffer-file-name))
  11098. link (org-make-link cpltxt))
  11099. (org-store-link-props :type "image" :file buffer-file-name))
  11100. ((eq major-mode 'dired-mode)
  11101. ;; link to the file in the current line
  11102. (setq cpltxt (concat "file:"
  11103. (abbreviate-file-name
  11104. (expand-file-name
  11105. (dired-get-filename nil t))))
  11106. link (org-make-link cpltxt)))
  11107. ((and buffer-file-name (org-mode-p))
  11108. ;; Just link to current headline
  11109. (setq cpltxt (concat "file:"
  11110. (abbreviate-file-name buffer-file-name)))
  11111. ;; Add a context search string
  11112. (when (org-xor org-context-in-file-links arg)
  11113. ;; Check if we are on a target
  11114. (if (org-in-regexp "<<\\(.*?\\)>>")
  11115. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11116. (setq txt (cond
  11117. ((org-on-heading-p) nil)
  11118. ((org-region-active-p)
  11119. (buffer-substring (region-beginning) (region-end)))
  11120. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11121. (when (or (null txt) (string-match "\\S-" txt))
  11122. (setq cpltxt
  11123. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11124. desc "NONE"))))
  11125. (if (string-match "::\\'" cpltxt)
  11126. (setq cpltxt (substring cpltxt 0 -2)))
  11127. (setq link (org-make-link cpltxt)))
  11128. ((buffer-file-name (buffer-base-buffer))
  11129. ;; Just link to this file here.
  11130. (setq cpltxt (concat "file:"
  11131. (abbreviate-file-name
  11132. (buffer-file-name (buffer-base-buffer)))))
  11133. ;; Add a context string
  11134. (when (org-xor org-context-in-file-links arg)
  11135. (setq txt (if (org-region-active-p)
  11136. (buffer-substring (region-beginning) (region-end))
  11137. (buffer-substring (point-at-bol) (point-at-eol))))
  11138. ;; Only use search option if there is some text.
  11139. (when (string-match "\\S-" txt)
  11140. (setq cpltxt
  11141. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11142. desc "NONE")))
  11143. (setq link (org-make-link cpltxt)))
  11144. ((interactive-p)
  11145. (error "Cannot link to a buffer which is not visiting a file"))
  11146. (t (setq link nil)))
  11147. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11148. (setq link (or link cpltxt)
  11149. desc (or desc cpltxt))
  11150. (if (equal desc "NONE") (setq desc nil))
  11151. (if (and (interactive-p) link)
  11152. (progn
  11153. (setq org-stored-links
  11154. (cons (list link desc) org-stored-links))
  11155. (message "Stored: %s" (or desc link)))
  11156. (and link (org-make-link-string link desc)))))
  11157. (defun org-store-link-props (&rest plist)
  11158. "Store link properties, extract names and addresses."
  11159. (let (x adr)
  11160. (when (setq x (plist-get plist :from))
  11161. (setq adr (mail-extract-address-components x))
  11162. (plist-put plist :fromname (car adr))
  11163. (plist-put plist :fromaddress (nth 1 adr)))
  11164. (when (setq x (plist-get plist :to))
  11165. (setq adr (mail-extract-address-components x))
  11166. (plist-put plist :toname (car adr))
  11167. (plist-put plist :toaddress (nth 1 adr))))
  11168. (let ((from (plist-get plist :from))
  11169. (to (plist-get plist :to)))
  11170. (when (and from to org-from-is-user-regexp)
  11171. (plist-put plist :fromto
  11172. (if (string-match org-from-is-user-regexp from)
  11173. (concat "to %t")
  11174. (concat "from %f")))))
  11175. (setq org-store-link-plist plist))
  11176. (defun org-add-link-props (&rest plist)
  11177. "Add these properties to the link property list."
  11178. (let (key value)
  11179. (while plist
  11180. (setq key (pop plist) value (pop plist))
  11181. (setq org-store-link-plist
  11182. (plist-put org-store-link-plist key value)))))
  11183. (defun org-email-link-description (&optional fmt)
  11184. "Return the description part of an email link.
  11185. This takes information from `org-store-link-plist' and formats it
  11186. according to FMT (default from `org-email-link-description-format')."
  11187. (setq fmt (or fmt org-email-link-description-format))
  11188. (let* ((p org-store-link-plist)
  11189. (to (plist-get p :toaddress))
  11190. (from (plist-get p :fromaddress))
  11191. (table
  11192. (list
  11193. (cons "%c" (plist-get p :fromto))
  11194. (cons "%F" (plist-get p :from))
  11195. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11196. (cons "%T" (plist-get p :to))
  11197. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11198. (cons "%s" (plist-get p :subject))
  11199. (cons "%m" (plist-get p :message-id)))))
  11200. (when (string-match "%c" fmt)
  11201. ;; Check if the user wrote this message
  11202. (if (and org-from-is-user-regexp from to
  11203. (save-match-data (string-match org-from-is-user-regexp from)))
  11204. (setq fmt (replace-match "to %t" t t fmt))
  11205. (setq fmt (replace-match "from %f" t t fmt))))
  11206. (org-replace-escapes fmt table)))
  11207. (defun org-make-org-heading-search-string (&optional string heading)
  11208. "Make search string for STRING or current headline."
  11209. (interactive)
  11210. (let ((s (or string (org-get-heading))))
  11211. (unless (and string (not heading))
  11212. ;; We are using a headline, clean up garbage in there.
  11213. (if (string-match org-todo-regexp s)
  11214. (setq s (replace-match "" t t s)))
  11215. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11216. (setq s (replace-match "" t t s)))
  11217. (setq s (org-trim s))
  11218. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11219. org-comment-string "\\)") s)
  11220. (setq s (replace-match "" t t s)))
  11221. (while (string-match org-ts-regexp s)
  11222. (setq s (replace-match "" t t s))))
  11223. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11224. (setq s (replace-match " " t t s)))
  11225. (or string (setq s (concat "*" s))) ; Add * for headlines
  11226. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11227. (defun org-make-link (&rest strings)
  11228. "Concatenate STRINGS."
  11229. (apply 'concat strings))
  11230. (defun org-make-link-string (link &optional description)
  11231. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11232. (unless (string-match "\\S-" link)
  11233. (error "Empty link"))
  11234. (when (stringp description)
  11235. ;; Remove brackets from the description, they are fatal.
  11236. (while (string-match "\\[" description)
  11237. (setq description (replace-match "{" t t description)))
  11238. (while (string-match "\\]" description)
  11239. (setq description (replace-match "}" t t description))))
  11240. (when (equal (org-link-escape link) description)
  11241. ;; No description needed, it is identical
  11242. (setq description nil))
  11243. (when (and (not description)
  11244. (not (equal link (org-link-escape link))))
  11245. (setq description link))
  11246. (concat "[[" (org-link-escape link) "]"
  11247. (if description (concat "[" description "]") "")
  11248. "]"))
  11249. (defconst org-link-escape-chars
  11250. '((?\ . "%20")
  11251. (?\[ . "%5B")
  11252. (?\] . "%5D")
  11253. (?\340 . "%E0") ; `a
  11254. (?\342 . "%E2") ; ^a
  11255. (?\347 . "%E7") ; ,c
  11256. (?\350 . "%E8") ; `e
  11257. (?\351 . "%E9") ; 'e
  11258. (?\352 . "%EA") ; ^e
  11259. (?\356 . "%EE") ; ^i
  11260. (?\364 . "%F4") ; ^o
  11261. (?\371 . "%F9") ; `u
  11262. (?\373 . "%FB") ; ^u
  11263. (?\; . "%3B")
  11264. (?? . "%3F")
  11265. (?= . "%3D")
  11266. (?+ . "%2B")
  11267. )
  11268. "Association list of escapes for some characters problematic in links.
  11269. This is the list that is used for internal purposes.")
  11270. (defconst org-link-escape-chars-browser
  11271. '((?\ . "%20")) ; 32 for the SPC char
  11272. "Association list of escapes for some characters problematic in links.
  11273. This is the list that is used before handing over to the browser.")
  11274. (defun org-link-escape (text &optional table)
  11275. "Escape charaters in TEXT that are problematic for links."
  11276. (setq table (or table org-link-escape-chars))
  11277. (when text
  11278. (let ((re (mapconcat (lambda (x) (regexp-quote
  11279. (char-to-string (car x))))
  11280. table "\\|")))
  11281. (while (string-match re text)
  11282. (setq text
  11283. (replace-match
  11284. (cdr (assoc (string-to-char (match-string 0 text))
  11285. table))
  11286. t t text)))
  11287. text)))
  11288. (defun org-link-unescape (text &optional table)
  11289. "Reverse the action of `org-link-escape'."
  11290. (setq table (or table org-link-escape-chars))
  11291. (when text
  11292. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11293. table "\\|")))
  11294. (while (string-match re text)
  11295. (setq text
  11296. (replace-match
  11297. (char-to-string (car (rassoc (match-string 0 text) table)))
  11298. t t text)))
  11299. text)))
  11300. (defun org-xor (a b)
  11301. "Exclusive or."
  11302. (if a (not b) b))
  11303. (defun org-get-header (header)
  11304. "Find a header field in the current buffer."
  11305. (save-excursion
  11306. (goto-char (point-min))
  11307. (let ((case-fold-search t) s)
  11308. (cond
  11309. ((eq header 'from)
  11310. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11311. (setq s (match-string 1)))
  11312. (while (string-match "\"" s)
  11313. (setq s (replace-match "" t t s)))
  11314. (if (string-match "[<(].*" s)
  11315. (setq s (replace-match "" t t s))))
  11316. ((eq header 'message-id)
  11317. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11318. (setq s (match-string 1))))
  11319. ((eq header 'subject)
  11320. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11321. (setq s (match-string 1)))))
  11322. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11323. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11324. s)))
  11325. (defun org-fixup-message-id-for-http (s)
  11326. "Replace special characters in a message id, so it can be used in an http query."
  11327. (while (string-match "<" s)
  11328. (setq s (replace-match "%3C" t t s)))
  11329. (while (string-match ">" s)
  11330. (setq s (replace-match "%3E" t t s)))
  11331. (while (string-match "@" s)
  11332. (setq s (replace-match "%40" t t s)))
  11333. s)
  11334. ;;;###autoload
  11335. (defun org-insert-link-global ()
  11336. "Insert a link like Org-mode does.
  11337. This command can be called in any mode to insert a link in Org-mode syntax."
  11338. (interactive)
  11339. (org-load-modules-maybe)
  11340. (org-run-like-in-org-mode 'org-insert-link))
  11341. (defun org-insert-link (&optional complete-file)
  11342. "Insert a link. At the prompt, enter the link.
  11343. Completion can be used to select a link previously stored with
  11344. `org-store-link'. When the empty string is entered (i.e. if you just
  11345. press RET at the prompt), the link defaults to the most recently
  11346. stored link. As SPC triggers completion in the minibuffer, you need to
  11347. use M-SPC or C-q SPC to force the insertion of a space character.
  11348. You will also be prompted for a description, and if one is given, it will
  11349. be displayed in the buffer instead of the link.
  11350. If there is already a link at point, this command will allow you to edit link
  11351. and description parts.
  11352. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11353. selected using completion. The path to the file will be relative to
  11354. the current directory if the file is in the current directory or a
  11355. subdirectory. Otherwise, the link will be the absolute path as
  11356. completed in the minibuffer (i.e. normally ~/path/to/file).
  11357. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11358. is in the current directory or below.
  11359. With three \\[universal-argument] prefixes, negate the meaning of
  11360. `org-keep-stored-link-after-insertion'."
  11361. (interactive "P")
  11362. (let* ((wcf (current-window-configuration))
  11363. (region (if (org-region-active-p)
  11364. (buffer-substring (region-beginning) (region-end))))
  11365. (remove (and region (list (region-beginning) (region-end))))
  11366. (desc region)
  11367. tmphist ; byte-compile incorrectly complains about this
  11368. link entry file)
  11369. (cond
  11370. ((org-in-regexp org-bracket-link-regexp 1)
  11371. ;; We do have a link at point, and we are going to edit it.
  11372. (setq remove (list (match-beginning 0) (match-end 0)))
  11373. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11374. (setq link (read-string "Link: "
  11375. (org-link-unescape
  11376. (org-match-string-no-properties 1)))))
  11377. ((or (org-in-regexp org-angle-link-re)
  11378. (org-in-regexp org-plain-link-re))
  11379. ;; Convert to bracket link
  11380. (setq remove (list (match-beginning 0) (match-end 0))
  11381. link (read-string "Link: "
  11382. (org-remove-angle-brackets (match-string 0)))))
  11383. ((equal complete-file '(4))
  11384. ;; Completing read for file names.
  11385. (setq file (read-file-name "File: "))
  11386. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11387. (pwd1 (file-name-as-directory (abbreviate-file-name
  11388. (expand-file-name ".")))))
  11389. (cond
  11390. ((equal complete-file '(16))
  11391. (setq link (org-make-link
  11392. "file:"
  11393. (abbreviate-file-name (expand-file-name file)))))
  11394. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11395. (setq link (org-make-link "file:" (match-string 1 file))))
  11396. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11397. (expand-file-name file))
  11398. (setq link (org-make-link
  11399. "file:" (match-string 1 (expand-file-name file)))))
  11400. (t (setq link (org-make-link "file:" file))))))
  11401. (t
  11402. ;; Read link, with completion for stored links.
  11403. (with-output-to-temp-buffer "*Org Links*"
  11404. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11405. (when org-stored-links
  11406. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11407. (princ (mapconcat
  11408. (lambda (x)
  11409. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11410. (reverse org-stored-links) "\n"))))
  11411. (let ((cw (selected-window)))
  11412. (select-window (get-buffer-window "*Org Links*"))
  11413. (shrink-window-if-larger-than-buffer)
  11414. (setq truncate-lines t)
  11415. (select-window cw))
  11416. ;; Fake a link history, containing the stored links.
  11417. (setq tmphist (append (mapcar 'car org-stored-links)
  11418. org-insert-link-history))
  11419. (unwind-protect
  11420. (setq link (org-completing-read
  11421. "Link: "
  11422. (append
  11423. (mapcar (lambda (x) (list (concat (car x) ":")))
  11424. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11425. (mapcar (lambda (x) (list (concat x ":")))
  11426. org-link-types))
  11427. nil nil nil
  11428. 'tmphist
  11429. (or (car (car org-stored-links)))))
  11430. (set-window-configuration wcf)
  11431. (kill-buffer "*Org Links*"))
  11432. (setq entry (assoc link org-stored-links))
  11433. (or entry (push link org-insert-link-history))
  11434. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11435. (not org-keep-stored-link-after-insertion))
  11436. (setq org-stored-links (delq (assoc link org-stored-links)
  11437. org-stored-links)))
  11438. (setq desc (or desc (nth 1 entry)))))
  11439. (if (string-match org-plain-link-re link)
  11440. ;; URL-like link, normalize the use of angular brackets.
  11441. (setq link (org-make-link (org-remove-angle-brackets link))))
  11442. ;; Check if we are linking to the current file with a search option
  11443. ;; If yes, simplify the link by using only the search option.
  11444. (when (and buffer-file-name
  11445. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11446. (let* ((path (match-string 1 link))
  11447. (case-fold-search nil)
  11448. (search (match-string 2 link)))
  11449. (save-match-data
  11450. (if (equal (file-truename buffer-file-name) (file-truename path))
  11451. ;; We are linking to this same file, with a search option
  11452. (setq link search)))))
  11453. ;; Check if we can/should use a relative path. If yes, simplify the link
  11454. (when (string-match "\\<file:\\(.*\\)" link)
  11455. (let* ((path (match-string 1 link))
  11456. (origpath path)
  11457. (case-fold-search nil))
  11458. (cond
  11459. ((eq org-link-file-path-type 'absolute)
  11460. (setq path (abbreviate-file-name (expand-file-name path))))
  11461. ((eq org-link-file-path-type 'noabbrev)
  11462. (setq path (expand-file-name path)))
  11463. ((eq org-link-file-path-type 'relative)
  11464. (setq path (file-relative-name path)))
  11465. (t
  11466. (save-match-data
  11467. (if (string-match (concat "^" (regexp-quote
  11468. (file-name-as-directory
  11469. (expand-file-name "."))))
  11470. (expand-file-name path))
  11471. ;; We are linking a file with relative path name.
  11472. (setq path (substring (expand-file-name path)
  11473. (match-end 0)))))))
  11474. (setq link (concat "file:" path))
  11475. (if (equal desc origpath)
  11476. (setq desc path))))
  11477. (setq desc (read-string "Description: " desc))
  11478. (unless (string-match "\\S-" desc) (setq desc nil))
  11479. (if remove (apply 'delete-region remove))
  11480. (insert (org-make-link-string link desc))))
  11481. (defun org-completing-read (&rest args)
  11482. (let ((minibuffer-local-completion-map
  11483. (copy-keymap minibuffer-local-completion-map)))
  11484. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11485. (apply 'completing-read args)))
  11486. ;;; Opening/following a link
  11487. (defvar org-link-search-failed nil)
  11488. (defun org-next-link ()
  11489. "Move forward to the next link.
  11490. If the link is in hidden text, expose it."
  11491. (interactive)
  11492. (when (and org-link-search-failed (eq this-command last-command))
  11493. (goto-char (point-min))
  11494. (message "Link search wrapped back to beginning of buffer"))
  11495. (setq org-link-search-failed nil)
  11496. (let* ((pos (point))
  11497. (ct (org-context))
  11498. (a (assoc :link ct)))
  11499. (if a (goto-char (nth 2 a)))
  11500. (if (re-search-forward org-any-link-re nil t)
  11501. (progn
  11502. (goto-char (match-beginning 0))
  11503. (if (org-invisible-p) (org-show-context)))
  11504. (goto-char pos)
  11505. (setq org-link-search-failed t)
  11506. (error "No further link found"))))
  11507. (defun org-previous-link ()
  11508. "Move backward to the previous link.
  11509. If the link is in hidden text, expose it."
  11510. (interactive)
  11511. (when (and org-link-search-failed (eq this-command last-command))
  11512. (goto-char (point-max))
  11513. (message "Link search wrapped back to end of buffer"))
  11514. (setq org-link-search-failed nil)
  11515. (let* ((pos (point))
  11516. (ct (org-context))
  11517. (a (assoc :link ct)))
  11518. (if a (goto-char (nth 1 a)))
  11519. (if (re-search-backward org-any-link-re nil t)
  11520. (progn
  11521. (goto-char (match-beginning 0))
  11522. (if (org-invisible-p) (org-show-context)))
  11523. (goto-char pos)
  11524. (setq org-link-search-failed t)
  11525. (error "No further link found"))))
  11526. (defun org-find-file-at-mouse (ev)
  11527. "Open file link or URL at mouse."
  11528. (interactive "e")
  11529. (mouse-set-point ev)
  11530. (org-open-at-point 'in-emacs))
  11531. (defun org-open-at-mouse (ev)
  11532. "Open file link or URL at mouse."
  11533. (interactive "e")
  11534. (mouse-set-point ev)
  11535. (org-open-at-point))
  11536. (defvar org-window-config-before-follow-link nil
  11537. "The window configuration before following a link.
  11538. This is saved in case the need arises to restore it.")
  11539. (defvar org-open-link-marker (make-marker)
  11540. "Marker pointing to the location where `org-open-at-point; was called.")
  11541. ;;;###autoload
  11542. (defun org-open-at-point-global ()
  11543. "Follow a link like Org-mode does.
  11544. This command can be called in any mode to follow a link that has
  11545. Org-mode syntax."
  11546. (interactive)
  11547. (org-run-like-in-org-mode 'org-open-at-point))
  11548. (defun org-open-at-point (&optional in-emacs)
  11549. "Open link at or after point.
  11550. If there is no link at point, this function will search forward up to
  11551. the end of the current subtree.
  11552. Normally, files will be opened by an appropriate application. If the
  11553. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11554. (interactive "P")
  11555. (org-load-modules-maybe)
  11556. (move-marker org-open-link-marker (point))
  11557. (setq org-window-config-before-follow-link (current-window-configuration))
  11558. (org-remove-occur-highlights nil nil t)
  11559. (if (org-at-timestamp-p t)
  11560. (org-follow-timestamp-link)
  11561. (let (type path link line search (pos (point)))
  11562. (catch 'match
  11563. (save-excursion
  11564. (skip-chars-forward "^]\n\r")
  11565. (when (org-in-regexp org-bracket-link-regexp)
  11566. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11567. (while (string-match " *\n *" link)
  11568. (setq link (replace-match " " t t link)))
  11569. (setq link (org-link-expand-abbrev link))
  11570. (if (string-match org-link-re-with-space2 link)
  11571. (setq type (match-string 1 link) path (match-string 2 link))
  11572. (setq type "thisfile" path link))
  11573. (throw 'match t)))
  11574. (when (get-text-property (point) 'org-linked-text)
  11575. (setq type "thisfile"
  11576. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11577. (1+ (point)) (point))
  11578. path (buffer-substring
  11579. (previous-single-property-change pos 'org-linked-text)
  11580. (next-single-property-change pos 'org-linked-text)))
  11581. (throw 'match t))
  11582. (save-excursion
  11583. (when (or (org-in-regexp org-angle-link-re)
  11584. (org-in-regexp org-plain-link-re))
  11585. (setq type (match-string 1) path (match-string 2))
  11586. (throw 'match t)))
  11587. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11588. (setq type "tree-match"
  11589. path (match-string 1))
  11590. (throw 'match t))
  11591. (save-excursion
  11592. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11593. (setq type "tags"
  11594. path (match-string 1))
  11595. (while (string-match ":" path)
  11596. (setq path (replace-match "+" t t path)))
  11597. (throw 'match t))))
  11598. (unless path
  11599. (error "No link found"))
  11600. ;; Remove any trailing spaces in path
  11601. (if (string-match " +\\'" path)
  11602. (setq path (replace-match "" t t path)))
  11603. (cond
  11604. ((assoc type org-link-protocols)
  11605. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11606. ((equal type "mailto")
  11607. (let ((cmd (car org-link-mailto-program))
  11608. (args (cdr org-link-mailto-program)) args1
  11609. (address path) (subject "") a)
  11610. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11611. (setq address (match-string 1 path)
  11612. subject (org-link-escape (match-string 2 path))))
  11613. (while args
  11614. (cond
  11615. ((not (stringp (car args))) (push (pop args) args1))
  11616. (t (setq a (pop args))
  11617. (if (string-match "%a" a)
  11618. (setq a (replace-match address t t a)))
  11619. (if (string-match "%s" a)
  11620. (setq a (replace-match subject t t a)))
  11621. (push a args1))))
  11622. (apply cmd (nreverse args1))))
  11623. ((member type '("http" "https" "ftp" "news"))
  11624. (browse-url (concat type ":" (org-link-escape
  11625. path org-link-escape-chars-browser))))
  11626. ((member type '("message"))
  11627. (browse-url (concat type ":" path)))
  11628. ((string= type "tags")
  11629. (org-tags-view in-emacs path))
  11630. ((string= type "thisfile")
  11631. (if in-emacs
  11632. (switch-to-buffer-other-window
  11633. (org-get-buffer-for-internal-link (current-buffer)))
  11634. (org-mark-ring-push))
  11635. (let ((cmd `(org-link-search
  11636. ,path
  11637. ,(cond ((equal in-emacs '(4)) 'occur)
  11638. ((equal in-emacs '(16)) 'org-occur)
  11639. (t nil))
  11640. ,pos)))
  11641. (condition-case nil (eval cmd)
  11642. (error (progn (widen) (eval cmd))))))
  11643. ((string= type "tree-match")
  11644. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11645. ((string= type "file")
  11646. (if (string-match "::\\([0-9]+\\)\\'" path)
  11647. (setq line (string-to-number (match-string 1 path))
  11648. path (substring path 0 (match-beginning 0)))
  11649. (if (string-match "::\\(.+\\)\\'" path)
  11650. (setq search (match-string 1 path)
  11651. path (substring path 0 (match-beginning 0)))))
  11652. (if (string-match "[*?{]" (file-name-nondirectory path))
  11653. (dired path)
  11654. (org-open-file path in-emacs line search)))
  11655. ((string= type "news")
  11656. (require 'org-gnus)
  11657. (org-gnus-follow-link path))
  11658. ((string= type "shell")
  11659. (let ((cmd path))
  11660. (if (or (not org-confirm-shell-link-function)
  11661. (funcall org-confirm-shell-link-function
  11662. (format "Execute \"%s\" in shell? "
  11663. (org-add-props cmd nil
  11664. 'face 'org-warning))))
  11665. (progn
  11666. (message "Executing %s" cmd)
  11667. (shell-command cmd))
  11668. (error "Abort"))))
  11669. ((string= type "elisp")
  11670. (let ((cmd path))
  11671. (if (or (not org-confirm-elisp-link-function)
  11672. (funcall org-confirm-elisp-link-function
  11673. (format "Execute \"%s\" as elisp? "
  11674. (org-add-props cmd nil
  11675. 'face 'org-warning))))
  11676. (message "%s => %s" cmd (eval (read cmd)))
  11677. (error "Abort"))))
  11678. (t
  11679. (browse-url-at-point)))))
  11680. (move-marker org-open-link-marker nil)
  11681. (run-hook-with-args 'org-follow-link-hook))
  11682. ;;; File search
  11683. (defvar org-create-file-search-functions nil
  11684. "List of functions to construct the right search string for a file link.
  11685. These functions are called in turn with point at the location to
  11686. which the link should point.
  11687. A function in the hook should first test if it would like to
  11688. handle this file type, for example by checking the major-mode or
  11689. the file extension. If it decides not to handle this file, it
  11690. should just return nil to give other functions a chance. If it
  11691. does handle the file, it must return the search string to be used
  11692. when following the link. The search string will be part of the
  11693. file link, given after a double colon, and `org-open-at-point'
  11694. will automatically search for it. If special measures must be
  11695. taken to make the search successful, another function should be
  11696. added to the companion hook `org-execute-file-search-functions',
  11697. which see.
  11698. A function in this hook may also use `setq' to set the variable
  11699. `description' to provide a suggestion for the descriptive text to
  11700. be used for this link when it gets inserted into an Org-mode
  11701. buffer with \\[org-insert-link].")
  11702. (defvar org-execute-file-search-functions nil
  11703. "List of functions to execute a file search triggered by a link.
  11704. Functions added to this hook must accept a single argument, the
  11705. search string that was part of the file link, the part after the
  11706. double colon. The function must first check if it would like to
  11707. handle this search, for example by checking the major-mode or the
  11708. file extension. If it decides not to handle this search, it
  11709. should just return nil to give other functions a chance. If it
  11710. does handle the search, it must return a non-nil value to keep
  11711. other functions from trying.
  11712. Each function can access the current prefix argument through the
  11713. variable `current-prefix-argument'. Note that a single prefix is
  11714. used to force opening a link in Emacs, so it may be good to only
  11715. use a numeric or double prefix to guide the search function.
  11716. In case this is needed, a function in this hook can also restore
  11717. the window configuration before `org-open-at-point' was called using:
  11718. (set-window-configuration org-window-config-before-follow-link)")
  11719. (defun org-link-search (s &optional type avoid-pos)
  11720. "Search for a link search option.
  11721. If S is surrounded by forward slashes, it is interpreted as a
  11722. regular expression. In org-mode files, this will create an `org-occur'
  11723. sparse tree. In ordinary files, `occur' will be used to list matches.
  11724. If the current buffer is in `dired-mode', grep will be used to search
  11725. in all files. If AVOID-POS is given, ignore matches near that position."
  11726. (let ((case-fold-search t)
  11727. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11728. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11729. (append '(("") (" ") ("\t") ("\n"))
  11730. org-emphasis-alist)
  11731. "\\|") "\\)"))
  11732. (pos (point))
  11733. (pre "") (post "")
  11734. words re0 re1 re2 re3 re4 re5 re2a reall)
  11735. (cond
  11736. ;; First check if there are any special
  11737. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11738. ;; Now try the builtin stuff
  11739. ((save-excursion
  11740. (goto-char (point-min))
  11741. (and
  11742. (re-search-forward
  11743. (concat "<<" (regexp-quote s0) ">>") nil t)
  11744. (setq pos (match-beginning 0))))
  11745. ;; There is an exact target for this
  11746. (goto-char pos))
  11747. ((string-match "^/\\(.*\\)/$" s)
  11748. ;; A regular expression
  11749. (cond
  11750. ((org-mode-p)
  11751. (org-occur (match-string 1 s)))
  11752. ;;((eq major-mode 'dired-mode)
  11753. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11754. (t (org-do-occur (match-string 1 s)))))
  11755. (t
  11756. ;; A normal search strings
  11757. (when (equal (string-to-char s) ?*)
  11758. ;; Anchor on headlines, post may include tags.
  11759. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11760. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11761. s (substring s 1)))
  11762. (remove-text-properties
  11763. 0 (length s)
  11764. '(face nil mouse-face nil keymap nil fontified nil) s)
  11765. ;; Make a series of regular expressions to find a match
  11766. (setq words (org-split-string s "[ \n\r\t]+")
  11767. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11768. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11769. "\\)" markers)
  11770. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11771. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11772. re1 (concat pre re2 post)
  11773. re3 (concat pre re4 post)
  11774. re5 (concat pre ".*" re4)
  11775. re2 (concat pre re2)
  11776. re2a (concat pre re2a)
  11777. re4 (concat pre re4)
  11778. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11779. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11780. re5 "\\)"
  11781. ))
  11782. (cond
  11783. ((eq type 'org-occur) (org-occur reall))
  11784. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11785. (t (goto-char (point-min))
  11786. (if (or (org-search-not-self 1 re0 nil t)
  11787. (org-search-not-self 1 re1 nil t)
  11788. (org-search-not-self 1 re2 nil t)
  11789. (org-search-not-self 1 re2a nil t)
  11790. (org-search-not-self 1 re3 nil t)
  11791. (org-search-not-self 1 re4 nil t)
  11792. (org-search-not-self 1 re5 nil t)
  11793. )
  11794. (goto-char (match-beginning 1))
  11795. (goto-char pos)
  11796. (error "No match")))))
  11797. (t
  11798. ;; Normal string-search
  11799. (goto-char (point-min))
  11800. (if (search-forward s nil t)
  11801. (goto-char (match-beginning 0))
  11802. (error "No match"))))
  11803. (and (org-mode-p) (org-show-context 'link-search))))
  11804. (defun org-search-not-self (group &rest args)
  11805. "Execute `re-search-forward', but only accept matches that do not
  11806. enclose the position of `org-open-link-marker'."
  11807. (let ((m org-open-link-marker))
  11808. (catch 'exit
  11809. (while (apply 're-search-forward args)
  11810. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11811. (goto-char (match-end group))
  11812. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11813. (> (match-beginning 0) (marker-position m))
  11814. (< (match-end 0) (marker-position m)))
  11815. (save-match-data
  11816. (or (not (org-in-regexp
  11817. org-bracket-link-analytic-regexp 1))
  11818. (not (match-end 4)) ; no description
  11819. (and (<= (match-beginning 4) (point))
  11820. (>= (match-end 4) (point))))))
  11821. (throw 'exit (point))))))))
  11822. (defun org-get-buffer-for-internal-link (buffer)
  11823. "Return a buffer to be used for displaying the link target of internal links."
  11824. (cond
  11825. ((not org-display-internal-link-with-indirect-buffer)
  11826. buffer)
  11827. ((string-match "(Clone)$" (buffer-name buffer))
  11828. (message "Buffer is already a clone, not making another one")
  11829. ;; we also do not modify visibility in this case
  11830. buffer)
  11831. (t ; make a new indirect buffer for displaying the link
  11832. (let* ((bn (buffer-name buffer))
  11833. (ibn (concat bn "(Clone)"))
  11834. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11835. (with-current-buffer ib (org-overview))
  11836. ib))))
  11837. (defun org-do-occur (regexp &optional cleanup)
  11838. "Call the Emacs command `occur'.
  11839. If CLEANUP is non-nil, remove the printout of the regular expression
  11840. in the *Occur* buffer. This is useful if the regex is long and not useful
  11841. to read."
  11842. (occur regexp)
  11843. (when cleanup
  11844. (let ((cwin (selected-window)) win beg end)
  11845. (when (setq win (get-buffer-window "*Occur*"))
  11846. (select-window win))
  11847. (goto-char (point-min))
  11848. (when (re-search-forward "match[a-z]+" nil t)
  11849. (setq beg (match-end 0))
  11850. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11851. (setq end (1- (match-beginning 0)))))
  11852. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11853. (goto-char (point-min))
  11854. (select-window cwin))))
  11855. ;;; The mark ring for links jumps
  11856. (defvar org-mark-ring nil
  11857. "Mark ring for positions before jumps in Org-mode.")
  11858. (defvar org-mark-ring-last-goto nil
  11859. "Last position in the mark ring used to go back.")
  11860. ;; Fill and close the ring
  11861. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11862. (loop for i from 1 to org-mark-ring-length do
  11863. (push (make-marker) org-mark-ring))
  11864. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11865. org-mark-ring)
  11866. (defun org-mark-ring-push (&optional pos buffer)
  11867. "Put the current position or POS into the mark ring and rotate it."
  11868. (interactive)
  11869. (setq pos (or pos (point)))
  11870. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11871. (move-marker (car org-mark-ring)
  11872. (or pos (point))
  11873. (or buffer (current-buffer)))
  11874. (message "%s"
  11875. (substitute-command-keys
  11876. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11877. (defun org-mark-ring-goto (&optional n)
  11878. "Jump to the previous position in the mark ring.
  11879. With prefix arg N, jump back that many stored positions. When
  11880. called several times in succession, walk through the entire ring.
  11881. Org-mode commands jumping to a different position in the current file,
  11882. or to another Org-mode file, automatically push the old position
  11883. onto the ring."
  11884. (interactive "p")
  11885. (let (p m)
  11886. (if (eq last-command this-command)
  11887. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11888. (setq p org-mark-ring))
  11889. (setq org-mark-ring-last-goto p)
  11890. (setq m (car p))
  11891. (switch-to-buffer (marker-buffer m))
  11892. (goto-char m)
  11893. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11894. (defun org-remove-angle-brackets (s)
  11895. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11896. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11897. s)
  11898. (defun org-add-angle-brackets (s)
  11899. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11900. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11901. s)
  11902. ;;; Following specific links
  11903. (defun org-follow-timestamp-link ()
  11904. (cond
  11905. ((org-at-date-range-p t)
  11906. (let ((org-agenda-start-on-weekday)
  11907. (t1 (match-string 1))
  11908. (t2 (match-string 2)))
  11909. (setq t1 (time-to-days (org-time-string-to-time t1))
  11910. t2 (time-to-days (org-time-string-to-time t2)))
  11911. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11912. ((org-at-timestamp-p t)
  11913. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11914. (substring (match-string 1) 0 10)))
  11915. 1))
  11916. (t (error "This should not happen"))))
  11917. ;;; BibTeX links
  11918. ;; Use the custom search meachnism to construct and use search strings for
  11919. ;; file links to BibTeX database entries.
  11920. (defun org-create-file-search-in-bibtex ()
  11921. "Create the search string and description for a BibTeX database entry."
  11922. (when (eq major-mode 'bibtex-mode)
  11923. ;; yes, we want to construct this search string.
  11924. ;; Make a good description for this entry, using names, year and the title
  11925. ;; Put it into the `description' variable which is dynamically scoped.
  11926. (let ((bibtex-autokey-names 1)
  11927. (bibtex-autokey-names-stretch 1)
  11928. (bibtex-autokey-name-case-convert-function 'identity)
  11929. (bibtex-autokey-name-separator " & ")
  11930. (bibtex-autokey-additional-names " et al.")
  11931. (bibtex-autokey-year-length 4)
  11932. (bibtex-autokey-name-year-separator " ")
  11933. (bibtex-autokey-titlewords 3)
  11934. (bibtex-autokey-titleword-separator " ")
  11935. (bibtex-autokey-titleword-case-convert-function 'identity)
  11936. (bibtex-autokey-titleword-length 'infty)
  11937. (bibtex-autokey-year-title-separator ": "))
  11938. (setq description (bibtex-generate-autokey)))
  11939. ;; Now parse the entry, get the key and return it.
  11940. (save-excursion
  11941. (bibtex-beginning-of-entry)
  11942. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  11943. (defun org-execute-file-search-in-bibtex (s)
  11944. "Find the link search string S as a key for a database entry."
  11945. (when (eq major-mode 'bibtex-mode)
  11946. ;; Yes, we want to do the search in this file.
  11947. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  11948. (goto-char (point-min))
  11949. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  11950. (regexp-quote s) "[ \t\n]*,") nil t)
  11951. (goto-char (match-beginning 0)))
  11952. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  11953. ;; Use double prefix to indicate that any web link should be browsed
  11954. (let ((b (current-buffer)) (p (point)))
  11955. ;; Restore the window configuration because we just use the web link
  11956. (set-window-configuration org-window-config-before-follow-link)
  11957. (save-excursion (set-buffer b) (goto-char p)
  11958. (bibtex-url)))
  11959. (recenter 0)) ; Move entry start to beginning of window
  11960. ;; return t to indicate that the search is done.
  11961. t))
  11962. ;; Finally add the functions to the right hooks.
  11963. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  11964. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  11965. ;; end of Bibtex link setup
  11966. ;;; Following file links
  11967. (defun org-open-file (path &optional in-emacs line search)
  11968. "Open the file at PATH.
  11969. First, this expands any special file name abbreviations. Then the
  11970. configuration variable `org-file-apps' is checked if it contains an
  11971. entry for this file type, and if yes, the corresponding command is launched.
  11972. If no application is found, Emacs simply visits the file.
  11973. With optional argument IN-EMACS, Emacs will visit the file.
  11974. Optional LINE specifies a line to go to, optional SEARCH a string to
  11975. search for. If LINE or SEARCH is given, the file will always be
  11976. opened in Emacs.
  11977. If the file does not exist, an error is thrown."
  11978. (setq in-emacs (or in-emacs line search))
  11979. (let* ((file (if (equal path "")
  11980. buffer-file-name
  11981. (substitute-in-file-name (expand-file-name path))))
  11982. (apps (append org-file-apps (org-default-apps)))
  11983. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  11984. (dirp (if remp nil (file-directory-p file)))
  11985. (dfile (downcase file))
  11986. (old-buffer (current-buffer))
  11987. (old-pos (point))
  11988. (old-mode major-mode)
  11989. ext cmd)
  11990. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  11991. (setq ext (match-string 1 dfile))
  11992. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  11993. (setq ext (match-string 1 dfile))))
  11994. (if in-emacs
  11995. (setq cmd 'emacs)
  11996. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  11997. (and dirp (cdr (assoc 'directory apps)))
  11998. (cdr (assoc ext apps))
  11999. (cdr (assoc t apps)))))
  12000. (when (eq cmd 'mailcap)
  12001. (require 'mailcap)
  12002. (mailcap-parse-mailcaps)
  12003. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12004. (command (mailcap-mime-info mime-type)))
  12005. (if (stringp command)
  12006. (setq cmd command)
  12007. (setq cmd 'emacs))))
  12008. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12009. (not (file-exists-p file))
  12010. (not org-open-non-existing-files))
  12011. (error "No such file: %s" file))
  12012. (cond
  12013. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12014. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12015. (while (string-match "['\"]%s['\"]" cmd)
  12016. (setq cmd (replace-match "%s" t t cmd)))
  12017. (while (string-match "%s" cmd)
  12018. (setq cmd (replace-match
  12019. (save-match-data (shell-quote-argument file))
  12020. t t cmd)))
  12021. (save-window-excursion
  12022. (start-process-shell-command cmd nil cmd)))
  12023. ((or (stringp cmd)
  12024. (eq cmd 'emacs))
  12025. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12026. (widen)
  12027. (if line (goto-line line)
  12028. (if search (org-link-search search))))
  12029. ((consp cmd)
  12030. (eval cmd))
  12031. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12032. (and (org-mode-p) (eq old-mode 'org-mode)
  12033. (or (not (equal old-buffer (current-buffer)))
  12034. (not (equal old-pos (point))))
  12035. (org-mark-ring-push old-pos old-buffer))))
  12036. (defun org-default-apps ()
  12037. "Return the default applications for this operating system."
  12038. (cond
  12039. ((eq system-type 'darwin)
  12040. org-file-apps-defaults-macosx)
  12041. ((eq system-type 'windows-nt)
  12042. org-file-apps-defaults-windowsnt)
  12043. (t org-file-apps-defaults-gnu)))
  12044. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12045. (defun org-file-remote-p (file)
  12046. "Test whether FILE specifies a location on a remote system.
  12047. Return non-nil if the location is indeed remote.
  12048. For example, the filename \"/user@host:/foo\" specifies a location
  12049. on the system \"/user@host:\"."
  12050. (cond ((fboundp 'file-remote-p)
  12051. (file-remote-p file))
  12052. ((fboundp 'tramp-handle-file-remote-p)
  12053. (tramp-handle-file-remote-p file))
  12054. ((and (boundp 'ange-ftp-name-format)
  12055. (string-match (car ange-ftp-name-format) file))
  12056. t)
  12057. (t nil)))
  12058. ;;;; Hooks for remember.el, and refiling
  12059. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12060. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12061. ;;;###autoload
  12062. (defun org-remember-insinuate ()
  12063. "Setup remember.el for use wiht Org-mode."
  12064. (require 'remember)
  12065. (setq remember-annotation-functions '(org-remember-annotation))
  12066. (setq remember-handler-functions '(org-remember-handler))
  12067. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12068. ;;;###autoload
  12069. (defun org-remember-annotation ()
  12070. "Return a link to the current location as an annotation for remember.el.
  12071. If you are using Org-mode files as target for data storage with
  12072. remember.el, then the annotations should include a link compatible with the
  12073. conventions in Org-mode. This function returns such a link."
  12074. (org-store-link nil))
  12075. (defconst org-remember-help
  12076. "Select a destination location for the note.
  12077. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12078. RET on headline -> Store as sublevel entry to current headline
  12079. RET at beg-of-buf -> Append to file as level 2 headline
  12080. <left>/<right> -> before/after current headline, same headings level")
  12081. (defvar org-remember-previous-location nil)
  12082. (defvar org-force-remember-template-char) ;; dynamically scoped
  12083. ;; Save the major mode of the buffer we called remember from
  12084. (defvar org-select-template-temp-major-mode nil)
  12085. ;; Temporary store the buffer where remember was called from
  12086. (defvar org-select-template-original-buffer nil)
  12087. (defun org-select-remember-template (&optional use-char)
  12088. (when org-remember-templates
  12089. (let* ((pre-selected-templates
  12090. (mapcar
  12091. (lambda (tpl)
  12092. (let ((ctxt (nth 5 tpl))
  12093. (mode org-select-template-temp-major-mode)
  12094. (buf org-select-template-original-buffer))
  12095. (and (or (not ctxt) (eq ctxt t)
  12096. (and (listp ctxt) (memq mode ctxt))
  12097. (and (functionp ctxt)
  12098. (with-current-buffer buf
  12099. ;; Protect the user-defined function from error
  12100. (condition-case nil (funcall ctxt) (error nil)))))
  12101. tpl)))
  12102. org-remember-templates))
  12103. ;; If no template at this point, add the default templates:
  12104. (pre-selected-templates1
  12105. (if (not (delq nil pre-selected-templates))
  12106. (mapcar (lambda(x) (if (not (nth 5 x)) x))
  12107. org-remember-templates)
  12108. pre-selected-templates))
  12109. ;; Then unconditionnally add template for any contexts
  12110. (pre-selected-templates2
  12111. (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
  12112. org-remember-templates)
  12113. (delq nil pre-selected-templates1)))
  12114. (templates (mapcar (lambda (x)
  12115. (if (stringp (car x))
  12116. (append (list (nth 1 x) (car x)) (cddr x))
  12117. (append (list (car x) "") (cdr x))))
  12118. (delq nil pre-selected-templates2)))
  12119. (char (or use-char
  12120. (cond
  12121. ((= (length templates) 1)
  12122. (caar templates))
  12123. ((and (boundp 'org-force-remember-template-char)
  12124. org-force-remember-template-char)
  12125. (if (stringp org-force-remember-template-char)
  12126. (string-to-char org-force-remember-template-char)
  12127. org-force-remember-template-char))
  12128. (t
  12129. (message "Select template: %s"
  12130. (mapconcat
  12131. (lambda (x)
  12132. (cond
  12133. ((not (string-match "\\S-" (nth 1 x)))
  12134. (format "[%c]" (car x)))
  12135. ((equal (downcase (car x))
  12136. (downcase (aref (nth 1 x) 0)))
  12137. (format "[%c]%s" (car x)
  12138. (substring (nth 1 x) 1)))
  12139. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12140. templates " "))
  12141. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12142. (when (equal char0 ?\C-g)
  12143. (jump-to-register remember-register)
  12144. (kill-buffer remember-buffer))
  12145. char0))))))
  12146. (cddr (assoc char templates)))))
  12147. (defvar x-last-selected-text)
  12148. (defvar x-last-selected-text-primary)
  12149. ;;;###autoload
  12150. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12151. "Initialize *remember* buffer with template, invoke `org-mode'.
  12152. This function should be placed into `remember-mode-hook' and in fact requires
  12153. to be run from that hook to function properly."
  12154. (if org-remember-templates
  12155. (let* ((entry (org-select-remember-template use-char))
  12156. (tpl (car entry))
  12157. (plist-p (if org-store-link-plist t nil))
  12158. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12159. (string-match "\\S-" (nth 1 entry)))
  12160. (nth 1 entry)
  12161. org-default-notes-file))
  12162. (headline (nth 2 entry))
  12163. (v-c (or (and (eq window-system 'x)
  12164. (fboundp 'x-cut-buffer-or-selection-value)
  12165. (x-cut-buffer-or-selection-value))
  12166. (org-bound-and-true-p x-last-selected-text)
  12167. (org-bound-and-true-p x-last-selected-text-primary)
  12168. (and (> (length kill-ring) 0) (current-kill 0))))
  12169. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12170. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12171. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12172. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12173. ;; `initial' and `annotation' are bound in `remember'
  12174. (v-i (if (boundp 'initial) initial))
  12175. (v-a (if (and (boundp 'annotation) annotation)
  12176. (if (equal annotation "[[]]") "" annotation)
  12177. ""))
  12178. (v-A (if (and v-a
  12179. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12180. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12181. v-a))
  12182. (v-n user-full-name)
  12183. (org-startup-folded nil)
  12184. org-time-was-given org-end-time-was-given x
  12185. prompt completions char time pos default histvar)
  12186. (setq org-store-link-plist
  12187. (append (list :annotation v-a :initial v-i)
  12188. org-store-link-plist))
  12189. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12190. (erase-buffer)
  12191. (insert (substitute-command-keys
  12192. (format
  12193. "## Filing location: Select interactively, default, or last used:
  12194. ## %s to select file and header location interactively.
  12195. ## %s \"%s\" -> \"* %s\"
  12196. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12197. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12198. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12199. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12200. (abbreviate-file-name (or file org-default-notes-file))
  12201. (or headline "")
  12202. (or (car org-remember-previous-location) "???")
  12203. (or (cdr org-remember-previous-location) "???"))))
  12204. (insert tpl) (goto-char (point-min))
  12205. ;; Simple %-escapes
  12206. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12207. (when (and initial (equal (match-string 0) "%i"))
  12208. (save-match-data
  12209. (let* ((lead (buffer-substring
  12210. (point-at-bol) (match-beginning 0))))
  12211. (setq v-i (mapconcat 'identity
  12212. (org-split-string initial "\n")
  12213. (concat "\n" lead))))))
  12214. (replace-match
  12215. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12216. t t))
  12217. ;; %[] Insert contents of a file.
  12218. (goto-char (point-min))
  12219. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12220. (let ((start (match-beginning 0))
  12221. (end (match-end 0))
  12222. (filename (expand-file-name (match-string 1))))
  12223. (goto-char start)
  12224. (delete-region start end)
  12225. (condition-case error
  12226. (insert-file-contents filename)
  12227. (error (insert (format "%%![Couldn't insert %s: %s]"
  12228. filename error))))))
  12229. ;; %() embedded elisp
  12230. (goto-char (point-min))
  12231. (while (re-search-forward "%\\((.+)\\)" nil t)
  12232. (goto-char (match-beginning 0))
  12233. (let ((template-start (point)))
  12234. (forward-char 1)
  12235. (let ((result
  12236. (condition-case error
  12237. (eval (read (current-buffer)))
  12238. (error (format "%%![Error: %s]" error)))))
  12239. (delete-region template-start (point))
  12240. (insert result))))
  12241. ;; From the property list
  12242. (when plist-p
  12243. (goto-char (point-min))
  12244. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12245. (and (setq x (or (plist-get org-store-link-plist
  12246. (intern (match-string 1))) ""))
  12247. (replace-match x t t))))
  12248. ;; Turn on org-mode in the remember buffer, set local variables
  12249. (org-mode)
  12250. (org-set-local 'org-finish-function 'org-remember-finalize)
  12251. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12252. (org-set-local 'org-default-notes-file file))
  12253. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12254. (org-set-local 'org-remember-default-headline headline))
  12255. ;; Interactive template entries
  12256. (goto-char (point-min))
  12257. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12258. (setq char (if (match-end 3) (match-string 3))
  12259. prompt (if (match-end 2) (match-string 2)))
  12260. (goto-char (match-beginning 0))
  12261. (replace-match "")
  12262. (setq completions nil default nil)
  12263. (when prompt
  12264. (setq completions (org-split-string prompt "|")
  12265. prompt (pop completions)
  12266. default (car completions)
  12267. histvar (intern (concat
  12268. "org-remember-template-prompt-history::"
  12269. (or prompt "")))
  12270. completions (mapcar 'list completions)))
  12271. (cond
  12272. ((member char '("G" "g"))
  12273. (let* ((org-last-tags-completion-table
  12274. (org-global-tags-completion-table
  12275. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12276. (org-add-colon-after-tag-completion t)
  12277. (ins (completing-read
  12278. (if prompt (concat prompt ": ") "Tags: ")
  12279. 'org-tags-completion-function nil nil nil
  12280. 'org-tags-history)))
  12281. (setq ins (mapconcat 'identity
  12282. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12283. ":"))
  12284. (when (string-match "\\S-" ins)
  12285. (or (equal (char-before) ?:) (insert ":"))
  12286. (insert ins)
  12287. (or (equal (char-after) ?:) (insert ":")))))
  12288. (char
  12289. (setq org-time-was-given (equal (upcase char) char))
  12290. (setq time (org-read-date (equal (upcase char) "U") t nil
  12291. prompt))
  12292. (org-insert-time-stamp time org-time-was-given
  12293. (member char '("u" "U"))
  12294. nil nil (list org-end-time-was-given)))
  12295. (t
  12296. (insert (org-completing-read
  12297. (concat (if prompt prompt "Enter string")
  12298. (if default (concat " [" default "]"))
  12299. ": ")
  12300. completions nil nil nil histvar default)))))
  12301. (goto-char (point-min))
  12302. (if (re-search-forward "%\\?" nil t)
  12303. (replace-match "")
  12304. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12305. (org-mode)
  12306. (org-set-local 'org-finish-function 'org-remember-finalize))
  12307. (when (save-excursion
  12308. (goto-char (point-min))
  12309. (re-search-forward "%!" nil t))
  12310. (replace-match "")
  12311. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12312. (defun org-remember-finish-immediately ()
  12313. "File remember note immediately.
  12314. This should be run in `post-command-hook' and will remove itself
  12315. from that hook."
  12316. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12317. (when org-finish-function
  12318. (funcall org-finish-function)))
  12319. (defvar org-clock-marker) ; Defined below
  12320. (defun org-remember-finalize ()
  12321. "Finalize the remember process."
  12322. (unless (fboundp 'remember-finalize)
  12323. (defalias 'remember-finalize 'remember-buffer))
  12324. (when (and org-clock-marker
  12325. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12326. ;; FIXME: test this, this is w/o notetaking!
  12327. (let (org-log-note-clock-out) (org-clock-out)))
  12328. (when buffer-file-name
  12329. (save-buffer)
  12330. (setq buffer-file-name nil))
  12331. (remember-finalize))
  12332. ;;;###autoload
  12333. (defun org-remember (&optional goto org-force-remember-template-char)
  12334. "Call `remember'. If this is already a remember buffer, re-apply template.
  12335. If there is an active region, make sure remember uses it as initial content
  12336. of the remember buffer.
  12337. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12338. anything, just go to the file/headline where the selected template usually
  12339. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12340. note stored by remember.
  12341. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12342. associated with a template in `org-remember-templates'."
  12343. (interactive "P")
  12344. (cond
  12345. ((equal goto '(4)) (org-go-to-remember-target))
  12346. ((equal goto '(16)) (org-remember-goto-last-stored))
  12347. (t
  12348. ;; set temporary variables that will be needed in
  12349. ;; `org-select-remember-template'
  12350. (setq org-select-template-temp-major-mode major-mode)
  12351. (setq org-select-template-original-buffer (current-buffer))
  12352. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12353. (progn
  12354. (when (< (length org-remember-templates) 2)
  12355. (error "No other template available"))
  12356. (erase-buffer)
  12357. (let ((annotation (plist-get org-store-link-plist :annotation))
  12358. (initial (plist-get org-store-link-plist :initial)))
  12359. (org-remember-apply-template))
  12360. (message "Press C-c C-c to remember data"))
  12361. (if (org-region-active-p)
  12362. (remember (buffer-substring (point) (mark)))
  12363. (call-interactively 'remember))))))
  12364. (defun org-remember-goto-last-stored ()
  12365. "Go to the location where the last remember note was stored."
  12366. (interactive)
  12367. (bookmark-jump "org-remember-last-stored")
  12368. (message "This is the last note stored by remember"))
  12369. (defun org-go-to-remember-target (&optional template-key)
  12370. "Go to the target location of a remember template.
  12371. The user is queried for the template."
  12372. (interactive)
  12373. (let* (org-select-template-temp-major-mode
  12374. (entry (org-select-remember-template template-key))
  12375. (file (nth 1 entry))
  12376. (heading (nth 2 entry))
  12377. visiting)
  12378. (unless (and file (stringp file) (string-match "\\S-" file))
  12379. (setq file org-default-notes-file))
  12380. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12381. (setq heading org-remember-default-headline))
  12382. (setq visiting (org-find-base-buffer-visiting file))
  12383. (if (not visiting) (find-file-noselect file))
  12384. (switch-to-buffer (or visiting (get-file-buffer file)))
  12385. (widen)
  12386. (goto-char (point-min))
  12387. (if (re-search-forward
  12388. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12389. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12390. nil t)
  12391. (goto-char (match-beginning 0))
  12392. (error "Target headline not found: %s" heading))))
  12393. (defvar org-note-abort nil) ; dynamically scoped
  12394. ;;;###autoload
  12395. (defun org-remember-handler ()
  12396. "Store stuff from remember.el into an org file.
  12397. First prompts for an org file. If the user just presses return, the value
  12398. of `org-default-notes-file' is used.
  12399. Then the command offers the headings tree of the selected file in order to
  12400. file the text at a specific location.
  12401. You can either immediately press RET to get the note appended to the
  12402. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12403. find a better place. Then press RET or <left> or <right> in insert the note.
  12404. Key Cursor position Note gets inserted
  12405. -----------------------------------------------------------------------------
  12406. RET buffer-start as level 1 heading at end of file
  12407. RET on headline as sublevel of the heading at cursor
  12408. RET no heading at cursor position, level taken from context.
  12409. Or use prefix arg to specify level manually.
  12410. <left> on headline as same level, before current heading
  12411. <right> on headline as same level, after current heading
  12412. So the fastest way to store the note is to press RET RET to append it to
  12413. the default file. This way your current train of thought is not
  12414. interrupted, in accordance with the principles of remember.el.
  12415. You can also get the fast execution without prompting by using
  12416. C-u C-c C-c to exit the remember buffer. See also the variable
  12417. `org-remember-store-without-prompt'.
  12418. Before being stored away, the function ensures that the text has a
  12419. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12420. is constructed from the current date and some additional data.
  12421. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12422. also indented so that it starts in the same column as the headline
  12423. \(i.e. after the stars).
  12424. See also the variable `org-reverse-note-order'."
  12425. (goto-char (point-min))
  12426. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12427. (replace-match ""))
  12428. (goto-char (point-max))
  12429. (beginning-of-line 1)
  12430. (while (looking-at "[ \t]*$\\|##.*")
  12431. (delete-region (1- (point)) (point-max))
  12432. (beginning-of-line 1))
  12433. (catch 'quit
  12434. (if org-note-abort (throw 'quit nil))
  12435. (let* ((txt (buffer-substring (point-min) (point-max)))
  12436. (fastp (org-xor (equal current-prefix-arg '(4))
  12437. org-remember-store-without-prompt))
  12438. (file (cond
  12439. (fastp org-default-notes-file)
  12440. ((and (eq org-remember-interactive-interface 'refile)
  12441. org-refile-targets)
  12442. org-default-notes-file)
  12443. ((not (and (equal current-prefix-arg '(16))
  12444. org-remember-previous-location))
  12445. (org-get-org-file))))
  12446. (heading org-remember-default-headline)
  12447. (visiting (and file (org-find-base-buffer-visiting file)))
  12448. (org-startup-folded nil)
  12449. (org-startup-align-all-tables nil)
  12450. (org-goto-start-pos 1)
  12451. spos exitcmd level indent reversed)
  12452. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12453. (setq file (car org-remember-previous-location)
  12454. heading (cdr org-remember-previous-location)
  12455. fastp t))
  12456. (setq current-prefix-arg nil)
  12457. (if (string-match "[ \t\n]+\\'" txt)
  12458. (setq txt (replace-match "" t t txt)))
  12459. ;; Modify text so that it becomes a nice subtree which can be inserted
  12460. ;; into an org tree.
  12461. (let* ((lines (split-string txt "\n"))
  12462. first)
  12463. (setq first (car lines) lines (cdr lines))
  12464. (if (string-match "^\\*+ " first)
  12465. ;; Is already a headline
  12466. (setq indent nil)
  12467. ;; We need to add a headline: Use time and first buffer line
  12468. (setq lines (cons first lines)
  12469. first (concat "* " (current-time-string)
  12470. " (" (remember-buffer-desc) ")")
  12471. indent " "))
  12472. (if (and org-adapt-indentation indent)
  12473. (setq lines (mapcar
  12474. (lambda (x)
  12475. (if (string-match "\\S-" x)
  12476. (concat indent x) x))
  12477. lines)))
  12478. (setq txt (concat first "\n"
  12479. (mapconcat 'identity lines "\n"))))
  12480. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12481. (setq txt (replace-match "\n\n" t t txt))
  12482. (if (string-match "[ \t\n]*\\'" txt)
  12483. (setq txt (replace-match "\n" t t txt))))
  12484. ;; Put the modified text back into the remember buffer, for refile.
  12485. (erase-buffer)
  12486. (insert txt)
  12487. (goto-char (point-min))
  12488. (when (and (eq org-remember-interactive-interface 'refile)
  12489. (not fastp))
  12490. (org-refile nil (or visiting (find-file-noselect file)))
  12491. (throw 'quit t))
  12492. ;; Find the file
  12493. (if (not visiting) (find-file-noselect file))
  12494. (with-current-buffer (or visiting (get-file-buffer file))
  12495. (unless (org-mode-p)
  12496. (error "Target files for remember notes must be in Org-mode"))
  12497. (save-excursion
  12498. (save-restriction
  12499. (widen)
  12500. (and (goto-char (point-min))
  12501. (not (re-search-forward "^\\* " nil t))
  12502. (insert "\n* " (or heading "Notes") "\n"))
  12503. (setq reversed (org-notes-order-reversed-p))
  12504. ;; Find the default location
  12505. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12506. (goto-char (point-min))
  12507. (if (re-search-forward
  12508. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12509. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12510. nil t)
  12511. (setq org-goto-start-pos (match-beginning 0))
  12512. (when fastp
  12513. (goto-char (point-max))
  12514. (unless (bolp) (newline))
  12515. (insert "* " heading "\n")
  12516. (setq org-goto-start-pos (point-at-bol 0)))))
  12517. ;; Ask the User for a location, using the appropriate interface
  12518. (cond
  12519. (fastp (setq spos org-goto-start-pos
  12520. exitcmd 'return))
  12521. ((eq org-remember-interactive-interface 'outline)
  12522. (setq spos (org-get-location (current-buffer)
  12523. org-remember-help)
  12524. exitcmd (cdr spos)
  12525. spos (car spos)))
  12526. ((eq org-remember-interactive-interface 'outline-path-completion)
  12527. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12528. (org-refile-use-outline-path t))
  12529. (setq spos (org-refile-get-location "Heading: ")
  12530. exitcmd 'return
  12531. spos (nth 3 spos))))
  12532. (t (error "this should not hapen")))
  12533. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12534. ; not handle this note
  12535. (goto-char spos)
  12536. (cond ((org-on-heading-p t)
  12537. (org-back-to-heading t)
  12538. (setq level (funcall outline-level))
  12539. (cond
  12540. ((eq exitcmd 'return)
  12541. ;; sublevel of current
  12542. (setq org-remember-previous-location
  12543. (cons (abbreviate-file-name file)
  12544. (org-get-heading 'notags)))
  12545. (if reversed
  12546. (outline-next-heading)
  12547. (org-end-of-subtree t)
  12548. (if (not (bolp))
  12549. (if (looking-at "[ \t]*\n")
  12550. (beginning-of-line 2)
  12551. (end-of-line 1)
  12552. (insert "\n"))))
  12553. (bookmark-set "org-remember-last-stored")
  12554. (org-paste-subtree (org-get-valid-level level 1) txt))
  12555. ((eq exitcmd 'left)
  12556. ;; before current
  12557. (bookmark-set "org-remember-last-stored")
  12558. (org-paste-subtree level txt))
  12559. ((eq exitcmd 'right)
  12560. ;; after current
  12561. (org-end-of-subtree t)
  12562. (bookmark-set "org-remember-last-stored")
  12563. (org-paste-subtree level txt))
  12564. (t (error "This should not happen"))))
  12565. ((and (bobp) (not reversed))
  12566. ;; Put it at the end, one level below level 1
  12567. (save-restriction
  12568. (widen)
  12569. (goto-char (point-max))
  12570. (if (not (bolp)) (newline))
  12571. (bookmark-set "org-remember-last-stored")
  12572. (org-paste-subtree (org-get-valid-level 1 1) txt)))
  12573. ((and (bobp) reversed)
  12574. ;; Put it at the start, as level 1
  12575. (save-restriction
  12576. (widen)
  12577. (goto-char (point-min))
  12578. (re-search-forward "^\\*+ " nil t)
  12579. (beginning-of-line 1)
  12580. (bookmark-set "org-remember-last-stored")
  12581. (org-paste-subtree 1 txt)))
  12582. (t
  12583. ;; Put it right there, with automatic level determined by
  12584. ;; org-paste-subtree or from prefix arg
  12585. (bookmark-set "org-remember-last-stored")
  12586. (org-paste-subtree
  12587. (if (numberp current-prefix-arg) current-prefix-arg)
  12588. txt)))
  12589. (when remember-save-after-remembering
  12590. (save-buffer)
  12591. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12592. t) ;; return t to indicate that we took care of this note.
  12593. (defun org-get-org-file ()
  12594. "Read a filename, with default directory `org-directory'."
  12595. (let ((default (or org-default-notes-file remember-data-file)))
  12596. (read-file-name (format "File name [%s]: " default)
  12597. (file-name-as-directory org-directory)
  12598. default)))
  12599. (defun org-notes-order-reversed-p ()
  12600. "Check if the current file should receive notes in reversed order."
  12601. (cond
  12602. ((not org-reverse-note-order) nil)
  12603. ((eq t org-reverse-note-order) t)
  12604. ((not (listp org-reverse-note-order)) nil)
  12605. (t (catch 'exit
  12606. (let ((all org-reverse-note-order)
  12607. entry)
  12608. (while (setq entry (pop all))
  12609. (if (string-match (car entry) buffer-file-name)
  12610. (throw 'exit (cdr entry))))
  12611. nil)))))
  12612. ;;; Refiling
  12613. (defvar org-refile-target-table nil
  12614. "The list of refile targets, created by `org-refile'.")
  12615. (defvar org-agenda-new-buffers nil
  12616. "Buffers created to visit agenda files.")
  12617. (defun org-get-refile-targets (&optional default-buffer)
  12618. "Produce a table with refile targets."
  12619. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12620. targets txt re files f desc descre)
  12621. (with-current-buffer (or default-buffer (current-buffer))
  12622. (while (setq entry (pop entries))
  12623. (setq files (car entry) desc (cdr entry))
  12624. (cond
  12625. ((null files) (setq files (list (current-buffer))))
  12626. ((eq files 'org-agenda-files)
  12627. (setq files (org-agenda-files 'unrestricted)))
  12628. ((and (symbolp files) (fboundp files))
  12629. (setq files (funcall files)))
  12630. ((and (symbolp files) (boundp files))
  12631. (setq files (symbol-value files))))
  12632. (if (stringp files) (setq files (list files)))
  12633. (cond
  12634. ((eq (car desc) :tag)
  12635. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12636. ((eq (car desc) :todo)
  12637. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12638. ((eq (car desc) :regexp)
  12639. (setq descre (cdr desc)))
  12640. ((eq (car desc) :level)
  12641. (setq descre (concat "^\\*\\{" (number-to-string
  12642. (if org-odd-levels-only
  12643. (1- (* 2 (cdr desc)))
  12644. (cdr desc)))
  12645. "\\}[ \t]")))
  12646. ((eq (car desc) :maxlevel)
  12647. (setq descre (concat "^\\*\\{1," (number-to-string
  12648. (if org-odd-levels-only
  12649. (1- (* 2 (cdr desc)))
  12650. (cdr desc)))
  12651. "\\}[ \t]")))
  12652. (t (error "Bad refiling target description %s" desc)))
  12653. (while (setq f (pop files))
  12654. (save-excursion
  12655. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12656. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12657. (save-excursion
  12658. (save-restriction
  12659. (widen)
  12660. (goto-char (point-min))
  12661. (while (re-search-forward descre nil t)
  12662. (goto-char (point-at-bol))
  12663. (when (looking-at org-complex-heading-regexp)
  12664. (setq txt (match-string 4)
  12665. re (concat "^" (regexp-quote
  12666. (buffer-substring (match-beginning 1)
  12667. (match-end 4)))))
  12668. (if (match-end 5) (setq re (concat re "[ \t]+"
  12669. (regexp-quote
  12670. (match-string 5)))))
  12671. (setq re (concat re "[ \t]*$"))
  12672. (when org-refile-use-outline-path
  12673. (setq txt (mapconcat 'identity
  12674. (append
  12675. (if (eq org-refile-use-outline-path 'file)
  12676. (list (file-name-nondirectory
  12677. (buffer-file-name (buffer-base-buffer))))
  12678. (if (eq org-refile-use-outline-path 'full-file-path)
  12679. (list (buffer-file-name (buffer-base-buffer)))))
  12680. (org-get-outline-path)
  12681. (list txt))
  12682. "/")))
  12683. (push (list txt f re (point)) targets))
  12684. (goto-char (point-at-eol))))))))
  12685. (nreverse targets))))
  12686. (defun org-get-outline-path ()
  12687. "Return the outline path to the current entry, as a list."
  12688. (let (rtn)
  12689. (save-excursion
  12690. (while (org-up-heading-safe)
  12691. (when (looking-at org-complex-heading-regexp)
  12692. (push (org-match-string-no-properties 4) rtn)))
  12693. rtn)))
  12694. (defvar org-refile-history nil
  12695. "History for refiling operations.")
  12696. (defun org-refile (&optional goto default-buffer)
  12697. "Move the entry at point to another heading.
  12698. The list of target headings is compiled using the information in
  12699. `org-refile-targets', which see. This list is created upon first use, and
  12700. you can update it by calling this command with a double prefix (`C-u C-u').
  12701. FIXME: Can we find a better way of updating?
  12702. At the target location, the entry is filed as a subitem of the target heading.
  12703. Depending on `org-reverse-note-order', the new subitem will either be the
  12704. first of the last subitem.
  12705. With prefix arg GOTO, the command will only visit the target location,
  12706. not actually move anything.
  12707. With a double prefix `C-c C-c', go to the location where the last refiling
  12708. operation has put the subtree.
  12709. With a double prefix argument, the command can be used to jump to any
  12710. heading in the current buffer."
  12711. (interactive "P")
  12712. (let* ((cbuf (current-buffer))
  12713. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12714. pos it nbuf file re level reversed)
  12715. (if (equal goto '(16))
  12716. (org-refile-goto-last-stored)
  12717. (when (setq it (org-refile-get-location
  12718. (if goto "Goto: " "Refile to: ") default-buffer))
  12719. (setq file (nth 1 it)
  12720. re (nth 2 it)
  12721. pos (nth 3 it))
  12722. (setq nbuf (or (find-buffer-visiting file)
  12723. (find-file-noselect file)))
  12724. (if goto
  12725. (progn
  12726. (switch-to-buffer nbuf)
  12727. (goto-char pos)
  12728. (org-show-context 'org-goto))
  12729. (org-copy-special)
  12730. (save-excursion
  12731. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12732. (find-file-noselect file))))
  12733. (setq reversed (org-notes-order-reversed-p))
  12734. (save-excursion
  12735. (save-restriction
  12736. (widen)
  12737. (goto-char pos)
  12738. (looking-at outline-regexp)
  12739. (setq level (org-get-valid-level (funcall outline-level) 1))
  12740. (goto-char
  12741. (if reversed
  12742. (outline-next-heading)
  12743. (or (save-excursion (outline-get-next-sibling))
  12744. (org-end-of-subtree t t)
  12745. (point-max))))
  12746. (bookmark-set "org-refile-last-stored")
  12747. (org-paste-subtree level))))
  12748. (org-cut-special)
  12749. (message "Entry refiled to \"%s\"" (car it)))))))
  12750. (defun org-refile-goto-last-stored ()
  12751. "Go to the location where the last refile was stored."
  12752. (interactive)
  12753. (bookmark-jump "org-refile-last-stored")
  12754. (message "This is the location of the last refile"))
  12755. (defun org-refile-get-location (&optional prompt default-buffer)
  12756. "Prompt the user for a refile location, using PROMPT."
  12757. (let ((org-refile-targets org-refile-targets)
  12758. (org-refile-use-outline-path org-refile-use-outline-path))
  12759. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12760. (unless org-refile-target-table
  12761. (error "No refile targets"))
  12762. (let* ((cbuf (current-buffer))
  12763. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12764. (fname (and filename (file-truename filename)))
  12765. (tbl (mapcar
  12766. (lambda (x)
  12767. (if (not (equal fname (file-truename (nth 1 x))))
  12768. (cons (concat (car x) " (" (file-name-nondirectory
  12769. (nth 1 x)) ")")
  12770. (cdr x))
  12771. x))
  12772. org-refile-target-table))
  12773. (completion-ignore-case t))
  12774. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12775. tbl)))
  12776. ;;;; Dynamic blocks
  12777. (defun org-find-dblock (name)
  12778. "Find the first dynamic block with name NAME in the buffer.
  12779. If not found, stay at current position and return nil."
  12780. (let (pos)
  12781. (save-excursion
  12782. (goto-char (point-min))
  12783. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12784. nil t)
  12785. (match-beginning 0))))
  12786. (if pos (goto-char pos))
  12787. pos))
  12788. (defconst org-dblock-start-re
  12789. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12790. "Matches the startline of a dynamic block, with parameters.")
  12791. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12792. "Matches the end of a dyhamic block.")
  12793. (defun org-create-dblock (plist)
  12794. "Create a dynamic block section, with parameters taken from PLIST.
  12795. PLIST must containe a :name entry which is used as name of the block."
  12796. (unless (bolp) (newline))
  12797. (let ((name (plist-get plist :name)))
  12798. (insert "#+BEGIN: " name)
  12799. (while plist
  12800. (if (eq (car plist) :name)
  12801. (setq plist (cddr plist))
  12802. (insert " " (prin1-to-string (pop plist)))))
  12803. (insert "\n\n#+END:\n")
  12804. (beginning-of-line -2)))
  12805. (defun org-prepare-dblock ()
  12806. "Prepare dynamic block for refresh.
  12807. This empties the block, puts the cursor at the insert position and returns
  12808. the property list including an extra property :name with the block name."
  12809. (unless (looking-at org-dblock-start-re)
  12810. (error "Not at a dynamic block"))
  12811. (let* ((begdel (1+ (match-end 0)))
  12812. (name (org-no-properties (match-string 1)))
  12813. (params (append (list :name name)
  12814. (read (concat "(" (match-string 3) ")")))))
  12815. (unless (re-search-forward org-dblock-end-re nil t)
  12816. (error "Dynamic block not terminated"))
  12817. (setq params
  12818. (append params
  12819. (list :content (buffer-substring
  12820. begdel (match-beginning 0)))))
  12821. (delete-region begdel (match-beginning 0))
  12822. (goto-char begdel)
  12823. (open-line 1)
  12824. params))
  12825. (defun org-map-dblocks (&optional command)
  12826. "Apply COMMAND to all dynamic blocks in the current buffer.
  12827. If COMMAND is not given, use `org-update-dblock'."
  12828. (let ((cmd (or command 'org-update-dblock))
  12829. pos)
  12830. (save-excursion
  12831. (goto-char (point-min))
  12832. (while (re-search-forward org-dblock-start-re nil t)
  12833. (goto-char (setq pos (match-beginning 0)))
  12834. (condition-case nil
  12835. (funcall cmd)
  12836. (error (message "Error during update of dynamic block")))
  12837. (goto-char pos)
  12838. (unless (re-search-forward org-dblock-end-re nil t)
  12839. (error "Dynamic block not terminated"))))))
  12840. (defun org-dblock-update (&optional arg)
  12841. "User command for updating dynamic blocks.
  12842. Update the dynamic block at point. With prefix ARG, update all dynamic
  12843. blocks in the buffer."
  12844. (interactive "P")
  12845. (if arg
  12846. (org-update-all-dblocks)
  12847. (or (looking-at org-dblock-start-re)
  12848. (org-beginning-of-dblock))
  12849. (org-update-dblock)))
  12850. (defun org-update-dblock ()
  12851. "Update the dynamic block at point
  12852. This means to empty the block, parse for parameters and then call
  12853. the correct writing function."
  12854. (save-window-excursion
  12855. (let* ((pos (point))
  12856. (line (org-current-line))
  12857. (params (org-prepare-dblock))
  12858. (name (plist-get params :name))
  12859. (cmd (intern (concat "org-dblock-write:" name))))
  12860. (message "Updating dynamic block `%s' at line %d..." name line)
  12861. (funcall cmd params)
  12862. (message "Updating dynamic block `%s' at line %d...done" name line)
  12863. (goto-char pos))))
  12864. (defun org-beginning-of-dblock ()
  12865. "Find the beginning of the dynamic block at point.
  12866. Error if there is no scuh block at point."
  12867. (let ((pos (point))
  12868. beg)
  12869. (end-of-line 1)
  12870. (if (and (re-search-backward org-dblock-start-re nil t)
  12871. (setq beg (match-beginning 0))
  12872. (re-search-forward org-dblock-end-re nil t)
  12873. (> (match-end 0) pos))
  12874. (goto-char beg)
  12875. (goto-char pos)
  12876. (error "Not in a dynamic block"))))
  12877. (defun org-update-all-dblocks ()
  12878. "Update all dynamic blocks in the buffer.
  12879. This function can be used in a hook."
  12880. (when (org-mode-p)
  12881. (org-map-dblocks 'org-update-dblock)))
  12882. ;;;; Completion
  12883. (defconst org-additional-option-like-keywords
  12884. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12885. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  12886. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  12887. (defun org-complete (&optional arg)
  12888. "Perform completion on word at point.
  12889. At the beginning of a headline, this completes TODO keywords as given in
  12890. `org-todo-keywords'.
  12891. If the current word is preceded by a backslash, completes the TeX symbols
  12892. that are supported for HTML support.
  12893. If the current word is preceded by \"#+\", completes special words for
  12894. setting file options.
  12895. In the line after \"#+STARTUP:, complete valid keywords.\"
  12896. At all other locations, this simply calls the value of
  12897. `org-completion-fallback-command'."
  12898. (interactive "P")
  12899. (org-without-partial-completion
  12900. (catch 'exit
  12901. (let* ((end (point))
  12902. (beg1 (save-excursion
  12903. (skip-chars-backward (org-re "[:alnum:]_@"))
  12904. (point)))
  12905. (beg (save-excursion
  12906. (skip-chars-backward "a-zA-Z0-9_:$")
  12907. (point)))
  12908. (confirm (lambda (x) (stringp (car x))))
  12909. (searchhead (equal (char-before beg) ?*))
  12910. (tag (and (equal (char-before beg1) ?:)
  12911. (equal (char-after (point-at-bol)) ?*)))
  12912. (prop (and (equal (char-before beg1) ?:)
  12913. (not (equal (char-after (point-at-bol)) ?*))))
  12914. (texp (equal (char-before beg) ?\\))
  12915. (link (equal (char-before beg) ?\[))
  12916. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  12917. beg)
  12918. "#+"))
  12919. (startup (string-match "^#\\+STARTUP:.*"
  12920. (buffer-substring (point-at-bol) (point))))
  12921. (completion-ignore-case opt)
  12922. (type nil)
  12923. (tbl nil)
  12924. (table (cond
  12925. (opt
  12926. (setq type :opt)
  12927. (append
  12928. (mapcar
  12929. (lambda (x)
  12930. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  12931. (cons (match-string 2 x) (match-string 1 x)))
  12932. (org-split-string (org-get-current-options) "\n"))
  12933. (mapcar 'list org-additional-option-like-keywords)))
  12934. (startup
  12935. (setq type :startup)
  12936. org-startup-options)
  12937. (link (append org-link-abbrev-alist-local
  12938. org-link-abbrev-alist))
  12939. (texp
  12940. (setq type :tex)
  12941. org-html-entities)
  12942. ((string-match "\\`\\*+[ \t]+\\'"
  12943. (buffer-substring (point-at-bol) beg))
  12944. (setq type :todo)
  12945. (mapcar 'list org-todo-keywords-1))
  12946. (searchhead
  12947. (setq type :searchhead)
  12948. (save-excursion
  12949. (goto-char (point-min))
  12950. (while (re-search-forward org-todo-line-regexp nil t)
  12951. (push (list
  12952. (org-make-org-heading-search-string
  12953. (match-string 3) t))
  12954. tbl)))
  12955. tbl)
  12956. (tag (setq type :tag beg beg1)
  12957. (or org-tag-alist (org-get-buffer-tags)))
  12958. (prop (setq type :prop beg beg1)
  12959. (mapcar 'list (org-buffer-property-keys nil t t)))
  12960. (t (progn
  12961. (call-interactively org-completion-fallback-command)
  12962. (throw 'exit nil)))))
  12963. (pattern (buffer-substring-no-properties beg end))
  12964. (completion (try-completion pattern table confirm)))
  12965. (cond ((eq completion t)
  12966. (if (not (assoc (upcase pattern) table))
  12967. (message "Already complete")
  12968. (if (equal type :opt)
  12969. (insert (substring (cdr (assoc (upcase pattern) table))
  12970. (length pattern)))
  12971. (if (memq type '(:tag :prop)) (insert ":")))))
  12972. ((null completion)
  12973. (message "Can't find completion for \"%s\"" pattern)
  12974. (ding))
  12975. ((not (string= pattern completion))
  12976. (delete-region beg end)
  12977. (if (string-match " +$" completion)
  12978. (setq completion (replace-match "" t t completion)))
  12979. (insert completion)
  12980. (if (get-buffer-window "*Completions*")
  12981. (delete-window (get-buffer-window "*Completions*")))
  12982. (if (assoc completion table)
  12983. (if (eq type :todo) (insert " ")
  12984. (if (memq type '(:tag :prop)) (insert ":"))))
  12985. (if (and (equal type :opt) (assoc completion table))
  12986. (message "%s" (substitute-command-keys
  12987. "Press \\[org-complete] again to insert example settings"))))
  12988. (t
  12989. (message "Making completion list...")
  12990. (let ((list (sort (all-completions pattern table confirm)
  12991. 'string<)))
  12992. (with-output-to-temp-buffer "*Completions*"
  12993. (condition-case nil
  12994. ;; Protection needed for XEmacs and emacs 21
  12995. (display-completion-list list pattern)
  12996. (error (display-completion-list list)))))
  12997. (message "Making completion list...%s" "done")))))))
  12998. ;;;; TODO, DEADLINE, Comments
  12999. (defun org-toggle-comment ()
  13000. "Change the COMMENT state of an entry."
  13001. (interactive)
  13002. (save-excursion
  13003. (org-back-to-heading)
  13004. (let (case-fold-search)
  13005. (if (looking-at (concat outline-regexp
  13006. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13007. (replace-match "" t t nil 1)
  13008. (if (looking-at outline-regexp)
  13009. (progn
  13010. (goto-char (match-end 0))
  13011. (insert org-comment-string " ")))))))
  13012. (defvar org-last-todo-state-is-todo nil
  13013. "This is non-nil when the last TODO state change led to a TODO state.
  13014. If the last change removed the TODO tag or switched to DONE, then
  13015. this is nil.")
  13016. (defvar org-setting-tags nil) ; dynamically skiped
  13017. ;; FIXME: better place
  13018. (defun org-property-or-variable-value (var &optional inherit)
  13019. "Check if there is a property fixing the value of VAR.
  13020. If yes, return this value. If not, return the current value of the variable."
  13021. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13022. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13023. (read prop)
  13024. (symbol-value var))))
  13025. (defun org-parse-local-options (string var)
  13026. "Parse STRING for startup setting relevant for variable VAR."
  13027. (let ((rtn (symbol-value var))
  13028. e opts)
  13029. (save-match-data
  13030. (if (or (not string) (not (string-match "\\S-" string)))
  13031. rtn
  13032. (setq opts (delq nil (mapcar (lambda (x)
  13033. (setq e (assoc x org-startup-options))
  13034. (if (eq (nth 1 e) var) e nil))
  13035. (org-split-string string "[ \t]+"))))
  13036. (if (not opts)
  13037. rtn
  13038. (setq rtn nil)
  13039. (while (setq e (pop opts))
  13040. (if (not (nth 3 e))
  13041. (setq rtn (nth 2 e))
  13042. (if (not (listp rtn)) (setq rtn nil))
  13043. (push (nth 2 e) rtn)))
  13044. rtn)))))
  13045. (defvar org-blocker-hook nil
  13046. "Hook for functions that are allowed to block a state change.
  13047. Each function gets as its single argument a property list, see
  13048. `org-trigger-hook' for more information about this list.
  13049. If any of the functions in this hook returns nil, the state change
  13050. is blocked.")
  13051. (defvar org-trigger-hook nil
  13052. "Hook for functions that are triggered by a state change.
  13053. Each function gets as its single argument a property list with at least
  13054. the following elements:
  13055. (:type type-of-change :position pos-at-entry-start
  13056. :from old-state :to new-state)
  13057. Depending on the type, more properties may be present.
  13058. This mechanism is currently implemented for:
  13059. TODO state changes
  13060. ------------------
  13061. :type todo-state-change
  13062. :from previous state (keyword as a string), or nil
  13063. :to new state (keyword as a string), or nil")
  13064. (defun org-todo (&optional arg)
  13065. "Change the TODO state of an item.
  13066. The state of an item is given by a keyword at the start of the heading,
  13067. like
  13068. *** TODO Write paper
  13069. *** DONE Call mom
  13070. The different keywords are specified in the variable `org-todo-keywords'.
  13071. By default the available states are \"TODO\" and \"DONE\".
  13072. So for this example: when the item starts with TODO, it is changed to DONE.
  13073. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13074. DONE are present, add TODO at the beginning of the heading.
  13075. With C-u prefix arg, use completion to determine the new state.
  13076. With numeric prefix arg, switch to that state.
  13077. For calling through lisp, arg is also interpreted in the following way:
  13078. 'none -> empty state
  13079. \"\"(empty string) -> switch to empty state
  13080. 'done -> switch to DONE
  13081. 'nextset -> switch to the next set of keywords
  13082. 'previousset -> switch to the previous set of keywords
  13083. \"WAITING\" -> switch to the specified keyword, but only if it
  13084. really is a member of `org-todo-keywords'."
  13085. (interactive "P")
  13086. (save-excursion
  13087. (catch 'exit
  13088. (org-back-to-heading)
  13089. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13090. (or (looking-at (concat " +" org-todo-regexp " *"))
  13091. (looking-at " *"))
  13092. (let* ((match-data (match-data))
  13093. (startpos (point-at-bol))
  13094. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13095. (org-log-done org-log-done)
  13096. (org-log-repeat org-log-repeat)
  13097. (org-todo-log-states org-todo-log-states)
  13098. (this (match-string 1))
  13099. (hl-pos (match-beginning 0))
  13100. (head (org-get-todo-sequence-head this))
  13101. (ass (assoc head org-todo-kwd-alist))
  13102. (interpret (nth 1 ass))
  13103. (done-word (nth 3 ass))
  13104. (final-done-word (nth 4 ass))
  13105. (last-state (or this ""))
  13106. (completion-ignore-case t)
  13107. (member (member this org-todo-keywords-1))
  13108. (tail (cdr member))
  13109. (state (cond
  13110. ((and org-todo-key-trigger
  13111. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13112. (and (not arg) org-use-fast-todo-selection
  13113. (not (eq org-use-fast-todo-selection 'prefix)))))
  13114. ;; Use fast selection
  13115. (org-fast-todo-selection))
  13116. ((and (equal arg '(4))
  13117. (or (not org-use-fast-todo-selection)
  13118. (not org-todo-key-trigger)))
  13119. ;; Read a state with completion
  13120. (completing-read "State: " (mapcar (lambda(x) (list x))
  13121. org-todo-keywords-1)
  13122. nil t))
  13123. ((eq arg 'right)
  13124. (if this
  13125. (if tail (car tail) nil)
  13126. (car org-todo-keywords-1)))
  13127. ((eq arg 'left)
  13128. (if (equal member org-todo-keywords-1)
  13129. nil
  13130. (if this
  13131. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13132. org-todo-keywords-1)
  13133. (org-last org-todo-keywords-1))))
  13134. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13135. (setq arg nil))) ; hack to fall back to cycling
  13136. (arg
  13137. ;; user or caller requests a specific state
  13138. (cond
  13139. ((equal arg "") nil)
  13140. ((eq arg 'none) nil)
  13141. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13142. ((eq arg 'nextset)
  13143. (or (car (cdr (member head org-todo-heads)))
  13144. (car org-todo-heads)))
  13145. ((eq arg 'previousset)
  13146. (let ((org-todo-heads (reverse org-todo-heads)))
  13147. (or (car (cdr (member head org-todo-heads)))
  13148. (car org-todo-heads))))
  13149. ((car (member arg org-todo-keywords-1)))
  13150. ((nth (1- (prefix-numeric-value arg))
  13151. org-todo-keywords-1))))
  13152. ((null member) (or head (car org-todo-keywords-1)))
  13153. ((equal this final-done-word) nil) ;; -> make empty
  13154. ((null tail) nil) ;; -> first entry
  13155. ((eq interpret 'sequence)
  13156. (car tail))
  13157. ((memq interpret '(type priority))
  13158. (if (eq this-command last-command)
  13159. (car tail)
  13160. (if (> (length tail) 0)
  13161. (or done-word (car org-done-keywords))
  13162. nil)))
  13163. (t nil)))
  13164. (next (if state (concat " " state " ") " "))
  13165. (change-plist (list :type 'todo-state-change :from this :to state
  13166. :position startpos))
  13167. dolog now-done-p)
  13168. (when org-blocker-hook
  13169. (unless (save-excursion
  13170. (save-match-data
  13171. (run-hook-with-args-until-failure
  13172. 'org-blocker-hook change-plist)))
  13173. (if (interactive-p)
  13174. (error "TODO state change from %s to %s blocked" this state)
  13175. ;; fail silently
  13176. (message "TODO state change from %s to %s blocked" this state)
  13177. (throw 'exit nil))))
  13178. (store-match-data match-data)
  13179. (replace-match next t t)
  13180. (unless (pos-visible-in-window-p hl-pos)
  13181. (message "TODO state changed to %s" (org-trim next)))
  13182. (unless head
  13183. (setq head (org-get-todo-sequence-head state)
  13184. ass (assoc head org-todo-kwd-alist)
  13185. interpret (nth 1 ass)
  13186. done-word (nth 3 ass)
  13187. final-done-word (nth 4 ass)))
  13188. (when (memq arg '(nextset previousset))
  13189. (message "Keyword-Set %d/%d: %s"
  13190. (- (length org-todo-sets) -1
  13191. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13192. (length org-todo-sets)
  13193. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13194. (setq org-last-todo-state-is-todo
  13195. (not (member state org-done-keywords)))
  13196. (setq now-done-p (and (member state org-done-keywords)
  13197. (not (member this org-done-keywords))))
  13198. (and logging (org-local-logging logging))
  13199. (when (and (or org-todo-log-states org-log-done)
  13200. (not (memq arg '(nextset previousset))))
  13201. ;; we need to look at recording a time and note
  13202. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13203. (nth 2 (assoc this org-todo-log-states))))
  13204. (when (and state
  13205. (member state org-not-done-keywords)
  13206. (not (member this org-not-done-keywords)))
  13207. ;; This is now a todo state and was not one before
  13208. ;; If there was a CLOSED time stamp, get rid of it.
  13209. (org-add-planning-info nil nil 'closed))
  13210. (when (and now-done-p org-log-done)
  13211. ;; It is now done, and it was not done before
  13212. (org-add-planning-info 'closed (org-current-time))
  13213. (if (and (not dolog) (eq 'note org-log-done))
  13214. (org-add-log-maybe 'done state 'findpos 'note)))
  13215. (when (and state dolog)
  13216. ;; This is a non-nil state, and we need to log it
  13217. (org-add-log-maybe 'state state 'findpos dolog)))
  13218. ;; Fixup tag positioning
  13219. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13220. (run-hooks 'org-after-todo-state-change-hook)
  13221. (if (and arg (not (member state org-done-keywords)))
  13222. (setq head (org-get-todo-sequence-head state)))
  13223. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13224. ;; Do we need to trigger a repeat?
  13225. (when now-done-p (org-auto-repeat-maybe state))
  13226. ;; Fixup cursor location if close to the keyword
  13227. (if (and (outline-on-heading-p)
  13228. (not (bolp))
  13229. (save-excursion (beginning-of-line 1)
  13230. (looking-at org-todo-line-regexp))
  13231. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13232. (progn
  13233. (goto-char (or (match-end 2) (match-end 1)))
  13234. (just-one-space)))
  13235. (when org-trigger-hook
  13236. (save-excursion
  13237. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13238. (defun org-local-logging (value)
  13239. "Get logging settings from a property VALUE."
  13240. (let* (words w a)
  13241. ;; directly set the variables, they are already local.
  13242. (setq org-log-done nil
  13243. org-log-repeat nil
  13244. org-todo-log-states nil)
  13245. (setq words (org-split-string value))
  13246. (while (setq w (pop words))
  13247. (cond
  13248. ((setq a (assoc w org-startup-options))
  13249. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13250. (set (nth 1 a) (nth 2 a))))
  13251. ((setq a (org-extract-log-state-settings w))
  13252. (and (member (car a) org-todo-keywords-1)
  13253. (push a org-todo-log-states)))))))
  13254. (defun org-get-todo-sequence-head (kwd)
  13255. "Return the head of the TODO sequence to which KWD belongs.
  13256. If KWD is not set, check if there is a text property remembering the
  13257. right sequence."
  13258. (let (p)
  13259. (cond
  13260. ((not kwd)
  13261. (or (get-text-property (point-at-bol) 'org-todo-head)
  13262. (progn
  13263. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13264. nil (point-at-eol)))
  13265. (get-text-property p 'org-todo-head))))
  13266. ((not (member kwd org-todo-keywords-1))
  13267. (car org-todo-keywords-1))
  13268. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13269. (defun org-fast-todo-selection ()
  13270. "Fast TODO keyword selection with single keys.
  13271. Returns the new TODO keyword, or nil if no state change should occur."
  13272. (let* ((fulltable org-todo-key-alist)
  13273. (done-keywords org-done-keywords) ;; needed for the faces.
  13274. (maxlen (apply 'max (mapcar
  13275. (lambda (x)
  13276. (if (stringp (car x)) (string-width (car x)) 0))
  13277. fulltable)))
  13278. (expert nil)
  13279. (fwidth (+ maxlen 3 1 3))
  13280. (ncol (/ (- (window-width) 4) fwidth))
  13281. tg cnt e c tbl
  13282. groups ingroup)
  13283. (save-window-excursion
  13284. (if expert
  13285. (set-buffer (get-buffer-create " *Org todo*"))
  13286. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13287. (erase-buffer)
  13288. (org-set-local 'org-done-keywords done-keywords)
  13289. (setq tbl fulltable cnt 0)
  13290. (while (setq e (pop tbl))
  13291. (cond
  13292. ((equal e '(:startgroup))
  13293. (push '() groups) (setq ingroup t)
  13294. (when (not (= cnt 0))
  13295. (setq cnt 0)
  13296. (insert "\n"))
  13297. (insert "{ "))
  13298. ((equal e '(:endgroup))
  13299. (setq ingroup nil cnt 0)
  13300. (insert "}\n"))
  13301. (t
  13302. (setq tg (car e) c (cdr e))
  13303. (if ingroup (push tg (car groups)))
  13304. (setq tg (org-add-props tg nil 'face
  13305. (org-get-todo-face tg)))
  13306. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13307. (insert "[" c "] " tg (make-string
  13308. (- fwidth 4 (length tg)) ?\ ))
  13309. (when (= (setq cnt (1+ cnt)) ncol)
  13310. (insert "\n")
  13311. (if ingroup (insert " "))
  13312. (setq cnt 0)))))
  13313. (insert "\n")
  13314. (goto-char (point-min))
  13315. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13316. (fit-window-to-buffer))
  13317. (message "[a-z..]:Set [SPC]:clear")
  13318. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13319. (cond
  13320. ((or (= c ?\C-g)
  13321. (and (= c ?q) (not (rassoc c fulltable))))
  13322. (setq quit-flag t))
  13323. ((= c ?\ ) nil)
  13324. ((setq e (rassoc c fulltable) tg (car e))
  13325. tg)
  13326. (t (setq quit-flag t))))))
  13327. (defun org-get-repeat ()
  13328. "Check if tere is a deadline/schedule with repeater in this entry."
  13329. (save-match-data
  13330. (save-excursion
  13331. (org-back-to-heading t)
  13332. (if (re-search-forward
  13333. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13334. (match-string 1)))))
  13335. (defvar org-last-changed-timestamp)
  13336. (defvar org-log-post-message)
  13337. (defvar org-log-note-purpose)
  13338. (defun org-auto-repeat-maybe (done-word)
  13339. "Check if the current headline contains a repeated deadline/schedule.
  13340. If yes, set TODO state back to what it was and change the base date
  13341. of repeating deadline/scheduled time stamps to new date.
  13342. This function is run automatically after each state change to a DONE state."
  13343. ;; last-state is dynamically scoped into this function
  13344. (let* ((repeat (org-get-repeat))
  13345. (aa (assoc last-state org-todo-kwd-alist))
  13346. (interpret (nth 1 aa))
  13347. (head (nth 2 aa))
  13348. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13349. (msg "Entry repeats: ")
  13350. (org-log-done nil)
  13351. (org-todo-log-states nil)
  13352. (nshiftmax 10) (nshift 0)
  13353. re type n what ts mb0 time)
  13354. (when repeat
  13355. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13356. (org-todo (if (eq interpret 'type) last-state head))
  13357. (when (and org-log-repeat
  13358. (or (not (memq 'org-add-log-note
  13359. (default-value 'post-command-hook)))
  13360. (eq org-log-note-purpose 'done)))
  13361. ;; Make sure a note is taken;
  13362. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13363. 'findpos org-log-repeat))
  13364. (org-back-to-heading t)
  13365. (org-add-planning-info nil nil 'closed)
  13366. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13367. org-deadline-time-regexp "\\)\\|\\("
  13368. org-ts-regexp "\\)"))
  13369. (while (re-search-forward
  13370. re (save-excursion (outline-next-heading) (point)) t)
  13371. (setq type (if (match-end 1) org-scheduled-string
  13372. (if (match-end 3) org-deadline-string "Plain:"))
  13373. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  13374. mb0 (match-beginning 0))
  13375. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  13376. (setq n (string-to-number (match-string 2 ts))
  13377. what (match-string 3 ts))
  13378. (if (equal what "w") (setq n (* n 7) what "d"))
  13379. ;; Preparation, see if we need to modify the start date for the change
  13380. (when (match-end 1)
  13381. (setq time (save-match-data (org-time-string-to-time ts)))
  13382. (cond
  13383. ((equal (match-string 1 ts) ".")
  13384. ;; Shift starting date to today
  13385. (org-timestamp-change
  13386. (- (time-to-days (current-time)) (time-to-days time))
  13387. 'day))
  13388. ((equal (match-string 1 ts) "+")
  13389. (while (< (time-to-days time) (time-to-days (current-time)))
  13390. (when (= (incf nshift) nshiftmax)
  13391. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  13392. (error "Abort")))
  13393. (org-timestamp-change n (cdr (assoc what whata)))
  13394. (sit-for .0001) ;; so we can watch the date shifting
  13395. (org-at-timestamp-p t)
  13396. (setq ts (match-string 1))
  13397. (setq time (save-match-data (org-time-string-to-time ts))))
  13398. (org-timestamp-change (- n) (cdr (assoc what whata)))
  13399. ;; rematch, so that we have everything in place for the real shift
  13400. (org-at-timestamp-p t)
  13401. (setq ts (match-string 1))
  13402. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  13403. (org-timestamp-change n (cdr (assoc what whata)))
  13404. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13405. (setq org-log-post-message msg)
  13406. (message "%s" msg))))
  13407. (defun org-show-todo-tree (arg)
  13408. "Make a compact tree which shows all headlines marked with TODO.
  13409. The tree will show the lines where the regexp matches, and all higher
  13410. headlines above the match.
  13411. With a \\[universal-argument] prefix, also show the DONE entries.
  13412. With a numeric prefix N, construct a sparse tree for the Nth element
  13413. of `org-todo-keywords-1'."
  13414. (interactive "P")
  13415. (let ((case-fold-search nil)
  13416. (kwd-re
  13417. (cond ((null arg) org-not-done-regexp)
  13418. ((equal arg '(4))
  13419. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13420. (mapcar 'list org-todo-keywords-1))))
  13421. (concat "\\("
  13422. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13423. "\\)\\>")))
  13424. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13425. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13426. org-todo-keywords-1)))
  13427. (t (error "Invalid prefix argument: %s" arg)))))
  13428. (message "%d TODO entries found"
  13429. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13430. (defun org-deadline (&optional remove)
  13431. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13432. With argument REMOVE, remove any deadline from the item."
  13433. (interactive "P")
  13434. (if remove
  13435. (progn
  13436. (org-remove-timestamp-with-keyword org-deadline-string)
  13437. (message "Item no longer has a deadline."))
  13438. (org-add-planning-info 'deadline nil 'closed)))
  13439. (defun org-schedule (&optional remove)
  13440. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13441. With argument REMOVE, remove any scheduling date from the item."
  13442. (interactive "P")
  13443. (if remove
  13444. (progn
  13445. (org-remove-timestamp-with-keyword org-scheduled-string)
  13446. (message "Item is no longer scheduled."))
  13447. (org-add-planning-info 'scheduled nil 'closed)))
  13448. (defun org-remove-timestamp-with-keyword (keyword)
  13449. "Remove all time stamps with KEYWORD in the current entry."
  13450. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13451. beg)
  13452. (save-excursion
  13453. (org-back-to-heading t)
  13454. (setq beg (point))
  13455. (org-end-of-subtree t t)
  13456. (while (re-search-backward re beg t)
  13457. (replace-match "")
  13458. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13459. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13460. (defun org-add-planning-info (what &optional time &rest remove)
  13461. "Insert new timestamp with keyword in the line directly after the headline.
  13462. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13463. If non is given, the user is prompted for a date.
  13464. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13465. be removed."
  13466. (interactive)
  13467. (let (org-time-was-given org-end-time-was-given ts
  13468. end default-time default-input)
  13469. (when (and (not time) (memq what '(scheduled deadline)))
  13470. ;; Try to get a default date/time from existing timestamp
  13471. (save-excursion
  13472. (org-back-to-heading t)
  13473. (setq end (save-excursion (outline-next-heading) (point)))
  13474. (when (re-search-forward (if (eq what 'scheduled)
  13475. org-scheduled-time-regexp
  13476. org-deadline-time-regexp)
  13477. end t)
  13478. (setq ts (match-string 1)
  13479. default-time
  13480. (apply 'encode-time (org-parse-time-string ts))
  13481. default-input (and ts (org-get-compact-tod ts))))))
  13482. (when what
  13483. ;; If necessary, get the time from the user
  13484. (setq time (or time (org-read-date nil 'to-time nil nil
  13485. default-time default-input))))
  13486. (when (and org-insert-labeled-timestamps-at-point
  13487. (member what '(scheduled deadline)))
  13488. (insert
  13489. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13490. (org-insert-time-stamp time org-time-was-given
  13491. nil nil nil (list org-end-time-was-given))
  13492. (setq what nil))
  13493. (save-excursion
  13494. (save-restriction
  13495. (let (col list elt ts buffer-invisibility-spec)
  13496. (org-back-to-heading t)
  13497. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13498. (goto-char (match-end 1))
  13499. (setq col (current-column))
  13500. (goto-char (match-end 0))
  13501. (if (eobp) (insert "\n") (forward-char 1))
  13502. (if (and (not (looking-at outline-regexp))
  13503. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13504. "[^\r\n]*"))
  13505. (not (equal (match-string 1) org-clock-string)))
  13506. (narrow-to-region (match-beginning 0) (match-end 0))
  13507. (insert-before-markers "\n")
  13508. (backward-char 1)
  13509. (narrow-to-region (point) (point))
  13510. (indent-to-column col))
  13511. ;; Check if we have to remove something.
  13512. (setq list (cons what remove))
  13513. (while list
  13514. (setq elt (pop list))
  13515. (goto-char (point-min))
  13516. (when (or (and (eq elt 'scheduled)
  13517. (re-search-forward org-scheduled-time-regexp nil t))
  13518. (and (eq elt 'deadline)
  13519. (re-search-forward org-deadline-time-regexp nil t))
  13520. (and (eq elt 'closed)
  13521. (re-search-forward org-closed-time-regexp nil t)))
  13522. (replace-match "")
  13523. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13524. (if (looking-at " +") (replace-match ""))))
  13525. (goto-char (point-max))
  13526. (when what
  13527. (insert
  13528. (if (not (equal (char-before) ?\ )) " " "")
  13529. (cond ((eq what 'scheduled) org-scheduled-string)
  13530. ((eq what 'deadline) org-deadline-string)
  13531. ((eq what 'closed) org-closed-string))
  13532. " ")
  13533. (setq ts (org-insert-time-stamp
  13534. time
  13535. (or org-time-was-given
  13536. (and (eq what 'closed) org-log-done-with-time))
  13537. (eq what 'closed)
  13538. nil nil (list org-end-time-was-given)))
  13539. (end-of-line 1))
  13540. (goto-char (point-min))
  13541. (widen)
  13542. (if (looking-at "[ \t]+\r?\n")
  13543. (replace-match ""))
  13544. ts)))))
  13545. (defvar org-log-note-marker (make-marker))
  13546. (defvar org-log-note-purpose nil)
  13547. (defvar org-log-note-state nil)
  13548. (defvar org-log-note-how nil)
  13549. (defvar org-log-note-window-configuration nil)
  13550. (defvar org-log-note-return-to (make-marker))
  13551. (defvar org-log-post-message nil
  13552. "Message to be displayed after a log note has been stored.
  13553. The auto-repeater uses this.")
  13554. (defun org-add-log-maybe (&optional purpose state findpos how)
  13555. "Set up the post command hook to take a note.
  13556. If this is about to TODO state change, the new state is expected in STATE.
  13557. When FINDPOS is non-nil, find the correct position for the note in
  13558. the current entry. If not, assume that it can be inserted at point."
  13559. (save-excursion
  13560. (when findpos
  13561. (org-back-to-heading t)
  13562. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13563. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13564. "[^\r\n]*\\)?"))
  13565. (goto-char (match-end 0))
  13566. (unless org-log-states-order-reversed
  13567. (and (= (char-after) ?\n) (forward-char 1))
  13568. (org-skip-over-state-notes)
  13569. (skip-chars-backward " \t\n\r")))
  13570. (move-marker org-log-note-marker (point))
  13571. (setq org-log-note-purpose purpose
  13572. org-log-note-state state
  13573. org-log-note-how how)
  13574. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13575. (defun org-skip-over-state-notes ()
  13576. "Skip past the list of State notes in an entry."
  13577. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13578. (while (looking-at "[ \t]*- State")
  13579. (condition-case nil
  13580. (org-next-item)
  13581. (error (org-end-of-item)))))
  13582. (defun org-add-log-note (&optional purpose)
  13583. "Pop up a window for taking a note, and add this note later at point."
  13584. (remove-hook 'post-command-hook 'org-add-log-note)
  13585. (setq org-log-note-window-configuration (current-window-configuration))
  13586. (delete-other-windows)
  13587. (move-marker org-log-note-return-to (point))
  13588. (switch-to-buffer (marker-buffer org-log-note-marker))
  13589. (goto-char org-log-note-marker)
  13590. (org-switch-to-buffer-other-window "*Org Note*")
  13591. (erase-buffer)
  13592. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13593. (org-store-log-note)
  13594. (let ((org-inhibit-startup t)) (org-mode))
  13595. (insert (format "# Insert note for %s.
  13596. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13597. (cond
  13598. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13599. ((eq org-log-note-purpose 'done) "closed todo item")
  13600. ((eq org-log-note-purpose 'state)
  13601. (format "state change to \"%s\"" org-log-note-state))
  13602. (t (error "This should not happen")))))
  13603. (org-set-local 'org-finish-function 'org-store-log-note)))
  13604. (defun org-store-log-note ()
  13605. "Finish taking a log note, and insert it to where it belongs."
  13606. (let ((txt (buffer-string))
  13607. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13608. lines ind)
  13609. (kill-buffer (current-buffer))
  13610. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13611. (setq txt (replace-match "" t t txt)))
  13612. (if (string-match "\\s-+\\'" txt)
  13613. (setq txt (replace-match "" t t txt)))
  13614. (setq lines (org-split-string txt "\n"))
  13615. (when (and note (string-match "\\S-" note))
  13616. (setq note
  13617. (org-replace-escapes
  13618. note
  13619. (list (cons "%u" (user-login-name))
  13620. (cons "%U" user-full-name)
  13621. (cons "%t" (format-time-string
  13622. (org-time-stamp-format 'long 'inactive)
  13623. (current-time)))
  13624. (cons "%s" (if org-log-note-state
  13625. (concat "\"" org-log-note-state "\"")
  13626. "")))))
  13627. (if lines (setq note (concat note " \\\\")))
  13628. (push note lines))
  13629. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13630. (when lines
  13631. (save-excursion
  13632. (set-buffer (marker-buffer org-log-note-marker))
  13633. (save-excursion
  13634. (goto-char org-log-note-marker)
  13635. (move-marker org-log-note-marker nil)
  13636. (end-of-line 1)
  13637. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13638. (indent-relative nil)
  13639. (insert "- " (pop lines))
  13640. (org-indent-line-function)
  13641. (beginning-of-line 1)
  13642. (looking-at "[ \t]*")
  13643. (setq ind (concat (match-string 0) " "))
  13644. (end-of-line 1)
  13645. (while lines (insert "\n" ind (pop lines)))))))
  13646. (set-window-configuration org-log-note-window-configuration)
  13647. (with-current-buffer (marker-buffer org-log-note-return-to)
  13648. (goto-char org-log-note-return-to))
  13649. (move-marker org-log-note-return-to nil)
  13650. (and org-log-post-message (message "%s" org-log-post-message)))
  13651. ;; FIXME: what else would be useful?
  13652. ;; - priority
  13653. ;; - date
  13654. (defun org-sparse-tree (&optional arg)
  13655. "Create a sparse tree, prompt for the details.
  13656. This command can create sparse trees. You first need to select the type
  13657. of match used to create the tree:
  13658. t Show entries with a specific TODO keyword.
  13659. T Show entries selected by a tags match.
  13660. p Enter a property name and its value (both with completion on existing
  13661. names/values) and show entries with that property.
  13662. r Show entries matching a regular expression
  13663. d Show deadlines due within `org-deadline-warning-days'."
  13664. (interactive "P")
  13665. (let (ans kwd value)
  13666. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13667. (setq ans (read-char-exclusive))
  13668. (cond
  13669. ((equal ans ?d)
  13670. (call-interactively 'org-check-deadlines))
  13671. ((equal ans ?b)
  13672. (call-interactively 'org-check-before-date))
  13673. ((equal ans ?t)
  13674. (org-show-todo-tree '(4)))
  13675. ((equal ans ?T)
  13676. (call-interactively 'org-tags-sparse-tree))
  13677. ((member ans '(?p ?P))
  13678. (setq kwd (completing-read "Property: "
  13679. (mapcar 'list (org-buffer-property-keys))))
  13680. (setq value (completing-read "Value: "
  13681. (mapcar 'list (org-property-values kwd))))
  13682. (unless (string-match "\\`{.*}\\'" value)
  13683. (setq value (concat "\"" value "\"")))
  13684. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13685. ((member ans '(?r ?R ?/))
  13686. (call-interactively 'org-occur))
  13687. (t (error "No such sparse tree command \"%c\"" ans)))))
  13688. (defvar org-occur-highlights nil
  13689. "List of overlays used for occur matches.")
  13690. (make-variable-buffer-local 'org-occur-highlights)
  13691. (defvar org-occur-parameters nil
  13692. "Parameters of the active org-occur calls.
  13693. This is a list, each call to org-occur pushes as cons cell,
  13694. containing the regular expression and the callback, onto the list.
  13695. The list can contain several entries if `org-occur' has been called
  13696. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  13697. will only contain one set of parameters. When the highlights are
  13698. removed (for example with `C-c C-c', or with the next edit (depending
  13699. on `org-remove-highlights-with-change'), this variable is emptied
  13700. as well.")
  13701. (make-variable-buffer-local 'org-occur-parameters)
  13702. (defun org-occur (regexp &optional keep-previous callback)
  13703. "Make a compact tree which shows all matches of REGEXP.
  13704. The tree will show the lines where the regexp matches, and all higher
  13705. headlines above the match. It will also show the heading after the match,
  13706. to make sure editing the matching entry is easy.
  13707. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13708. call to `org-occur' will be kept, to allow stacking of calls to this
  13709. command.
  13710. If CALLBACK is non-nil, it is a function which is called to confirm
  13711. that the match should indeed be shown."
  13712. (interactive "sRegexp: \nP")
  13713. (unless keep-previous
  13714. (org-remove-occur-highlights nil nil t))
  13715. (push (cons regexp callback) org-occur-parameters)
  13716. (let ((cnt 0))
  13717. (save-excursion
  13718. (goto-char (point-min))
  13719. (if (or (not keep-previous) ; do not want to keep
  13720. (not org-occur-highlights)) ; no previous matches
  13721. ;; hide everything
  13722. (org-overview))
  13723. (while (re-search-forward regexp nil t)
  13724. (when (or (not callback)
  13725. (save-match-data (funcall callback)))
  13726. (setq cnt (1+ cnt))
  13727. (when org-highlight-sparse-tree-matches
  13728. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13729. (org-show-context 'occur-tree))))
  13730. (when org-remove-highlights-with-change
  13731. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13732. nil 'local))
  13733. (unless org-sparse-tree-open-archived-trees
  13734. (org-hide-archived-subtrees (point-min) (point-max)))
  13735. (run-hooks 'org-occur-hook)
  13736. (if (interactive-p)
  13737. (message "%d match(es) for regexp %s" cnt regexp))
  13738. cnt))
  13739. (defun org-show-context (&optional key)
  13740. "Make sure point and context and visible.
  13741. How much context is shown depends upon the variables
  13742. `org-show-hierarchy-above', `org-show-following-heading'. and
  13743. `org-show-siblings'."
  13744. (let ((heading-p (org-on-heading-p t))
  13745. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13746. (following-p (org-get-alist-option org-show-following-heading key))
  13747. (entry-p (org-get-alist-option org-show-entry-below key))
  13748. (siblings-p (org-get-alist-option org-show-siblings key)))
  13749. (catch 'exit
  13750. ;; Show heading or entry text
  13751. (if (and heading-p (not entry-p))
  13752. (org-flag-heading nil) ; only show the heading
  13753. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13754. (org-show-hidden-entry))) ; show entire entry
  13755. (when following-p
  13756. ;; Show next sibling, or heading below text
  13757. (save-excursion
  13758. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13759. (org-flag-heading nil))))
  13760. (when siblings-p (org-show-siblings))
  13761. (when hierarchy-p
  13762. ;; show all higher headings, possibly with siblings
  13763. (save-excursion
  13764. (while (and (condition-case nil
  13765. (progn (org-up-heading-all 1) t)
  13766. (error nil))
  13767. (not (bobp)))
  13768. (org-flag-heading nil)
  13769. (when siblings-p (org-show-siblings))))))))
  13770. (defun org-reveal (&optional siblings)
  13771. "Show current entry, hierarchy above it, and the following headline.
  13772. This can be used to show a consistent set of context around locations
  13773. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13774. not t for the search context.
  13775. With optional argument SIBLINGS, on each level of the hierarchy all
  13776. siblings are shown. This repairs the tree structure to what it would
  13777. look like when opened with hierarchical calls to `org-cycle'."
  13778. (interactive "P")
  13779. (let ((org-show-hierarchy-above t)
  13780. (org-show-following-heading t)
  13781. (org-show-siblings (if siblings t org-show-siblings)))
  13782. (org-show-context nil)))
  13783. (defun org-highlight-new-match (beg end)
  13784. "Highlight from BEG to END and mark the highlight is an occur headline."
  13785. (let ((ov (org-make-overlay beg end)))
  13786. (org-overlay-put ov 'face 'secondary-selection)
  13787. (push ov org-occur-highlights)))
  13788. (defun org-remove-occur-highlights (&optional beg end noremove)
  13789. "Remove the occur highlights from the buffer.
  13790. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13791. from the `before-change-functions' in the current buffer."
  13792. (interactive)
  13793. (unless org-inhibit-highlight-removal
  13794. (mapc 'org-delete-overlay org-occur-highlights)
  13795. (setq org-occur-highlights nil)
  13796. (setq org-occur-parameters nil)
  13797. (unless noremove
  13798. (remove-hook 'before-change-functions
  13799. 'org-remove-occur-highlights 'local))))
  13800. ;;;; Priorities
  13801. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13802. "Regular expression matching the priority indicator.")
  13803. (defvar org-remove-priority-next-time nil)
  13804. (defun org-priority-up ()
  13805. "Increase the priority of the current item."
  13806. (interactive)
  13807. (org-priority 'up))
  13808. (defun org-priority-down ()
  13809. "Decrease the priority of the current item."
  13810. (interactive)
  13811. (org-priority 'down))
  13812. (defun org-priority (&optional action)
  13813. "Change the priority of an item by ARG.
  13814. ACTION can be `set', `up', `down', or a character."
  13815. (interactive)
  13816. (setq action (or action 'set))
  13817. (let (current new news have remove)
  13818. (save-excursion
  13819. (org-back-to-heading)
  13820. (if (looking-at org-priority-regexp)
  13821. (setq current (string-to-char (match-string 2))
  13822. have t)
  13823. (setq current org-default-priority))
  13824. (cond
  13825. ((or (eq action 'set) (integerp action))
  13826. (if (integerp action)
  13827. (setq new action)
  13828. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13829. (setq new (read-char-exclusive)))
  13830. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13831. (= (upcase org-lowest-priority) org-lowest-priority))
  13832. (setq new (upcase new)))
  13833. (cond ((equal new ?\ ) (setq remove t))
  13834. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13835. (error "Priority must be between `%c' and `%c'"
  13836. org-highest-priority org-lowest-priority))))
  13837. ((eq action 'up)
  13838. (if (and (not have) (eq last-command this-command))
  13839. (setq new org-lowest-priority)
  13840. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13841. org-default-priority (1- current)))))
  13842. ((eq action 'down)
  13843. (if (and (not have) (eq last-command this-command))
  13844. (setq new org-highest-priority)
  13845. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13846. org-default-priority (1+ current)))))
  13847. (t (error "Invalid action")))
  13848. (if (or (< (upcase new) org-highest-priority)
  13849. (> (upcase new) org-lowest-priority))
  13850. (setq remove t))
  13851. (setq news (format "%c" new))
  13852. (if have
  13853. (if remove
  13854. (replace-match "" t t nil 1)
  13855. (replace-match news t t nil 2))
  13856. (if remove
  13857. (error "No priority cookie found in line")
  13858. (looking-at org-todo-line-regexp)
  13859. (if (match-end 2)
  13860. (progn
  13861. (goto-char (match-end 2))
  13862. (insert " [#" news "]"))
  13863. (goto-char (match-beginning 3))
  13864. (insert "[#" news "] ")))))
  13865. (org-preserve-lc (org-set-tags nil 'align))
  13866. (if remove
  13867. (message "Priority removed")
  13868. (message "Priority of current item set to %s" news))))
  13869. (defun org-get-priority (s)
  13870. "Find priority cookie and return priority."
  13871. (save-match-data
  13872. (if (not (string-match org-priority-regexp s))
  13873. (* 1000 (- org-lowest-priority org-default-priority))
  13874. (* 1000 (- org-lowest-priority
  13875. (string-to-char (match-string 2 s)))))))
  13876. ;;;; Tags
  13877. (defun org-scan-tags (action matcher &optional todo-only)
  13878. "Scan headline tags with inheritance and produce output ACTION.
  13879. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13880. evaluated, testing if a given set of tags qualifies a headline for
  13881. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13882. are included in the output."
  13883. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13884. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13885. (org-re
  13886. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13887. (props (list 'face nil
  13888. 'done-face 'org-done
  13889. 'undone-face nil
  13890. 'mouse-face 'highlight
  13891. 'org-not-done-regexp org-not-done-regexp
  13892. 'org-todo-regexp org-todo-regexp
  13893. 'keymap org-agenda-keymap
  13894. 'help-echo
  13895. (format "mouse-2 or RET jump to org file %s"
  13896. (abbreviate-file-name
  13897. (or (buffer-file-name (buffer-base-buffer))
  13898. (buffer-name (buffer-base-buffer)))))))
  13899. (case-fold-search nil)
  13900. lspos
  13901. tags tags-list tags-alist (llast 0) rtn level category i txt
  13902. todo marker entry priority)
  13903. (save-excursion
  13904. (goto-char (point-min))
  13905. (when (eq action 'sparse-tree)
  13906. (org-overview)
  13907. (org-remove-occur-highlights))
  13908. (while (re-search-forward re nil t)
  13909. (catch :skip
  13910. (setq todo (if (match-end 1) (match-string 2))
  13911. tags (if (match-end 4) (match-string 4)))
  13912. (goto-char (setq lspos (1+ (match-beginning 0))))
  13913. (setq level (org-reduced-level (funcall outline-level))
  13914. category (org-get-category))
  13915. (setq i llast llast level)
  13916. ;; remove tag lists from same and sublevels
  13917. (while (>= i level)
  13918. (when (setq entry (assoc i tags-alist))
  13919. (setq tags-alist (delete entry tags-alist)))
  13920. (setq i (1- i)))
  13921. ;; add the nex tags
  13922. (when tags
  13923. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13924. tags-alist
  13925. (cons (cons level tags) tags-alist)))
  13926. ;; compile tags for current headline
  13927. (setq tags-list
  13928. (if org-use-tag-inheritance
  13929. (apply 'append (mapcar 'cdr tags-alist))
  13930. tags))
  13931. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13932. (eval matcher)
  13933. (or (not org-agenda-skip-archived-trees)
  13934. (not (member org-archive-tag tags-list))))
  13935. (and (eq action 'agenda) (org-agenda-skip))
  13936. ;; list this headline
  13937. (if (eq action 'sparse-tree)
  13938. (progn
  13939. (and org-highlight-sparse-tree-matches
  13940. (org-get-heading) (match-end 0)
  13941. (org-highlight-new-match
  13942. (match-beginning 0) (match-beginning 1)))
  13943. (org-show-context 'tags-tree))
  13944. (setq txt (org-format-agenda-item
  13945. ""
  13946. (concat
  13947. (if org-tags-match-list-sublevels
  13948. (make-string (1- level) ?.) "")
  13949. (org-get-heading))
  13950. category tags-list)
  13951. priority (org-get-priority txt))
  13952. (goto-char lspos)
  13953. (setq marker (org-agenda-new-marker))
  13954. (org-add-props txt props
  13955. 'org-marker marker 'org-hd-marker marker 'org-category category
  13956. 'priority priority 'type "tagsmatch")
  13957. (push txt rtn))
  13958. ;; if we are to skip sublevels, jump to end of subtree
  13959. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  13960. (when (and (eq action 'sparse-tree)
  13961. (not org-sparse-tree-open-archived-trees))
  13962. (org-hide-archived-subtrees (point-min) (point-max)))
  13963. (nreverse rtn)))
  13964. (defvar todo-only) ;; dynamically scoped
  13965. (defun org-tags-sparse-tree (&optional todo-only match)
  13966. "Create a sparse tree according to tags string MATCH.
  13967. MATCH can contain positive and negative selection of tags, like
  13968. \"+WORK+URGENT-WITHBOSS\".
  13969. If optional argument TODO_ONLY is non-nil, only select lines that are
  13970. also TODO lines."
  13971. (interactive "P")
  13972. (org-prepare-agenda-buffers (list (current-buffer)))
  13973. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13974. (defvar org-cached-props nil)
  13975. (defun org-cached-entry-get (pom property)
  13976. (if (or (eq t org-use-property-inheritance)
  13977. (member property org-use-property-inheritance))
  13978. ;; Caching is not possible, check it directly
  13979. (org-entry-get pom property 'inherit)
  13980. ;; Get all properties, so that we can do complicated checks easily
  13981. (cdr (assoc property (or org-cached-props
  13982. (setq org-cached-props
  13983. (org-entry-properties pom)))))))
  13984. (defun org-global-tags-completion-table (&optional files)
  13985. "Return the list of all tags in all agenda buffer/files."
  13986. (save-excursion
  13987. (org-uniquify
  13988. (delq nil
  13989. (apply 'append
  13990. (mapcar
  13991. (lambda (file)
  13992. (set-buffer (find-file-noselect file))
  13993. (append (org-get-buffer-tags)
  13994. (mapcar (lambda (x) (if (stringp (car-safe x))
  13995. (list (car-safe x)) nil))
  13996. org-tag-alist)))
  13997. (if (and files (car files))
  13998. files
  13999. (org-agenda-files))))))))
  14000. (defun org-make-tags-matcher (match)
  14001. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14002. ;; todo-only is scoped dynamically into this function, and the function
  14003. ;; may change it it the matcher asksk for it.
  14004. (unless match
  14005. ;; Get a new match request, with completion
  14006. (let ((org-last-tags-completion-table
  14007. (org-global-tags-completion-table)))
  14008. (setq match (completing-read
  14009. "Match: " 'org-tags-completion-function nil nil nil
  14010. 'org-tags-history))))
  14011. ;; Parse the string and create a lisp form
  14012. (let ((match0 match)
  14013. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14014. minus tag mm
  14015. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14016. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14017. (if (string-match "/+" match)
  14018. ;; match contains also a todo-matching request
  14019. (progn
  14020. (setq tagsmatch (substring match 0 (match-beginning 0))
  14021. todomatch (substring match (match-end 0)))
  14022. (if (string-match "^!" todomatch)
  14023. (setq todo-only t todomatch (substring todomatch 1)))
  14024. (if (string-match "^\\s-*$" todomatch)
  14025. (setq todomatch nil)))
  14026. ;; only matching tags
  14027. (setq tagsmatch match todomatch nil))
  14028. ;; Make the tags matcher
  14029. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14030. (setq tagsmatcher t)
  14031. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14032. (while (setq term (pop orterms))
  14033. (while (and (equal (substring term -1) "\\") orterms)
  14034. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14035. (while (string-match re term)
  14036. (setq minus (and (match-end 1)
  14037. (equal (match-string 1 term) "-"))
  14038. tag (match-string 2 term)
  14039. re-p (equal (string-to-char tag) ?{)
  14040. level-p (match-end 3)
  14041. prop-p (match-end 4)
  14042. mm (cond
  14043. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14044. (level-p `(= level ,(string-to-number
  14045. (match-string 3 term))))
  14046. (prop-p
  14047. (setq pn (match-string 4 term)
  14048. pv (match-string 5 term)
  14049. cat-p (equal pn "CATEGORY")
  14050. re-p (equal (string-to-char pv) ?{)
  14051. pv (substring pv 1 -1))
  14052. (if (equal pn "CATEGORY")
  14053. (setq gv '(get-text-property (point) 'org-category))
  14054. (setq gv `(org-cached-entry-get nil ,pn)))
  14055. (if re-p
  14056. `(string-match ,pv (or ,gv ""))
  14057. `(equal ,pv (or ,gv ""))))
  14058. (t `(member ,(downcase tag) tags-list)))
  14059. mm (if minus (list 'not mm) mm)
  14060. term (substring term (match-end 0)))
  14061. (push mm tagsmatcher))
  14062. (push (if (> (length tagsmatcher) 1)
  14063. (cons 'and tagsmatcher)
  14064. (car tagsmatcher))
  14065. orlist)
  14066. (setq tagsmatcher nil))
  14067. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14068. (setq tagsmatcher
  14069. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14070. ;; Make the todo matcher
  14071. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14072. (setq todomatcher t)
  14073. (setq orterms (org-split-string todomatch "|") orlist nil)
  14074. (while (setq term (pop orterms))
  14075. (while (string-match re term)
  14076. (setq minus (and (match-end 1)
  14077. (equal (match-string 1 term) "-"))
  14078. kwd (match-string 2 term)
  14079. re-p (equal (string-to-char kwd) ?{)
  14080. term (substring term (match-end 0))
  14081. mm (if re-p
  14082. `(string-match ,(substring kwd 1 -1) todo)
  14083. (list 'equal 'todo kwd))
  14084. mm (if minus (list 'not mm) mm))
  14085. (push mm todomatcher))
  14086. (push (if (> (length todomatcher) 1)
  14087. (cons 'and todomatcher)
  14088. (car todomatcher))
  14089. orlist)
  14090. (setq todomatcher nil))
  14091. (setq todomatcher (if (> (length orlist) 1)
  14092. (cons 'or orlist) (car orlist))))
  14093. ;; Return the string and lisp forms of the matcher
  14094. (setq matcher (if todomatcher
  14095. (list 'and tagsmatcher todomatcher)
  14096. tagsmatcher))
  14097. (cons match0 matcher)))
  14098. (defun org-match-any-p (re list)
  14099. "Does re match any element of list?"
  14100. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14101. (delq nil list))
  14102. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14103. (defvar org-tags-overlay (org-make-overlay 1 1))
  14104. (org-detach-overlay org-tags-overlay)
  14105. (defun org-align-tags-here (to-col)
  14106. ;; Assumes that this is a headline
  14107. (let ((pos (point)) (col (current-column)) tags)
  14108. (beginning-of-line 1)
  14109. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14110. (< pos (match-beginning 2)))
  14111. (progn
  14112. (setq tags (match-string 2))
  14113. (goto-char (match-beginning 1))
  14114. (insert " ")
  14115. (delete-region (point) (1+ (match-end 0)))
  14116. (backward-char 1)
  14117. (move-to-column
  14118. (max (1+ (current-column))
  14119. (1+ col)
  14120. (if (> to-col 0)
  14121. to-col
  14122. (- (abs to-col) (length tags))))
  14123. t)
  14124. (insert tags)
  14125. (move-to-column (min (current-column) col) t))
  14126. (goto-char pos))))
  14127. (defun org-set-tags (&optional arg just-align)
  14128. "Set the tags for the current headline.
  14129. With prefix ARG, realign all tags in headings in the current buffer."
  14130. (interactive "P")
  14131. (let* ((re (concat "^" outline-regexp))
  14132. (current (org-get-tags-string))
  14133. (col (current-column))
  14134. (org-setting-tags t)
  14135. table current-tags inherited-tags ; computed below when needed
  14136. tags p0 c0 c1 rpl)
  14137. (if arg
  14138. (save-excursion
  14139. (goto-char (point-min))
  14140. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14141. (while (re-search-forward re nil t)
  14142. (org-set-tags nil t)
  14143. (end-of-line 1)))
  14144. (message "All tags realigned to column %d" org-tags-column))
  14145. (if just-align
  14146. (setq tags current)
  14147. ;; Get a new set of tags from the user
  14148. (save-excursion
  14149. (setq table (or org-tag-alist (org-get-buffer-tags))
  14150. org-last-tags-completion-table table
  14151. current-tags (org-split-string current ":")
  14152. inherited-tags (nreverse
  14153. (nthcdr (length current-tags)
  14154. (nreverse (org-get-tags-at))))
  14155. tags
  14156. (if (or (eq t org-use-fast-tag-selection)
  14157. (and org-use-fast-tag-selection
  14158. (delq nil (mapcar 'cdr table))))
  14159. (org-fast-tag-selection
  14160. current-tags inherited-tags table
  14161. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14162. (let ((org-add-colon-after-tag-completion t))
  14163. (org-trim
  14164. (org-without-partial-completion
  14165. (completing-read "Tags: " 'org-tags-completion-function
  14166. nil nil current 'org-tags-history)))))))
  14167. (while (string-match "[-+&]+" tags)
  14168. ;; No boolean logic, just a list
  14169. (setq tags (replace-match ":" t t tags))))
  14170. (if (string-match "\\`[\t ]*\\'" tags)
  14171. (setq tags "")
  14172. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14173. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14174. ;; Insert new tags at the correct column
  14175. (beginning-of-line 1)
  14176. (cond
  14177. ((and (equal current "") (equal tags "")))
  14178. ((re-search-forward
  14179. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14180. (point-at-eol) t)
  14181. (if (equal tags "")
  14182. (setq rpl "")
  14183. (goto-char (match-beginning 0))
  14184. (setq c0 (current-column) p0 (point)
  14185. c1 (max (1+ c0) (if (> org-tags-column 0)
  14186. org-tags-column
  14187. (- (- org-tags-column) (length tags))))
  14188. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14189. (replace-match rpl t t)
  14190. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14191. tags)
  14192. (t (error "Tags alignment failed")))
  14193. (move-to-column col)
  14194. (unless just-align
  14195. (run-hooks 'org-after-tags-change-hook)))))
  14196. (defun org-change-tag-in-region (beg end tag off)
  14197. "Add or remove TAG for each entry in the region.
  14198. This works in the agenda, and also in an org-mode buffer."
  14199. (interactive
  14200. (list (region-beginning) (region-end)
  14201. (let ((org-last-tags-completion-table
  14202. (if (org-mode-p)
  14203. (org-get-buffer-tags)
  14204. (org-global-tags-completion-table))))
  14205. (completing-read
  14206. "Tag: " 'org-tags-completion-function nil nil nil
  14207. 'org-tags-history))
  14208. (progn
  14209. (message "[s]et or [r]emove? ")
  14210. (equal (read-char-exclusive) ?r))))
  14211. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14212. (let ((agendap (equal major-mode 'org-agenda-mode))
  14213. l1 l2 m buf pos newhead (cnt 0))
  14214. (goto-char end)
  14215. (setq l2 (1- (org-current-line)))
  14216. (goto-char beg)
  14217. (setq l1 (org-current-line))
  14218. (loop for l from l1 to l2 do
  14219. (goto-line l)
  14220. (setq m (get-text-property (point) 'org-hd-marker))
  14221. (when (or (and (org-mode-p) (org-on-heading-p))
  14222. (and agendap m))
  14223. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14224. pos (if agendap m (point)))
  14225. (with-current-buffer buf
  14226. (save-excursion
  14227. (save-restriction
  14228. (goto-char pos)
  14229. (setq cnt (1+ cnt))
  14230. (org-toggle-tag tag (if off 'off 'on))
  14231. (setq newhead (org-get-heading)))))
  14232. (and agendap (org-agenda-change-all-lines newhead m))))
  14233. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14234. (defun org-tags-completion-function (string predicate &optional flag)
  14235. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14236. (confirm (lambda (x) (stringp (car x)))))
  14237. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14238. (setq s1 (match-string 1 string)
  14239. s2 (match-string 2 string))
  14240. (setq s1 "" s2 string))
  14241. (cond
  14242. ((eq flag nil)
  14243. ;; try completion
  14244. (setq rtn (try-completion s2 ctable confirm))
  14245. (if (stringp rtn)
  14246. (setq rtn
  14247. (concat s1 s2 (substring rtn (length s2))
  14248. (if (and org-add-colon-after-tag-completion
  14249. (assoc rtn ctable))
  14250. ":" ""))))
  14251. rtn)
  14252. ((eq flag t)
  14253. ;; all-completions
  14254. (all-completions s2 ctable confirm)
  14255. )
  14256. ((eq flag 'lambda)
  14257. ;; exact match?
  14258. (assoc s2 ctable)))
  14259. ))
  14260. (defun org-fast-tag-insert (kwd tags face &optional end)
  14261. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14262. (insert (format "%-12s" (concat kwd ":"))
  14263. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14264. (or end "")))
  14265. (defun org-fast-tag-show-exit (flag)
  14266. (save-excursion
  14267. (goto-line 3)
  14268. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14269. (replace-match ""))
  14270. (when flag
  14271. (end-of-line 1)
  14272. (move-to-column (- (window-width) 19) t)
  14273. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14274. (defun org-set-current-tags-overlay (current prefix)
  14275. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14276. (if (featurep 'xemacs)
  14277. (org-overlay-display org-tags-overlay (concat prefix s)
  14278. 'secondary-selection)
  14279. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14280. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14281. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14282. "Fast tag selection with single keys.
  14283. CURRENT is the current list of tags in the headline, INHERITED is the
  14284. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14285. possibly with grouping information. TODO-TABLE is a similar table with
  14286. TODO keywords, should these have keys assigned to them.
  14287. If the keys are nil, a-z are automatically assigned.
  14288. Returns the new tags string, or nil to not change the current settings."
  14289. (let* ((fulltable (append table todo-table))
  14290. (maxlen (apply 'max (mapcar
  14291. (lambda (x)
  14292. (if (stringp (car x)) (string-width (car x)) 0))
  14293. fulltable)))
  14294. (buf (current-buffer))
  14295. (expert (eq org-fast-tag-selection-single-key 'expert))
  14296. (buffer-tags nil)
  14297. (fwidth (+ maxlen 3 1 3))
  14298. (ncol (/ (- (window-width) 4) fwidth))
  14299. (i-face 'org-done)
  14300. (c-face 'org-todo)
  14301. tg cnt e c char c1 c2 ntable tbl rtn
  14302. ov-start ov-end ov-prefix
  14303. (exit-after-next org-fast-tag-selection-single-key)
  14304. (done-keywords org-done-keywords)
  14305. groups ingroup)
  14306. (save-excursion
  14307. (beginning-of-line 1)
  14308. (if (looking-at
  14309. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14310. (setq ov-start (match-beginning 1)
  14311. ov-end (match-end 1)
  14312. ov-prefix "")
  14313. (setq ov-start (1- (point-at-eol))
  14314. ov-end (1+ ov-start))
  14315. (skip-chars-forward "^\n\r")
  14316. (setq ov-prefix
  14317. (concat
  14318. (buffer-substring (1- (point)) (point))
  14319. (if (> (current-column) org-tags-column)
  14320. " "
  14321. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14322. (org-move-overlay org-tags-overlay ov-start ov-end)
  14323. (save-window-excursion
  14324. (if expert
  14325. (set-buffer (get-buffer-create " *Org tags*"))
  14326. (delete-other-windows)
  14327. (split-window-vertically)
  14328. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14329. (erase-buffer)
  14330. (org-set-local 'org-done-keywords done-keywords)
  14331. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14332. (org-fast-tag-insert "Current" current c-face "\n\n")
  14333. (org-fast-tag-show-exit exit-after-next)
  14334. (org-set-current-tags-overlay current ov-prefix)
  14335. (setq tbl fulltable char ?a cnt 0)
  14336. (while (setq e (pop tbl))
  14337. (cond
  14338. ((equal e '(:startgroup))
  14339. (push '() groups) (setq ingroup t)
  14340. (when (not (= cnt 0))
  14341. (setq cnt 0)
  14342. (insert "\n"))
  14343. (insert "{ "))
  14344. ((equal e '(:endgroup))
  14345. (setq ingroup nil cnt 0)
  14346. (insert "}\n"))
  14347. (t
  14348. (setq tg (car e) c2 nil)
  14349. (if (cdr e)
  14350. (setq c (cdr e))
  14351. ;; automatically assign a character.
  14352. (setq c1 (string-to-char
  14353. (downcase (substring
  14354. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14355. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14356. (while (or (rassoc char ntable) (rassoc char table))
  14357. (setq char (1+ char)))
  14358. (setq c2 c1))
  14359. (setq c (or c2 char)))
  14360. (if ingroup (push tg (car groups)))
  14361. (setq tg (org-add-props tg nil 'face
  14362. (cond
  14363. ((not (assoc tg table))
  14364. (org-get-todo-face tg))
  14365. ((member tg current) c-face)
  14366. ((member tg inherited) i-face)
  14367. (t nil))))
  14368. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14369. (insert "[" c "] " tg (make-string
  14370. (- fwidth 4 (length tg)) ?\ ))
  14371. (push (cons tg c) ntable)
  14372. (when (= (setq cnt (1+ cnt)) ncol)
  14373. (insert "\n")
  14374. (if ingroup (insert " "))
  14375. (setq cnt 0)))))
  14376. (setq ntable (nreverse ntable))
  14377. (insert "\n")
  14378. (goto-char (point-min))
  14379. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14380. (fit-window-to-buffer))
  14381. (setq rtn
  14382. (catch 'exit
  14383. (while t
  14384. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14385. (if groups " [!] no groups" " [!]groups")
  14386. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14387. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14388. (cond
  14389. ((= c ?\r) (throw 'exit t))
  14390. ((= c ?!)
  14391. (setq groups (not groups))
  14392. (goto-char (point-min))
  14393. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14394. ((= c ?\C-c)
  14395. (if (not expert)
  14396. (org-fast-tag-show-exit
  14397. (setq exit-after-next (not exit-after-next)))
  14398. (setq expert nil)
  14399. (delete-other-windows)
  14400. (split-window-vertically)
  14401. (org-switch-to-buffer-other-window " *Org tags*")
  14402. (and (fboundp 'fit-window-to-buffer)
  14403. (fit-window-to-buffer))))
  14404. ((or (= c ?\C-g)
  14405. (and (= c ?q) (not (rassoc c ntable))))
  14406. (org-detach-overlay org-tags-overlay)
  14407. (setq quit-flag t))
  14408. ((= c ?\ )
  14409. (setq current nil)
  14410. (if exit-after-next (setq exit-after-next 'now)))
  14411. ((= c ?\t)
  14412. (condition-case nil
  14413. (setq tg (completing-read
  14414. "Tag: "
  14415. (or buffer-tags
  14416. (with-current-buffer buf
  14417. (org-get-buffer-tags)))))
  14418. (quit (setq tg "")))
  14419. (when (string-match "\\S-" tg)
  14420. (add-to-list 'buffer-tags (list tg))
  14421. (if (member tg current)
  14422. (setq current (delete tg current))
  14423. (push tg current)))
  14424. (if exit-after-next (setq exit-after-next 'now)))
  14425. ((setq e (rassoc c todo-table) tg (car e))
  14426. (with-current-buffer buf
  14427. (save-excursion (org-todo tg)))
  14428. (if exit-after-next (setq exit-after-next 'now)))
  14429. ((setq e (rassoc c ntable) tg (car e))
  14430. (if (member tg current)
  14431. (setq current (delete tg current))
  14432. (loop for g in groups do
  14433. (if (member tg g)
  14434. (mapc (lambda (x)
  14435. (setq current (delete x current)))
  14436. g)))
  14437. (push tg current))
  14438. (if exit-after-next (setq exit-after-next 'now))))
  14439. ;; Create a sorted list
  14440. (setq current
  14441. (sort current
  14442. (lambda (a b)
  14443. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14444. (if (eq exit-after-next 'now) (throw 'exit t))
  14445. (goto-char (point-min))
  14446. (beginning-of-line 2)
  14447. (delete-region (point) (point-at-eol))
  14448. (org-fast-tag-insert "Current" current c-face)
  14449. (org-set-current-tags-overlay current ov-prefix)
  14450. (while (re-search-forward
  14451. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14452. (setq tg (match-string 1))
  14453. (add-text-properties
  14454. (match-beginning 1) (match-end 1)
  14455. (list 'face
  14456. (cond
  14457. ((member tg current) c-face)
  14458. ((member tg inherited) i-face)
  14459. (t (get-text-property (match-beginning 1) 'face))))))
  14460. (goto-char (point-min)))))
  14461. (org-detach-overlay org-tags-overlay)
  14462. (if rtn
  14463. (mapconcat 'identity current ":")
  14464. nil))))
  14465. (defun org-get-tags-string ()
  14466. "Get the TAGS string in the current headline."
  14467. (unless (org-on-heading-p t)
  14468. (error "Not on a heading"))
  14469. (save-excursion
  14470. (beginning-of-line 1)
  14471. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14472. (org-match-string-no-properties 1)
  14473. "")))
  14474. (defun org-get-tags ()
  14475. "Get the list of tags specified in the current headline."
  14476. (org-split-string (org-get-tags-string) ":"))
  14477. (defun org-get-buffer-tags ()
  14478. "Get a table of all tags used in the buffer, for completion."
  14479. (let (tags)
  14480. (save-excursion
  14481. (goto-char (point-min))
  14482. (while (re-search-forward
  14483. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14484. (when (equal (char-after (point-at-bol 0)) ?*)
  14485. (mapc (lambda (x) (add-to-list 'tags x))
  14486. (org-split-string (org-match-string-no-properties 1) ":")))))
  14487. (mapcar 'list tags)))
  14488. ;;;; Properties
  14489. ;;; Setting and retrieving properties
  14490. (defconst org-special-properties
  14491. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14492. "TIMESTAMP" "TIMESTAMP_IA")
  14493. "The special properties valid in Org-mode.
  14494. These are properties that are not defined in the property drawer,
  14495. but in some other way.")
  14496. (defconst org-default-properties
  14497. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14498. "LOCATION" "LOGGING" "COLUMNS")
  14499. "Some properties that are used by Org-mode for various purposes.
  14500. Being in this list makes sure that they are offered for completion.")
  14501. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14502. "Regular expression matching the first line of a property drawer.")
  14503. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14504. "Regular expression matching the first line of a property drawer.")
  14505. (defun org-property-action ()
  14506. "Do an action on properties."
  14507. (interactive)
  14508. (let (c)
  14509. (org-at-property-p)
  14510. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14511. (setq c (read-char-exclusive))
  14512. (cond
  14513. ((equal c ?s)
  14514. (call-interactively 'org-set-property))
  14515. ((equal c ?d)
  14516. (call-interactively 'org-delete-property))
  14517. ((equal c ?D)
  14518. (call-interactively 'org-delete-property-globally))
  14519. ((equal c ?c)
  14520. (call-interactively 'org-compute-property-at-point))
  14521. (t (error "No such property action %c" c)))))
  14522. (defun org-at-property-p ()
  14523. "Is the cursor in a property line?"
  14524. ;; FIXME: Does not check if we are actually in the drawer.
  14525. ;; FIXME: also returns true on any drawers.....
  14526. ;; This is used by C-c C-c for property action.
  14527. (save-excursion
  14528. (beginning-of-line 1)
  14529. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14530. (defmacro org-with-point-at (pom &rest body)
  14531. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14532. (declare (indent 1) (debug t))
  14533. `(save-excursion
  14534. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14535. (save-excursion
  14536. (goto-char (or pom (point)))
  14537. ,@body)))
  14538. (defun org-get-property-block (&optional beg end force)
  14539. "Return the (beg . end) range of the body of the property drawer.
  14540. BEG and END can be beginning and end of subtree, if not given
  14541. they will be found.
  14542. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14543. (catch 'exit
  14544. (save-excursion
  14545. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14546. (end (or end (progn (outline-next-heading) (point)))))
  14547. (goto-char beg)
  14548. (if (re-search-forward org-property-start-re end t)
  14549. (setq beg (1+ (match-end 0)))
  14550. (if force
  14551. (save-excursion
  14552. (org-insert-property-drawer)
  14553. (setq end (progn (outline-next-heading) (point))))
  14554. (throw 'exit nil))
  14555. (goto-char beg)
  14556. (if (re-search-forward org-property-start-re end t)
  14557. (setq beg (1+ (match-end 0)))))
  14558. (if (re-search-forward org-property-end-re end t)
  14559. (setq end (match-beginning 0))
  14560. (or force (throw 'exit nil))
  14561. (goto-char beg)
  14562. (setq end beg)
  14563. (org-indent-line-function)
  14564. (insert ":END:\n"))
  14565. (cons beg end)))))
  14566. (defun org-entry-properties (&optional pom which)
  14567. "Get all properties of the entry at point-or-marker POM.
  14568. This includes the TODO keyword, the tags, time strings for deadline,
  14569. scheduled, and clocking, and any additional properties defined in the
  14570. entry. The return value is an alist, keys may occur multiple times
  14571. if the property key was used several times.
  14572. POM may also be nil, in which case the current entry is used.
  14573. If WHICH is nil or `all', get all properties. If WHICH is
  14574. `special' or `standard', only get that subclass."
  14575. (setq which (or which 'all))
  14576. (org-with-point-at pom
  14577. (let ((clockstr (substring org-clock-string 0 -1))
  14578. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14579. beg end range props sum-props key value string clocksum)
  14580. (save-excursion
  14581. (when (condition-case nil (org-back-to-heading t) (error nil))
  14582. (setq beg (point))
  14583. (setq sum-props (get-text-property (point) 'org-summaries))
  14584. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14585. (outline-next-heading)
  14586. (setq end (point))
  14587. (when (memq which '(all special))
  14588. ;; Get the special properties, like TODO and tags
  14589. (goto-char beg)
  14590. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14591. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14592. (when (looking-at org-priority-regexp)
  14593. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14594. (when (and (setq value (org-get-tags-string))
  14595. (string-match "\\S-" value))
  14596. (push (cons "TAGS" value) props))
  14597. (when (setq value (org-get-tags-at))
  14598. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14599. props))
  14600. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14601. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14602. string (if (equal key clockstr)
  14603. (org-no-properties
  14604. (org-trim
  14605. (buffer-substring
  14606. (match-beginning 3) (goto-char (point-at-eol)))))
  14607. (substring (org-match-string-no-properties 3) 1 -1)))
  14608. (unless key
  14609. (if (= (char-after (match-beginning 3)) ?\[)
  14610. (setq key "TIMESTAMP_IA")
  14611. (setq key "TIMESTAMP")))
  14612. (when (or (equal key clockstr) (not (assoc key props)))
  14613. (push (cons key string) props)))
  14614. )
  14615. (when (memq which '(all standard))
  14616. ;; Get the standard properties, like :PORP: ...
  14617. (setq range (org-get-property-block beg end))
  14618. (when range
  14619. (goto-char (car range))
  14620. (while (re-search-forward
  14621. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14622. (cdr range) t)
  14623. (setq key (org-match-string-no-properties 1)
  14624. value (org-trim (or (org-match-string-no-properties 2) "")))
  14625. (unless (member key excluded)
  14626. (push (cons key (or value "")) props)))))
  14627. (if clocksum
  14628. (push (cons "CLOCKSUM"
  14629. (org-column-number-to-string (/ (float clocksum) 60.)
  14630. 'add_times))
  14631. props))
  14632. (append sum-props (nreverse props)))))))
  14633. (defun org-entry-get (pom property &optional inherit)
  14634. "Get value of PROPERTY for entry at point-or-marker POM.
  14635. If INHERIT is non-nil and the entry does not have the property,
  14636. then also check higher levels of the hierarchy.
  14637. If the property is present but empty, the return value is the empty string.
  14638. If the property is not present at all, nil is returned."
  14639. (org-with-point-at pom
  14640. (if inherit
  14641. (org-entry-get-with-inheritance property)
  14642. (if (member property org-special-properties)
  14643. ;; We need a special property. Use brute force, get all properties.
  14644. (cdr (assoc property (org-entry-properties nil 'special)))
  14645. (let ((range (org-get-property-block)))
  14646. (if (and range
  14647. (goto-char (car range))
  14648. (re-search-forward
  14649. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14650. (cdr range) t))
  14651. ;; Found the property, return it.
  14652. (if (match-end 1)
  14653. (org-match-string-no-properties 1)
  14654. "")))))))
  14655. (defun org-entry-delete (pom property)
  14656. "Delete the property PROPERTY from entry at point-or-marker POM."
  14657. (org-with-point-at pom
  14658. (if (member property org-special-properties)
  14659. nil ; cannot delete these properties.
  14660. (let ((range (org-get-property-block)))
  14661. (if (and range
  14662. (goto-char (car range))
  14663. (re-search-forward
  14664. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14665. (cdr range) t))
  14666. (progn
  14667. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14668. t)
  14669. nil)))))
  14670. ;; Multi-values properties are properties that contain multiple values
  14671. ;; These values are assumed to be single words, separated by whitespace.
  14672. (defun org-entry-add-to-multivalued-property (pom property value)
  14673. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14674. (let* ((old (org-entry-get pom property))
  14675. (values (and old (org-split-string old "[ \t]"))))
  14676. (unless (member value values)
  14677. (setq values (cons value values))
  14678. (org-entry-put pom property
  14679. (mapconcat 'identity values " ")))))
  14680. (defun org-entry-remove-from-multivalued-property (pom property value)
  14681. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14682. (let* ((old (org-entry-get pom property))
  14683. (values (and old (org-split-string old "[ \t]"))))
  14684. (when (member value values)
  14685. (setq values (delete value values))
  14686. (org-entry-put pom property
  14687. (mapconcat 'identity values " ")))))
  14688. (defun org-entry-member-in-multivalued-property (pom property value)
  14689. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14690. (let* ((old (org-entry-get pom property))
  14691. (values (and old (org-split-string old "[ \t]"))))
  14692. (member value values)))
  14693. (defvar org-entry-property-inherited-from (make-marker))
  14694. (defun org-entry-get-with-inheritance (property)
  14695. "Get entry property, and search higher levels if not present."
  14696. (let (tmp)
  14697. (save-excursion
  14698. (save-restriction
  14699. (widen)
  14700. (catch 'ex
  14701. (while t
  14702. (when (setq tmp (org-entry-get nil property))
  14703. (org-back-to-heading t)
  14704. (move-marker org-entry-property-inherited-from (point))
  14705. (throw 'ex tmp))
  14706. (or (org-up-heading-safe) (throw 'ex nil)))))
  14707. (or tmp (cdr (assoc property org-local-properties))
  14708. (cdr (assoc property org-global-properties))))))
  14709. (defun org-entry-put (pom property value)
  14710. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14711. (org-with-point-at pom
  14712. (org-back-to-heading t)
  14713. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14714. range)
  14715. (cond
  14716. ((equal property "TODO")
  14717. (when (and (stringp value) (string-match "\\S-" value)
  14718. (not (member value org-todo-keywords-1)))
  14719. (error "\"%s\" is not a valid TODO state" value))
  14720. (if (or (not value)
  14721. (not (string-match "\\S-" value)))
  14722. (setq value 'none))
  14723. (org-todo value)
  14724. (org-set-tags nil 'align))
  14725. ((equal property "PRIORITY")
  14726. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14727. (string-to-char value) ?\ ))
  14728. (org-set-tags nil 'align))
  14729. ((equal property "SCHEDULED")
  14730. (if (re-search-forward org-scheduled-time-regexp end t)
  14731. (cond
  14732. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14733. ((eq value 'later) (org-timestamp-change 1 'day))
  14734. (t (call-interactively 'org-schedule)))
  14735. (call-interactively 'org-schedule)))
  14736. ((equal property "DEADLINE")
  14737. (if (re-search-forward org-deadline-time-regexp end t)
  14738. (cond
  14739. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14740. ((eq value 'later) (org-timestamp-change 1 'day))
  14741. (t (call-interactively 'org-deadline)))
  14742. (call-interactively 'org-deadline)))
  14743. ((member property org-special-properties)
  14744. (error "The %s property can not yet be set with `org-entry-put'"
  14745. property))
  14746. (t ; a non-special property
  14747. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14748. (setq range (org-get-property-block beg end 'force))
  14749. (goto-char (car range))
  14750. (if (re-search-forward
  14751. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14752. (progn
  14753. (delete-region (match-beginning 1) (match-end 1))
  14754. (goto-char (match-beginning 1)))
  14755. (goto-char (cdr range))
  14756. (insert "\n")
  14757. (backward-char 1)
  14758. (org-indent-line-function)
  14759. (insert ":" property ":"))
  14760. (and value (insert " " value))
  14761. (org-indent-line-function)))))))
  14762. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14763. "Get all property keys in the current buffer.
  14764. With INCLUDE-SPECIALS, also list the special properties that relect things
  14765. like tags and TODO state.
  14766. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14767. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14768. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14769. formats in the current buffer."
  14770. (let (rtn range cfmt cols s p)
  14771. (save-excursion
  14772. (save-restriction
  14773. (widen)
  14774. (goto-char (point-min))
  14775. (while (re-search-forward org-property-start-re nil t)
  14776. (setq range (org-get-property-block))
  14777. (goto-char (car range))
  14778. (while (re-search-forward
  14779. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14780. (cdr range) t)
  14781. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14782. (outline-next-heading))))
  14783. (when include-specials
  14784. (setq rtn (append org-special-properties rtn)))
  14785. (when include-defaults
  14786. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14787. (when include-columns
  14788. (save-excursion
  14789. (save-restriction
  14790. (widen)
  14791. (goto-char (point-min))
  14792. (while (re-search-forward
  14793. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14794. nil t)
  14795. (setq cfmt (match-string 2) s 0)
  14796. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14797. cfmt s)
  14798. (setq s (match-end 0)
  14799. p (match-string 1 cfmt))
  14800. (unless (or (equal p "ITEM")
  14801. (member p org-special-properties))
  14802. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14803. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14804. (defun org-property-values (key)
  14805. "Return a list of all values of property KEY."
  14806. (save-excursion
  14807. (save-restriction
  14808. (widen)
  14809. (goto-char (point-min))
  14810. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14811. values)
  14812. (while (re-search-forward re nil t)
  14813. (add-to-list 'values (org-trim (match-string 1))))
  14814. (delete "" values)))))
  14815. (defun org-insert-property-drawer ()
  14816. "Insert a property drawer into the current entry."
  14817. (interactive)
  14818. (org-back-to-heading t)
  14819. (looking-at outline-regexp)
  14820. (let ((indent (- (match-end 0)(match-beginning 0)))
  14821. (beg (point))
  14822. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14823. end hiddenp)
  14824. (outline-next-heading)
  14825. (setq end (point))
  14826. (goto-char beg)
  14827. (while (re-search-forward re end t))
  14828. (setq hiddenp (org-invisible-p))
  14829. (end-of-line 1)
  14830. (and (equal (char-after) ?\n) (forward-char 1))
  14831. (org-skip-over-state-notes)
  14832. (skip-chars-backward " \t\n\r")
  14833. (if (eq (char-before) ?*) (forward-char 1))
  14834. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14835. (beginning-of-line 0)
  14836. (indent-to-column indent)
  14837. (beginning-of-line 2)
  14838. (indent-to-column indent)
  14839. (beginning-of-line 0)
  14840. (if hiddenp
  14841. (save-excursion
  14842. (org-back-to-heading t)
  14843. (hide-entry))
  14844. (org-flag-drawer t))))
  14845. (defun org-set-property (property value)
  14846. "In the current entry, set PROPERTY to VALUE.
  14847. When called interactively, this will prompt for a property name, offering
  14848. completion on existing and default properties. And then it will prompt
  14849. for a value, offering competion either on allowed values (via an inherited
  14850. xxx_ALL property) or on existing values in other instances of this property
  14851. in the current file."
  14852. (interactive
  14853. (let* ((prop (completing-read
  14854. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  14855. (cur (org-entry-get nil prop))
  14856. (allowed (org-property-get-allowed-values nil prop 'table))
  14857. (existing (mapcar 'list (org-property-values prop)))
  14858. (val (if allowed
  14859. (completing-read "Value: " allowed nil 'req-match)
  14860. (completing-read
  14861. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14862. (concat "[" cur "]") "")
  14863. ": ")
  14864. existing nil nil "" nil cur))))
  14865. (list prop (if (equal val "") cur val))))
  14866. (unless (equal (org-entry-get nil property) value)
  14867. (org-entry-put nil property value)))
  14868. (defun org-delete-property (property)
  14869. "In the current entry, delete PROPERTY."
  14870. (interactive
  14871. (let* ((prop (completing-read
  14872. "Property: " (org-entry-properties nil 'standard))))
  14873. (list prop)))
  14874. (message "Property %s %s" property
  14875. (if (org-entry-delete nil property)
  14876. "deleted"
  14877. "was not present in the entry")))
  14878. (defun org-delete-property-globally (property)
  14879. "Remove PROPERTY globally, from all entries."
  14880. (interactive
  14881. (let* ((prop (completing-read
  14882. "Globally remove property: "
  14883. (mapcar 'list (org-buffer-property-keys)))))
  14884. (list prop)))
  14885. (save-excursion
  14886. (save-restriction
  14887. (widen)
  14888. (goto-char (point-min))
  14889. (let ((cnt 0))
  14890. (while (re-search-forward
  14891. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14892. nil t)
  14893. (setq cnt (1+ cnt))
  14894. (replace-match ""))
  14895. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14896. (defvar org-columns-current-fmt-compiled) ; defined below
  14897. (defun org-compute-property-at-point ()
  14898. "Compute the property at point.
  14899. This looks for an enclosing column format, extracts the operator and
  14900. then applies it to the proerty in the column format's scope."
  14901. (interactive)
  14902. (unless (org-at-property-p)
  14903. (error "Not at a property"))
  14904. (let ((prop (org-match-string-no-properties 2)))
  14905. (org-columns-get-format-and-top-level)
  14906. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14907. (error "No operator defined for property %s" prop))
  14908. (org-columns-compute prop)))
  14909. (defun org-property-get-allowed-values (pom property &optional table)
  14910. "Get allowed values for the property PROPERTY.
  14911. When TABLE is non-nil, return an alist that can directly be used for
  14912. completion."
  14913. (let (vals)
  14914. (cond
  14915. ((equal property "TODO")
  14916. (setq vals (org-with-point-at pom
  14917. (append org-todo-keywords-1 '("")))))
  14918. ((equal property "PRIORITY")
  14919. (let ((n org-lowest-priority))
  14920. (while (>= n org-highest-priority)
  14921. (push (char-to-string n) vals)
  14922. (setq n (1- n)))))
  14923. ((member property org-special-properties))
  14924. (t
  14925. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14926. (when (and vals (string-match "\\S-" vals))
  14927. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14928. (setq vals (mapcar (lambda (x)
  14929. (cond ((stringp x) x)
  14930. ((numberp x) (number-to-string x))
  14931. ((symbolp x) (symbol-name x))
  14932. (t "???")))
  14933. vals)))))
  14934. (if table (mapcar 'list vals) vals)))
  14935. (defun org-property-previous-allowed-value (&optional previous)
  14936. "Switch to the next allowed value for this property."
  14937. (interactive)
  14938. (org-property-next-allowed-value t))
  14939. (defun org-property-next-allowed-value (&optional previous)
  14940. "Switch to the next allowed value for this property."
  14941. (interactive)
  14942. (unless (org-at-property-p)
  14943. (error "Not at a property"))
  14944. (let* ((key (match-string 2))
  14945. (value (match-string 3))
  14946. (allowed (or (org-property-get-allowed-values (point) key)
  14947. (and (member value '("[ ]" "[-]" "[X]"))
  14948. '("[ ]" "[X]"))))
  14949. nval)
  14950. (unless allowed
  14951. (error "Allowed values for this property have not been defined"))
  14952. (if previous (setq allowed (reverse allowed)))
  14953. (if (member value allowed)
  14954. (setq nval (car (cdr (member value allowed)))))
  14955. (setq nval (or nval (car allowed)))
  14956. (if (equal nval value)
  14957. (error "Only one allowed value for this property"))
  14958. (org-at-property-p)
  14959. (replace-match (concat " :" key ": " nval) t t)
  14960. (org-indent-line-function)
  14961. (beginning-of-line 1)
  14962. (skip-chars-forward " \t")))
  14963. (defun org-find-entry-with-id (ident)
  14964. "Locate the entry that contains the ID property with exact value IDENT.
  14965. IDENT can be a string, a symbol or a number, this function will search for
  14966. the string representation of it.
  14967. Return the position where this entry starts, or nil if there is no such entry."
  14968. (let ((id (cond
  14969. ((stringp ident) ident)
  14970. ((symbol-name ident) (symbol-name ident))
  14971. ((numberp ident) (number-to-string ident))
  14972. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14973. (case-fold-search nil))
  14974. (save-excursion
  14975. (save-restriction
  14976. (widen)
  14977. (goto-char (point-min))
  14978. (when (re-search-forward
  14979. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14980. nil t)
  14981. (org-back-to-heading)
  14982. (point))))))
  14983. ;;; Column View
  14984. (defvar org-columns-overlays nil
  14985. "Holds the list of current column overlays.")
  14986. (defvar org-columns-current-fmt nil
  14987. "Local variable, holds the currently active column format.")
  14988. (defvar org-columns-current-fmt-compiled nil
  14989. "Local variable, holds the currently active column format.
  14990. This is the compiled version of the format.")
  14991. (defvar org-columns-current-widths nil
  14992. "Loval variable, holds the currently widths of fields.")
  14993. (defvar org-columns-current-maxwidths nil
  14994. "Loval variable, holds the currently active maximum column widths.")
  14995. (defvar org-columns-begin-marker (make-marker)
  14996. "Points to the position where last a column creation command was called.")
  14997. (defvar org-columns-top-level-marker (make-marker)
  14998. "Points to the position where current columns region starts.")
  14999. (defvar org-columns-map (make-sparse-keymap)
  15000. "The keymap valid in column display.")
  15001. (defun org-columns-content ()
  15002. "Switch to contents view while in columns view."
  15003. (interactive)
  15004. (org-overview)
  15005. (org-content))
  15006. (org-defkey org-columns-map "c" 'org-columns-content)
  15007. (org-defkey org-columns-map "o" 'org-overview)
  15008. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15009. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15010. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15011. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15012. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15013. (org-defkey org-columns-map "q" 'org-columns-quit)
  15014. (org-defkey org-columns-map "r" 'org-columns-redo)
  15015. (org-defkey org-columns-map "g" 'org-columns-redo)
  15016. (org-defkey org-columns-map [left] 'backward-char)
  15017. (org-defkey org-columns-map "\M-b" 'backward-char)
  15018. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15019. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15020. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15021. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15022. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15023. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15024. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15025. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15026. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15027. (org-defkey org-columns-map ">" 'org-columns-widen)
  15028. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15029. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15030. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15031. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15032. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15033. '("Column"
  15034. ["Edit property" org-columns-edit-value t]
  15035. ["Next allowed value" org-columns-next-allowed-value t]
  15036. ["Previous allowed value" org-columns-previous-allowed-value t]
  15037. ["Show full value" org-columns-show-value t]
  15038. ["Edit allowed values" org-columns-edit-allowed t]
  15039. "--"
  15040. ["Edit column attributes" org-columns-edit-attributes t]
  15041. ["Increase column width" org-columns-widen t]
  15042. ["Decrease column width" org-columns-narrow t]
  15043. "--"
  15044. ["Move column right" org-columns-move-right t]
  15045. ["Move column left" org-columns-move-left t]
  15046. ["Add column" org-columns-new t]
  15047. ["Delete column" org-columns-delete t]
  15048. "--"
  15049. ["CONTENTS" org-columns-content t]
  15050. ["OVERVIEW" org-overview t]
  15051. ["Refresh columns display" org-columns-redo t]
  15052. "--"
  15053. ["Open link" org-columns-open-link t]
  15054. "--"
  15055. ["Quit" org-columns-quit t]))
  15056. (defun org-columns-new-overlay (beg end &optional string face)
  15057. "Create a new column overlay and add it to the list."
  15058. (let ((ov (org-make-overlay beg end)))
  15059. (org-overlay-put ov 'face (or face 'secondary-selection))
  15060. (org-overlay-display ov string face)
  15061. (push ov org-columns-overlays)
  15062. ov))
  15063. (defun org-columns-display-here (&optional props)
  15064. "Overlay the current line with column display."
  15065. (interactive)
  15066. (let* ((fmt org-columns-current-fmt-compiled)
  15067. (beg (point-at-bol))
  15068. (level-face (save-excursion
  15069. (beginning-of-line 1)
  15070. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15071. (org-get-level-face 2))))
  15072. (color (list :foreground
  15073. (face-attribute (or level-face 'default) :foreground)))
  15074. props pom property ass width f string ov column val modval)
  15075. ;; Check if the entry is in another buffer.
  15076. (unless props
  15077. (if (eq major-mode 'org-agenda-mode)
  15078. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15079. (get-text-property (point) 'org-marker))
  15080. props (if pom (org-entry-properties pom) nil))
  15081. (setq props (org-entry-properties nil))))
  15082. ;; Walk the format
  15083. (while (setq column (pop fmt))
  15084. (setq property (car column)
  15085. ass (if (equal property "ITEM")
  15086. (cons "ITEM"
  15087. (save-match-data
  15088. (org-no-properties
  15089. (org-remove-tabs
  15090. (buffer-substring-no-properties
  15091. (point-at-bol) (point-at-eol))))))
  15092. (assoc property props))
  15093. width (or (cdr (assoc property org-columns-current-maxwidths))
  15094. (nth 2 column)
  15095. (length property))
  15096. f (format "%%-%d.%ds | " width width)
  15097. val (or (cdr ass) "")
  15098. modval (if (equal property "ITEM")
  15099. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15100. string (format f (or modval val)))
  15101. ;; Create the overlay
  15102. (org-unmodified
  15103. (setq ov (org-columns-new-overlay
  15104. beg (setq beg (1+ beg)) string
  15105. (list color 'org-column)))
  15106. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15107. (org-overlay-put ov 'keymap org-columns-map)
  15108. (org-overlay-put ov 'org-columns-key property)
  15109. (org-overlay-put ov 'org-columns-value (cdr ass))
  15110. (org-overlay-put ov 'org-columns-value-modified modval)
  15111. (org-overlay-put ov 'org-columns-pom pom)
  15112. (org-overlay-put ov 'org-columns-format f))
  15113. (if (or (not (char-after beg))
  15114. (equal (char-after beg) ?\n))
  15115. (let ((inhibit-read-only t))
  15116. (save-excursion
  15117. (goto-char beg)
  15118. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15119. ;; Make the rest of the line disappear.
  15120. (org-unmodified
  15121. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15122. (org-overlay-put ov 'invisible t)
  15123. (org-overlay-put ov 'keymap org-columns-map)
  15124. (org-overlay-put ov 'intangible t)
  15125. (push ov org-columns-overlays)
  15126. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15127. (org-overlay-put ov 'keymap org-columns-map)
  15128. (push ov org-columns-overlays)
  15129. (let ((inhibit-read-only t))
  15130. (put-text-property (max (point-min) (1- (point-at-bol)))
  15131. (min (point-max) (1+ (point-at-eol)))
  15132. 'read-only "Type `e' to edit property")))))
  15133. (defvar org-previous-header-line-format nil
  15134. "The header line format before column view was turned on.")
  15135. (defvar org-columns-inhibit-recalculation nil
  15136. "Inhibit recomputing of columns on column view startup.")
  15137. (defvar header-line-format)
  15138. (defun org-columns-display-here-title ()
  15139. "Overlay the newline before the current line with the table title."
  15140. (interactive)
  15141. (let ((fmt org-columns-current-fmt-compiled)
  15142. string (title "")
  15143. property width f column str widths)
  15144. (while (setq column (pop fmt))
  15145. (setq property (car column)
  15146. str (or (nth 1 column) property)
  15147. width (or (cdr (assoc property org-columns-current-maxwidths))
  15148. (nth 2 column)
  15149. (length str))
  15150. widths (push width widths)
  15151. f (format "%%-%d.%ds | " width width)
  15152. string (format f str)
  15153. title (concat title string)))
  15154. (setq title (concat
  15155. (org-add-props " " nil 'display '(space :align-to 0))
  15156. (org-add-props title nil 'face '(:weight bold :underline t))))
  15157. (org-set-local 'org-previous-header-line-format header-line-format)
  15158. (org-set-local 'org-columns-current-widths (nreverse widths))
  15159. (setq header-line-format title)))
  15160. (defun org-columns-remove-overlays ()
  15161. "Remove all currently active column overlays."
  15162. (interactive)
  15163. (when (marker-buffer org-columns-begin-marker)
  15164. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15165. (when (local-variable-p 'org-previous-header-line-format)
  15166. (setq header-line-format org-previous-header-line-format)
  15167. (kill-local-variable 'org-previous-header-line-format))
  15168. (move-marker org-columns-begin-marker nil)
  15169. (move-marker org-columns-top-level-marker nil)
  15170. (org-unmodified
  15171. (mapc 'org-delete-overlay org-columns-overlays)
  15172. (setq org-columns-overlays nil)
  15173. (let ((inhibit-read-only t))
  15174. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15175. (defun org-columns-cleanup-item (item fmt)
  15176. "Remove from ITEM what is a column in the format FMT."
  15177. (if (not org-complex-heading-regexp)
  15178. item
  15179. (when (string-match org-complex-heading-regexp item)
  15180. (concat
  15181. (org-add-props (concat (match-string 1 item) " ") nil
  15182. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15183. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15184. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15185. " " (match-string 4 item)
  15186. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15187. (defun org-columns-show-value ()
  15188. "Show the full value of the property."
  15189. (interactive)
  15190. (let ((value (get-char-property (point) 'org-columns-value)))
  15191. (message "Value is: %s" (or value ""))))
  15192. (defun org-columns-quit ()
  15193. "Remove the column overlays and in this way exit column editing."
  15194. (interactive)
  15195. (org-unmodified
  15196. (org-columns-remove-overlays)
  15197. (let ((inhibit-read-only t))
  15198. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15199. (when (eq major-mode 'org-agenda-mode)
  15200. (message
  15201. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15202. (defun org-columns-check-computed ()
  15203. "Check if this column value is computed.
  15204. If yes, throw an error indicating that changing it does not make sense."
  15205. (let ((val (get-char-property (point) 'org-columns-value)))
  15206. (when (and (stringp val)
  15207. (get-char-property 0 'org-computed val))
  15208. (error "This value is computed from the entry's children"))))
  15209. (defun org-columns-todo (&optional arg)
  15210. "Change the TODO state during column view."
  15211. (interactive "P")
  15212. (org-columns-edit-value "TODO"))
  15213. (defun org-columns-set-tags-or-toggle (&optional arg)
  15214. "Toggle checkbox at point, or set tags for current headline."
  15215. (interactive "P")
  15216. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15217. (get-char-property (point) 'org-columns-value))
  15218. (org-columns-next-allowed-value)
  15219. (org-columns-edit-value "TAGS")))
  15220. (defun org-columns-edit-value (&optional key)
  15221. "Edit the value of the property at point in column view.
  15222. Where possible, use the standard interface for changing this line."
  15223. (interactive)
  15224. (org-columns-check-computed)
  15225. (let* ((external-key key)
  15226. (col (current-column))
  15227. (key (or key (get-char-property (point) 'org-columns-key)))
  15228. (value (get-char-property (point) 'org-columns-value))
  15229. (bol (point-at-bol)) (eol (point-at-eol))
  15230. (pom (or (get-text-property bol 'org-hd-marker)
  15231. (point))) ; keep despite of compiler waring
  15232. (line-overlays
  15233. (delq nil (mapcar (lambda (x)
  15234. (and (eq (overlay-buffer x) (current-buffer))
  15235. (>= (overlay-start x) bol)
  15236. (<= (overlay-start x) eol)
  15237. x))
  15238. org-columns-overlays)))
  15239. nval eval allowed)
  15240. (cond
  15241. ((equal key "CLOCKSUM")
  15242. (error "This special column cannot be edited"))
  15243. ((equal key "ITEM")
  15244. (setq eval '(org-with-point-at pom
  15245. (org-edit-headline))))
  15246. ((equal key "TODO")
  15247. (setq eval '(org-with-point-at pom
  15248. (let ((current-prefix-arg
  15249. (if external-key current-prefix-arg '(4))))
  15250. (call-interactively 'org-todo)))))
  15251. ((equal key "PRIORITY")
  15252. (setq eval '(org-with-point-at pom
  15253. (call-interactively 'org-priority))))
  15254. ((equal key "TAGS")
  15255. (setq eval '(org-with-point-at pom
  15256. (let ((org-fast-tag-selection-single-key
  15257. (if (eq org-fast-tag-selection-single-key 'expert)
  15258. t org-fast-tag-selection-single-key)))
  15259. (call-interactively 'org-set-tags)))))
  15260. ((equal key "DEADLINE")
  15261. (setq eval '(org-with-point-at pom
  15262. (call-interactively 'org-deadline))))
  15263. ((equal key "SCHEDULED")
  15264. (setq eval '(org-with-point-at pom
  15265. (call-interactively 'org-schedule))))
  15266. (t
  15267. (setq allowed (org-property-get-allowed-values pom key 'table))
  15268. (if allowed
  15269. (setq nval (completing-read "Value: " allowed nil t))
  15270. (setq nval (read-string "Edit: " value)))
  15271. (setq nval (org-trim nval))
  15272. (when (not (equal nval value))
  15273. (setq eval '(org-entry-put pom key nval)))))
  15274. (when eval
  15275. (let ((inhibit-read-only t))
  15276. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15277. (unwind-protect
  15278. (progn
  15279. (setq org-columns-overlays
  15280. (org-delete-all line-overlays org-columns-overlays))
  15281. (mapc 'org-delete-overlay line-overlays)
  15282. (org-columns-eval eval))
  15283. (org-columns-display-here))))
  15284. (move-to-column col)
  15285. (if (and (org-mode-p)
  15286. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15287. (org-columns-update key))))
  15288. (defun org-edit-headline () ; FIXME: this is not columns specific
  15289. "Edit the current headline, the part without TODO keyword, TAGS."
  15290. (org-back-to-heading)
  15291. (when (looking-at org-todo-line-regexp)
  15292. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15293. (txt (match-string 3))
  15294. (post "")
  15295. txt2)
  15296. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15297. (setq post (match-string 0 txt)
  15298. txt (substring txt 0 (match-beginning 0))))
  15299. (setq txt2 (read-string "Edit: " txt))
  15300. (when (not (equal txt txt2))
  15301. (beginning-of-line 1)
  15302. (insert pre txt2 post)
  15303. (delete-region (point) (point-at-eol))
  15304. (org-set-tags nil t)))))
  15305. (defun org-columns-edit-allowed ()
  15306. "Edit the list of allowed values for the current property."
  15307. (interactive)
  15308. (let* ((key (get-char-property (point) 'org-columns-key))
  15309. (key1 (concat key "_ALL"))
  15310. (allowed (org-entry-get (point) key1 t))
  15311. nval)
  15312. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15313. (setq nval (read-string "Allowed: " allowed))
  15314. (org-entry-put
  15315. (cond ((marker-position org-entry-property-inherited-from)
  15316. org-entry-property-inherited-from)
  15317. ((marker-position org-columns-top-level-marker)
  15318. org-columns-top-level-marker))
  15319. key1 nval)))
  15320. (defmacro org-no-warnings (&rest body)
  15321. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15322. (defun org-columns-eval (form)
  15323. (let (hidep)
  15324. (save-excursion
  15325. (beginning-of-line 1)
  15326. ;; `next-line' is needed here, because it skips invisible line.
  15327. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15328. (setq hidep (org-on-heading-p 1)))
  15329. (eval form)
  15330. (and hidep (hide-entry))))
  15331. (defun org-columns-previous-allowed-value ()
  15332. "Switch to the previous allowed value for this column."
  15333. (interactive)
  15334. (org-columns-next-allowed-value t))
  15335. (defun org-columns-next-allowed-value (&optional previous)
  15336. "Switch to the next allowed value for this column."
  15337. (interactive)
  15338. (org-columns-check-computed)
  15339. (let* ((col (current-column))
  15340. (key (get-char-property (point) 'org-columns-key))
  15341. (value (get-char-property (point) 'org-columns-value))
  15342. (bol (point-at-bol)) (eol (point-at-eol))
  15343. (pom (or (get-text-property bol 'org-hd-marker)
  15344. (point))) ; keep despite of compiler waring
  15345. (line-overlays
  15346. (delq nil (mapcar (lambda (x)
  15347. (and (eq (overlay-buffer x) (current-buffer))
  15348. (>= (overlay-start x) bol)
  15349. (<= (overlay-start x) eol)
  15350. x))
  15351. org-columns-overlays)))
  15352. (allowed (or (org-property-get-allowed-values pom key)
  15353. (and (memq
  15354. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15355. '(checkbox checkbox-n-of-m checkbox-percent))
  15356. '("[ ]" "[X]"))))
  15357. nval)
  15358. (when (equal key "ITEM")
  15359. (error "Cannot edit item headline from here"))
  15360. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15361. (error "Allowed values for this property have not been defined"))
  15362. (if (member key '("SCHEDULED" "DEADLINE"))
  15363. (setq nval (if previous 'earlier 'later))
  15364. (if previous (setq allowed (reverse allowed)))
  15365. (if (member value allowed)
  15366. (setq nval (car (cdr (member value allowed)))))
  15367. (setq nval (or nval (car allowed)))
  15368. (if (equal nval value)
  15369. (error "Only one allowed value for this property")))
  15370. (let ((inhibit-read-only t))
  15371. (remove-text-properties (1- bol) eol '(read-only t))
  15372. (unwind-protect
  15373. (progn
  15374. (setq org-columns-overlays
  15375. (org-delete-all line-overlays org-columns-overlays))
  15376. (mapc 'org-delete-overlay line-overlays)
  15377. (org-columns-eval '(org-entry-put pom key nval)))
  15378. (org-columns-display-here)))
  15379. (move-to-column col)
  15380. (if (and (org-mode-p)
  15381. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15382. (org-columns-update key))))
  15383. (defun org-verify-version (task)
  15384. (cond
  15385. ((eq task 'columns)
  15386. (if (or (featurep 'xemacs)
  15387. (< emacs-major-version 22))
  15388. (error "Emacs 22 is required for the columns feature")))))
  15389. (defun org-columns-open-link (&optional arg)
  15390. (interactive "P")
  15391. (let ((value (get-char-property (point) 'org-columns-value)))
  15392. (org-open-link-from-string value arg)))
  15393. (defun org-open-link-from-string (s &optional arg)
  15394. "Open a link in the string S, as if it was in Org-mode."
  15395. (interactive)
  15396. (with-temp-buffer
  15397. (let ((org-inhibit-startup t))
  15398. (org-mode)
  15399. (insert s)
  15400. (goto-char (point-min))
  15401. (org-open-at-point arg))))
  15402. (defun org-columns-get-format-and-top-level ()
  15403. (let (fmt)
  15404. (when (condition-case nil (org-back-to-heading) (error nil))
  15405. (move-marker org-entry-property-inherited-from nil)
  15406. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15407. (setq fmt (or fmt org-columns-default-format))
  15408. (org-set-local 'org-columns-current-fmt fmt)
  15409. (org-columns-compile-format fmt)
  15410. (if (marker-position org-entry-property-inherited-from)
  15411. (move-marker org-columns-top-level-marker
  15412. org-entry-property-inherited-from)
  15413. (move-marker org-columns-top-level-marker (point)))
  15414. fmt))
  15415. (defun org-columns ()
  15416. "Turn on column view on an org-mode file."
  15417. (interactive)
  15418. (org-verify-version 'columns)
  15419. (org-columns-remove-overlays)
  15420. (move-marker org-columns-begin-marker (point))
  15421. (let (beg end fmt cache maxwidths)
  15422. (setq fmt (org-columns-get-format-and-top-level))
  15423. (save-excursion
  15424. (goto-char org-columns-top-level-marker)
  15425. (setq beg (point))
  15426. (unless org-columns-inhibit-recalculation
  15427. (org-columns-compute-all))
  15428. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15429. (point-max)))
  15430. ;; Get and cache the properties
  15431. (goto-char beg)
  15432. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15433. (save-excursion
  15434. (save-restriction
  15435. (narrow-to-region beg end)
  15436. (org-clock-sum))))
  15437. (while (re-search-forward (concat "^" outline-regexp) end t)
  15438. (push (cons (org-current-line) (org-entry-properties)) cache))
  15439. (when cache
  15440. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15441. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15442. (org-columns-display-here-title)
  15443. (mapc (lambda (x)
  15444. (goto-line (car x))
  15445. (org-columns-display-here (cdr x)))
  15446. cache)))))
  15447. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15448. "Insert a new column, to the leeft o the current column."
  15449. (interactive)
  15450. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15451. cell)
  15452. (setq prop (completing-read
  15453. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15454. nil nil prop))
  15455. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15456. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15457. (if (string-match "\\S-" width)
  15458. (setq width (string-to-number width))
  15459. (setq width nil))
  15460. (setq fmt (completing-read "Summary [none]: "
  15461. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15462. nil t))
  15463. (if (string-match "\\S-" fmt)
  15464. (setq fmt (intern fmt))
  15465. (setq fmt nil))
  15466. (if (eq fmt 'none) (setq fmt nil))
  15467. (if editp
  15468. (progn
  15469. (setcar editp prop)
  15470. (setcdr editp (list title width nil fmt)))
  15471. (setq cell (nthcdr (1- (current-column))
  15472. org-columns-current-fmt-compiled))
  15473. (setcdr cell (cons (list prop title width nil fmt)
  15474. (cdr cell))))
  15475. (org-columns-store-format)
  15476. (org-columns-redo)))
  15477. (defun org-columns-delete ()
  15478. "Delete the column at point from columns view."
  15479. (interactive)
  15480. (let* ((n (current-column))
  15481. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15482. (when (y-or-n-p
  15483. (format "Are you sure you want to remove column \"%s\"? " title))
  15484. (setq org-columns-current-fmt-compiled
  15485. (delq (nth n org-columns-current-fmt-compiled)
  15486. org-columns-current-fmt-compiled))
  15487. (org-columns-store-format)
  15488. (org-columns-redo)
  15489. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15490. (backward-char 1)))))
  15491. (defun org-columns-edit-attributes ()
  15492. "Edit the attributes of the current column."
  15493. (interactive)
  15494. (let* ((n (current-column))
  15495. (info (nth n org-columns-current-fmt-compiled)))
  15496. (apply 'org-columns-new info)))
  15497. (defun org-columns-widen (arg)
  15498. "Make the column wider by ARG characters."
  15499. (interactive "p")
  15500. (let* ((n (current-column))
  15501. (entry (nth n org-columns-current-fmt-compiled))
  15502. (width (or (nth 2 entry)
  15503. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15504. (setq width (max 1 (+ width arg)))
  15505. (setcar (nthcdr 2 entry) width)
  15506. (org-columns-store-format)
  15507. (org-columns-redo)))
  15508. (defun org-columns-narrow (arg)
  15509. "Make the column nrrower by ARG characters."
  15510. (interactive "p")
  15511. (org-columns-widen (- arg)))
  15512. (defun org-columns-move-right ()
  15513. "Swap this column with the one to the right."
  15514. (interactive)
  15515. (let* ((n (current-column))
  15516. (cell (nthcdr n org-columns-current-fmt-compiled))
  15517. e)
  15518. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15519. (error "Cannot shift this column further to the right"))
  15520. (setq e (car cell))
  15521. (setcar cell (car (cdr cell)))
  15522. (setcdr cell (cons e (cdr (cdr cell))))
  15523. (org-columns-store-format)
  15524. (org-columns-redo)
  15525. (forward-char 1)))
  15526. (defun org-columns-move-left ()
  15527. "Swap this column with the one to the left."
  15528. (interactive)
  15529. (let* ((n (current-column)))
  15530. (when (= n 0)
  15531. (error "Cannot shift this column further to the left"))
  15532. (backward-char 1)
  15533. (org-columns-move-right)
  15534. (backward-char 1)))
  15535. (defun org-columns-store-format ()
  15536. "Store the text version of the current columns format in appropriate place.
  15537. This is either in the COLUMNS property of the node starting the current column
  15538. display, or in the #+COLUMNS line of the current buffer."
  15539. (let (fmt (cnt 0))
  15540. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15541. (org-set-local 'org-columns-current-fmt fmt)
  15542. (if (marker-position org-columns-top-level-marker)
  15543. (save-excursion
  15544. (goto-char org-columns-top-level-marker)
  15545. (if (and (org-at-heading-p)
  15546. (org-entry-get nil "COLUMNS"))
  15547. (org-entry-put nil "COLUMNS" fmt)
  15548. (goto-char (point-min))
  15549. ;; Overwrite all #+COLUMNS lines....
  15550. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15551. (setq cnt (1+ cnt))
  15552. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15553. (unless (> cnt 0)
  15554. (goto-char (point-min))
  15555. (or (org-on-heading-p t) (outline-next-heading))
  15556. (let ((inhibit-read-only t))
  15557. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15558. (org-set-local 'org-columns-default-format fmt))))))
  15559. (defvar org-overriding-columns-format nil
  15560. "When set, overrides any other definition.")
  15561. (defvar org-agenda-view-columns-initially nil
  15562. "When set, switch to columns view immediately after creating the agenda.")
  15563. (defun org-agenda-columns ()
  15564. "Turn on column view in the agenda."
  15565. (interactive)
  15566. (org-verify-version 'columns)
  15567. (org-columns-remove-overlays)
  15568. (move-marker org-columns-begin-marker (point))
  15569. (let (fmt cache maxwidths m)
  15570. (cond
  15571. ((and (local-variable-p 'org-overriding-columns-format)
  15572. org-overriding-columns-format)
  15573. (setq fmt org-overriding-columns-format))
  15574. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15575. (setq fmt (org-entry-get m "COLUMNS" t)))
  15576. ((and (boundp 'org-columns-current-fmt)
  15577. (local-variable-p 'org-columns-current-fmt)
  15578. org-columns-current-fmt)
  15579. (setq fmt org-columns-current-fmt))
  15580. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15581. (setq m (get-text-property m 'org-hd-marker))
  15582. (setq fmt (org-entry-get m "COLUMNS" t))))
  15583. (setq fmt (or fmt org-columns-default-format))
  15584. (org-set-local 'org-columns-current-fmt fmt)
  15585. (org-columns-compile-format fmt)
  15586. (save-excursion
  15587. ;; Get and cache the properties
  15588. (goto-char (point-min))
  15589. (while (not (eobp))
  15590. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15591. (get-text-property (point) 'org-marker)))
  15592. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15593. (beginning-of-line 2))
  15594. (when cache
  15595. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15596. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15597. (org-columns-display-here-title)
  15598. (mapc (lambda (x)
  15599. (goto-line (car x))
  15600. (org-columns-display-here (cdr x)))
  15601. cache)))))
  15602. (defun org-columns-get-autowidth-alist (s cache)
  15603. "Derive the maximum column widths from the format and the cache."
  15604. (let ((start 0) rtn)
  15605. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15606. (push (cons (match-string 1 s) 1) rtn)
  15607. (setq start (match-end 0)))
  15608. (mapc (lambda (x)
  15609. (setcdr x (apply 'max
  15610. (mapcar
  15611. (lambda (y)
  15612. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15613. cache))))
  15614. rtn)
  15615. rtn))
  15616. (defun org-columns-compute-all ()
  15617. "Compute all columns that have operators defined."
  15618. (org-unmodified
  15619. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15620. (let ((columns org-columns-current-fmt-compiled) col)
  15621. (while (setq col (pop columns))
  15622. (when (nth 3 col)
  15623. (save-excursion
  15624. (org-columns-compute (car col)))))))
  15625. (defun org-columns-update (property)
  15626. "Recompute PROPERTY, and update the columns display for it."
  15627. (org-columns-compute property)
  15628. (let (fmt val pos)
  15629. (save-excursion
  15630. (mapc (lambda (ov)
  15631. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15632. (setq pos (org-overlay-start ov))
  15633. (goto-char pos)
  15634. (when (setq val (cdr (assoc property
  15635. (get-text-property
  15636. (point-at-bol) 'org-summaries))))
  15637. (setq fmt (org-overlay-get ov 'org-columns-format))
  15638. (org-overlay-put ov 'org-columns-value val)
  15639. (org-overlay-put ov 'display (format fmt val)))))
  15640. org-columns-overlays))))
  15641. (defun org-columns-compute (property)
  15642. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15643. (interactive)
  15644. (let* ((re (concat "^" outline-regexp))
  15645. (lmax 30) ; Does anyone use deeper levels???
  15646. (lsum (make-vector lmax 0))
  15647. (lflag (make-vector lmax nil))
  15648. (level 0)
  15649. (ass (assoc property org-columns-current-fmt-compiled))
  15650. (format (nth 4 ass))
  15651. (printf (nth 5 ass))
  15652. (beg org-columns-top-level-marker)
  15653. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15654. (save-excursion
  15655. ;; Find the region to compute
  15656. (goto-char beg)
  15657. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15658. (goto-char end)
  15659. ;; Walk the tree from the back and do the computations
  15660. (while (re-search-backward re beg t)
  15661. (setq sumpos (match-beginning 0)
  15662. last-level level
  15663. level (org-outline-level)
  15664. val (org-entry-get nil property)
  15665. valflag (and val (string-match "\\S-" val)))
  15666. (cond
  15667. ((< level last-level)
  15668. ;; put the sum of lower levels here as a property
  15669. (setq sum (aref lsum last-level) ; current sum
  15670. flag (aref lflag last-level) ; any valid entries from children?
  15671. str (org-column-number-to-string sum format printf)
  15672. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15673. useval (if flag str1 (if valflag val ""))
  15674. sum-alist (get-text-property sumpos 'org-summaries))
  15675. (if (assoc property sum-alist)
  15676. (setcdr (assoc property sum-alist) useval)
  15677. (push (cons property useval) sum-alist)
  15678. (org-unmodified
  15679. (add-text-properties sumpos (1+ sumpos)
  15680. (list 'org-summaries sum-alist))))
  15681. (when val
  15682. (org-entry-put nil property (if flag str val)))
  15683. ;; add current to current level accumulator
  15684. (when (or flag valflag)
  15685. (aset lsum level (+ (aref lsum level)
  15686. (if flag sum (org-column-string-to-number
  15687. (if flag str val) format))))
  15688. (aset lflag level t))
  15689. ;; clear accumulators for deeper levels
  15690. (loop for l from (1+ level) to (1- lmax) do
  15691. (aset lsum l 0)
  15692. (aset lflag l nil)))
  15693. ((>= level last-level)
  15694. ;; add what we have here to the accumulator for this level
  15695. (aset lsum level (+ (aref lsum level)
  15696. (org-column-string-to-number (or val "0") format)))
  15697. (and valflag (aset lflag level t)))
  15698. (t (error "This should not happen")))))))
  15699. (defun org-columns-redo ()
  15700. "Construct the column display again."
  15701. (interactive)
  15702. (message "Recomputing columns...")
  15703. (save-excursion
  15704. (if (marker-position org-columns-begin-marker)
  15705. (goto-char org-columns-begin-marker))
  15706. (org-columns-remove-overlays)
  15707. (if (org-mode-p)
  15708. (call-interactively 'org-columns)
  15709. (call-interactively 'org-agenda-columns)))
  15710. (message "Recomputing columns...done"))
  15711. (defun org-columns-not-in-agenda ()
  15712. (if (eq major-mode 'org-agenda-mode)
  15713. (error "This command is only allowed in Org-mode buffers")))
  15714. (defun org-string-to-number (s)
  15715. "Convert string to number, and interpret hh:mm:ss."
  15716. (if (not (string-match ":" s))
  15717. (string-to-number s)
  15718. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15719. (while l
  15720. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15721. sum)))
  15722. (defun org-column-number-to-string (n fmt &optional printf)
  15723. "Convert a computed column number to a string value, according to FMT."
  15724. (cond
  15725. ((eq fmt 'add_times)
  15726. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15727. (format "%d:%02d" h m)))
  15728. ((eq fmt 'checkbox)
  15729. (cond ((= n (floor n)) "[X]")
  15730. ((> n 1.) "[-]")
  15731. (t "[ ]")))
  15732. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15733. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15734. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15735. (printf (format printf n))
  15736. ((eq fmt 'currency)
  15737. (format "%.2f" n))
  15738. (t (number-to-string n))))
  15739. (defun org-nofm-to-completion (n m &optional percent)
  15740. (if (not percent)
  15741. (format "[%d/%d]" n m)
  15742. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15743. (defun org-column-string-to-number (s fmt)
  15744. "Convert a column value to a number that can be used for column computing."
  15745. (cond
  15746. ((string-match ":" s)
  15747. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15748. (while l
  15749. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15750. sum))
  15751. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15752. (if (equal s "[X]") 1. 0.000001))
  15753. (t (string-to-number s))))
  15754. (defun org-columns-uncompile-format (cfmt)
  15755. "Turn the compiled columns format back into a string representation."
  15756. (let ((rtn "") e s prop title op width fmt printf)
  15757. (while (setq e (pop cfmt))
  15758. (setq prop (car e)
  15759. title (nth 1 e)
  15760. width (nth 2 e)
  15761. op (nth 3 e)
  15762. fmt (nth 4 e)
  15763. printf (nth 5 e))
  15764. (cond
  15765. ((eq fmt 'add_times) (setq op ":"))
  15766. ((eq fmt 'checkbox) (setq op "X"))
  15767. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15768. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15769. ((eq fmt 'add_numbers) (setq op "+"))
  15770. ((eq fmt 'currency) (setq op "$")))
  15771. (if (and op printf) (setq op (concat op ";" printf)))
  15772. (if (equal title prop) (setq title nil))
  15773. (setq s (concat "%" (if width (number-to-string width))
  15774. prop
  15775. (if title (concat "(" title ")"))
  15776. (if op (concat "{" op "}"))))
  15777. (setq rtn (concat rtn " " s)))
  15778. (org-trim rtn)))
  15779. (defun org-columns-compile-format (fmt)
  15780. "Turn a column format string into an alist of specifications.
  15781. The alist has one entry for each column in the format. The elements of
  15782. that list are:
  15783. property the property
  15784. title the title field for the columns
  15785. width the column width in characters, can be nil for automatic
  15786. operator the operator if any
  15787. format the output format for computed results, derived from operator
  15788. printf a printf format for computed values"
  15789. (let ((start 0) width prop title op f printf)
  15790. (setq org-columns-current-fmt-compiled nil)
  15791. (while (string-match
  15792. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15793. fmt start)
  15794. (setq start (match-end 0)
  15795. width (match-string 1 fmt)
  15796. prop (match-string 2 fmt)
  15797. title (or (match-string 3 fmt) prop)
  15798. op (match-string 4 fmt)
  15799. f nil
  15800. printf nil)
  15801. (if width (setq width (string-to-number width)))
  15802. (when (and op (string-match ";" op))
  15803. (setq printf (substring op (match-end 0))
  15804. op (substring op 0 (match-beginning 0))))
  15805. (cond
  15806. ((equal op "+") (setq f 'add_numbers))
  15807. ((equal op "$") (setq f 'currency))
  15808. ((equal op ":") (setq f 'add_times))
  15809. ((equal op "X") (setq f 'checkbox))
  15810. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15811. ((equal op "X%") (setq f 'checkbox-percent))
  15812. )
  15813. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15814. (setq org-columns-current-fmt-compiled
  15815. (nreverse org-columns-current-fmt-compiled))))
  15816. ;;; Dynamic block for Column view
  15817. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  15818. "Get the column view of the current buffer or subtree.
  15819. The first optional argument MAXLEVEL sets the level limit. A
  15820. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  15821. empty rows, an empty row being one where all the column view
  15822. specifiers except ITEM are empty. This function returns a list
  15823. containing the title row and all other rows. Each row is a list
  15824. of fields."
  15825. (save-excursion
  15826. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15827. (n (length title)) row tbl)
  15828. (goto-char (point-min))
  15829. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  15830. (or (null maxlevel)
  15831. (>= maxlevel
  15832. (if org-odd-levels-only
  15833. (/ (1+ (length (match-string 1))) 2)
  15834. (length (match-string 1))))))
  15835. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15836. (setq row nil)
  15837. (loop for i from 0 to (1- n) do
  15838. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15839. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15840. "")
  15841. row))
  15842. (setq row (nreverse row))
  15843. (unless (and skip-empty-rows
  15844. (eq 1 (length (delete "" (delete-dups row)))))
  15845. (push row tbl))))
  15846. (append (list title 'hline) (nreverse tbl)))))
  15847. (defun org-dblock-write:columnview (params)
  15848. "Write the column view table.
  15849. PARAMS is a property list of parameters:
  15850. :width enforce same column widths with <N> specifiers.
  15851. :id the :ID: property of the entry where the columns view
  15852. should be built, as a string. When `local', call locally.
  15853. When `global' call column view with the cursor at the beginning
  15854. of the buffer (usually this means that the whole buffer switches
  15855. to column view).
  15856. :hlines When t, insert a hline before each item. When a number, insert
  15857. a hline before each level <= that number.
  15858. :vlines When t, make each column a colgroup to enforce vertical lines.
  15859. :maxlevel When set to a number, don't capture headlines below this level.
  15860. :skip-empty-rows
  15861. When t, skip rows where all specifiers other than ITEM are empty."
  15862. (let ((pos (move-marker (make-marker) (point)))
  15863. (hlines (plist-get params :hlines))
  15864. (vlines (plist-get params :vlines))
  15865. (maxlevel (plist-get params :maxlevel))
  15866. (skip-empty-rows (plist-get params :skip-empty-rows))
  15867. tbl id idpos nfields tmp)
  15868. (save-excursion
  15869. (save-restriction
  15870. (when (setq id (plist-get params :id))
  15871. (cond ((not id) nil)
  15872. ((eq id 'global) (goto-char (point-min)))
  15873. ((eq id 'local) nil)
  15874. ((setq idpos (org-find-entry-with-id id))
  15875. (goto-char idpos))
  15876. (t (error "Cannot find entry with :ID: %s" id))))
  15877. (org-columns)
  15878. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  15879. (setq nfields (length (car tbl)))
  15880. (org-columns-quit)))
  15881. (goto-char pos)
  15882. (move-marker pos nil)
  15883. (when tbl
  15884. (when (plist-get params :hlines)
  15885. (setq tmp nil)
  15886. (while tbl
  15887. (if (eq (car tbl) 'hline)
  15888. (push (pop tbl) tmp)
  15889. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15890. (if (and (not (eq (car tmp) 'hline))
  15891. (or (eq hlines t)
  15892. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15893. (push 'hline tmp)))
  15894. (push (pop tbl) tmp)))
  15895. (setq tbl (nreverse tmp)))
  15896. (when vlines
  15897. (setq tbl (mapcar (lambda (x)
  15898. (if (eq 'hline x) x (cons "" x)))
  15899. tbl))
  15900. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15901. (setq pos (point))
  15902. (insert (org-listtable-to-string tbl))
  15903. (when (plist-get params :width)
  15904. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15905. org-columns-current-widths "|")))
  15906. (goto-char pos)
  15907. (org-table-align))))
  15908. (defun org-listtable-to-string (tbl)
  15909. "Convert a listtable TBL to a string that contains the Org-mode table.
  15910. The table still need to be alligned. The resulting string has no leading
  15911. and tailing newline characters."
  15912. (mapconcat
  15913. (lambda (x)
  15914. (cond
  15915. ((listp x)
  15916. (concat "|" (mapconcat 'identity x "|") "|"))
  15917. ((eq x 'hline) "|-|")
  15918. (t (error "Garbage in listtable: %s" x))))
  15919. tbl "\n"))
  15920. (defun org-insert-columns-dblock ()
  15921. "Create a dynamic block capturing a column view table."
  15922. (interactive)
  15923. (let ((defaults '(:name "columnview" :hlines 1))
  15924. (id (completing-read
  15925. "Capture columns (local, global, entry with :ID: property) [local]: "
  15926. (append '(("global") ("local"))
  15927. (mapcar 'list (org-property-values "ID"))))))
  15928. (if (equal id "") (setq id 'local))
  15929. (if (equal id "global") (setq id 'global))
  15930. (setq defaults (append defaults (list :id id)))
  15931. (org-create-dblock defaults)
  15932. (org-update-dblock)))
  15933. ;;;; Timestamps
  15934. (defvar org-last-changed-timestamp nil)
  15935. (defvar org-time-was-given) ; dynamically scoped parameter
  15936. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15937. (defvar org-ts-what) ; dynamically scoped parameter
  15938. (defun org-time-stamp (arg)
  15939. "Prompt for a date/time and insert a time stamp.
  15940. If the user specifies a time like HH:MM, or if this command is called
  15941. with a prefix argument, the time stamp will contain date and time.
  15942. Otherwise, only the date will be included. All parts of a date not
  15943. specified by the user will be filled in from the current date/time.
  15944. So if you press just return without typing anything, the time stamp
  15945. will represent the current date/time. If there is already a timestamp
  15946. at the cursor, it will be modified."
  15947. (interactive "P")
  15948. (let* ((ts nil)
  15949. (default-time
  15950. ;; Default time is either today, or, when entering a range,
  15951. ;; the range start.
  15952. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15953. (save-excursion
  15954. (re-search-backward
  15955. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15956. (- (point) 20) t)))
  15957. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15958. (current-time)))
  15959. (default-input (and ts (org-get-compact-tod ts)))
  15960. org-time-was-given org-end-time-was-given time)
  15961. (cond
  15962. ((and (org-at-timestamp-p)
  15963. (eq last-command 'org-time-stamp)
  15964. (eq this-command 'org-time-stamp))
  15965. (insert "--")
  15966. (setq time (let ((this-command this-command))
  15967. (org-read-date arg 'totime nil nil default-time default-input)))
  15968. (org-insert-time-stamp time (or org-time-was-given arg)))
  15969. ((org-at-timestamp-p)
  15970. (setq time (let ((this-command this-command))
  15971. (org-read-date arg 'totime nil nil default-time default-input)))
  15972. (when (org-at-timestamp-p) ; just to get the match data
  15973. (replace-match "")
  15974. (setq org-last-changed-timestamp
  15975. (org-insert-time-stamp
  15976. time (or org-time-was-given arg)
  15977. nil nil nil (list org-end-time-was-given))))
  15978. (message "Timestamp updated"))
  15979. (t
  15980. (setq time (let ((this-command this-command))
  15981. (org-read-date arg 'totime nil nil default-time default-input)))
  15982. (org-insert-time-stamp time (or org-time-was-given arg)
  15983. nil nil nil (list org-end-time-was-given))))))
  15984. ;; FIXME: can we use this for something else????
  15985. ;; like computing time differences?????
  15986. (defun org-get-compact-tod (s)
  15987. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15988. (let* ((t1 (match-string 1 s))
  15989. (h1 (string-to-number (match-string 2 s)))
  15990. (m1 (string-to-number (match-string 3 s)))
  15991. (t2 (and (match-end 4) (match-string 5 s)))
  15992. (h2 (and t2 (string-to-number (match-string 6 s))))
  15993. (m2 (and t2 (string-to-number (match-string 7 s))))
  15994. dh dm)
  15995. (if (not t2)
  15996. t1
  15997. (setq dh (- h2 h1) dm (- m2 m1))
  15998. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15999. (concat t1 "+" (number-to-string dh)
  16000. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16001. (defun org-time-stamp-inactive (&optional arg)
  16002. "Insert an inactive time stamp.
  16003. An inactive time stamp is enclosed in square brackets instead of angle
  16004. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16005. does not link to the calendar and cannot be changed with the S-cursor keys.
  16006. So these are more for recording a certain time/date."
  16007. (interactive "P")
  16008. (let (org-time-was-given org-end-time-was-given time)
  16009. (setq time (org-read-date arg 'totime))
  16010. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16011. nil nil (list org-end-time-was-given))))
  16012. (defvar org-date-ovl (org-make-overlay 1 1))
  16013. (org-overlay-put org-date-ovl 'face 'org-warning)
  16014. (org-detach-overlay org-date-ovl)
  16015. (defvar org-ans1) ; dynamically scoped parameter
  16016. (defvar org-ans2) ; dynamically scoped parameter
  16017. (defvar org-plain-time-of-day-regexp) ; defined below
  16018. (defvar org-read-date-overlay nil)
  16019. (defvar org-dcst nil) ; dynamically scoped
  16020. (defun org-read-date (&optional with-time to-time from-string prompt
  16021. default-time default-input)
  16022. "Read a date, possibly a time, and make things smooth for the user.
  16023. The prompt will suggest to enter an ISO date, but you can also enter anything
  16024. which will at least partially be understood by `parse-time-string'.
  16025. Unrecognized parts of the date will default to the current day, month, year,
  16026. hour and minute. If this command is called to replace a timestamp at point,
  16027. of to enter the second timestamp of a range, the default time is taken from the
  16028. existing stamp. For example,
  16029. 3-2-5 --> 2003-02-05
  16030. feb 15 --> currentyear-02-15
  16031. sep 12 9 --> 2009-09-12
  16032. 12:45 --> today 12:45
  16033. 22 sept 0:34 --> currentyear-09-22 0:34
  16034. 12 --> currentyear-currentmonth-12
  16035. Fri --> nearest Friday (today or later)
  16036. etc.
  16037. Furthermore you can specify a relative date by giving, as the *first* thing
  16038. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16039. change in days weeks, months, years.
  16040. With a single plus or minus, the date is relative to today. With a double
  16041. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16042. +4d --> four days from today
  16043. +4 --> same as above
  16044. +2w --> two weeks from today
  16045. ++5 --> five days from default date
  16046. The function understands only English month and weekday abbreviations,
  16047. but this can be configured with the variables `parse-time-months' and
  16048. `parse-time-weekdays'.
  16049. While prompting, a calendar is popped up - you can also select the
  16050. date with the mouse (button 1). The calendar shows a period of three
  16051. months. To scroll it to other months, use the keys `>' and `<'.
  16052. If you don't like the calendar, turn it off with
  16053. \(setq org-read-date-popup-calendar nil)
  16054. With optional argument TO-TIME, the date will immediately be converted
  16055. to an internal time.
  16056. With an optional argument WITH-TIME, the prompt will suggest to also
  16057. insert a time. Note that when WITH-TIME is not set, you can still
  16058. enter a time, and this function will inform the calling routine about
  16059. this change. The calling routine may then choose to change the format
  16060. used to insert the time stamp into the buffer to include the time.
  16061. With optional argument FROM-STRING, read from this string instead from
  16062. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16063. the time/date that is used for everything that is not specified by the
  16064. user."
  16065. (require 'parse-time)
  16066. (let* ((org-time-stamp-rounding-minutes
  16067. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  16068. (org-dcst org-display-custom-times)
  16069. (ct (org-current-time))
  16070. (def (or default-time ct))
  16071. (defdecode (decode-time def))
  16072. (dummy (progn
  16073. (when (< (nth 2 defdecode) org-extend-today-until)
  16074. (setcar (nthcdr 2 defdecode) -1)
  16075. (setcar (nthcdr 1 defdecode) 59)
  16076. (setq def (apply 'encode-time defdecode)
  16077. defdecode (decode-time def)))))
  16078. (calendar-move-hook nil)
  16079. (view-diary-entries-initially nil)
  16080. (view-calendar-holidays-initially nil)
  16081. (timestr (format-time-string
  16082. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16083. (prompt (concat (if prompt (concat prompt " ") "")
  16084. (format "Date+time [%s]: " timestr)))
  16085. ans (org-ans0 "") org-ans1 org-ans2 final)
  16086. (cond
  16087. (from-string (setq ans from-string))
  16088. (org-read-date-popup-calendar
  16089. (save-excursion
  16090. (save-window-excursion
  16091. (calendar)
  16092. (calendar-forward-day (- (time-to-days def)
  16093. (calendar-absolute-from-gregorian
  16094. (calendar-current-date))))
  16095. (org-eval-in-calendar nil t)
  16096. (let* ((old-map (current-local-map))
  16097. (map (copy-keymap calendar-mode-map))
  16098. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16099. (org-defkey map (kbd "RET") 'org-calendar-select)
  16100. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16101. 'org-calendar-select-mouse)
  16102. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16103. 'org-calendar-select-mouse)
  16104. (org-defkey minibuffer-local-map [(meta shift left)]
  16105. (lambda () (interactive)
  16106. (org-eval-in-calendar '(calendar-backward-month 1))))
  16107. (org-defkey minibuffer-local-map [(meta shift right)]
  16108. (lambda () (interactive)
  16109. (org-eval-in-calendar '(calendar-forward-month 1))))
  16110. (org-defkey minibuffer-local-map [(meta shift up)]
  16111. (lambda () (interactive)
  16112. (org-eval-in-calendar '(calendar-backward-year 1))))
  16113. (org-defkey minibuffer-local-map [(meta shift down)]
  16114. (lambda () (interactive)
  16115. (org-eval-in-calendar '(calendar-forward-year 1))))
  16116. (org-defkey minibuffer-local-map [(shift up)]
  16117. (lambda () (interactive)
  16118. (org-eval-in-calendar '(calendar-backward-week 1))))
  16119. (org-defkey minibuffer-local-map [(shift down)]
  16120. (lambda () (interactive)
  16121. (org-eval-in-calendar '(calendar-forward-week 1))))
  16122. (org-defkey minibuffer-local-map [(shift left)]
  16123. (lambda () (interactive)
  16124. (org-eval-in-calendar '(calendar-backward-day 1))))
  16125. (org-defkey minibuffer-local-map [(shift right)]
  16126. (lambda () (interactive)
  16127. (org-eval-in-calendar '(calendar-forward-day 1))))
  16128. (org-defkey minibuffer-local-map ">"
  16129. (lambda () (interactive)
  16130. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16131. (org-defkey minibuffer-local-map "<"
  16132. (lambda () (interactive)
  16133. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16134. (unwind-protect
  16135. (progn
  16136. (use-local-map map)
  16137. (add-hook 'post-command-hook 'org-read-date-display)
  16138. (setq org-ans0 (read-string prompt default-input nil nil))
  16139. ;; org-ans0: from prompt
  16140. ;; org-ans1: from mouse click
  16141. ;; org-ans2: from calendar motion
  16142. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16143. (remove-hook 'post-command-hook 'org-read-date-display)
  16144. (use-local-map old-map)
  16145. (when org-read-date-overlay
  16146. (org-delete-overlay org-read-date-overlay)
  16147. (setq org-read-date-overlay nil)))))))
  16148. (t ; Naked prompt only
  16149. (unwind-protect
  16150. (setq ans (read-string prompt default-input nil timestr))
  16151. (when org-read-date-overlay
  16152. (org-delete-overlay org-read-date-overlay)
  16153. (setq org-read-date-overlay nil)))))
  16154. (setq final (org-read-date-analyze ans def defdecode))
  16155. (if to-time
  16156. (apply 'encode-time final)
  16157. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16158. (format "%04d-%02d-%02d %02d:%02d"
  16159. (nth 5 final) (nth 4 final) (nth 3 final)
  16160. (nth 2 final) (nth 1 final))
  16161. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16162. (defvar def)
  16163. (defvar defdecode)
  16164. (defvar with-time)
  16165. (defun org-read-date-display ()
  16166. "Display the currrent date prompt interpretation in the minibuffer."
  16167. (when org-read-date-display-live
  16168. (when org-read-date-overlay
  16169. (org-delete-overlay org-read-date-overlay))
  16170. (let ((p (point)))
  16171. (end-of-line 1)
  16172. (while (not (equal (buffer-substring
  16173. (max (point-min) (- (point) 4)) (point))
  16174. " "))
  16175. (insert " "))
  16176. (goto-char p))
  16177. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16178. " " (or org-ans1 org-ans2)))
  16179. (org-end-time-was-given nil)
  16180. (f (org-read-date-analyze ans def defdecode))
  16181. (fmts (if org-dcst
  16182. org-time-stamp-custom-formats
  16183. org-time-stamp-formats))
  16184. (fmt (if (or with-time
  16185. (and (boundp 'org-time-was-given) org-time-was-given))
  16186. (cdr fmts)
  16187. (car fmts)))
  16188. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16189. (when (and org-end-time-was-given
  16190. (string-match org-plain-time-of-day-regexp txt))
  16191. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16192. org-end-time-was-given
  16193. (substring txt (match-end 0)))))
  16194. (setq org-read-date-overlay
  16195. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16196. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16197. (defun org-read-date-analyze (ans def defdecode)
  16198. "Analyze the combined answer of the date prompt."
  16199. ;; FIXME: cleanup and comment
  16200. (let (delta deltan deltaw deltadef year month day
  16201. hour minute second wday pm h2 m2 tl wday1)
  16202. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16203. (setq ans (replace-match "" t t ans)
  16204. deltan (car delta)
  16205. deltaw (nth 1 delta)
  16206. deltadef (nth 2 delta)))
  16207. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16208. (when (string-match
  16209. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16210. (setq year (if (match-end 2)
  16211. (string-to-number (match-string 2 ans))
  16212. (string-to-number (format-time-string "%Y")))
  16213. month (string-to-number (match-string 3 ans))
  16214. day (string-to-number (match-string 4 ans)))
  16215. (if (< year 100) (setq year (+ 2000 year)))
  16216. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16217. t nil ans)))
  16218. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16219. ;; If there is a time with am/pm, and *no* time without it, we convert
  16220. ;; so that matching will be successful.
  16221. (loop for i from 1 to 2 do ; twice, for end time as well
  16222. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16223. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16224. (setq hour (string-to-number (match-string 1 ans))
  16225. minute (if (match-end 3)
  16226. (string-to-number (match-string 3 ans))
  16227. 0)
  16228. pm (equal ?p
  16229. (string-to-char (downcase (match-string 4 ans)))))
  16230. (if (and (= hour 12) (not pm))
  16231. (setq hour 0)
  16232. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16233. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16234. t t ans))))
  16235. ;; Check if a time range is given as a duration
  16236. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16237. (setq hour (string-to-number (match-string 1 ans))
  16238. h2 (+ hour (string-to-number (match-string 3 ans)))
  16239. minute (string-to-number (match-string 2 ans))
  16240. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16241. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16242. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16243. ;; Check if there is a time range
  16244. (when (boundp 'org-end-time-was-given)
  16245. (setq org-time-was-given nil)
  16246. (when (and (string-match org-plain-time-of-day-regexp ans)
  16247. (match-end 8))
  16248. (setq org-end-time-was-given (match-string 8 ans))
  16249. (setq ans (concat (substring ans 0 (match-beginning 7))
  16250. (substring ans (match-end 7))))))
  16251. (setq tl (parse-time-string ans)
  16252. day (or (nth 3 tl) (nth 3 defdecode))
  16253. month (or (nth 4 tl)
  16254. (if (and org-read-date-prefer-future
  16255. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16256. (1+ (nth 4 defdecode))
  16257. (nth 4 defdecode)))
  16258. year (or (nth 5 tl)
  16259. (if (and org-read-date-prefer-future
  16260. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16261. (1+ (nth 5 defdecode))
  16262. (nth 5 defdecode)))
  16263. hour (or (nth 2 tl) (nth 2 defdecode))
  16264. minute (or (nth 1 tl) (nth 1 defdecode))
  16265. second (or (nth 0 tl) 0)
  16266. wday (nth 6 tl))
  16267. (when deltan
  16268. (unless deltadef
  16269. (let ((now (decode-time (current-time))))
  16270. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16271. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16272. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16273. ((equal deltaw "m") (setq month (+ month deltan)))
  16274. ((equal deltaw "y") (setq year (+ year deltan)))))
  16275. (when (and wday (not (nth 3 tl)))
  16276. ;; Weekday was given, but no day, so pick that day in the week
  16277. ;; on or after the derived date.
  16278. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16279. (unless (equal wday wday1)
  16280. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16281. (if (and (boundp 'org-time-was-given)
  16282. (nth 2 tl))
  16283. (setq org-time-was-given t))
  16284. (if (< year 100) (setq year (+ 2000 year)))
  16285. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16286. (list second minute hour day month year)))
  16287. (defvar parse-time-weekdays)
  16288. (defun org-read-date-get-relative (s today default)
  16289. "Check string S for special relative date string.
  16290. TODAY and DEFAULT are internal times, for today and for a default.
  16291. Return shift list (N what def-flag)
  16292. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16293. N is the number of WHATs to shift.
  16294. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16295. the DEFAULT date rather than TODAY."
  16296. (when (string-match
  16297. (concat
  16298. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16299. "\\([0-9]+\\)?"
  16300. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16301. "\\([ \t]\\|$\\)") s)
  16302. (let* ((dir (if (match-end 1)
  16303. (string-to-char (substring (match-string 1 s) -1))
  16304. ?+))
  16305. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16306. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16307. (what (if (match-end 3) (match-string 3 s) "d"))
  16308. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16309. (date (if rel default today))
  16310. (wday (nth 6 (decode-time date)))
  16311. delta)
  16312. (if wday1
  16313. (progn
  16314. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16315. (if (= dir ?-) (setq delta (- delta 7)))
  16316. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16317. (list delta "d" rel))
  16318. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16319. (defun org-eval-in-calendar (form &optional keepdate)
  16320. "Eval FORM in the calendar window and return to current window.
  16321. Also, store the cursor date in variable org-ans2."
  16322. (let ((sw (selected-window)))
  16323. (select-window (get-buffer-window "*Calendar*"))
  16324. (eval form)
  16325. (when (and (not keepdate) (calendar-cursor-to-date))
  16326. (let* ((date (calendar-cursor-to-date))
  16327. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16328. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16329. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16330. (select-window sw)))
  16331. ; ;; Update the prompt to show new default date
  16332. ; (save-excursion
  16333. ; (goto-char (point-min))
  16334. ; (when (and org-ans2
  16335. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16336. ; (get-text-property (match-end 0) 'field))
  16337. ; (let ((inhibit-read-only t))
  16338. ; (replace-match (concat "[" org-ans2 "]") t t)
  16339. ; (add-text-properties (point-min) (1+ (match-end 0))
  16340. ; (text-properties-at (1+ (point-min)))))))))
  16341. (defun org-calendar-select ()
  16342. "Return to `org-read-date' with the date currently selected.
  16343. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16344. (interactive)
  16345. (when (calendar-cursor-to-date)
  16346. (let* ((date (calendar-cursor-to-date))
  16347. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16348. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16349. (if (active-minibuffer-window) (exit-minibuffer))))
  16350. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16351. "Insert a date stamp for the date given by the internal TIME.
  16352. WITH-HM means, use the stamp format that includes the time of the day.
  16353. INACTIVE means use square brackets instead of angular ones, so that the
  16354. stamp will not contribute to the agenda.
  16355. PRE and POST are optional strings to be inserted before and after the
  16356. stamp.
  16357. The command returns the inserted time stamp."
  16358. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16359. stamp)
  16360. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16361. (insert-before-markers (or pre ""))
  16362. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16363. (when (listp extra)
  16364. (setq extra (car extra))
  16365. (if (and (stringp extra)
  16366. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16367. (setq extra (format "-%02d:%02d"
  16368. (string-to-number (match-string 1 extra))
  16369. (string-to-number (match-string 2 extra))))
  16370. (setq extra nil)))
  16371. (when extra
  16372. (backward-char 1)
  16373. (insert-before-markers extra)
  16374. (forward-char 1))
  16375. (insert-before-markers (or post ""))
  16376. stamp))
  16377. (defun org-toggle-time-stamp-overlays ()
  16378. "Toggle the use of custom time stamp formats."
  16379. (interactive)
  16380. (setq org-display-custom-times (not org-display-custom-times))
  16381. (unless org-display-custom-times
  16382. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16383. (while (setq p (next-single-property-change p 'display))
  16384. (if (and (get-text-property p 'display)
  16385. (eq (get-text-property p 'face) 'org-date))
  16386. (remove-text-properties
  16387. p (setq p (next-single-property-change p 'display))
  16388. '(display t))))
  16389. (set-buffer-modified-p bmp)))
  16390. (if (featurep 'xemacs)
  16391. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16392. (org-restart-font-lock)
  16393. (setq org-table-may-need-update t)
  16394. (if org-display-custom-times
  16395. (message "Time stamps are overlayed with custom format")
  16396. (message "Time stamp overlays removed")))
  16397. (defun org-display-custom-time (beg end)
  16398. "Overlay modified time stamp format over timestamp between BED and END."
  16399. (let* ((ts (buffer-substring beg end))
  16400. t1 w1 with-hm tf time str w2 (off 0))
  16401. (save-match-data
  16402. (setq t1 (org-parse-time-string ts t))
  16403. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  16404. (setq off (- (match-end 0) (match-beginning 0)))))
  16405. (setq end (- end off))
  16406. (setq w1 (- end beg)
  16407. with-hm (and (nth 1 t1) (nth 2 t1))
  16408. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16409. time (org-fix-decoded-time t1)
  16410. str (org-add-props
  16411. (format-time-string
  16412. (substring tf 1 -1) (apply 'encode-time time))
  16413. nil 'mouse-face 'highlight)
  16414. w2 (length str))
  16415. (if (not (= w2 w1))
  16416. (add-text-properties (1+ beg) (+ 2 beg)
  16417. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16418. (if (featurep 'xemacs)
  16419. (progn
  16420. (put-text-property beg end 'invisible t)
  16421. (put-text-property beg end 'end-glyph (make-glyph str)))
  16422. (put-text-property beg end 'display str))))
  16423. (defun org-translate-time (string)
  16424. "Translate all timestamps in STRING to custom format.
  16425. But do this only if the variable `org-display-custom-times' is set."
  16426. (when org-display-custom-times
  16427. (save-match-data
  16428. (let* ((start 0)
  16429. (re org-ts-regexp-both)
  16430. t1 with-hm inactive tf time str beg end)
  16431. (while (setq start (string-match re string start))
  16432. (setq beg (match-beginning 0)
  16433. end (match-end 0)
  16434. t1 (save-match-data
  16435. (org-parse-time-string (substring string beg end) t))
  16436. with-hm (and (nth 1 t1) (nth 2 t1))
  16437. inactive (equal (substring string beg (1+ beg)) "[")
  16438. tf (funcall (if with-hm 'cdr 'car)
  16439. org-time-stamp-custom-formats)
  16440. time (org-fix-decoded-time t1)
  16441. str (format-time-string
  16442. (concat
  16443. (if inactive "[" "<") (substring tf 1 -1)
  16444. (if inactive "]" ">"))
  16445. (apply 'encode-time time))
  16446. string (replace-match str t t string)
  16447. start (+ start (length str)))))))
  16448. string)
  16449. (defun org-fix-decoded-time (time)
  16450. "Set 0 instead of nil for the first 6 elements of time.
  16451. Don't touch the rest."
  16452. (let ((n 0))
  16453. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16454. (defun org-days-to-time (timestamp-string)
  16455. "Difference between TIMESTAMP-STRING and now in days."
  16456. (- (time-to-days (org-time-string-to-time timestamp-string))
  16457. (time-to-days (current-time))))
  16458. (defun org-deadline-close (timestamp-string &optional ndays)
  16459. "Is the time in TIMESTAMP-STRING close to the current date?"
  16460. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16461. (and (< (org-days-to-time timestamp-string) ndays)
  16462. (not (org-entry-is-done-p))))
  16463. (defun org-get-wdays (ts)
  16464. "Get the deadline lead time appropriate for timestring TS."
  16465. (cond
  16466. ((<= org-deadline-warning-days 0)
  16467. ;; 0 or negative, enforce this value no matter what
  16468. (- org-deadline-warning-days))
  16469. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16470. ;; lead time is specified.
  16471. (floor (* (string-to-number (match-string 1 ts))
  16472. (cdr (assoc (match-string 2 ts)
  16473. '(("d" . 1) ("w" . 7)
  16474. ("m" . 30.4) ("y" . 365.25)))))))
  16475. ;; go for the default.
  16476. (t org-deadline-warning-days)))
  16477. (defun org-calendar-select-mouse (ev)
  16478. "Return to `org-read-date' with the date currently selected.
  16479. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16480. (interactive "e")
  16481. (mouse-set-point ev)
  16482. (when (calendar-cursor-to-date)
  16483. (let* ((date (calendar-cursor-to-date))
  16484. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16485. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16486. (if (active-minibuffer-window) (exit-minibuffer))))
  16487. (defun org-check-deadlines (ndays)
  16488. "Check if there are any deadlines due or past due.
  16489. A deadline is considered due if it happens within `org-deadline-warning-days'
  16490. days from today's date. If the deadline appears in an entry marked DONE,
  16491. it is not shown. The prefix arg NDAYS can be used to test that many
  16492. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16493. (interactive "P")
  16494. (let* ((org-warn-days
  16495. (cond
  16496. ((equal ndays '(4)) 100000)
  16497. (ndays (prefix-numeric-value ndays))
  16498. (t (abs org-deadline-warning-days))))
  16499. (case-fold-search nil)
  16500. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16501. (callback
  16502. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16503. (message "%d deadlines past-due or due within %d days"
  16504. (org-occur regexp nil callback)
  16505. org-warn-days)))
  16506. (defun org-check-before-date (date)
  16507. "Check if there are deadlines or scheduled entries before DATE."
  16508. (interactive (list (org-read-date)))
  16509. (let ((case-fold-search nil)
  16510. (regexp (concat "\\<\\(" org-deadline-string
  16511. "\\|" org-scheduled-string
  16512. "\\) *<\\([^>]+\\)>"))
  16513. (callback
  16514. (lambda () (time-less-p
  16515. (org-time-string-to-time (match-string 2))
  16516. (org-time-string-to-time date)))))
  16517. (message "%d entries before %s"
  16518. (org-occur regexp nil callback) date)))
  16519. (defun org-evaluate-time-range (&optional to-buffer)
  16520. "Evaluate a time range by computing the difference between start and end.
  16521. Normally the result is just printed in the echo area, but with prefix arg
  16522. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16523. If the time range is actually in a table, the result is inserted into the
  16524. next column.
  16525. For time difference computation, a year is assumed to be exactly 365
  16526. days in order to avoid rounding problems."
  16527. (interactive "P")
  16528. (or
  16529. (org-clock-update-time-maybe)
  16530. (save-excursion
  16531. (unless (org-at-date-range-p t)
  16532. (goto-char (point-at-bol))
  16533. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16534. (if (not (org-at-date-range-p t))
  16535. (error "Not at a time-stamp range, and none found in current line")))
  16536. (let* ((ts1 (match-string 1))
  16537. (ts2 (match-string 2))
  16538. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16539. (match-end (match-end 0))
  16540. (time1 (org-time-string-to-time ts1))
  16541. (time2 (org-time-string-to-time ts2))
  16542. (t1 (time-to-seconds time1))
  16543. (t2 (time-to-seconds time2))
  16544. (diff (abs (- t2 t1)))
  16545. (negative (< (- t2 t1) 0))
  16546. ;; (ys (floor (* 365 24 60 60)))
  16547. (ds (* 24 60 60))
  16548. (hs (* 60 60))
  16549. (fy "%dy %dd %02d:%02d")
  16550. (fy1 "%dy %dd")
  16551. (fd "%dd %02d:%02d")
  16552. (fd1 "%dd")
  16553. (fh "%02d:%02d")
  16554. y d h m align)
  16555. (if havetime
  16556. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16557. y 0
  16558. d (floor (/ diff ds)) diff (mod diff ds)
  16559. h (floor (/ diff hs)) diff (mod diff hs)
  16560. m (floor (/ diff 60)))
  16561. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16562. y 0
  16563. d (floor (+ (/ diff ds) 0.5))
  16564. h 0 m 0))
  16565. (if (not to-buffer)
  16566. (message "%s" (org-make-tdiff-string y d h m))
  16567. (if (org-at-table-p)
  16568. (progn
  16569. (goto-char match-end)
  16570. (setq align t)
  16571. (and (looking-at " *|") (goto-char (match-end 0))))
  16572. (goto-char match-end))
  16573. (if (looking-at
  16574. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16575. (replace-match ""))
  16576. (if negative (insert " -"))
  16577. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16578. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16579. (insert " " (format fh h m))))
  16580. (if align (org-table-align))
  16581. (message "Time difference inserted")))))
  16582. (defun org-make-tdiff-string (y d h m)
  16583. (let ((fmt "")
  16584. (l nil))
  16585. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16586. l (push y l)))
  16587. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16588. l (push d l)))
  16589. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16590. l (push h l)))
  16591. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16592. l (push m l)))
  16593. (apply 'format fmt (nreverse l))))
  16594. (defun org-time-string-to-time (s)
  16595. (apply 'encode-time (org-parse-time-string s)))
  16596. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16597. "Convert a time stamp to an absolute day number.
  16598. If there is a specifyer for a cyclic time stamp, get the closest date to
  16599. DAYNR."
  16600. (cond
  16601. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16602. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16603. daynr
  16604. (+ daynr 1000)))
  16605. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16606. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16607. (time-to-days (current-time))) (match-string 0 s)
  16608. prefer))
  16609. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16610. (defun org-time-from-absolute (d)
  16611. "Return the time corresponding to date D.
  16612. D may be an absolute day number, or a calendar-type list (month day year)."
  16613. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16614. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16615. (defun org-calendar-holiday ()
  16616. "List of holidays, for Diary display in Org-mode."
  16617. (require 'holidays)
  16618. (let ((hl (funcall
  16619. (if (fboundp 'calendar-check-holidays)
  16620. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16621. (if hl (mapconcat 'identity hl "; "))))
  16622. (defun org-diary-sexp-entry (sexp entry date)
  16623. "Process a SEXP diary ENTRY for DATE."
  16624. (require 'diary-lib)
  16625. (let ((result (if calendar-debug-sexp
  16626. (let ((stack-trace-on-error t))
  16627. (eval (car (read-from-string sexp))))
  16628. (condition-case nil
  16629. (eval (car (read-from-string sexp)))
  16630. (error
  16631. (beep)
  16632. (message "Bad sexp at line %d in %s: %s"
  16633. (org-current-line)
  16634. (buffer-file-name) sexp)
  16635. (sleep-for 2))))))
  16636. (cond ((stringp result) result)
  16637. ((and (consp result)
  16638. (stringp (cdr result))) (cdr result))
  16639. (result entry)
  16640. (t nil))))
  16641. (defun org-diary-to-ical-string (frombuf)
  16642. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16643. This uses the icalendar.el library."
  16644. (let* ((tmpdir (if (featurep 'xemacs)
  16645. (temp-directory)
  16646. temporary-file-directory))
  16647. (tmpfile (make-temp-name
  16648. (expand-file-name "orgics" tmpdir)))
  16649. buf rtn b e)
  16650. (save-excursion
  16651. (set-buffer frombuf)
  16652. (icalendar-export-region (point-min) (point-max) tmpfile)
  16653. (setq buf (find-buffer-visiting tmpfile))
  16654. (set-buffer buf)
  16655. (goto-char (point-min))
  16656. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16657. (setq b (match-beginning 0)))
  16658. (goto-char (point-max))
  16659. (if (re-search-backward "^END:VEVENT" nil t)
  16660. (setq e (match-end 0)))
  16661. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16662. (kill-buffer buf)
  16663. (kill-buffer frombuf)
  16664. (delete-file tmpfile)
  16665. rtn))
  16666. (defun org-closest-date (start current change prefer)
  16667. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16668. When PREFER is `past' return a date that is either CURRENT or past.
  16669. When PREFER is `future', return a date that is either CURRENT or future."
  16670. ;; Make the proper lists from the dates
  16671. (catch 'exit
  16672. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16673. dn dw sday cday n1 n2
  16674. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16675. (setq start (org-date-to-gregorian start)
  16676. current (org-date-to-gregorian
  16677. (if org-agenda-repeating-timestamp-show-all
  16678. current
  16679. (time-to-days (current-time))))
  16680. sday (calendar-absolute-from-gregorian start)
  16681. cday (calendar-absolute-from-gregorian current))
  16682. (if (<= cday sday) (throw 'exit sday))
  16683. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16684. (setq dn (string-to-number (match-string 1 change))
  16685. dw (cdr (assoc (match-string 2 change) a1)))
  16686. (error "Invalid change specifyer: %s" change))
  16687. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16688. (cond
  16689. ((eq dw 'day)
  16690. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16691. n2 (+ n1 dn)))
  16692. ((eq dw 'year)
  16693. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16694. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16695. (setq date1 (list m d y1)
  16696. n1 (calendar-absolute-from-gregorian date1)
  16697. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16698. n2 (calendar-absolute-from-gregorian date2)))
  16699. ((eq dw 'month)
  16700. ;; approx number of month between the tow dates
  16701. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16702. ;; How often does dn fit in there?
  16703. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16704. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16705. m (+ m nm)
  16706. ny (floor (/ m 12))
  16707. y (+ y ny)
  16708. m (- m (* ny 12)))
  16709. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16710. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16711. (setq m2 (+ m dn) y2 y)
  16712. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16713. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16714. (while (< n2 cday)
  16715. (setq n1 n2 m m2 y y2)
  16716. (setq m2 (+ m dn) y2 y)
  16717. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16718. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16719. (if org-agenda-repeating-timestamp-show-all
  16720. (cond
  16721. ((eq prefer 'past) n1)
  16722. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16723. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16724. (cond
  16725. ((eq prefer 'past) n1)
  16726. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16727. (t (if (= cday n1) n1 n2)))))))
  16728. (defun org-date-to-gregorian (date)
  16729. "Turn any specification of DATE into a gregorian date for the calendar."
  16730. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16731. ((and (listp date) (= (length date) 3)) date)
  16732. ((stringp date)
  16733. (setq date (org-parse-time-string date))
  16734. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16735. ((listp date)
  16736. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16737. (defun org-parse-time-string (s &optional nodefault)
  16738. "Parse the standard Org-mode time string.
  16739. This should be a lot faster than the normal `parse-time-string'.
  16740. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16741. hour and minute fields will be nil if not given."
  16742. (if (string-match org-ts-regexp0 s)
  16743. (list 0
  16744. (if (or (match-beginning 8) (not nodefault))
  16745. (string-to-number (or (match-string 8 s) "0")))
  16746. (if (or (match-beginning 7) (not nodefault))
  16747. (string-to-number (or (match-string 7 s) "0")))
  16748. (string-to-number (match-string 4 s))
  16749. (string-to-number (match-string 3 s))
  16750. (string-to-number (match-string 2 s))
  16751. nil nil nil)
  16752. (make-list 9 0)))
  16753. (defun org-timestamp-up (&optional arg)
  16754. "Increase the date item at the cursor by one.
  16755. If the cursor is on the year, change the year. If it is on the month or
  16756. the day, change that.
  16757. With prefix ARG, change by that many units."
  16758. (interactive "p")
  16759. (org-timestamp-change (prefix-numeric-value arg)))
  16760. (defun org-timestamp-down (&optional arg)
  16761. "Decrease the date item at the cursor by one.
  16762. If the cursor is on the year, change the year. If it is on the month or
  16763. the day, change that.
  16764. With prefix ARG, change by that many units."
  16765. (interactive "p")
  16766. (org-timestamp-change (- (prefix-numeric-value arg))))
  16767. (defun org-timestamp-up-day (&optional arg)
  16768. "Increase the date in the time stamp by one day.
  16769. With prefix ARG, change that many days."
  16770. (interactive "p")
  16771. (if (and (not (org-at-timestamp-p t))
  16772. (org-on-heading-p))
  16773. (org-todo 'up)
  16774. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16775. (defun org-timestamp-down-day (&optional arg)
  16776. "Decrease the date in the time stamp by one day.
  16777. With prefix ARG, change that many days."
  16778. (interactive "p")
  16779. (if (and (not (org-at-timestamp-p t))
  16780. (org-on-heading-p))
  16781. (org-todo 'down)
  16782. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16783. (defsubst org-pos-in-match-range (pos n)
  16784. (and (match-beginning n)
  16785. (<= (match-beginning n) pos)
  16786. (>= (match-end n) pos)))
  16787. (defun org-at-timestamp-p (&optional inactive-ok)
  16788. "Determine if the cursor is in or at a timestamp."
  16789. (interactive)
  16790. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16791. (pos (point))
  16792. (ans (or (looking-at tsr)
  16793. (save-excursion
  16794. (skip-chars-backward "^[<\n\r\t")
  16795. (if (> (point) (point-min)) (backward-char 1))
  16796. (and (looking-at tsr)
  16797. (> (- (match-end 0) pos) -1))))))
  16798. (and ans
  16799. (boundp 'org-ts-what)
  16800. (setq org-ts-what
  16801. (cond
  16802. ((= pos (match-beginning 0)) 'bracket)
  16803. ((= pos (1- (match-end 0))) 'bracket)
  16804. ((org-pos-in-match-range pos 2) 'year)
  16805. ((org-pos-in-match-range pos 3) 'month)
  16806. ((org-pos-in-match-range pos 7) 'hour)
  16807. ((org-pos-in-match-range pos 8) 'minute)
  16808. ((or (org-pos-in-match-range pos 4)
  16809. (org-pos-in-match-range pos 5)) 'day)
  16810. ((and (> pos (or (match-end 8) (match-end 5)))
  16811. (< pos (match-end 0)))
  16812. (- pos (or (match-end 8) (match-end 5))))
  16813. (t 'day))))
  16814. ans))
  16815. (defun org-toggle-timestamp-type ()
  16816. "Toggle the type (<active> or [inactive]) of a time stamp."
  16817. (interactive)
  16818. (when (org-at-timestamp-p t)
  16819. (save-excursion
  16820. (goto-char (match-beginning 0))
  16821. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16822. (goto-char (1- (match-end 0)))
  16823. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16824. (message "Timestamp is now %sactive"
  16825. (if (equal (char-before) ?>) "in" ""))))
  16826. (defun org-timestamp-change (n &optional what)
  16827. "Change the date in the time stamp at point.
  16828. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16829. `year', `minute', `second'. If WHAT is not given, the cursor position
  16830. in the timestamp determines what will be changed."
  16831. (let ((pos (point))
  16832. with-hm inactive
  16833. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16834. org-ts-what
  16835. extra rem
  16836. ts time time0)
  16837. (if (not (org-at-timestamp-p t))
  16838. (error "Not at a timestamp"))
  16839. (if (and (not what) (eq org-ts-what 'bracket))
  16840. (org-toggle-timestamp-type)
  16841. (if (and (not what) (not (eq org-ts-what 'day))
  16842. org-display-custom-times
  16843. (get-text-property (point) 'display)
  16844. (not (get-text-property (1- (point)) 'display)))
  16845. (setq org-ts-what 'day))
  16846. (setq org-ts-what (or what org-ts-what)
  16847. inactive (= (char-after (match-beginning 0)) ?\[)
  16848. ts (match-string 0))
  16849. (replace-match "")
  16850. (if (string-match
  16851. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  16852. ts)
  16853. (setq extra (match-string 1 ts)))
  16854. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16855. (setq with-hm t))
  16856. (setq time0 (org-parse-time-string ts))
  16857. (when (and (eq org-ts-what 'minute)
  16858. (eq current-prefix-arg nil))
  16859. (setq n (* dm (org-no-warnings (signum n))))
  16860. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16861. (setcar (cdr time0) (+ (nth 1 time0)
  16862. (if (> n 0) (- rem) (- dm rem))))))
  16863. (setq time
  16864. (encode-time (or (car time0) 0)
  16865. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16866. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16867. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16868. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16869. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16870. (nthcdr 6 time0)))
  16871. (when (integerp org-ts-what)
  16872. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16873. (if (eq what 'calendar)
  16874. (let ((cal-date (org-get-date-from-calendar)))
  16875. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16876. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16877. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16878. (setcar time0 (or (car time0) 0))
  16879. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16880. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16881. (setq time (apply 'encode-time time0))))
  16882. (setq org-last-changed-timestamp
  16883. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16884. (org-clock-update-time-maybe)
  16885. (goto-char pos)
  16886. ;; Try to recenter the calendar window, if any
  16887. (if (and org-calendar-follow-timestamp-change
  16888. (get-buffer-window "*Calendar*" t)
  16889. (memq org-ts-what '(day month year)))
  16890. (org-recenter-calendar (time-to-days time))))))
  16891. ;; FIXME: does not yet work for lead times
  16892. (defun org-modify-ts-extra (s pos n dm)
  16893. "Change the different parts of the lead-time and repeat fields in timestamp."
  16894. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16895. ng h m new rem)
  16896. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16897. (cond
  16898. ((or (org-pos-in-match-range pos 2)
  16899. (org-pos-in-match-range pos 3))
  16900. (setq m (string-to-number (match-string 3 s))
  16901. h (string-to-number (match-string 2 s)))
  16902. (if (org-pos-in-match-range pos 2)
  16903. (setq h (+ h n))
  16904. (setq n (* dm (org-no-warnings (signum n))))
  16905. (when (not (= 0 (setq rem (% m dm))))
  16906. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16907. (setq m (+ m n)))
  16908. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16909. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16910. (setq h (min 24 (max 0 h)))
  16911. (setq ng 1 new (format "-%02d:%02d" h m)))
  16912. ((org-pos-in-match-range pos 6)
  16913. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16914. ((org-pos-in-match-range pos 5)
  16915. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16916. ((org-pos-in-match-range pos 9)
  16917. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16918. ((org-pos-in-match-range pos 8)
  16919. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16920. (when ng
  16921. (setq s (concat
  16922. (substring s 0 (match-beginning ng))
  16923. new
  16924. (substring s (match-end ng))))))
  16925. s))
  16926. (defun org-recenter-calendar (date)
  16927. "If the calendar is visible, recenter it to DATE."
  16928. (let* ((win (selected-window))
  16929. (cwin (get-buffer-window "*Calendar*" t))
  16930. (calendar-move-hook nil))
  16931. (when cwin
  16932. (select-window cwin)
  16933. (calendar-goto-date (if (listp date) date
  16934. (calendar-gregorian-from-absolute date)))
  16935. (select-window win))))
  16936. (defun org-goto-calendar (&optional arg)
  16937. "Go to the Emacs calendar at the current date.
  16938. If there is a time stamp in the current line, go to that date.
  16939. A prefix ARG can be used to force the current date."
  16940. (interactive "P")
  16941. (let ((tsr org-ts-regexp) diff
  16942. (calendar-move-hook nil)
  16943. (view-calendar-holidays-initially nil)
  16944. (view-diary-entries-initially nil))
  16945. (if (or (org-at-timestamp-p)
  16946. (save-excursion
  16947. (beginning-of-line 1)
  16948. (looking-at (concat ".*" tsr))))
  16949. (let ((d1 (time-to-days (current-time)))
  16950. (d2 (time-to-days
  16951. (org-time-string-to-time (match-string 1)))))
  16952. (setq diff (- d2 d1))))
  16953. (calendar)
  16954. (calendar-goto-today)
  16955. (if (and diff (not arg)) (calendar-forward-day diff))))
  16956. (defun org-get-date-from-calendar ()
  16957. "Return a list (month day year) of date at point in calendar."
  16958. (with-current-buffer "*Calendar*"
  16959. (save-match-data
  16960. (calendar-cursor-to-date))))
  16961. (defun org-date-from-calendar ()
  16962. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16963. If there is already a time stamp at the cursor position, update it."
  16964. (interactive)
  16965. (if (org-at-timestamp-p t)
  16966. (org-timestamp-change 0 'calendar)
  16967. (let ((cal-date (org-get-date-from-calendar)))
  16968. (org-insert-time-stamp
  16969. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16970. (defvar appt-time-msg-list)
  16971. ;;;###autoload
  16972. (defun org-agenda-to-appt (&optional refresh filter)
  16973. "Activate appointments found in `org-agenda-files'.
  16974. With a \\[universal-argument] prefix, refresh the list of
  16975. appointements.
  16976. If FILTER is t, interactively prompt the user for a regular
  16977. expression, and filter out entries that don't match it.
  16978. If FILTER is a string, use this string as a regular expression
  16979. for filtering entries out.
  16980. FILTER can also be an alist with the car of each cell being
  16981. either 'headline or 'category. For example:
  16982. '((headline \"IMPORTANT\")
  16983. (category \"Work\"))
  16984. will only add headlines containing IMPORTANT or headlines
  16985. belonging to the \"Work\" category."
  16986. (interactive "P")
  16987. (require 'calendar)
  16988. (if refresh (setq appt-time-msg-list nil))
  16989. (if (eq filter t)
  16990. (setq filter (read-from-minibuffer "Regexp filter: ")))
  16991. (let* ((cnt 0) ; count added events
  16992. (org-agenda-new-buffers nil)
  16993. (org-deadline-warning-days 0)
  16994. (today (org-date-to-gregorian
  16995. (time-to-days (current-time))))
  16996. (files (org-agenda-files)) entries file)
  16997. ;; Get all entries which may contain an appt
  16998. (while (setq file (pop files))
  16999. (setq entries
  17000. (append entries
  17001. (org-agenda-get-day-entries
  17002. file today :timestamp :scheduled :deadline))))
  17003. (setq entries (delq nil entries))
  17004. ;; Map thru entries and find if we should filter them out
  17005. (mapc
  17006. (lambda(x)
  17007. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17008. (cat (get-text-property 1 'org-category x))
  17009. (tod (get-text-property 1 'time-of-day x))
  17010. (ok (or (null filter)
  17011. (and (stringp filter) (string-match filter evt))
  17012. (and (listp filter)
  17013. (or (string-match
  17014. (cadr (assoc 'category filter)) cat)
  17015. (string-match
  17016. (cadr (assoc 'headline filter)) evt))))))
  17017. ;; FIXME: Shall we remove text-properties for the appt text?
  17018. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17019. (when (and ok tod)
  17020. (setq tod (number-to-string tod)
  17021. tod (when (string-match
  17022. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17023. (concat (match-string 1 tod) ":"
  17024. (match-string 2 tod))))
  17025. (appt-add tod evt)
  17026. (setq cnt (1+ cnt))))) entries)
  17027. (org-release-buffers org-agenda-new-buffers)
  17028. (if (eq cnt 0)
  17029. (message "No event to add")
  17030. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17031. ;;; The clock for measuring work time.
  17032. (defvar org-mode-line-string "")
  17033. (put 'org-mode-line-string 'risky-local-variable t)
  17034. (defvar org-mode-line-timer nil)
  17035. (defvar org-clock-heading "")
  17036. (defvar org-clock-start-time "")
  17037. (defun org-update-mode-line ()
  17038. (let* ((delta (- (time-to-seconds (current-time))
  17039. (time-to-seconds org-clock-start-time)))
  17040. (h (floor delta 3600))
  17041. (m (floor (- delta (* 3600 h)) 60)))
  17042. (setq org-mode-line-string
  17043. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17044. 'help-echo "Org-mode clock is running"))
  17045. (force-mode-line-update)))
  17046. (defvar org-clock-marker (make-marker)
  17047. "Marker recording the last clock-in.")
  17048. (defvar org-clock-mode-line-entry nil
  17049. "Information for the modeline about the running clock.")
  17050. (defun org-clock-in ()
  17051. "Start the clock on the current item.
  17052. If necessary, clock-out of the currently active clock."
  17053. (interactive)
  17054. (org-clock-out t)
  17055. (let (ts)
  17056. (save-excursion
  17057. (org-back-to-heading t)
  17058. (when (and org-clock-in-switch-to-state
  17059. (not (looking-at (concat outline-regexp "[ \t]*"
  17060. org-clock-in-switch-to-state
  17061. "\\>"))))
  17062. (org-todo org-clock-in-switch-to-state))
  17063. (if (and org-clock-heading-function
  17064. (functionp org-clock-heading-function))
  17065. (setq org-clock-heading (funcall org-clock-heading-function))
  17066. (if (looking-at org-complex-heading-regexp)
  17067. (setq org-clock-heading (match-string 4))
  17068. (setq org-clock-heading "???")))
  17069. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17070. (org-clock-find-position)
  17071. (insert "\n") (backward-char 1)
  17072. (indent-relative)
  17073. (insert org-clock-string " ")
  17074. (setq org-clock-start-time (current-time))
  17075. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17076. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17077. (or global-mode-string (setq global-mode-string '("")))
  17078. (or (memq 'org-mode-line-string global-mode-string)
  17079. (setq global-mode-string
  17080. (append global-mode-string '(org-mode-line-string))))
  17081. (org-update-mode-line)
  17082. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17083. (message "Clock started at %s" ts))))
  17084. (defun org-clock-find-position ()
  17085. "Find the location where the next clock line should be inserted."
  17086. (org-back-to-heading t)
  17087. (catch 'exit
  17088. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17089. (re (concat "^[ \t]*" org-clock-string))
  17090. (cnt 0)
  17091. first last)
  17092. (goto-char beg)
  17093. (when (eobp) (newline) (setq end (max (point) end)))
  17094. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17095. ;; we seem to have a CLOCK drawer, so go there.
  17096. (beginning-of-line 2)
  17097. (throw 'exit t))
  17098. ;; Lets count the CLOCK lines
  17099. (goto-char beg)
  17100. (while (re-search-forward re end t)
  17101. (setq first (or first (match-beginning 0))
  17102. last (match-beginning 0)
  17103. cnt (1+ cnt)))
  17104. (when (and (integerp org-clock-into-drawer)
  17105. (>= (1+ cnt) org-clock-into-drawer))
  17106. ;; Wrap current entries into a new drawer
  17107. (goto-char last)
  17108. (beginning-of-line 2)
  17109. (if (org-at-item-p) (org-end-of-item))
  17110. (insert ":END:\n")
  17111. (beginning-of-line 0)
  17112. (org-indent-line-function)
  17113. (goto-char first)
  17114. (insert ":CLOCK:\n")
  17115. (beginning-of-line 0)
  17116. (org-indent-line-function)
  17117. (org-flag-drawer t)
  17118. (beginning-of-line 2)
  17119. (throw 'exit nil))
  17120. (goto-char beg)
  17121. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17122. (not (equal (match-string 1) org-clock-string)))
  17123. ;; Planning info, skip to after it
  17124. (beginning-of-line 2)
  17125. (or (bolp) (newline)))
  17126. (when (eq t org-clock-into-drawer)
  17127. (insert ":CLOCK:\n:END:\n")
  17128. (beginning-of-line -1)
  17129. (org-indent-line-function)
  17130. (org-flag-drawer t)
  17131. (beginning-of-line 2)
  17132. (org-indent-line-function)))))
  17133. (defun org-clock-out (&optional fail-quietly)
  17134. "Stop the currently running clock.
  17135. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17136. (interactive)
  17137. (catch 'exit
  17138. (if (not (marker-buffer org-clock-marker))
  17139. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17140. (let (ts te s h m)
  17141. (save-excursion
  17142. (set-buffer (marker-buffer org-clock-marker))
  17143. (goto-char org-clock-marker)
  17144. (beginning-of-line 1)
  17145. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17146. (equal (match-string 1) org-clock-string))
  17147. (setq ts (match-string 2))
  17148. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17149. (goto-char (match-end 0))
  17150. (delete-region (point) (point-at-eol))
  17151. (insert "--")
  17152. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17153. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17154. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17155. h (floor (/ s 3600))
  17156. s (- s (* 3600 h))
  17157. m (floor (/ s 60))
  17158. s (- s (* 60 s)))
  17159. (insert " => " (format "%2d:%02d" h m))
  17160. (move-marker org-clock-marker nil)
  17161. (when org-log-note-clock-out
  17162. (org-add-log-maybe 'clock-out))
  17163. (when org-mode-line-timer
  17164. (cancel-timer org-mode-line-timer)
  17165. (setq org-mode-line-timer nil))
  17166. (setq global-mode-string
  17167. (delq 'org-mode-line-string global-mode-string))
  17168. (force-mode-line-update)
  17169. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17170. (defun org-clock-cancel ()
  17171. "Cancel the running clock be removing the start timestamp."
  17172. (interactive)
  17173. (if (not (marker-buffer org-clock-marker))
  17174. (error "No active clock"))
  17175. (save-excursion
  17176. (set-buffer (marker-buffer org-clock-marker))
  17177. (goto-char org-clock-marker)
  17178. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17179. (setq global-mode-string
  17180. (delq 'org-mode-line-string global-mode-string))
  17181. (force-mode-line-update)
  17182. (message "Clock canceled"))
  17183. (defun org-clock-goto (&optional delete-windows)
  17184. "Go to the currently clocked-in entry."
  17185. (interactive "P")
  17186. (if (not (marker-buffer org-clock-marker))
  17187. (error "No active clock"))
  17188. (switch-to-buffer-other-window
  17189. (marker-buffer org-clock-marker))
  17190. (if delete-windows (delete-other-windows))
  17191. (goto-char org-clock-marker)
  17192. (org-show-entry)
  17193. (org-back-to-heading)
  17194. (recenter))
  17195. (defvar org-clock-file-total-minutes nil
  17196. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17197. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17198. (defun org-clock-sum (&optional tstart tend)
  17199. "Sum the times for each subtree.
  17200. Puts the resulting times in minutes as a text property on each headline."
  17201. (interactive)
  17202. (let* ((bmp (buffer-modified-p))
  17203. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17204. org-clock-string
  17205. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17206. (lmax 30)
  17207. (ltimes (make-vector lmax 0))
  17208. (t1 0)
  17209. (level 0)
  17210. ts te dt
  17211. time)
  17212. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17213. (save-excursion
  17214. (goto-char (point-max))
  17215. (while (re-search-backward re nil t)
  17216. (cond
  17217. ((match-end 2)
  17218. ;; Two time stamps
  17219. (setq ts (match-string 2)
  17220. te (match-string 3)
  17221. ts (time-to-seconds
  17222. (apply 'encode-time (org-parse-time-string ts)))
  17223. te (time-to-seconds
  17224. (apply 'encode-time (org-parse-time-string te)))
  17225. ts (if tstart (max ts tstart) ts)
  17226. te (if tend (min te tend) te)
  17227. dt (- te ts)
  17228. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17229. ((match-end 4)
  17230. ;; A naket time
  17231. (setq t1 (+ t1 (string-to-number (match-string 5))
  17232. (* 60 (string-to-number (match-string 4))))))
  17233. (t ;; A headline
  17234. (setq level (- (match-end 1) (match-beginning 1)))
  17235. (when (or (> t1 0) (> (aref ltimes level) 0))
  17236. (loop for l from 0 to level do
  17237. (aset ltimes l (+ (aref ltimes l) t1)))
  17238. (setq t1 0 time (aref ltimes level))
  17239. (loop for l from level to (1- lmax) do
  17240. (aset ltimes l 0))
  17241. (goto-char (match-beginning 0))
  17242. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17243. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17244. (set-buffer-modified-p bmp)))
  17245. (defun org-clock-display (&optional total-only)
  17246. "Show subtree times in the entire buffer.
  17247. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17248. in the echo area."
  17249. (interactive)
  17250. (org-remove-clock-overlays)
  17251. (let (time h m p)
  17252. (org-clock-sum)
  17253. (unless total-only
  17254. (save-excursion
  17255. (goto-char (point-min))
  17256. (while (or (and (equal (setq p (point)) (point-min))
  17257. (get-text-property p :org-clock-minutes))
  17258. (setq p (next-single-property-change
  17259. (point) :org-clock-minutes)))
  17260. (goto-char p)
  17261. (when (setq time (get-text-property p :org-clock-minutes))
  17262. (org-put-clock-overlay time (funcall outline-level))))
  17263. (setq h (/ org-clock-file-total-minutes 60)
  17264. m (- org-clock-file-total-minutes (* 60 h)))
  17265. ;; Arrange to remove the overlays upon next change.
  17266. (when org-remove-highlights-with-change
  17267. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17268. nil 'local))))
  17269. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17270. (defvar org-clock-overlays nil)
  17271. (make-variable-buffer-local 'org-clock-overlays)
  17272. (defun org-put-clock-overlay (time &optional level)
  17273. "Put an overlays on the current line, displaying TIME.
  17274. If LEVEL is given, prefix time with a corresponding number of stars.
  17275. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17276. will be easy to remove."
  17277. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17278. (l (if level (org-get-valid-level level 0) 0))
  17279. (off 0)
  17280. ov tx)
  17281. (move-to-column c)
  17282. (unless (eolp) (skip-chars-backward "^ \t"))
  17283. (skip-chars-backward " \t")
  17284. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17285. tx (concat (buffer-substring (1- (point)) (point))
  17286. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17287. (org-add-props (format "%s %2d:%02d%s"
  17288. (make-string l ?*) h m
  17289. (make-string (- 16 l) ?\ ))
  17290. '(face secondary-selection))
  17291. ""))
  17292. (if (not (featurep 'xemacs))
  17293. (org-overlay-put ov 'display tx)
  17294. (org-overlay-put ov 'invisible t)
  17295. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17296. (push ov org-clock-overlays)))
  17297. (defun org-remove-clock-overlays (&optional beg end noremove)
  17298. "Remove the occur highlights from the buffer.
  17299. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17300. from the `before-change-functions' in the current buffer."
  17301. (interactive)
  17302. (unless org-inhibit-highlight-removal
  17303. (mapc 'org-delete-overlay org-clock-overlays)
  17304. (setq org-clock-overlays nil)
  17305. (unless noremove
  17306. (remove-hook 'before-change-functions
  17307. 'org-remove-clock-overlays 'local))))
  17308. (defun org-clock-out-if-current ()
  17309. "Clock out if the current entry contains the running clock.
  17310. This is used to stop the clock after a TODO entry is marked DONE,
  17311. and is only done if the variable `org-clock-out-when-done' is not nil."
  17312. (when (and org-clock-out-when-done
  17313. (member state org-done-keywords)
  17314. (equal (marker-buffer org-clock-marker) (current-buffer))
  17315. (< (point) org-clock-marker)
  17316. (> (save-excursion (outline-next-heading) (point))
  17317. org-clock-marker))
  17318. ;; Clock out, but don't accept a logging message for this.
  17319. (let ((org-log-note-clock-out nil))
  17320. (org-clock-out))))
  17321. (add-hook 'org-after-todo-state-change-hook
  17322. 'org-clock-out-if-current)
  17323. (defun org-check-running-clock ()
  17324. "Check if the current buffer contains the running clock.
  17325. If yes, offer to stop it and to save the buffer with the changes."
  17326. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17327. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17328. (buffer-name))))
  17329. (org-clock-out)
  17330. (when (y-or-n-p "Save changed buffer?")
  17331. (save-buffer))))
  17332. (defun org-clock-report (&optional arg)
  17333. "Create a table containing a report about clocked time.
  17334. If the cursor is inside an existing clocktable block, then the table
  17335. will be updated. If not, a new clocktable will be inserted.
  17336. When called with a prefix argument, move to the first clock table in the
  17337. buffer and update it."
  17338. (interactive "P")
  17339. (org-remove-clock-overlays)
  17340. (when arg
  17341. (org-find-dblock "clocktable")
  17342. (org-show-entry))
  17343. (if (org-in-clocktable-p)
  17344. (goto-char (org-in-clocktable-p))
  17345. (org-create-dblock (list :name "clocktable"
  17346. :maxlevel 2 :scope 'file)))
  17347. (org-update-dblock))
  17348. (defun org-in-clocktable-p ()
  17349. "Check if the cursor is in a clocktable."
  17350. (let ((pos (point)) start)
  17351. (save-excursion
  17352. (end-of-line 1)
  17353. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17354. (setq start (match-beginning 0))
  17355. (re-search-forward "^#\\+END:.*" nil t)
  17356. (>= (match-end 0) pos)
  17357. start))))
  17358. (defun org-clock-update-time-maybe ()
  17359. "If this is a CLOCK line, update it and return t.
  17360. Otherwise, return nil."
  17361. (interactive)
  17362. (save-excursion
  17363. (beginning-of-line 1)
  17364. (skip-chars-forward " \t")
  17365. (when (looking-at org-clock-string)
  17366. (let ((re (concat "[ \t]*" org-clock-string
  17367. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17368. "\\([ \t]*=>.*\\)?"))
  17369. ts te h m s)
  17370. (if (not (looking-at re))
  17371. nil
  17372. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17373. (end-of-line 1)
  17374. (setq ts (match-string 1)
  17375. te (match-string 2))
  17376. (setq s (- (time-to-seconds
  17377. (apply 'encode-time (org-parse-time-string te)))
  17378. (time-to-seconds
  17379. (apply 'encode-time (org-parse-time-string ts))))
  17380. h (floor (/ s 3600))
  17381. s (- s (* 3600 h))
  17382. m (floor (/ s 60))
  17383. s (- s (* 60 s)))
  17384. (insert " => " (format "%2d:%02d" h m))
  17385. t)))))
  17386. (defun org-clock-special-range (key &optional time as-strings)
  17387. "Return two times bordering a special time range.
  17388. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17389. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17390. A week starts Monday 0:00 and ends Sunday 24:00.
  17391. The range is determined relative to TIME. TIME defaults to the current time.
  17392. The return value is a cons cell with two internal times like the ones
  17393. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17394. the returned times will be formatted strings."
  17395. (let* ((tm (decode-time (or time (current-time))))
  17396. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17397. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17398. (dow (nth 6 tm))
  17399. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17400. (cond
  17401. ((eq key 'today)
  17402. (setq h 0 m 0 h1 24 m1 0))
  17403. ((eq key 'yesterday)
  17404. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17405. ((eq key 'thisweek)
  17406. (setq diff (if (= dow 0) 6 (1- dow))
  17407. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17408. ((eq key 'lastweek)
  17409. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17410. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17411. ((eq key 'thismonth)
  17412. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17413. ((eq key 'lastmonth)
  17414. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17415. ((eq key 'thisyear)
  17416. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17417. ((eq key 'lastyear)
  17418. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17419. (t (error "No such time block %s" key)))
  17420. (setq ts (encode-time s m h d month y)
  17421. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17422. (or d1 d) (or month1 month) (or y1 y)))
  17423. (setq fm (cdr org-time-stamp-formats))
  17424. (if as-strings
  17425. (cons (format-time-string fm ts) (format-time-string fm te))
  17426. (cons ts te))))
  17427. (defun org-dblock-write:clocktable (params)
  17428. "Write the standard clocktable."
  17429. (catch 'exit
  17430. (let* ((hlchars '((1 . "*") (2 . "/")))
  17431. (ins (make-marker))
  17432. (total-time nil)
  17433. (scope (plist-get params :scope))
  17434. (tostring (plist-get params :tostring))
  17435. (multifile (plist-get params :multifile))
  17436. (header (plist-get params :header))
  17437. (maxlevel (or (plist-get params :maxlevel) 3))
  17438. (step (plist-get params :step))
  17439. (emph (plist-get params :emphasize))
  17440. (ts (plist-get params :tstart))
  17441. (te (plist-get params :tend))
  17442. (block (plist-get params :block))
  17443. (link (plist-get params :link))
  17444. ipos time h m p level hlc hdl
  17445. cc beg end pos tbl)
  17446. (when step
  17447. (org-clocktable-steps params)
  17448. (throw 'exit nil))
  17449. (when block
  17450. (setq cc (org-clock-special-range block nil t)
  17451. ts (car cc) te (cdr cc)))
  17452. (if ts (setq ts (time-to-seconds
  17453. (apply 'encode-time (org-parse-time-string ts)))))
  17454. (if te (setq te (time-to-seconds
  17455. (apply 'encode-time (org-parse-time-string te)))))
  17456. (move-marker ins (point))
  17457. (setq ipos (point))
  17458. ;; Get the right scope
  17459. (setq pos (point))
  17460. (save-restriction
  17461. (cond
  17462. ((not scope))
  17463. ((eq scope 'file) (widen))
  17464. ((eq scope 'subtree) (org-narrow-to-subtree))
  17465. ((eq scope 'tree)
  17466. (while (org-up-heading-safe))
  17467. (org-narrow-to-subtree))
  17468. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17469. (symbol-name scope)))
  17470. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17471. (catch 'exit
  17472. (while (org-up-heading-safe)
  17473. (looking-at outline-regexp)
  17474. (if (<= (org-reduced-level (funcall outline-level)) level)
  17475. (throw 'exit nil))))
  17476. (org-narrow-to-subtree))
  17477. ((or (listp scope) (eq scope 'agenda))
  17478. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17479. (scope 'agenda)
  17480. (p1 (copy-sequence params))
  17481. file)
  17482. (plist-put p1 :tostring t)
  17483. (plist-put p1 :multifile t)
  17484. (plist-put p1 :scope 'file)
  17485. (org-prepare-agenda-buffers files)
  17486. (while (setq file (pop files))
  17487. (with-current-buffer (find-buffer-visiting file)
  17488. (push (org-clocktable-add-file
  17489. file (org-dblock-write:clocktable p1)) tbl)
  17490. (setq total-time (+ (or total-time 0)
  17491. org-clock-file-total-minutes)))))))
  17492. (goto-char pos)
  17493. (unless (eq scope 'agenda)
  17494. (org-clock-sum ts te)
  17495. (goto-char (point-min))
  17496. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17497. (goto-char p)
  17498. (when (setq time (get-text-property p :org-clock-minutes))
  17499. (save-excursion
  17500. (beginning-of-line 1)
  17501. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17502. (setq level (org-reduced-level
  17503. (- (match-end 1) (match-beginning 1))))
  17504. (<= level maxlevel))
  17505. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17506. hdl (if (not link)
  17507. (match-string 2)
  17508. (org-make-link-string
  17509. (format "file:%s::%s"
  17510. (buffer-file-name)
  17511. (save-match-data
  17512. (org-make-org-heading-search-string
  17513. (match-string 2))))
  17514. (match-string 2)))
  17515. h (/ time 60)
  17516. m (- time (* 60 h)))
  17517. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17518. (push (concat
  17519. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17520. (make-string (1- level) ?|)
  17521. hlc (format "%d:%02d" h m) hlc
  17522. " |") tbl))))))
  17523. (setq tbl (nreverse tbl))
  17524. (if tostring
  17525. (if tbl (mapconcat 'identity tbl "\n") nil)
  17526. (goto-char ins)
  17527. (insert-before-markers
  17528. (or header
  17529. (concat
  17530. "Clock summary at ["
  17531. (substring
  17532. (format-time-string (cdr org-time-stamp-formats))
  17533. 1 -1)
  17534. "]."
  17535. (if block
  17536. (format " Considered range is /%s/." block)
  17537. "")
  17538. "\n\n"))
  17539. (if (eq scope 'agenda) "|File" "")
  17540. "|L|Headline|Time|\n")
  17541. (setq total-time (or total-time org-clock-file-total-minutes)
  17542. h (/ total-time 60)
  17543. m (- total-time (* 60 h)))
  17544. (insert-before-markers
  17545. "|-\n|"
  17546. (if (eq scope 'agenda) "|" "")
  17547. "|"
  17548. "*Total time*| "
  17549. (format "*%d:%02d*" h m)
  17550. "|\n|-\n")
  17551. (setq tbl (delq nil tbl))
  17552. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17553. (equal (substring (car tbl) 0 2) "|-"))
  17554. (pop tbl))
  17555. (insert-before-markers (mapconcat
  17556. 'identity (delq nil tbl)
  17557. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17558. (backward-delete-char 1)
  17559. (goto-char ipos)
  17560. (skip-chars-forward "^|")
  17561. (org-table-align))))))
  17562. (defun org-clocktable-steps (params)
  17563. (let* ((p1 (copy-sequence params))
  17564. (ts (plist-get p1 :tstart))
  17565. (te (plist-get p1 :tend))
  17566. (step0 (plist-get p1 :step))
  17567. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17568. (block (plist-get p1 :block))
  17569. cc)
  17570. (when block
  17571. (setq cc (org-clock-special-range block nil t)
  17572. ts (car cc) te (cdr cc)))
  17573. (if ts (setq ts (time-to-seconds
  17574. (apply 'encode-time (org-parse-time-string ts)))))
  17575. (if te (setq te (time-to-seconds
  17576. (apply 'encode-time (org-parse-time-string te)))))
  17577. (plist-put p1 :header "")
  17578. (plist-put p1 :step nil)
  17579. (plist-put p1 :block nil)
  17580. (while (< ts te)
  17581. (or (bolp) (insert "\n"))
  17582. (plist-put p1 :tstart (format-time-string
  17583. (car org-time-stamp-formats)
  17584. (seconds-to-time ts)))
  17585. (plist-put p1 :tend (format-time-string
  17586. (car org-time-stamp-formats)
  17587. (seconds-to-time (setq ts (+ ts step)))))
  17588. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17589. (plist-get p1 :tstart) "\n")
  17590. (org-dblock-write:clocktable p1)
  17591. (re-search-forward "#\\+END:")
  17592. (end-of-line 0))))
  17593. (defun org-clocktable-add-file (file table)
  17594. (if table
  17595. (let ((lines (org-split-string table "\n"))
  17596. (ff (file-name-nondirectory file)))
  17597. (mapconcat 'identity
  17598. (mapcar (lambda (x)
  17599. (if (string-match org-table-dataline-regexp x)
  17600. (concat "|" ff x)
  17601. x))
  17602. lines)
  17603. "\n"))))
  17604. ;; FIXME: I don't think anybody uses this, ask David
  17605. (defun org-collect-clock-time-entries ()
  17606. "Return an internal list with clocking information.
  17607. This list has one entry for each CLOCK interval.
  17608. FIXME: describe the elements."
  17609. (interactive)
  17610. (let ((re (concat "^[ \t]*" org-clock-string
  17611. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17612. rtn beg end next cont level title total closedp leafp
  17613. clockpos titlepos h m donep)
  17614. (save-excursion
  17615. (org-clock-sum)
  17616. (goto-char (point-min))
  17617. (while (re-search-forward re nil t)
  17618. (setq clockpos (match-beginning 0)
  17619. beg (match-string 1) end (match-string 2)
  17620. cont (match-end 0))
  17621. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17622. end (apply 'encode-time (org-parse-time-string end)))
  17623. (org-back-to-heading t)
  17624. (setq donep (org-entry-is-done-p))
  17625. (setq titlepos (point)
  17626. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17627. h (/ total 60) m (- total (* 60 h))
  17628. total (cons h m))
  17629. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17630. (setq level (- (match-end 1) (match-beginning 1))
  17631. title (org-match-string-no-properties 2))
  17632. (save-excursion (outline-next-heading) (setq next (point)))
  17633. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17634. (goto-char next)
  17635. (setq leafp (and (looking-at "^\\*+ ")
  17636. (<= (- (match-end 0) (point)) level)))
  17637. (push (list beg end clockpos closedp donep
  17638. total title titlepos level leafp)
  17639. rtn)
  17640. (goto-char cont)))
  17641. (nreverse rtn)))
  17642. ;;;; Agenda, and Diary Integration
  17643. ;;; Define the Org-agenda-mode
  17644. (defvar org-agenda-mode-map (make-sparse-keymap)
  17645. "Keymap for `org-agenda-mode'.")
  17646. (defvar org-agenda-menu) ; defined later in this file.
  17647. (defvar org-agenda-follow-mode nil)
  17648. (defvar org-agenda-show-log nil)
  17649. (defvar org-agenda-redo-command nil)
  17650. (defvar org-agenda-query-string nil)
  17651. (defvar org-agenda-mode-hook nil)
  17652. (defvar org-agenda-type nil)
  17653. (defvar org-agenda-force-single-file nil)
  17654. (defun org-agenda-mode ()
  17655. "Mode for time-sorted view on action items in Org-mode files.
  17656. The following commands are available:
  17657. \\{org-agenda-mode-map}"
  17658. (interactive)
  17659. (kill-all-local-variables)
  17660. (setq org-agenda-undo-list nil
  17661. org-agenda-pending-undo-list nil)
  17662. (setq major-mode 'org-agenda-mode)
  17663. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17664. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17665. (setq mode-name "Org-Agenda")
  17666. (use-local-map org-agenda-mode-map)
  17667. (easy-menu-add org-agenda-menu)
  17668. (if org-startup-truncated (setq truncate-lines t))
  17669. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17670. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17671. ;; Make sure properties are removed when copying text
  17672. (when (boundp 'buffer-substring-filters)
  17673. (org-set-local 'buffer-substring-filters
  17674. (cons (lambda (x)
  17675. (set-text-properties 0 (length x) nil x) x)
  17676. buffer-substring-filters)))
  17677. (unless org-agenda-keep-modes
  17678. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17679. org-agenda-show-log nil))
  17680. (easy-menu-change
  17681. '("Agenda") "Agenda Files"
  17682. (append
  17683. (list
  17684. (vector
  17685. (if (get 'org-agenda-files 'org-restrict)
  17686. "Restricted to single file"
  17687. "Edit File List")
  17688. '(org-edit-agenda-file-list)
  17689. (not (get 'org-agenda-files 'org-restrict)))
  17690. "--")
  17691. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17692. (org-agenda-set-mode-name)
  17693. (apply
  17694. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17695. (list 'org-agenda-mode-hook)))
  17696. (substitute-key-definition 'undo 'org-agenda-undo
  17697. org-agenda-mode-map global-map)
  17698. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17699. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17700. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17701. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17702. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17703. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17704. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17705. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17706. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17707. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17708. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17709. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17710. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17711. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17712. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17713. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17714. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17715. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17716. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17717. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17718. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17719. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17720. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17721. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17722. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17723. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17724. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17725. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17726. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17727. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17728. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17729. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17730. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17731. (while l (org-defkey org-agenda-mode-map
  17732. (int-to-string (pop l)) 'digit-argument)))
  17733. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17734. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17735. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17736. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17737. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17738. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17739. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17740. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17741. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17742. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17743. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17744. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17745. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17746. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17747. (org-defkey org-agenda-mode-map "n" 'next-line)
  17748. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17749. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17750. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17751. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17752. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17753. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17754. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17755. (eval-after-load "calendar"
  17756. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17757. 'org-calendar-goto-agenda))
  17758. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17759. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17760. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17761. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17762. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17763. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17764. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17765. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17766. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17767. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17768. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17769. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17770. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17771. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17772. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17773. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17774. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17775. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17776. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17777. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17778. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17779. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17780. (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
  17781. (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
  17782. (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
  17783. (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
  17784. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17785. "Local keymap for agenda entries from Org-mode.")
  17786. (org-defkey org-agenda-keymap
  17787. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17788. (org-defkey org-agenda-keymap
  17789. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17790. (when org-agenda-mouse-1-follows-link
  17791. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17792. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17793. '("Agenda"
  17794. ("Agenda Files")
  17795. "--"
  17796. ["Show" org-agenda-show t]
  17797. ["Go To (other window)" org-agenda-goto t]
  17798. ["Go To (this window)" org-agenda-switch-to t]
  17799. ["Follow Mode" org-agenda-follow-mode
  17800. :style toggle :selected org-agenda-follow-mode :active t]
  17801. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17802. "--"
  17803. ["Cycle TODO" org-agenda-todo t]
  17804. ["Archive subtree" org-agenda-archive t]
  17805. ["Delete subtree" org-agenda-kill t]
  17806. "--"
  17807. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17808. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17809. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17810. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17811. "--"
  17812. ("Tags and Properties"
  17813. ["Show all Tags" org-agenda-show-tags t]
  17814. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17815. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17816. "--"
  17817. ["Column View" org-columns t])
  17818. ("Date/Schedule"
  17819. ["Schedule" org-agenda-schedule t]
  17820. ["Set Deadline" org-agenda-deadline t]
  17821. "--"
  17822. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17823. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17824. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17825. ("Clock"
  17826. ["Clock in" org-agenda-clock-in t]
  17827. ["Clock out" org-agenda-clock-out t]
  17828. ["Clock cancel" org-agenda-clock-cancel t]
  17829. ["Goto running clock" org-clock-goto t])
  17830. ("Priority"
  17831. ["Set Priority" org-agenda-priority t]
  17832. ["Increase Priority" org-agenda-priority-up t]
  17833. ["Decrease Priority" org-agenda-priority-down t]
  17834. ["Show Priority" org-agenda-show-priority t])
  17835. ("Calendar/Diary"
  17836. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17837. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17838. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17839. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17840. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17841. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17842. "--"
  17843. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17844. "--"
  17845. ("View"
  17846. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17847. :style radio :selected (equal org-agenda-ndays 1)]
  17848. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17849. :style radio :selected (equal org-agenda-ndays 7)]
  17850. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17851. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17852. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17853. :style radio :selected (member org-agenda-ndays '(365 366))]
  17854. "--"
  17855. ["Show Logbook entries" org-agenda-log-mode
  17856. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17857. ["Include Diary" org-agenda-toggle-diary
  17858. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17859. ["Use Time Grid" org-agenda-toggle-time-grid
  17860. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17861. ["Write view to file" org-write-agenda t]
  17862. ["Rebuild buffer" org-agenda-redo t]
  17863. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17864. "--"
  17865. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17866. "--"
  17867. ["Quit" org-agenda-quit t]
  17868. ["Exit and Release Buffers" org-agenda-exit t]
  17869. ))
  17870. ;;; Agenda undo
  17871. (defvar org-agenda-allow-remote-undo t
  17872. "Non-nil means, allow remote undo from the agenda buffer.")
  17873. (defvar org-agenda-undo-list nil
  17874. "List of undoable operations in the agenda since last refresh.")
  17875. (defvar org-agenda-undo-has-started-in nil
  17876. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17877. (defvar org-agenda-pending-undo-list nil
  17878. "In a series of undo commands, this is the list of remaning undo items.")
  17879. (defmacro org-if-unprotected (&rest body)
  17880. "Execute BODY if there is no `org-protected' text property at point."
  17881. (declare (debug t))
  17882. `(unless (get-text-property (point) 'org-protected)
  17883. ,@body))
  17884. (defmacro org-with-remote-undo (_buffer &rest _body)
  17885. "Execute BODY while recording undo information in two buffers."
  17886. (declare (indent 1) (debug t))
  17887. `(let ((_cline (org-current-line))
  17888. (_cmd this-command)
  17889. (_buf1 (current-buffer))
  17890. (_buf2 ,_buffer)
  17891. (_undo1 buffer-undo-list)
  17892. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17893. _c1 _c2)
  17894. ,@_body
  17895. (when org-agenda-allow-remote-undo
  17896. (setq _c1 (org-verify-change-for-undo
  17897. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17898. _c2 (org-verify-change-for-undo
  17899. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17900. (when (or _c1 _c2)
  17901. ;; make sure there are undo boundaries
  17902. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17903. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17904. ;; remember which buffer to undo
  17905. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17906. org-agenda-undo-list)))))
  17907. (defun org-agenda-undo ()
  17908. "Undo a remote editing step in the agenda.
  17909. This undoes changes both in the agenda buffer and in the remote buffer
  17910. that have been changed along."
  17911. (interactive)
  17912. (or org-agenda-allow-remote-undo
  17913. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17914. (if (not (eq this-command last-command))
  17915. (setq org-agenda-undo-has-started-in nil
  17916. org-agenda-pending-undo-list org-agenda-undo-list))
  17917. (if (not org-agenda-pending-undo-list)
  17918. (error "No further undo information"))
  17919. (let* ((entry (pop org-agenda-pending-undo-list))
  17920. buf line cmd rembuf)
  17921. (setq cmd (pop entry) line (pop entry))
  17922. (setq rembuf (nth 2 entry))
  17923. (org-with-remote-undo rembuf
  17924. (while (bufferp (setq buf (pop entry)))
  17925. (if (pop entry)
  17926. (with-current-buffer buf
  17927. (let ((last-undo-buffer buf)
  17928. (inhibit-read-only t))
  17929. (unless (memq buf org-agenda-undo-has-started-in)
  17930. (push buf org-agenda-undo-has-started-in)
  17931. (make-local-variable 'pending-undo-list)
  17932. (undo-start))
  17933. (while (and pending-undo-list
  17934. (listp pending-undo-list)
  17935. (not (car pending-undo-list)))
  17936. (pop pending-undo-list))
  17937. (undo-more 1))))))
  17938. (goto-line line)
  17939. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17940. (defun org-verify-change-for-undo (l1 l2)
  17941. "Verify that a real change occurred between the undo lists L1 and L2."
  17942. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17943. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17944. (not (eq l1 l2)))
  17945. ;;; Agenda dispatch
  17946. (defvar org-agenda-restrict nil)
  17947. (defvar org-agenda-restrict-begin (make-marker))
  17948. (defvar org-agenda-restrict-end (make-marker))
  17949. (defvar org-agenda-last-dispatch-buffer nil)
  17950. (defvar org-agenda-overriding-restriction nil)
  17951. ;;;###autoload
  17952. (defun org-agenda (arg &optional keys restriction)
  17953. "Dispatch agenda commands to collect entries to the agenda buffer.
  17954. Prompts for a command to execute. Any prefix arg will be passed
  17955. on to the selected command. The default selections are:
  17956. a Call `org-agenda-list' to display the agenda for current day or week.
  17957. t Call `org-todo-list' to display the global todo list.
  17958. T Call `org-todo-list' to display the global todo list, select only
  17959. entries with a specific TODO keyword (the user gets a prompt).
  17960. m Call `org-tags-view' to display headlines with tags matching
  17961. a condition (the user is prompted for the condition).
  17962. M Like `m', but select only TODO entries, no ordinary headlines.
  17963. L Create a timeline for the current buffer.
  17964. e Export views to associated files.
  17965. More commands can be added by configuring the variable
  17966. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17967. searches can be pre-defined in this way.
  17968. If the current buffer is in Org-mode and visiting a file, you can also
  17969. first press `<' once to indicate that the agenda should be temporarily
  17970. \(until the next use of \\[org-agenda]) restricted to the current file.
  17971. Pressing `<' twice means to restrict to the current subtree or region
  17972. \(if active)."
  17973. (interactive "P")
  17974. (catch 'exit
  17975. (let* ((prefix-descriptions nil)
  17976. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  17977. (org-agenda-custom-commands
  17978. ;; normalize different versions
  17979. (delq nil
  17980. (mapcar
  17981. (lambda (x)
  17982. (cond ((stringp (cdr x))
  17983. (push x prefix-descriptions)
  17984. nil)
  17985. ((stringp (nth 1 x)) x)
  17986. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17987. (t (cons (car x) (cons "" (cdr x))))))
  17988. org-agenda-custom-commands)))
  17989. (buf (current-buffer))
  17990. (bfn (buffer-file-name (buffer-base-buffer)))
  17991. entry key type match lprops ans)
  17992. ;; Turn off restriction unless there is an overriding one
  17993. (unless org-agenda-overriding-restriction
  17994. (put 'org-agenda-files 'org-restrict nil)
  17995. (setq org-agenda-restrict nil)
  17996. (move-marker org-agenda-restrict-begin nil)
  17997. (move-marker org-agenda-restrict-end nil))
  17998. ;; Delete old local properties
  17999. (put 'org-agenda-redo-command 'org-lprops nil)
  18000. ;; Remember where this call originated
  18001. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18002. (unless keys
  18003. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18004. keys (car ans)
  18005. restriction (cdr ans)))
  18006. ;; Estabish the restriction, if any
  18007. (when (and (not org-agenda-overriding-restriction) restriction)
  18008. (put 'org-agenda-files 'org-restrict (list bfn))
  18009. (cond
  18010. ((eq restriction 'region)
  18011. (setq org-agenda-restrict t)
  18012. (move-marker org-agenda-restrict-begin (region-beginning))
  18013. (move-marker org-agenda-restrict-end (region-end)))
  18014. ((eq restriction 'subtree)
  18015. (save-excursion
  18016. (setq org-agenda-restrict t)
  18017. (org-back-to-heading t)
  18018. (move-marker org-agenda-restrict-begin (point))
  18019. (move-marker org-agenda-restrict-end
  18020. (progn (org-end-of-subtree t)))))))
  18021. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18022. ;; For example the todo list should not need it (but does...)
  18023. (cond
  18024. ((setq entry (assoc keys org-agenda-custom-commands))
  18025. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18026. (progn
  18027. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18028. (put 'org-agenda-redo-command 'org-lprops lprops)
  18029. (cond
  18030. ((eq type 'agenda)
  18031. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18032. ((eq type 'alltodo)
  18033. (org-let lprops '(org-todo-list current-prefix-arg)))
  18034. ((eq type 'search)
  18035. (org-let lprops '(org-search-view current-prefix-arg match nil)))
  18036. ((eq type 'stuck)
  18037. (org-let lprops '(org-agenda-list-stuck-projects
  18038. current-prefix-arg)))
  18039. ((eq type 'tags)
  18040. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18041. ((eq type 'tags-todo)
  18042. (org-let lprops '(org-tags-view '(4) match)))
  18043. ((eq type 'todo)
  18044. (org-let lprops '(org-todo-list match)))
  18045. ((eq type 'tags-tree)
  18046. (org-check-for-org-mode)
  18047. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18048. ((eq type 'todo-tree)
  18049. (org-check-for-org-mode)
  18050. (org-let lprops
  18051. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18052. (regexp-quote match) "\\>"))))
  18053. ((eq type 'occur-tree)
  18054. (org-check-for-org-mode)
  18055. (org-let lprops '(org-occur match)))
  18056. ((functionp type)
  18057. (org-let lprops '(funcall type match)))
  18058. ((fboundp type)
  18059. (org-let lprops '(funcall type match)))
  18060. (t (error "Invalid custom agenda command type %s" type))))
  18061. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18062. ((equal keys "C")
  18063. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18064. (customize-variable 'org-agenda-custom-commands))
  18065. ((equal keys "a") (call-interactively 'org-agenda-list))
  18066. ((equal keys "s") (call-interactively 'org-search-view))
  18067. ((equal keys "t") (call-interactively 'org-todo-list))
  18068. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18069. ((equal keys "m") (call-interactively 'org-tags-view))
  18070. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18071. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18072. ((equal keys "L")
  18073. (unless (org-mode-p)
  18074. (error "This is not an Org-mode file"))
  18075. (unless restriction
  18076. (put 'org-agenda-files 'org-restrict (list bfn))
  18077. (org-call-with-arg 'org-timeline arg)))
  18078. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18079. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18080. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18081. (t (error "Invalid agenda key"))))))
  18082. (defun org-agenda-normalize-custom-commands (cmds)
  18083. (delq nil
  18084. (mapcar
  18085. (lambda (x)
  18086. (cond ((stringp (cdr x)) nil)
  18087. ((stringp (nth 1 x)) x)
  18088. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18089. (t (cons (car x) (cons "" (cdr x))))))
  18090. cmds)))
  18091. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18092. "The user interface for selecting an agenda command."
  18093. (catch 'exit
  18094. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18095. (restrict-ok (and bfn (org-mode-p)))
  18096. (region-p (org-region-active-p))
  18097. (custom org-agenda-custom-commands)
  18098. (selstring "")
  18099. restriction second-time
  18100. c entry key type match prefixes rmheader header-end custom1 desc)
  18101. (save-window-excursion
  18102. (delete-other-windows)
  18103. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18104. (erase-buffer)
  18105. (insert (eval-when-compile
  18106. (let ((header
  18107. "
  18108. Press key for an agenda command: < Buffer,subtree/region restriction
  18109. -------------------------------- > Remove restriction
  18110. a Agenda for current week or day e Export agenda views
  18111. t List of all TODO entries T Entries with special TODO kwd
  18112. m Match a TAGS query M Like m, but only TODO entries
  18113. L Timeline for current buffer # List stuck projects (!=configure)
  18114. s Search for keywords C Configure custom agenda commands
  18115. / Multi-occur
  18116. ")
  18117. (start 0))
  18118. (while (string-match
  18119. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18120. header start)
  18121. (setq start (match-end 0))
  18122. (add-text-properties (match-beginning 2) (match-end 2)
  18123. '(face bold) header))
  18124. header)))
  18125. (setq header-end (move-marker (make-marker) (point)))
  18126. (while t
  18127. (setq custom1 custom)
  18128. (when (eq rmheader t)
  18129. (goto-line 1)
  18130. (re-search-forward ":" nil t)
  18131. (delete-region (match-end 0) (point-at-eol))
  18132. (forward-char 1)
  18133. (looking-at "-+")
  18134. (delete-region (match-end 0) (point-at-eol))
  18135. (move-marker header-end (match-end 0)))
  18136. (goto-char header-end)
  18137. (delete-region (point) (point-max))
  18138. (while (setq entry (pop custom1))
  18139. (setq key (car entry) desc (nth 1 entry)
  18140. type (nth 2 entry) match (nth 3 entry))
  18141. (if (> (length key) 1)
  18142. (add-to-list 'prefixes (string-to-char key))
  18143. (insert
  18144. (format
  18145. "\n%-4s%-14s: %s"
  18146. (org-add-props (copy-sequence key)
  18147. '(face bold))
  18148. (cond
  18149. ((string-match "\\S-" desc) desc)
  18150. ((eq type 'agenda) "Agenda for current week or day")
  18151. ((eq type 'alltodo) "List of all TODO entries")
  18152. ((eq type 'search) "Word search")
  18153. ((eq type 'stuck) "List of stuck projects")
  18154. ((eq type 'todo) "TODO keyword")
  18155. ((eq type 'tags) "Tags query")
  18156. ((eq type 'tags-todo) "Tags (TODO)")
  18157. ((eq type 'tags-tree) "Tags tree")
  18158. ((eq type 'todo-tree) "TODO kwd tree")
  18159. ((eq type 'occur-tree) "Occur tree")
  18160. ((functionp type) (if (symbolp type)
  18161. (symbol-name type)
  18162. "Lambda expression"))
  18163. (t "???"))
  18164. (cond
  18165. ((stringp match)
  18166. (org-add-props match nil 'face 'org-warning))
  18167. (match
  18168. (format "set of %d commands" (length match)))
  18169. (t ""))))))
  18170. (when prefixes
  18171. (mapc (lambda (x)
  18172. (insert
  18173. (format "\n%s %s"
  18174. (org-add-props (char-to-string x)
  18175. nil 'face 'bold)
  18176. (or (cdr (assoc (concat selstring (char-to-string x))
  18177. prefix-descriptions))
  18178. "Prefix key"))))
  18179. prefixes))
  18180. (goto-char (point-min))
  18181. (when (fboundp 'fit-window-to-buffer)
  18182. (if second-time
  18183. (if (not (pos-visible-in-window-p (point-max)))
  18184. (fit-window-to-buffer))
  18185. (setq second-time t)
  18186. (fit-window-to-buffer)))
  18187. (message "Press key for agenda command%s:"
  18188. (if (or restrict-ok org-agenda-overriding-restriction)
  18189. (if org-agenda-overriding-restriction
  18190. " (restriction lock active)"
  18191. (if restriction
  18192. (format " (restricted to %s)" restriction)
  18193. " (unrestricted)"))
  18194. ""))
  18195. (setq c (read-char-exclusive))
  18196. (message "")
  18197. (cond
  18198. ((assoc (char-to-string c) custom)
  18199. (setq selstring (concat selstring (char-to-string c)))
  18200. (throw 'exit (cons selstring restriction)))
  18201. ((memq c prefixes)
  18202. (setq selstring (concat selstring (char-to-string c))
  18203. prefixes nil
  18204. rmheader (or rmheader t)
  18205. custom (delq nil (mapcar
  18206. (lambda (x)
  18207. (if (or (= (length (car x)) 1)
  18208. (/= (string-to-char (car x)) c))
  18209. nil
  18210. (cons (substring (car x) 1) (cdr x))))
  18211. custom))))
  18212. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18213. (message "Restriction is only possible in Org-mode buffers")
  18214. (ding) (sit-for 1))
  18215. ((eq c ?1)
  18216. (org-agenda-remove-restriction-lock 'noupdate)
  18217. (setq restriction 'buffer))
  18218. ((eq c ?0)
  18219. (org-agenda-remove-restriction-lock 'noupdate)
  18220. (setq restriction (if region-p 'region 'subtree)))
  18221. ((eq c ?<)
  18222. (org-agenda-remove-restriction-lock 'noupdate)
  18223. (setq restriction
  18224. (cond
  18225. ((eq restriction 'buffer)
  18226. (if region-p 'region 'subtree))
  18227. ((memq restriction '(subtree region))
  18228. nil)
  18229. (t 'buffer))))
  18230. ((eq c ?>)
  18231. (org-agenda-remove-restriction-lock 'noupdate)
  18232. (setq restriction nil))
  18233. ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18234. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18235. ((and (> (length selstring) 0) (eq c ?\d))
  18236. (delete-window)
  18237. (org-agenda-get-restriction-and-command prefix-descriptions))
  18238. ((equal c ?q) (error "Abort"))
  18239. (t (error "Invalid key %c" c))))))))
  18240. (defun org-run-agenda-series (name series)
  18241. (org-prepare-agenda name)
  18242. (let* ((org-agenda-multi t)
  18243. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18244. (cmds (car series))
  18245. (gprops (nth 1 series))
  18246. match ;; The byte compiler incorrectly complains about this. Keep it!
  18247. cmd type lprops)
  18248. (while (setq cmd (pop cmds))
  18249. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18250. (cond
  18251. ((eq type 'agenda)
  18252. (org-let2 gprops lprops
  18253. '(call-interactively 'org-agenda-list)))
  18254. ((eq type 'alltodo)
  18255. (org-let2 gprops lprops
  18256. '(call-interactively 'org-todo-list)))
  18257. ((eq type 'search)
  18258. (org-let2 gprops lprops
  18259. '(org-search-view current-prefix-arg match nil)))
  18260. ((eq type 'stuck)
  18261. (org-let2 gprops lprops
  18262. '(call-interactively 'org-agenda-list-stuck-projects)))
  18263. ((eq type 'tags)
  18264. (org-let2 gprops lprops
  18265. '(org-tags-view current-prefix-arg match)))
  18266. ((eq type 'tags-todo)
  18267. (org-let2 gprops lprops
  18268. '(org-tags-view '(4) match)))
  18269. ((eq type 'todo)
  18270. (org-let2 gprops lprops
  18271. '(org-todo-list match)))
  18272. ((fboundp type)
  18273. (org-let2 gprops lprops
  18274. '(funcall type match)))
  18275. (t (error "Invalid type in command series"))))
  18276. (widen)
  18277. (setq org-agenda-redo-command redo)
  18278. (goto-char (point-min)))
  18279. (org-finalize-agenda))
  18280. ;;;###autoload
  18281. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18282. "Run an agenda command in batch mode and send the result to STDOUT.
  18283. If CMD-KEY is a string of length 1, it is used as a key in
  18284. `org-agenda-custom-commands' and triggers this command. If it is a
  18285. longer string it is used as a tags/todo match string.
  18286. Paramters are alternating variable names and values that will be bound
  18287. before running the agenda command."
  18288. (let (pars)
  18289. (while parameters
  18290. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18291. (if (> (length cmd-key) 2)
  18292. (eval (list 'let (nreverse pars)
  18293. (list 'org-tags-view nil cmd-key)))
  18294. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18295. (set-buffer org-agenda-buffer-name)
  18296. (princ (org-encode-for-stdout (buffer-string)))))
  18297. (defun org-encode-for-stdout (string)
  18298. (if (fboundp 'encode-coding-string)
  18299. (encode-coding-string string buffer-file-coding-system)
  18300. string))
  18301. (defvar org-agenda-info nil)
  18302. ;;;###autoload
  18303. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18304. "Run an agenda command in batch mode and send the result to STDOUT.
  18305. If CMD-KEY is a string of length 1, it is used as a key in
  18306. `org-agenda-custom-commands' and triggers this command. If it is a
  18307. longer string it is used as a tags/todo match string.
  18308. Paramters are alternating variable names and values that will be bound
  18309. before running the agenda command.
  18310. The output gives a line for each selected agenda item. Each
  18311. item is a list of comma-separated values, like this:
  18312. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18313. category The category of the item
  18314. head The headline, without TODO kwd, TAGS and PRIORITY
  18315. type The type of the agenda entry, can be
  18316. todo selected in TODO match
  18317. tagsmatch selected in tags match
  18318. diary imported from diary
  18319. deadline a deadline on given date
  18320. scheduled scheduled on given date
  18321. timestamp entry has timestamp on given date
  18322. closed entry was closed on given date
  18323. upcoming-deadline warning about deadline
  18324. past-scheduled forwarded scheduled item
  18325. block entry has date block including g. date
  18326. todo The todo keyword, if any
  18327. tags All tags including inherited ones, separated by colons
  18328. date The relevant date, like 2007-2-14
  18329. time The time, like 15:00-16:50
  18330. extra Sting with extra planning info
  18331. priority-l The priority letter if any was given
  18332. priority-n The computed numerical priority
  18333. agenda-day The day in the agenda where this is listed"
  18334. (let (pars)
  18335. (while parameters
  18336. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18337. (push (list 'org-agenda-remove-tags t) pars)
  18338. (if (> (length cmd-key) 2)
  18339. (eval (list 'let (nreverse pars)
  18340. (list 'org-tags-view nil cmd-key)))
  18341. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18342. (set-buffer org-agenda-buffer-name)
  18343. (let* ((lines (org-split-string (buffer-string) "\n"))
  18344. line)
  18345. (while (setq line (pop lines))
  18346. (catch 'next
  18347. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18348. (setq org-agenda-info
  18349. (org-fix-agenda-info (text-properties-at 0 line)))
  18350. (princ
  18351. (org-encode-for-stdout
  18352. (mapconcat 'org-agenda-export-csv-mapper
  18353. '(org-category txt type todo tags date time-of-day extra
  18354. priority-letter priority agenda-day)
  18355. ",")))
  18356. (princ "\n"))))))
  18357. (defun org-fix-agenda-info (props)
  18358. "Make sure all properties on an agenda item have a canonical form,
  18359. so the export commands can easily use it."
  18360. (let (tmp re)
  18361. (when (setq tmp (plist-get props 'tags))
  18362. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18363. (when (setq tmp (plist-get props 'date))
  18364. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18365. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18366. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18367. (setq tmp (calendar-date-string tmp)))
  18368. (setq props (plist-put props 'date tmp)))
  18369. (when (setq tmp (plist-get props 'day))
  18370. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18371. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18372. (setq tmp (calendar-date-string tmp)))
  18373. (setq props (plist-put props 'day tmp))
  18374. (setq props (plist-put props 'agenda-day tmp)))
  18375. (when (setq tmp (plist-get props 'txt))
  18376. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18377. (plist-put props 'priority-letter (match-string 1 tmp))
  18378. (setq tmp (replace-match "" t t tmp)))
  18379. (when (and (setq re (plist-get props 'org-todo-regexp))
  18380. (setq re (concat "\\`\\.*" re " ?"))
  18381. (string-match re tmp))
  18382. (plist-put props 'todo (match-string 1 tmp))
  18383. (setq tmp (replace-match "" t t tmp)))
  18384. (plist-put props 'txt tmp)))
  18385. props)
  18386. (defun org-agenda-export-csv-mapper (prop)
  18387. (let ((res (plist-get org-agenda-info prop)))
  18388. (setq res
  18389. (cond
  18390. ((not res) "")
  18391. ((stringp res) res)
  18392. (t (prin1-to-string res))))
  18393. (while (string-match "," res)
  18394. (setq res (replace-match ";" t t res)))
  18395. (org-trim res)))
  18396. ;;;###autoload
  18397. (defun org-store-agenda-views (&rest parameters)
  18398. (interactive)
  18399. (eval (list 'org-batch-store-agenda-views)))
  18400. ;; FIXME, why is this a macro?????
  18401. ;;;###autoload
  18402. (defmacro org-batch-store-agenda-views (&rest parameters)
  18403. "Run all custom agenda commands that have a file argument."
  18404. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18405. (pop-up-frames nil)
  18406. (dir default-directory)
  18407. pars cmd thiscmdkey files opts)
  18408. (while parameters
  18409. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18410. (setq pars (reverse pars))
  18411. (save-window-excursion
  18412. (while cmds
  18413. (setq cmd (pop cmds)
  18414. thiscmdkey (car cmd)
  18415. opts (nth 4 cmd)
  18416. files (nth 5 cmd))
  18417. (if (stringp files) (setq files (list files)))
  18418. (when files
  18419. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18420. (list 'org-agenda nil thiscmdkey)))
  18421. (set-buffer org-agenda-buffer-name)
  18422. (while files
  18423. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18424. (list 'org-write-agenda
  18425. (expand-file-name (pop files) dir) t))))
  18426. (and (get-buffer org-agenda-buffer-name)
  18427. (kill-buffer org-agenda-buffer-name)))))))
  18428. (defun org-write-agenda (file &optional nosettings)
  18429. "Write the current buffer (an agenda view) as a file.
  18430. Depending on the extension of the file name, plain text (.txt),
  18431. HTML (.html or .htm) or Postscript (.ps) is produced.
  18432. If the extension is .ics, run icalendar export over all files used
  18433. to construct the agenda and limit the export to entries listed in the
  18434. agenda now.
  18435. If NOSETTINGS is given, do not scope the settings of
  18436. `org-agenda-exporter-settings' into the export commands. This is used when
  18437. the settings have already been scoped and we do not wish to overrule other,
  18438. higher priority settings."
  18439. (interactive "FWrite agenda to file: ")
  18440. (if (not (file-writable-p file))
  18441. (error "Cannot write agenda to file %s" file))
  18442. (cond
  18443. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18444. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18445. (org-let (if nosettings nil org-agenda-exporter-settings)
  18446. '(save-excursion
  18447. (save-window-excursion
  18448. (cond
  18449. ((string-match "\\.html?\\'" file)
  18450. (set-buffer (htmlize-buffer (current-buffer)))
  18451. (when (and org-agenda-export-html-style
  18452. (string-match "<style>" org-agenda-export-html-style))
  18453. ;; replace <style> section with org-agenda-export-html-style
  18454. (goto-char (point-min))
  18455. (kill-region (- (search-forward "<style") 6)
  18456. (search-forward "</style>"))
  18457. (insert org-agenda-export-html-style))
  18458. (write-file file)
  18459. (kill-buffer (current-buffer))
  18460. (message "HTML written to %s" file))
  18461. ((string-match "\\.ps\\'" file)
  18462. (ps-print-buffer-with-faces file)
  18463. (message "Postscript written to %s" file))
  18464. ((string-match "\\.ics\\'" file)
  18465. (let ((org-agenda-marker-table
  18466. (org-create-marker-find-array
  18467. (org-agenda-collect-markers)))
  18468. (org-icalendar-verify-function 'org-check-agenda-marker-table)
  18469. (org-combined-agenda-icalendar-file file))
  18470. (apply 'org-export-icalendar 'combine (org-agenda-files))))
  18471. (t
  18472. (let ((bs (buffer-string)))
  18473. (find-file file)
  18474. (insert bs)
  18475. (save-buffer 0)
  18476. (kill-buffer (current-buffer))
  18477. (message "Plain text written to %s" file))))))
  18478. (set-buffer org-agenda-buffer-name)))
  18479. (defun org-agenda-collect-markers ()
  18480. "Collect the markers pointing to entries in the agenda buffer."
  18481. (let (m markers)
  18482. (save-excursion
  18483. (goto-char (point-min))
  18484. (while (not (eobp))
  18485. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  18486. (get-text-property (point) 'org-marker)))
  18487. (push m markers))
  18488. (beginning-of-line 2)))
  18489. (nreverse markers)))
  18490. (defun org-create-marker-find-array (marker-list)
  18491. "Create a alist of files names with all marker positions in that file."
  18492. (let (f tbl m a p)
  18493. (while (setq m (pop marker-list))
  18494. (setq p (marker-position m)
  18495. f (buffer-file-name (or (buffer-base-buffer
  18496. (marker-buffer m))
  18497. (marker-buffer m))))
  18498. (if (setq a (assoc f tbl))
  18499. (push (marker-position m) (cdr a))
  18500. (push (list f p) tbl)))
  18501. (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
  18502. tbl)))
  18503. (defvar org-agenda-marker-table nil) ; dyamically scoped parameter
  18504. (defun org-check-agenda-marker-table ()
  18505. "Check of the current entry is on the marker list."
  18506. (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  18507. a)
  18508. (and (setq a (assoc file org-agenda-marker-table))
  18509. (save-match-data
  18510. (save-excursion
  18511. (org-back-to-heading t)
  18512. (member (point) (cdr a)))))))
  18513. (defmacro org-no-read-only (&rest body)
  18514. "Inhibit read-only for BODY."
  18515. `(let ((inhibit-read-only t)) ,@body))
  18516. (defun org-check-for-org-mode ()
  18517. "Make sure current buffer is in org-mode. Error if not."
  18518. (or (org-mode-p)
  18519. (error "Cannot execute org-mode agenda command on buffer in %s."
  18520. major-mode)))
  18521. (defun org-fit-agenda-window ()
  18522. "Fit the window to the buffer size."
  18523. (and (memq org-agenda-window-setup '(reorganize-frame))
  18524. (fboundp 'fit-window-to-buffer)
  18525. (fit-window-to-buffer
  18526. nil
  18527. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18528. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18529. ;;; Agenda file list
  18530. (defun org-agenda-files (&optional unrestricted)
  18531. "Get the list of agenda files.
  18532. Optional UNRESTRICTED means return the full list even if a restriction
  18533. is currently in place."
  18534. (let ((files
  18535. (cond
  18536. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18537. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18538. ((listp org-agenda-files) org-agenda-files)
  18539. (t (error "Invalid value of `org-agenda-files'")))))
  18540. (setq files (apply 'append
  18541. (mapcar (lambda (f)
  18542. (if (file-directory-p f)
  18543. (directory-files f t
  18544. org-agenda-file-regexp)
  18545. (list f)))
  18546. files)))
  18547. (if org-agenda-skip-unavailable-files
  18548. (delq nil
  18549. (mapcar (function
  18550. (lambda (file)
  18551. (and (file-readable-p file) file)))
  18552. files))
  18553. files))) ; `org-check-agenda-file' will remove them from the list
  18554. (defun org-edit-agenda-file-list ()
  18555. "Edit the list of agenda files.
  18556. Depending on setup, this either uses customize to edit the variable
  18557. `org-agenda-files', or it visits the file that is holding the list. In the
  18558. latter case, the buffer is set up in a way that saving it automatically kills
  18559. the buffer and restores the previous window configuration."
  18560. (interactive)
  18561. (if (stringp org-agenda-files)
  18562. (let ((cw (current-window-configuration)))
  18563. (find-file org-agenda-files)
  18564. (org-set-local 'org-window-configuration cw)
  18565. (org-add-hook 'after-save-hook
  18566. (lambda ()
  18567. (set-window-configuration
  18568. (prog1 org-window-configuration
  18569. (kill-buffer (current-buffer))))
  18570. (org-install-agenda-files-menu)
  18571. (message "New agenda file list installed"))
  18572. nil 'local)
  18573. (message "%s" (substitute-command-keys
  18574. "Edit list and finish with \\[save-buffer]")))
  18575. (customize-variable 'org-agenda-files)))
  18576. (defun org-store-new-agenda-file-list (list)
  18577. "Set new value for the agenda file list and save it correcly."
  18578. (if (stringp org-agenda-files)
  18579. (let ((f org-agenda-files) b)
  18580. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18581. (with-temp-file f
  18582. (insert (mapconcat 'identity list "\n") "\n")))
  18583. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18584. (setq org-agenda-files list)
  18585. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18586. (defun org-read-agenda-file-list ()
  18587. "Read the list of agenda files from a file."
  18588. (when (stringp org-agenda-files)
  18589. (with-temp-buffer
  18590. (insert-file-contents org-agenda-files)
  18591. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18592. ;;;###autoload
  18593. (defun org-cycle-agenda-files ()
  18594. "Cycle through the files in `org-agenda-files'.
  18595. If the current buffer visits an agenda file, find the next one in the list.
  18596. If the current buffer does not, find the first agenda file."
  18597. (interactive)
  18598. (let* ((fs (org-agenda-files t))
  18599. (files (append fs (list (car fs))))
  18600. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18601. file)
  18602. (unless files (error "No agenda files"))
  18603. (catch 'exit
  18604. (while (setq file (pop files))
  18605. (if (equal (file-truename file) tcf)
  18606. (when (car files)
  18607. (find-file (car files))
  18608. (throw 'exit t))))
  18609. (find-file (car fs)))
  18610. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18611. (defun org-agenda-file-to-front (&optional to-end)
  18612. "Move/add the current file to the top of the agenda file list.
  18613. If the file is not present in the list, it is added to the front. If it is
  18614. present, it is moved there. With optional argument TO-END, add/move to the
  18615. end of the list."
  18616. (interactive "P")
  18617. (let ((org-agenda-skip-unavailable-files nil)
  18618. (file-alist (mapcar (lambda (x)
  18619. (cons (file-truename x) x))
  18620. (org-agenda-files t)))
  18621. (ctf (file-truename buffer-file-name))
  18622. x had)
  18623. (setq x (assoc ctf file-alist) had x)
  18624. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18625. (if to-end
  18626. (setq file-alist (append (delq x file-alist) (list x)))
  18627. (setq file-alist (cons x (delq x file-alist))))
  18628. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18629. (org-install-agenda-files-menu)
  18630. (message "File %s to %s of agenda file list"
  18631. (if had "moved" "added") (if to-end "end" "front"))))
  18632. (defun org-remove-file (&optional file)
  18633. "Remove current file from the list of files in variable `org-agenda-files'.
  18634. These are the files which are being checked for agenda entries.
  18635. Optional argument FILE means, use this file instead of the current."
  18636. (interactive)
  18637. (let* ((org-agenda-skip-unavailable-files nil)
  18638. (file (or file buffer-file-name))
  18639. (true-file (file-truename file))
  18640. (afile (abbreviate-file-name file))
  18641. (files (delq nil (mapcar
  18642. (lambda (x)
  18643. (if (equal true-file
  18644. (file-truename x))
  18645. nil x))
  18646. (org-agenda-files t)))))
  18647. (if (not (= (length files) (length (org-agenda-files t))))
  18648. (progn
  18649. (org-store-new-agenda-file-list files)
  18650. (org-install-agenda-files-menu)
  18651. (message "Removed file: %s" afile))
  18652. (message "File was not in list: %s (not removed)" afile))))
  18653. (defun org-file-menu-entry (file)
  18654. (vector file (list 'find-file file) t))
  18655. (defun org-check-agenda-file (file)
  18656. "Make sure FILE exists. If not, ask user what to do."
  18657. (when (not (file-exists-p file))
  18658. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18659. (abbreviate-file-name file))
  18660. (let ((r (downcase (read-char-exclusive))))
  18661. (cond
  18662. ((equal r ?r)
  18663. (org-remove-file file)
  18664. (throw 'nextfile t))
  18665. (t (error "Abort"))))))
  18666. ;;; Agenda prepare and finalize
  18667. (defvar org-agenda-multi nil) ; dynammically scoped
  18668. (defvar org-agenda-buffer-name "*Org Agenda*")
  18669. (defvar org-pre-agenda-window-conf nil)
  18670. (defvar org-agenda-name nil)
  18671. (defun org-prepare-agenda (&optional name)
  18672. (setq org-todo-keywords-for-agenda nil)
  18673. (setq org-done-keywords-for-agenda nil)
  18674. (if org-agenda-multi
  18675. (progn
  18676. (setq buffer-read-only nil)
  18677. (goto-char (point-max))
  18678. (unless (or (bobp) org-agenda-compact-blocks)
  18679. (insert "\n" (make-string (window-width) ?=) "\n"))
  18680. (narrow-to-region (point) (point-max)))
  18681. (org-agenda-reset-markers)
  18682. (org-prepare-agenda-buffers (org-agenda-files))
  18683. (setq org-todo-keywords-for-agenda
  18684. (org-uniquify org-todo-keywords-for-agenda))
  18685. (setq org-done-keywords-for-agenda
  18686. (org-uniquify org-done-keywords-for-agenda))
  18687. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18688. (awin (get-buffer-window abuf)))
  18689. (cond
  18690. ((equal (current-buffer) abuf) nil)
  18691. (awin (select-window awin))
  18692. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18693. ((equal org-agenda-window-setup 'current-window)
  18694. (switch-to-buffer abuf))
  18695. ((equal org-agenda-window-setup 'other-window)
  18696. (org-switch-to-buffer-other-window abuf))
  18697. ((equal org-agenda-window-setup 'other-frame)
  18698. (switch-to-buffer-other-frame abuf))
  18699. ((equal org-agenda-window-setup 'reorganize-frame)
  18700. (delete-other-windows)
  18701. (org-switch-to-buffer-other-window abuf))))
  18702. (setq buffer-read-only nil)
  18703. (erase-buffer)
  18704. (org-agenda-mode)
  18705. (and name (not org-agenda-name)
  18706. (org-set-local 'org-agenda-name name)))
  18707. (setq buffer-read-only nil))
  18708. (defun org-finalize-agenda ()
  18709. "Finishing touch for the agenda buffer, called just before displaying it."
  18710. (unless org-agenda-multi
  18711. (save-excursion
  18712. (let ((inhibit-read-only t))
  18713. (goto-char (point-min))
  18714. (while (org-activate-bracket-links (point-max))
  18715. (add-text-properties (match-beginning 0) (match-end 0)
  18716. '(face org-link)))
  18717. (org-agenda-align-tags)
  18718. (unless org-agenda-with-colors
  18719. (remove-text-properties (point-min) (point-max) '(face nil))))
  18720. (if (and (boundp 'org-overriding-columns-format)
  18721. org-overriding-columns-format)
  18722. (org-set-local 'org-overriding-columns-format
  18723. org-overriding-columns-format))
  18724. (if (and (boundp 'org-agenda-view-columns-initially)
  18725. org-agenda-view-columns-initially)
  18726. (org-agenda-columns))
  18727. (when org-agenda-fontify-priorities
  18728. (org-fontify-priorities))
  18729. (run-hooks 'org-finalize-agenda-hook)
  18730. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18731. )))
  18732. (defun org-fontify-priorities ()
  18733. "Make highest priority lines bold, and lowest italic."
  18734. (interactive)
  18735. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18736. (org-delete-overlay o)))
  18737. (org-overlays-in (point-min) (point-max)))
  18738. (save-excursion
  18739. (let ((inhibit-read-only t)
  18740. b e p ov h l)
  18741. (goto-char (point-min))
  18742. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18743. (setq h (or (get-char-property (point) 'org-highest-priority)
  18744. org-highest-priority)
  18745. l (or (get-char-property (point) 'org-lowest-priority)
  18746. org-lowest-priority)
  18747. p (string-to-char (match-string 1))
  18748. b (match-beginning 0) e (point-at-eol)
  18749. ov (org-make-overlay b e))
  18750. (org-overlay-put
  18751. ov 'face
  18752. (cond ((listp org-agenda-fontify-priorities)
  18753. (cdr (assoc p org-agenda-fontify-priorities)))
  18754. ((equal p l) 'italic)
  18755. ((equal p h) 'bold)))
  18756. (org-overlay-put ov 'org-type 'org-priority)))))
  18757. (defun org-prepare-agenda-buffers (files)
  18758. "Create buffers for all agenda files, protect archived trees and comments."
  18759. (interactive)
  18760. (let ((pa '(:org-archived t))
  18761. (pc '(:org-comment t))
  18762. (pall '(:org-archived t :org-comment t))
  18763. (inhibit-read-only t)
  18764. (rea (concat ":" org-archive-tag ":"))
  18765. bmp file re)
  18766. (save-excursion
  18767. (save-restriction
  18768. (while (setq file (pop files))
  18769. (if (bufferp file)
  18770. (set-buffer file)
  18771. (org-check-agenda-file file)
  18772. (set-buffer (org-get-agenda-file-buffer file)))
  18773. (widen)
  18774. (setq bmp (buffer-modified-p))
  18775. (org-refresh-category-properties)
  18776. (setq org-todo-keywords-for-agenda
  18777. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18778. (setq org-done-keywords-for-agenda
  18779. (append org-done-keywords-for-agenda org-done-keywords))
  18780. (save-excursion
  18781. (remove-text-properties (point-min) (point-max) pall)
  18782. (when org-agenda-skip-archived-trees
  18783. (goto-char (point-min))
  18784. (while (re-search-forward rea nil t)
  18785. (if (org-on-heading-p t)
  18786. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18787. (goto-char (point-min))
  18788. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18789. (while (re-search-forward re nil t)
  18790. (add-text-properties
  18791. (match-beginning 0) (org-end-of-subtree t) pc)))
  18792. (set-buffer-modified-p bmp))))))
  18793. (defvar org-agenda-skip-function nil
  18794. "Function to be called at each match during agenda construction.
  18795. If this function returns nil, the current match should not be skipped.
  18796. Otherwise, the function must return a position from where the search
  18797. should be continued.
  18798. This may also be a Lisp form, it will be evaluated.
  18799. Never set this variable using `setq' or so, because then it will apply
  18800. to all future agenda commands. Instead, bind it with `let' to scope
  18801. it dynamically into the agenda-constructing command. A good way to set
  18802. it is through options in org-agenda-custom-commands.")
  18803. (defun org-agenda-skip ()
  18804. "Throw to `:skip' in places that should be skipped.
  18805. Also moves point to the end of the skipped region, so that search can
  18806. continue from there."
  18807. (let ((p (point-at-bol)) to fp)
  18808. (and org-agenda-skip-archived-trees
  18809. (get-text-property p :org-archived)
  18810. (org-end-of-subtree t)
  18811. (throw :skip t))
  18812. (and (get-text-property p :org-comment)
  18813. (org-end-of-subtree t)
  18814. (throw :skip t))
  18815. (if (equal (char-after p) ?#) (throw :skip t))
  18816. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18817. (consp org-agenda-skip-function))
  18818. (setq to (save-excursion
  18819. (save-match-data
  18820. (if fp
  18821. (funcall org-agenda-skip-function)
  18822. (eval org-agenda-skip-function))))))
  18823. (goto-char to)
  18824. (throw :skip t))))
  18825. (defvar org-agenda-markers nil
  18826. "List of all currently active markers created by `org-agenda'.")
  18827. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18828. "Creation time of the last agenda marker.")
  18829. (defun org-agenda-new-marker (&optional pos)
  18830. "Return a new agenda marker.
  18831. Org-mode keeps a list of these markers and resets them when they are
  18832. no longer in use."
  18833. (let ((m (copy-marker (or pos (point)))))
  18834. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18835. (push m org-agenda-markers)
  18836. m))
  18837. (defun org-agenda-reset-markers ()
  18838. "Reset markers created by `org-agenda'."
  18839. (while org-agenda-markers
  18840. (move-marker (pop org-agenda-markers) nil)))
  18841. (defun org-get-agenda-file-buffer (file)
  18842. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18843. it to the list of buffers which might be released later."
  18844. (let ((buf (org-find-base-buffer-visiting file)))
  18845. (if buf
  18846. buf ; just return it
  18847. ;; Make a new buffer and remember it
  18848. (setq buf (find-file-noselect file))
  18849. (if buf (push buf org-agenda-new-buffers))
  18850. buf)))
  18851. (defun org-release-buffers (blist)
  18852. "Release all buffers in list, asking the user for confirmation when needed.
  18853. When a buffer is unmodified, it is just killed. When modified, it is saved
  18854. \(if the user agrees) and then killed."
  18855. (let (buf file)
  18856. (while (setq buf (pop blist))
  18857. (setq file (buffer-file-name buf))
  18858. (when (and (buffer-modified-p buf)
  18859. file
  18860. (y-or-n-p (format "Save file %s? " file)))
  18861. (with-current-buffer buf (save-buffer)))
  18862. (kill-buffer buf))))
  18863. (defun org-get-category (&optional pos)
  18864. "Get the category applying to position POS."
  18865. (get-text-property (or pos (point)) 'org-category))
  18866. ;;; Agenda timeline
  18867. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18868. (defun org-timeline (&optional include-all)
  18869. "Show a time-sorted view of the entries in the current org file.
  18870. Only entries with a time stamp of today or later will be listed. With
  18871. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18872. under the current date.
  18873. If the buffer contains an active region, only check the region for
  18874. dates."
  18875. (interactive "P")
  18876. (require 'calendar)
  18877. (org-compile-prefix-format 'timeline)
  18878. (org-set-sorting-strategy 'timeline)
  18879. (let* ((dopast t)
  18880. (dotodo include-all)
  18881. (doclosed org-agenda-show-log)
  18882. (entry buffer-file-name)
  18883. (date (calendar-current-date))
  18884. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18885. (end (if (org-region-active-p) (region-end) (point-max)))
  18886. (day-numbers (org-get-all-dates beg end 'no-ranges
  18887. t doclosed ; always include today
  18888. org-timeline-show-empty-dates))
  18889. (org-deadline-warning-days 0)
  18890. (org-agenda-only-exact-dates t)
  18891. (today (time-to-days (current-time)))
  18892. (past t)
  18893. args
  18894. s e rtn d emptyp)
  18895. (setq org-agenda-redo-command
  18896. (list 'progn
  18897. (list 'org-switch-to-buffer-other-window (current-buffer))
  18898. (list 'org-timeline (list 'quote include-all))))
  18899. (if (not dopast)
  18900. ;; Remove past dates from the list of dates.
  18901. (setq day-numbers (delq nil (mapcar (lambda(x)
  18902. (if (>= x today) x nil))
  18903. day-numbers))))
  18904. (org-prepare-agenda (concat "Timeline "
  18905. (file-name-nondirectory buffer-file-name)))
  18906. (if doclosed (push :closed args))
  18907. (push :timestamp args)
  18908. (push :deadline args)
  18909. (push :scheduled args)
  18910. (push :sexp args)
  18911. (if dotodo (push :todo args))
  18912. (while (setq d (pop day-numbers))
  18913. (if (and (listp d) (eq (car d) :omitted))
  18914. (progn
  18915. (setq s (point))
  18916. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18917. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18918. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18919. (if (and (>= d today)
  18920. dopast
  18921. past)
  18922. (progn
  18923. (setq past nil)
  18924. (insert (make-string 79 ?-) "\n")))
  18925. (setq date (calendar-gregorian-from-absolute d))
  18926. (setq s (point))
  18927. (setq rtn (and (not emptyp)
  18928. (apply 'org-agenda-get-day-entries entry
  18929. date args)))
  18930. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18931. (progn
  18932. (insert
  18933. (if (stringp org-agenda-format-date)
  18934. (format-time-string org-agenda-format-date
  18935. (org-time-from-absolute date))
  18936. (funcall org-agenda-format-date date))
  18937. "\n")
  18938. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18939. (put-text-property s (1- (point)) 'org-date-line t)
  18940. (if (equal d today)
  18941. (put-text-property s (1- (point)) 'org-today t))
  18942. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18943. (put-text-property s (1- (point)) 'day d)))))
  18944. (goto-char (point-min))
  18945. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18946. (point-min)))
  18947. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18948. (org-finalize-agenda)
  18949. (setq buffer-read-only t)))
  18950. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  18951. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18952. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18953. not every single day in the range. If FORCE-TODAY is non-nil, make
  18954. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18955. inactive time stamps (those in square brackets) are included.
  18956. When EMPTY is non-nil, also include days without any entries."
  18957. (let ((re (concat
  18958. (if pre-re pre-re "")
  18959. (if inactive org-ts-regexp-both org-ts-regexp)))
  18960. dates dates1 date day day1 day2 ts1 ts2)
  18961. (if force-today
  18962. (setq dates (list (time-to-days (current-time)))))
  18963. (save-excursion
  18964. (goto-char beg)
  18965. (while (re-search-forward re end t)
  18966. (setq day (time-to-days (org-time-string-to-time
  18967. (substring (match-string 1) 0 10))))
  18968. (or (memq day dates) (push day dates)))
  18969. (unless no-ranges
  18970. (goto-char beg)
  18971. (while (re-search-forward org-tr-regexp end t)
  18972. (setq ts1 (substring (match-string 1) 0 10)
  18973. ts2 (substring (match-string 2) 0 10)
  18974. day1 (time-to-days (org-time-string-to-time ts1))
  18975. day2 (time-to-days (org-time-string-to-time ts2)))
  18976. (while (< (setq day1 (1+ day1)) day2)
  18977. (or (memq day1 dates) (push day1 dates)))))
  18978. (setq dates (sort dates '<))
  18979. (when empty
  18980. (while (setq day (pop dates))
  18981. (setq day2 (car dates))
  18982. (push day dates1)
  18983. (when (and day2 empty)
  18984. (if (or (eq empty t)
  18985. (and (numberp empty) (<= (- day2 day) empty)))
  18986. (while (< (setq day (1+ day)) day2)
  18987. (push (list day) dates1))
  18988. (push (cons :omitted (- day2 day)) dates1))))
  18989. (setq dates (nreverse dates1)))
  18990. dates)))
  18991. ;;; Agenda Daily/Weekly
  18992. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18993. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18994. (defvar org-agenda-last-arguments nil
  18995. "The arguments of the previous call to org-agenda")
  18996. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18997. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18998. (defvar org-include-all-loc nil) ; local variable
  18999. (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
  19000. ;;;###autoload
  19001. (defun org-agenda-list (&optional include-all start-day ndays)
  19002. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19003. The view will be for the current day or week, but from the overview buffer
  19004. you will be able to go to other days/weeks.
  19005. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19006. all unfinished TODO items will also be shown, before the agenda.
  19007. This feature is considered obsolete, please use the TODO list or a block
  19008. agenda instead.
  19009. With a numeric prefix argument in an interactive call, the agenda will
  19010. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19011. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19012. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19013. given in `org-agenda-start-on-weekday'."
  19014. (interactive "P")
  19015. (if (and (integerp include-all) (> include-all 0))
  19016. (setq ndays include-all include-all nil))
  19017. (setq ndays (or ndays org-agenda-ndays)
  19018. start-day (or start-day org-agenda-start-day))
  19019. (if org-agenda-overriding-arguments
  19020. (setq include-all (car org-agenda-overriding-arguments)
  19021. start-day (nth 1 org-agenda-overriding-arguments)
  19022. ndays (nth 2 org-agenda-overriding-arguments)))
  19023. (if (stringp start-day)
  19024. ;; Convert to an absolute day number
  19025. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19026. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19027. (org-compile-prefix-format 'agenda)
  19028. (org-set-sorting-strategy 'agenda)
  19029. (require 'calendar)
  19030. (let* ((org-agenda-start-on-weekday
  19031. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19032. org-agenda-start-on-weekday nil))
  19033. (thefiles (org-agenda-files))
  19034. (files thefiles)
  19035. (today (time-to-days
  19036. (time-subtract (current-time)
  19037. (list 0 (* 3600 org-extend-today-until) 0))))
  19038. (sd (or start-day today))
  19039. (start (if (or (null org-agenda-start-on-weekday)
  19040. (< org-agenda-ndays 7))
  19041. sd
  19042. (let* ((nt (calendar-day-of-week
  19043. (calendar-gregorian-from-absolute sd)))
  19044. (n1 org-agenda-start-on-weekday)
  19045. (d (- nt n1)))
  19046. (- sd (+ (if (< d 0) 7 0) d)))))
  19047. (day-numbers (list start))
  19048. (day-cnt 0)
  19049. (inhibit-redisplay (not debug-on-error))
  19050. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19051. (setq org-agenda-redo-command
  19052. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19053. ;; Make the list of days
  19054. (setq ndays (or ndays org-agenda-ndays)
  19055. nd ndays)
  19056. (while (> ndays 1)
  19057. (push (1+ (car day-numbers)) day-numbers)
  19058. (setq ndays (1- ndays)))
  19059. (setq day-numbers (nreverse day-numbers))
  19060. (org-prepare-agenda "Day/Week")
  19061. (org-set-local 'org-starting-day (car day-numbers))
  19062. (org-set-local 'org-include-all-loc include-all)
  19063. (org-set-local 'org-agenda-span
  19064. (org-agenda-ndays-to-span nd))
  19065. (when (and (or include-all org-agenda-include-all-todo)
  19066. (member today day-numbers))
  19067. (setq files thefiles
  19068. rtnall nil)
  19069. (while (setq file (pop files))
  19070. (catch 'nextfile
  19071. (org-check-agenda-file file)
  19072. (setq date (calendar-gregorian-from-absolute today)
  19073. rtn (org-agenda-get-day-entries
  19074. file date :todo))
  19075. (setq rtnall (append rtnall rtn))))
  19076. (when rtnall
  19077. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19078. (add-text-properties (point-min) (1- (point))
  19079. (list 'face 'org-agenda-structure))
  19080. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19081. (unless org-agenda-compact-blocks
  19082. (setq s (point))
  19083. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19084. "-agenda:\n")
  19085. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19086. 'org-date-line t)))
  19087. (while (setq d (pop day-numbers))
  19088. (setq date (calendar-gregorian-from-absolute d)
  19089. s (point))
  19090. (if (or (setq todayp (= d today))
  19091. (and (not start-pos) (= d sd)))
  19092. (setq start-pos (point))
  19093. (if (and start-pos (not end-pos))
  19094. (setq end-pos (point))))
  19095. (setq files thefiles
  19096. rtnall nil)
  19097. (while (setq file (pop files))
  19098. (catch 'nextfile
  19099. (org-check-agenda-file file)
  19100. (if org-agenda-show-log
  19101. (setq rtn (org-agenda-get-day-entries
  19102. file date
  19103. :deadline :scheduled :timestamp :sexp :closed))
  19104. (setq rtn (org-agenda-get-day-entries
  19105. file date
  19106. :deadline :scheduled :sexp :timestamp)))
  19107. (setq rtnall (append rtnall rtn))))
  19108. (if org-agenda-include-diary
  19109. (progn
  19110. (require 'diary-lib)
  19111. (setq rtn (org-get-entries-from-diary date))
  19112. (setq rtnall (append rtnall rtn))))
  19113. (if (or rtnall org-agenda-show-all-dates)
  19114. (progn
  19115. (setq day-cnt (1+ day-cnt))
  19116. (insert
  19117. (if (stringp org-agenda-format-date)
  19118. (format-time-string org-agenda-format-date
  19119. (org-time-from-absolute date))
  19120. (funcall org-agenda-format-date date))
  19121. "\n")
  19122. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19123. (put-text-property s (1- (point)) 'org-date-line t)
  19124. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19125. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19126. (if rtnall (insert
  19127. (org-finalize-agenda-entries
  19128. (org-agenda-add-time-grid-maybe
  19129. rtnall nd todayp))
  19130. "\n"))
  19131. (put-text-property s (1- (point)) 'day d)
  19132. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19133. (goto-char (point-min))
  19134. (org-fit-agenda-window)
  19135. (unless (and (pos-visible-in-window-p (point-min))
  19136. (pos-visible-in-window-p (point-max)))
  19137. (goto-char (1- (point-max)))
  19138. (recenter -1)
  19139. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19140. (progn
  19141. (goto-char (or start-pos 1))
  19142. (recenter 1))))
  19143. (goto-char (or start-pos 1))
  19144. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19145. (org-finalize-agenda)
  19146. (setq buffer-read-only t)
  19147. (message "")))
  19148. (defun org-agenda-ndays-to-span (n)
  19149. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19150. ;;; Agenda word search
  19151. (defvar org-agenda-search-history nil)
  19152. (defvar org-todo-only nil)
  19153. (defvar org-search-syntax-table nil
  19154. "Special syntax table for org-mode search.
  19155. In this table, we have single quotes not as word constituents, to
  19156. that when \"+Ameli\" is searchd as a work, it will also match \"Ameli's\"")
  19157. (defun org-search-syntax-table ()
  19158. (unless org-search-syntax-table
  19159. (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
  19160. (modify-syntax-entry ?' "." org-search-syntax-table)
  19161. (modify-syntax-entry ?` "." org-search-syntax-table))
  19162. org-search-syntax-table)
  19163. ;;;###autoload
  19164. (defun org-search-view (&optional todo-only string edit-at)
  19165. "Show all entries that contain words or regular expressions.
  19166. If the first character of the search string is an asterisks,
  19167. search only the headlines.
  19168. With optional prefix argument TODO-ONLY, only consider entries that are
  19169. TODO entries. The argument STRING can be used to pass a default search
  19170. string into this function. If EDIT-AT is non-nil, it means that the
  19171. user should get a chance to edit this string, with cursor at position
  19172. EDIT-AT.
  19173. The search string is broken into \"words\" by splitting at whitespace.
  19174. The individual words are then interpreted as a boolean expression with
  19175. logical AND. Words prefixed with a minus must not occur in the entry.
  19176. Words without a prefix or prefixed with a plus must occur in the entry.
  19177. Matching is case-insensitive and the words are enclosed by word delimiters.
  19178. Words enclosed by curly braces are interpreted as regular expressions
  19179. that must or must not match in the entry.
  19180. If the search string starts with an asterisk, search only in headlines.
  19181. If (possibly after the leading star) the search string starts with an
  19182. exclamation mark, this also means to look at TODO entries only, an effect
  19183. that can also be achieved with a prefix argument.
  19184. This command searches the agenda files, and in addition the files listed
  19185. in `org-agenda-text-search-extra-files'."
  19186. (interactive "P")
  19187. (org-compile-prefix-format 'search)
  19188. (org-set-sorting-strategy 'search)
  19189. (org-prepare-agenda "SEARCH")
  19190. (let* ((props (list 'face nil
  19191. 'done-face 'org-done
  19192. 'org-not-done-regexp org-not-done-regexp
  19193. 'org-todo-regexp org-todo-regexp
  19194. 'mouse-face 'highlight
  19195. 'keymap org-agenda-keymap
  19196. 'help-echo (format "mouse-2 or RET jump to location")))
  19197. regexp rtn rtnall files file pos
  19198. marker priority category tags c neg re
  19199. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  19200. (unless (and (not edit-at)
  19201. (stringp string)
  19202. (string-match "\\S-" string))
  19203. (setq string (read-string "[+-]Word/{Regexp} ...: "
  19204. (cond
  19205. ((integerp edit-at) (cons string edit-at))
  19206. (edit-at string))
  19207. 'org-agenda-search-history)))
  19208. (org-set-local 'org-todo-only todo-only)
  19209. (setq org-agenda-redo-command
  19210. (list 'org-search-view (if todo-only t nil) string
  19211. '(if current-prefix-arg 1 nil)))
  19212. (setq org-agenda-query-string string)
  19213. (if (equal (string-to-char string) ?*)
  19214. (setq hdl-only t
  19215. words (substring string 1))
  19216. (setq words string))
  19217. (when (equal (string-to-char words) ?!)
  19218. (setq todo-only t
  19219. words (substring words 1)))
  19220. (setq words (org-split-string words))
  19221. (mapc (lambda (w)
  19222. (setq c (string-to-char w))
  19223. (if (equal c ?-)
  19224. (setq neg t w (substring w 1))
  19225. (if (equal c ?+)
  19226. (setq neg nil w (substring w 1))
  19227. (setq neg nil)))
  19228. (if (string-match "\\`{.*}\\'" w)
  19229. (setq re (substring w 1 -1))
  19230. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
  19231. (if neg (push re regexps-) (push re regexps+)))
  19232. words)
  19233. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  19234. (if (not regexps+)
  19235. (setq regexp (concat "^" org-outline-regexp))
  19236. (setq regexp (pop regexps+))
  19237. (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
  19238. regexp))))
  19239. (setq files (append (org-agenda-files) org-agenda-text-search-extra-files)
  19240. rtnall nil)
  19241. (while (setq file (pop files))
  19242. (setq ee nil)
  19243. (catch 'nextfile
  19244. (org-check-agenda-file file)
  19245. (setq buffer (if (file-exists-p file)
  19246. (org-get-agenda-file-buffer file)
  19247. (error "No such file %s" file)))
  19248. (if (not buffer)
  19249. ;; If file does not exist, make sure an error message is sent
  19250. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  19251. file))))
  19252. (with-current-buffer buffer
  19253. (with-syntax-table (org-search-syntax-table)
  19254. (unless (org-mode-p)
  19255. (error "Agenda file %s is not in `org-mode'" file))
  19256. (let ((case-fold-search t))
  19257. (save-excursion
  19258. (save-restriction
  19259. (if org-agenda-restrict
  19260. (narrow-to-region org-agenda-restrict-begin
  19261. org-agenda-restrict-end)
  19262. (widen))
  19263. (goto-char (point-min))
  19264. (unless (or (org-on-heading-p)
  19265. (outline-next-heading))
  19266. (throw 'nextfile t))
  19267. (goto-char (max (point-min) (1- (point))))
  19268. (while (re-search-forward regexp nil t)
  19269. (org-back-to-heading t)
  19270. (skip-chars-forward "* ")
  19271. (setq beg (point-at-bol)
  19272. beg1 (point)
  19273. end (progn (outline-next-heading) (point)))
  19274. (catch :skip
  19275. (goto-char beg)
  19276. (org-agenda-skip)
  19277. (setq str (buffer-substring-no-properties
  19278. (point-at-bol)
  19279. (if hdl-only (point-at-eol) end)))
  19280. (mapc (lambda (wr) (when (string-match wr str)
  19281. (goto-char (1- end))
  19282. (throw :skip t)))
  19283. regexps-)
  19284. (mapc (lambda (wr) (unless (string-match wr str)
  19285. (goto-char (1- end))
  19286. (throw :skip t)))
  19287. (if todo-only
  19288. (cons (concat "^\*+[ \t]+" org-not-done-regexp)
  19289. regexps+)
  19290. regexps+))
  19291. (goto-char beg)
  19292. (setq marker (org-agenda-new-marker (point))
  19293. category (org-get-category)
  19294. tags (org-get-tags-at (point))
  19295. txt (org-format-agenda-item
  19296. ""
  19297. (buffer-substring-no-properties
  19298. beg1 (point-at-eol))
  19299. category tags))
  19300. (org-add-props txt props
  19301. 'org-marker marker 'org-hd-marker marker
  19302. 'org-todo-regexp org-todo-regexp
  19303. 'priority 1000 'org-category category
  19304. 'type "search")
  19305. (push txt ee)
  19306. (goto-char (1- end))))))))))
  19307. (setq rtn (nreverse ee))
  19308. (setq rtnall (append rtnall rtn)))
  19309. (if org-agenda-overriding-header
  19310. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19311. nil 'face 'org-agenda-structure) "\n")
  19312. (insert "Search words: ")
  19313. (add-text-properties (point-min) (1- (point))
  19314. (list 'face 'org-agenda-structure))
  19315. (setq pos (point))
  19316. (insert string "\n")
  19317. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19318. (setq pos (point))
  19319. (unless org-agenda-multi
  19320. (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
  19321. (add-text-properties pos (1- (point))
  19322. (list 'face 'org-agenda-structure))))
  19323. (when rtnall
  19324. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19325. (goto-char (point-min))
  19326. (org-fit-agenda-window)
  19327. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  19328. (org-finalize-agenda)
  19329. (setq buffer-read-only t)))
  19330. ;;; Agenda TODO list
  19331. (defvar org-select-this-todo-keyword nil)
  19332. (defvar org-last-arg nil)
  19333. ;;;###autoload
  19334. (defun org-todo-list (arg)
  19335. "Show all TODO entries from all agenda file in a single list.
  19336. The prefix arg can be used to select a specific TODO keyword and limit
  19337. the list to these. When using \\[universal-argument], you will be prompted
  19338. for a keyword. A numeric prefix directly selects the Nth keyword in
  19339. `org-todo-keywords-1'."
  19340. (interactive "P")
  19341. (require 'calendar)
  19342. (org-compile-prefix-format 'todo)
  19343. (org-set-sorting-strategy 'todo)
  19344. (org-prepare-agenda "TODO")
  19345. (let* ((today (time-to-days (current-time)))
  19346. (date (calendar-gregorian-from-absolute today))
  19347. (kwds org-todo-keywords-for-agenda)
  19348. (completion-ignore-case t)
  19349. (org-select-this-todo-keyword
  19350. (if (stringp arg) arg
  19351. (and arg (integerp arg) (> arg 0)
  19352. (nth (1- arg) kwds))))
  19353. rtn rtnall files file pos)
  19354. (when (equal arg '(4))
  19355. (setq org-select-this-todo-keyword
  19356. (completing-read "Keyword (or KWD1|K2D2|...): "
  19357. (mapcar 'list kwds) nil nil)))
  19358. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19359. (org-set-local 'org-last-arg arg)
  19360. (setq org-agenda-redo-command
  19361. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19362. (setq files (org-agenda-files)
  19363. rtnall nil)
  19364. (while (setq file (pop files))
  19365. (catch 'nextfile
  19366. (org-check-agenda-file file)
  19367. (setq rtn (org-agenda-get-day-entries file date :todo))
  19368. (setq rtnall (append rtnall rtn))))
  19369. (if org-agenda-overriding-header
  19370. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19371. nil 'face 'org-agenda-structure) "\n")
  19372. (insert "Global list of TODO items of type: ")
  19373. (add-text-properties (point-min) (1- (point))
  19374. (list 'face 'org-agenda-structure))
  19375. (setq pos (point))
  19376. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19377. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19378. (setq pos (point))
  19379. (unless org-agenda-multi
  19380. (insert "Available with `N r': (0)ALL")
  19381. (let ((n 0) s)
  19382. (mapc (lambda (x)
  19383. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19384. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19385. (insert "\n "))
  19386. (insert " " s))
  19387. kwds))
  19388. (insert "\n"))
  19389. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19390. (when rtnall
  19391. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19392. (goto-char (point-min))
  19393. (org-fit-agenda-window)
  19394. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19395. (org-finalize-agenda)
  19396. (setq buffer-read-only t)))
  19397. ;;; Agenda tags match
  19398. ;;;###autoload
  19399. (defun org-tags-view (&optional todo-only match)
  19400. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19401. The prefix arg TODO-ONLY limits the search to TODO entries."
  19402. (interactive "P")
  19403. (org-compile-prefix-format 'tags)
  19404. (org-set-sorting-strategy 'tags)
  19405. (let* ((org-tags-match-list-sublevels
  19406. (if todo-only t org-tags-match-list-sublevels))
  19407. (completion-ignore-case t)
  19408. rtn rtnall files file pos matcher
  19409. buffer)
  19410. (setq matcher (org-make-tags-matcher match)
  19411. match (car matcher) matcher (cdr matcher))
  19412. (org-prepare-agenda (concat "TAGS " match))
  19413. (setq org-agenda-query-string match)
  19414. (setq org-agenda-redo-command
  19415. (list 'org-tags-view (list 'quote todo-only)
  19416. (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
  19417. (setq files (org-agenda-files)
  19418. rtnall nil)
  19419. (while (setq file (pop files))
  19420. (catch 'nextfile
  19421. (org-check-agenda-file file)
  19422. (setq buffer (if (file-exists-p file)
  19423. (org-get-agenda-file-buffer file)
  19424. (error "No such file %s" file)))
  19425. (if (not buffer)
  19426. ;; If file does not exist, merror message to agenda
  19427. (setq rtn (list
  19428. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19429. rtnall (append rtnall rtn))
  19430. (with-current-buffer buffer
  19431. (unless (org-mode-p)
  19432. (error "Agenda file %s is not in `org-mode'" file))
  19433. (save-excursion
  19434. (save-restriction
  19435. (if org-agenda-restrict
  19436. (narrow-to-region org-agenda-restrict-begin
  19437. org-agenda-restrict-end)
  19438. (widen))
  19439. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19440. (setq rtnall (append rtnall rtn))))))))
  19441. (if org-agenda-overriding-header
  19442. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19443. nil 'face 'org-agenda-structure) "\n")
  19444. (insert "Headlines with TAGS match: ")
  19445. (add-text-properties (point-min) (1- (point))
  19446. (list 'face 'org-agenda-structure))
  19447. (setq pos (point))
  19448. (insert match "\n")
  19449. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19450. (setq pos (point))
  19451. (unless org-agenda-multi
  19452. (insert "Press `C-u r' to search again with new search string\n"))
  19453. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19454. (when rtnall
  19455. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19456. (goto-char (point-min))
  19457. (org-fit-agenda-window)
  19458. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19459. (org-finalize-agenda)
  19460. (setq buffer-read-only t)))
  19461. ;;; Agenda Finding stuck projects
  19462. (defvar org-agenda-skip-regexp nil
  19463. "Regular expression used in skipping subtrees for the agenda.
  19464. This is basically a temporary global variable that can be set and then
  19465. used by user-defined selections using `org-agenda-skip-function'.")
  19466. (defvar org-agenda-overriding-header nil
  19467. "When this is set during todo and tags searches, will replace header.")
  19468. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19469. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19470. If yes, it returns the end position of this tree, causing agenda commands
  19471. to skip this subtree. This is a function that can be put into
  19472. `org-agenda-skip-function' for the duration of a command."
  19473. (let ((end (save-excursion (org-end-of-subtree t)))
  19474. skip)
  19475. (save-excursion
  19476. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19477. (and skip end)))
  19478. (defun org-agenda-skip-entry-if (&rest conditions)
  19479. "Skip entry if any of CONDITIONS is true.
  19480. See `org-agenda-skip-if' for details."
  19481. (org-agenda-skip-if nil conditions))
  19482. (defun org-agenda-skip-subtree-if (&rest conditions)
  19483. "Skip entry if any of CONDITIONS is true.
  19484. See `org-agenda-skip-if' for details."
  19485. (org-agenda-skip-if t conditions))
  19486. (defun org-agenda-skip-if (subtree conditions)
  19487. "Checks current entity for CONDITIONS.
  19488. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19489. the entry, i.e. the text before the next heading is checked.
  19490. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19491. from different tests. Valid conditions are:
  19492. scheduled Check if there is a scheduled cookie
  19493. notscheduled Check if there is no scheduled cookie
  19494. deadline Check if there is a deadline
  19495. notdeadline Check if there is no deadline
  19496. regexp Check if regexp matches
  19497. notregexp Check if regexp does not match.
  19498. The regexp is taken from the conditions list, it must come right after
  19499. the `regexp' or `notregexp' element.
  19500. If any of these conditions is met, this function returns the end point of
  19501. the entity, causing the search to continue from there. This is a function
  19502. that can be put into `org-agenda-skip-function' for the duration of a command."
  19503. (let (beg end m)
  19504. (org-back-to-heading t)
  19505. (setq beg (point)
  19506. end (if subtree
  19507. (progn (org-end-of-subtree t) (point))
  19508. (progn (outline-next-heading) (1- (point)))))
  19509. (goto-char beg)
  19510. (and
  19511. (or
  19512. (and (memq 'scheduled conditions)
  19513. (re-search-forward org-scheduled-time-regexp end t))
  19514. (and (memq 'notscheduled conditions)
  19515. (not (re-search-forward org-scheduled-time-regexp end t)))
  19516. (and (memq 'deadline conditions)
  19517. (re-search-forward org-deadline-time-regexp end t))
  19518. (and (memq 'notdeadline conditions)
  19519. (not (re-search-forward org-deadline-time-regexp end t)))
  19520. (and (setq m (memq 'regexp conditions))
  19521. (stringp (nth 1 m))
  19522. (re-search-forward (nth 1 m) end t))
  19523. (and (setq m (memq 'notregexp conditions))
  19524. (stringp (nth 1 m))
  19525. (not (re-search-forward (nth 1 m) end t))))
  19526. end)))
  19527. ;;;###autoload
  19528. (defun org-agenda-list-stuck-projects (&rest ignore)
  19529. "Create agenda view for projects that are stuck.
  19530. Stuck projects are project that have no next actions. For the definitions
  19531. of what a project is and how to check if it stuck, customize the variable
  19532. `org-stuck-projects'.
  19533. MATCH is being ignored."
  19534. (interactive)
  19535. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19536. ;; FIXME: we could have used org-agenda-skip-if here.
  19537. (org-agenda-overriding-header "List of stuck projects: ")
  19538. (matcher (nth 0 org-stuck-projects))
  19539. (todo (nth 1 org-stuck-projects))
  19540. (todo-wds (if (member "*" todo)
  19541. (progn
  19542. (org-prepare-agenda-buffers (org-agenda-files))
  19543. (org-delete-all
  19544. org-done-keywords-for-agenda
  19545. (copy-sequence org-todo-keywords-for-agenda)))
  19546. todo))
  19547. (todo-re (concat "^\\*+[ \t]+\\("
  19548. (mapconcat 'identity todo-wds "\\|")
  19549. "\\)\\>"))
  19550. (tags (nth 2 org-stuck-projects))
  19551. (tags-re (if (member "*" tags)
  19552. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19553. (concat "^\\*+ .*:\\("
  19554. (mapconcat 'identity tags "\\|")
  19555. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19556. (gen-re (nth 3 org-stuck-projects))
  19557. (re-list
  19558. (delq nil
  19559. (list
  19560. (if todo todo-re)
  19561. (if tags tags-re)
  19562. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19563. gen-re)))))
  19564. (setq org-agenda-skip-regexp
  19565. (if re-list
  19566. (mapconcat 'identity re-list "\\|")
  19567. (error "No information how to identify unstuck projects")))
  19568. (org-tags-view nil matcher)
  19569. (with-current-buffer org-agenda-buffer-name
  19570. (setq org-agenda-redo-command
  19571. '(org-agenda-list-stuck-projects
  19572. (or current-prefix-arg org-last-arg))))))
  19573. ;;; Diary integration
  19574. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19575. (defvar list-diary-entries-hook)
  19576. (defun org-get-entries-from-diary (date)
  19577. "Get the (Emacs Calendar) diary entries for DATE."
  19578. (require 'diary-lib)
  19579. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19580. (diary-display-hook '(fancy-diary-display))
  19581. (pop-up-frames nil)
  19582. (list-diary-entries-hook
  19583. (cons 'org-diary-default-entry list-diary-entries-hook))
  19584. (diary-file-name-prefix-function nil) ; turn this feature off
  19585. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19586. entries
  19587. (org-disable-agenda-to-diary t))
  19588. (save-excursion
  19589. (save-window-excursion
  19590. (funcall (if (fboundp 'diary-list-entries)
  19591. 'diary-list-entries 'list-diary-entries)
  19592. date 1)))
  19593. (if (not (get-buffer fancy-diary-buffer))
  19594. (setq entries nil)
  19595. (with-current-buffer fancy-diary-buffer
  19596. (setq buffer-read-only nil)
  19597. (if (zerop (buffer-size))
  19598. ;; No entries
  19599. (setq entries nil)
  19600. ;; Omit the date and other unnecessary stuff
  19601. (org-agenda-cleanup-fancy-diary)
  19602. ;; Add prefix to each line and extend the text properties
  19603. (if (zerop (buffer-size))
  19604. (setq entries nil)
  19605. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19606. (set-buffer-modified-p nil)
  19607. (kill-buffer fancy-diary-buffer)))
  19608. (when entries
  19609. (setq entries (org-split-string entries "\n"))
  19610. (setq entries
  19611. (mapcar
  19612. (lambda (x)
  19613. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19614. ;; Extend the text properties to the beginning of the line
  19615. (org-add-props x (text-properties-at (1- (length x)) x)
  19616. 'type "diary" 'date date))
  19617. entries)))))
  19618. (defun org-agenda-cleanup-fancy-diary ()
  19619. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19620. This gets rid of the date, the underline under the date, and
  19621. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19622. date. It also removes lines that contain only whitespace."
  19623. (goto-char (point-min))
  19624. (if (looking-at ".*?:[ \t]*")
  19625. (progn
  19626. (replace-match "")
  19627. (re-search-forward "\n=+$" nil t)
  19628. (replace-match "")
  19629. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19630. (re-search-forward "\n=+$" nil t)
  19631. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19632. (goto-char (point-min))
  19633. (while (re-search-forward "^ +\n" nil t)
  19634. (replace-match ""))
  19635. (goto-char (point-min))
  19636. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19637. (replace-match "")))
  19638. ;; Make sure entries from the diary have the right text properties.
  19639. (eval-after-load "diary-lib"
  19640. '(if (boundp 'diary-modify-entry-list-string-function)
  19641. ;; We can rely on the hook, nothing to do
  19642. nil
  19643. ;; Hook not avaiable, must use advice to make this work
  19644. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19645. "Make the position visible."
  19646. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19647. (stringp string)
  19648. buffer-file-name)
  19649. (setq string (org-modify-diary-entry-string string))))))
  19650. (defun org-modify-diary-entry-string (string)
  19651. "Add text properties to string, allowing org-mode to act on it."
  19652. (org-add-props string nil
  19653. 'mouse-face 'highlight
  19654. 'keymap org-agenda-keymap
  19655. 'help-echo (if buffer-file-name
  19656. (format "mouse-2 or RET jump to diary file %s"
  19657. (abbreviate-file-name buffer-file-name))
  19658. "")
  19659. 'org-agenda-diary-link t
  19660. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19661. (defun org-diary-default-entry ()
  19662. "Add a dummy entry to the diary.
  19663. Needed to avoid empty dates which mess up holiday display."
  19664. ;; Catch the error if dealing with the new add-to-diary-alist
  19665. (when org-disable-agenda-to-diary
  19666. (condition-case nil
  19667. (add-to-diary-list original-date "Org-mode dummy" "")
  19668. (error
  19669. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19670. ;;;###autoload
  19671. (defun org-diary (&rest args)
  19672. "Return diary information from org-files.
  19673. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19674. It accesses org files and extracts information from those files to be
  19675. listed in the diary. The function accepts arguments specifying what
  19676. items should be listed. The following arguments are allowed:
  19677. :timestamp List the headlines of items containing a date stamp or
  19678. date range matching the selected date. Deadlines will
  19679. also be listed, on the expiration day.
  19680. :sexp List entries resulting from diary-like sexps.
  19681. :deadline List any deadlines past due, or due within
  19682. `org-deadline-warning-days'. The listing occurs only
  19683. in the diary for *today*, not at any other date. If
  19684. an entry is marked DONE, it is no longer listed.
  19685. :scheduled List all items which are scheduled for the given date.
  19686. The diary for *today* also contains items which were
  19687. scheduled earlier and are not yet marked DONE.
  19688. :todo List all TODO items from the org-file. This may be a
  19689. long list - so this is not turned on by default.
  19690. Like deadlines, these entries only show up in the
  19691. diary for *today*, not at any other date.
  19692. The call in the diary file should look like this:
  19693. &%%(org-diary) ~/path/to/some/orgfile.org
  19694. Use a separate line for each org file to check. Or, if you omit the file name,
  19695. all files listed in `org-agenda-files' will be checked automatically:
  19696. &%%(org-diary)
  19697. If you don't give any arguments (as in the example above), the default
  19698. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19699. So the example above may also be written as
  19700. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19701. The function expects the lisp variables `entry' and `date' to be provided
  19702. by the caller, because this is how the calendar works. Don't use this
  19703. function from a program - use `org-agenda-get-day-entries' instead."
  19704. (when (> (- (time-to-seconds (current-time))
  19705. org-agenda-last-marker-time)
  19706. 5)
  19707. (org-agenda-reset-markers))
  19708. (org-compile-prefix-format 'agenda)
  19709. (org-set-sorting-strategy 'agenda)
  19710. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19711. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19712. (list entry)
  19713. (org-agenda-files t)))
  19714. file rtn results)
  19715. (org-prepare-agenda-buffers files)
  19716. ;; If this is called during org-agenda, don't return any entries to
  19717. ;; the calendar. Org Agenda will list these entries itself.
  19718. (if org-disable-agenda-to-diary (setq files nil))
  19719. (while (setq file (pop files))
  19720. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19721. (setq results (append results rtn)))
  19722. (if results
  19723. (concat (org-finalize-agenda-entries results) "\n"))))
  19724. ;;; Agenda entry finders
  19725. (defun org-agenda-get-day-entries (file date &rest args)
  19726. "Does the work for `org-diary' and `org-agenda'.
  19727. FILE is the path to a file to be checked for entries. DATE is date like
  19728. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19729. which kind of entries should be extracted. For details about these, see
  19730. the documentation of `org-diary'."
  19731. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19732. (let* ((org-startup-folded nil)
  19733. (org-startup-align-all-tables nil)
  19734. (buffer (if (file-exists-p file)
  19735. (org-get-agenda-file-buffer file)
  19736. (error "No such file %s" file)))
  19737. arg results rtn)
  19738. (if (not buffer)
  19739. ;; If file does not exist, make sure an error message ends up in diary
  19740. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19741. (with-current-buffer buffer
  19742. (unless (org-mode-p)
  19743. (error "Agenda file %s is not in `org-mode'" file))
  19744. (let ((case-fold-search nil))
  19745. (save-excursion
  19746. (save-restriction
  19747. (if org-agenda-restrict
  19748. (narrow-to-region org-agenda-restrict-begin
  19749. org-agenda-restrict-end)
  19750. (widen))
  19751. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19752. (while (setq arg (pop args))
  19753. (cond
  19754. ((and (eq arg :todo)
  19755. (equal date (calendar-current-date)))
  19756. (setq rtn (org-agenda-get-todos))
  19757. (setq results (append results rtn)))
  19758. ((eq arg :timestamp)
  19759. (setq rtn (org-agenda-get-blocks))
  19760. (setq results (append results rtn))
  19761. (setq rtn (org-agenda-get-timestamps))
  19762. (setq results (append results rtn)))
  19763. ((eq arg :sexp)
  19764. (setq rtn (org-agenda-get-sexps))
  19765. (setq results (append results rtn)))
  19766. ((eq arg :scheduled)
  19767. (setq rtn (org-agenda-get-scheduled))
  19768. (setq results (append results rtn)))
  19769. ((eq arg :closed)
  19770. (setq rtn (org-agenda-get-closed))
  19771. (setq results (append results rtn)))
  19772. ((eq arg :deadline)
  19773. (setq rtn (org-agenda-get-deadlines))
  19774. (setq results (append results rtn))))))))
  19775. results))))
  19776. (defun org-entry-is-todo-p ()
  19777. (member (org-get-todo-state) org-not-done-keywords))
  19778. (defun org-entry-is-done-p ()
  19779. (member (org-get-todo-state) org-done-keywords))
  19780. (defun org-get-todo-state ()
  19781. (save-excursion
  19782. (org-back-to-heading t)
  19783. (and (looking-at org-todo-line-regexp)
  19784. (match-end 2)
  19785. (match-string 2))))
  19786. (defun org-at-date-range-p (&optional inactive-ok)
  19787. "Is the cursor inside a date range?"
  19788. (interactive)
  19789. (save-excursion
  19790. (catch 'exit
  19791. (let ((pos (point)))
  19792. (skip-chars-backward "^[<\r\n")
  19793. (skip-chars-backward "<[")
  19794. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19795. (>= (match-end 0) pos)
  19796. (throw 'exit t))
  19797. (skip-chars-backward "^<[\r\n")
  19798. (skip-chars-backward "<[")
  19799. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19800. (>= (match-end 0) pos)
  19801. (throw 'exit t)))
  19802. nil)))
  19803. (defun org-agenda-get-todos ()
  19804. "Return the TODO information for agenda display."
  19805. (let* ((props (list 'face nil
  19806. 'done-face 'org-done
  19807. 'org-not-done-regexp org-not-done-regexp
  19808. 'org-todo-regexp org-todo-regexp
  19809. 'mouse-face 'highlight
  19810. 'keymap org-agenda-keymap
  19811. 'help-echo
  19812. (format "mouse-2 or RET jump to org file %s"
  19813. (abbreviate-file-name buffer-file-name))))
  19814. ;; FIXME: get rid of the \n at some point but watch out
  19815. (regexp (concat "^\\*+[ \t]+\\("
  19816. (if org-select-this-todo-keyword
  19817. (if (equal org-select-this-todo-keyword "*")
  19818. org-todo-regexp
  19819. (concat "\\<\\("
  19820. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19821. "\\)\\>"))
  19822. org-not-done-regexp)
  19823. "[^\n\r]*\\)"))
  19824. marker priority category tags
  19825. ee txt beg end)
  19826. (goto-char (point-min))
  19827. (while (re-search-forward regexp nil t)
  19828. (catch :skip
  19829. (save-match-data
  19830. (beginning-of-line)
  19831. (setq beg (point) end (progn (outline-next-heading) (point)))
  19832. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19833. (re-search-forward org-ts-regexp end t))
  19834. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19835. (re-search-forward org-scheduled-time-regexp end t))
  19836. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19837. (re-search-forward org-deadline-time-regexp end t)
  19838. (org-deadline-close (match-string 1))))
  19839. (goto-char (1+ beg))
  19840. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19841. (throw :skip nil)))
  19842. (goto-char beg)
  19843. (org-agenda-skip)
  19844. (goto-char (match-beginning 1))
  19845. (setq marker (org-agenda-new-marker (match-beginning 0))
  19846. category (org-get-category)
  19847. tags (org-get-tags-at (point))
  19848. txt (org-format-agenda-item "" (match-string 1) category tags)
  19849. priority (1+ (org-get-priority txt)))
  19850. (org-add-props txt props
  19851. 'org-marker marker 'org-hd-marker marker
  19852. 'priority priority 'org-category category
  19853. 'type "todo")
  19854. (push txt ee)
  19855. (if org-agenda-todo-list-sublevels
  19856. (goto-char (match-end 1))
  19857. (org-end-of-subtree 'invisible))))
  19858. (nreverse ee)))
  19859. (defconst org-agenda-no-heading-message
  19860. "No heading for this item in buffer or region.")
  19861. (defun org-agenda-get-timestamps ()
  19862. "Return the date stamp information for agenda display."
  19863. (let* ((props (list 'face nil
  19864. 'org-not-done-regexp org-not-done-regexp
  19865. 'org-todo-regexp org-todo-regexp
  19866. 'mouse-face 'highlight
  19867. 'keymap org-agenda-keymap
  19868. 'help-echo
  19869. (format "mouse-2 or RET jump to org file %s"
  19870. (abbreviate-file-name buffer-file-name))))
  19871. (d1 (calendar-absolute-from-gregorian date))
  19872. (remove-re
  19873. (concat
  19874. (regexp-quote
  19875. (format-time-string
  19876. "<%Y-%m-%d"
  19877. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19878. ".*?>"))
  19879. (regexp
  19880. (concat
  19881. (regexp-quote
  19882. (substring
  19883. (format-time-string
  19884. (car org-time-stamp-formats)
  19885. (apply 'encode-time ; DATE bound by calendar
  19886. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19887. 0 11))
  19888. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19889. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19890. marker hdmarker deadlinep scheduledp donep tmp priority category
  19891. ee txt timestr tags b0 b3 e3 head)
  19892. (goto-char (point-min))
  19893. (while (re-search-forward regexp nil t)
  19894. (setq b0 (match-beginning 0)
  19895. b3 (match-beginning 3) e3 (match-end 3))
  19896. (catch :skip
  19897. (and (org-at-date-range-p) (throw :skip nil))
  19898. (org-agenda-skip)
  19899. (if (and (match-end 1)
  19900. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19901. (throw :skip nil))
  19902. (if (and e3
  19903. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19904. (throw :skip nil))
  19905. (setq marker (org-agenda-new-marker b0)
  19906. category (org-get-category b0)
  19907. tmp (buffer-substring (max (point-min)
  19908. (- b0 org-ds-keyword-length))
  19909. b0)
  19910. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19911. deadlinep (string-match org-deadline-regexp tmp)
  19912. scheduledp (string-match org-scheduled-regexp tmp)
  19913. donep (org-entry-is-done-p))
  19914. (if (or scheduledp deadlinep) (throw :skip t))
  19915. (if (string-match ">" timestr)
  19916. ;; substring should only run to end of time stamp
  19917. (setq timestr (substring timestr 0 (match-end 0))))
  19918. (save-excursion
  19919. (if (re-search-backward "^\\*+ " nil t)
  19920. (progn
  19921. (goto-char (match-beginning 0))
  19922. (setq hdmarker (org-agenda-new-marker)
  19923. tags (org-get-tags-at))
  19924. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19925. (setq head (match-string 1))
  19926. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19927. (setq txt (org-format-agenda-item
  19928. nil head category tags timestr nil
  19929. remove-re)))
  19930. (setq txt org-agenda-no-heading-message))
  19931. (setq priority (org-get-priority txt))
  19932. (org-add-props txt props
  19933. 'org-marker marker 'org-hd-marker hdmarker)
  19934. (org-add-props txt nil 'priority priority
  19935. 'org-category category 'date date
  19936. 'type "timestamp")
  19937. (push txt ee))
  19938. (outline-next-heading)))
  19939. (nreverse ee)))
  19940. (defun org-agenda-get-sexps ()
  19941. "Return the sexp information for agenda display."
  19942. (require 'diary-lib)
  19943. (let* ((props (list 'face nil
  19944. 'mouse-face 'highlight
  19945. 'keymap org-agenda-keymap
  19946. 'help-echo
  19947. (format "mouse-2 or RET jump to org file %s"
  19948. (abbreviate-file-name buffer-file-name))))
  19949. (regexp "^&?%%(")
  19950. marker category ee txt tags entry result beg b sexp sexp-entry)
  19951. (goto-char (point-min))
  19952. (while (re-search-forward regexp nil t)
  19953. (catch :skip
  19954. (org-agenda-skip)
  19955. (setq beg (match-beginning 0))
  19956. (goto-char (1- (match-end 0)))
  19957. (setq b (point))
  19958. (forward-sexp 1)
  19959. (setq sexp (buffer-substring b (point)))
  19960. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19961. (org-trim (match-string 1))
  19962. ""))
  19963. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19964. (when result
  19965. (setq marker (org-agenda-new-marker beg)
  19966. category (org-get-category beg))
  19967. (if (string-match "\\S-" result)
  19968. (setq txt result)
  19969. (setq txt "SEXP entry returned empty string"))
  19970. (setq txt (org-format-agenda-item
  19971. "" txt category tags 'time))
  19972. (org-add-props txt props 'org-marker marker)
  19973. (org-add-props txt nil
  19974. 'org-category category 'date date
  19975. 'type "sexp")
  19976. (push txt ee))))
  19977. (nreverse ee)))
  19978. (defun org-agenda-get-closed ()
  19979. "Return the logged TODO entries for agenda display."
  19980. (let* ((props (list 'mouse-face 'highlight
  19981. 'org-not-done-regexp org-not-done-regexp
  19982. 'org-todo-regexp org-todo-regexp
  19983. 'keymap org-agenda-keymap
  19984. 'help-echo
  19985. (format "mouse-2 or RET jump to org file %s"
  19986. (abbreviate-file-name buffer-file-name))))
  19987. (regexp (concat
  19988. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19989. (regexp-quote
  19990. (substring
  19991. (format-time-string
  19992. (car org-time-stamp-formats)
  19993. (apply 'encode-time ; DATE bound by calendar
  19994. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19995. 1 11))))
  19996. marker hdmarker priority category tags closedp
  19997. ee txt timestr)
  19998. (goto-char (point-min))
  19999. (while (re-search-forward regexp nil t)
  20000. (catch :skip
  20001. (org-agenda-skip)
  20002. (setq marker (org-agenda-new-marker (match-beginning 0))
  20003. closedp (equal (match-string 1) org-closed-string)
  20004. category (org-get-category (match-beginning 0))
  20005. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  20006. ;; donep (org-entry-is-done-p)
  20007. )
  20008. (if (string-match "\\]" timestr)
  20009. ;; substring should only run to end of time stamp
  20010. (setq timestr (substring timestr 0 (match-end 0))))
  20011. (save-excursion
  20012. (if (re-search-backward "^\\*+ " nil t)
  20013. (progn
  20014. (goto-char (match-beginning 0))
  20015. (setq hdmarker (org-agenda-new-marker)
  20016. tags (org-get-tags-at))
  20017. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20018. (setq txt (org-format-agenda-item
  20019. (if closedp "Closed: " "Clocked: ")
  20020. (match-string 1) category tags timestr)))
  20021. (setq txt org-agenda-no-heading-message))
  20022. (setq priority 100000)
  20023. (org-add-props txt props
  20024. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  20025. 'priority priority 'org-category category
  20026. 'type "closed" 'date date
  20027. 'undone-face 'org-warning 'done-face 'org-done)
  20028. (push txt ee))
  20029. (goto-char (point-at-eol))))
  20030. (nreverse ee)))
  20031. (defun org-agenda-get-deadlines ()
  20032. "Return the deadline information for agenda display."
  20033. (let* ((props (list 'mouse-face 'highlight
  20034. 'org-not-done-regexp org-not-done-regexp
  20035. 'org-todo-regexp org-todo-regexp
  20036. 'keymap org-agenda-keymap
  20037. 'help-echo
  20038. (format "mouse-2 or RET jump to org file %s"
  20039. (abbreviate-file-name buffer-file-name))))
  20040. (regexp org-deadline-time-regexp)
  20041. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20042. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20043. d2 diff dfrac wdays pos pos1 category tags
  20044. ee txt head face s upcomingp donep timestr)
  20045. (goto-char (point-min))
  20046. (while (re-search-forward regexp nil t)
  20047. (catch :skip
  20048. (org-agenda-skip)
  20049. (setq s (match-string 1)
  20050. pos (1- (match-beginning 1))
  20051. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20052. diff (- d2 d1)
  20053. wdays (org-get-wdays s)
  20054. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  20055. upcomingp (and todayp (> diff 0)))
  20056. ;; When to show a deadline in the calendar:
  20057. ;; If the expiration is within wdays warning time.
  20058. ;; Past-due deadlines are only shown on the current date
  20059. (if (or (and (<= diff wdays)
  20060. (and todayp (not org-agenda-only-exact-dates)))
  20061. (= diff 0))
  20062. (save-excursion
  20063. (setq category (org-get-category))
  20064. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20065. (progn
  20066. (goto-char (match-end 0))
  20067. (setq pos1 (match-beginning 0))
  20068. (setq tags (org-get-tags-at pos1))
  20069. (setq head (buffer-substring-no-properties
  20070. (point)
  20071. (progn (skip-chars-forward "^\r\n")
  20072. (point))))
  20073. (setq donep (string-match org-looking-at-done-regexp head))
  20074. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20075. (setq timestr
  20076. (concat (substring s (match-beginning 1)) " "))
  20077. (setq timestr 'time))
  20078. (if (and donep
  20079. (or org-agenda-skip-deadline-if-done
  20080. (not (= diff 0))))
  20081. (setq txt nil)
  20082. (setq txt (org-format-agenda-item
  20083. (if (= diff 0)
  20084. (car org-agenda-deadline-leaders)
  20085. (format (nth 1 org-agenda-deadline-leaders)
  20086. diff))
  20087. head category tags timestr))))
  20088. (setq txt org-agenda-no-heading-message))
  20089. (when txt
  20090. (setq face (org-agenda-deadline-face dfrac wdays))
  20091. (org-add-props txt props
  20092. 'org-marker (org-agenda-new-marker pos)
  20093. 'org-hd-marker (org-agenda-new-marker pos1)
  20094. 'priority (+ (- diff)
  20095. (org-get-priority txt))
  20096. 'org-category category
  20097. 'type (if upcomingp "upcoming-deadline" "deadline")
  20098. 'date (if upcomingp date d2)
  20099. 'face (if donep 'org-done face)
  20100. 'undone-face face 'done-face 'org-done)
  20101. (push txt ee))))))
  20102. (nreverse ee)))
  20103. (defun org-agenda-deadline-face (fraction &optional wdays)
  20104. "Return the face to displaying a deadline item.
  20105. FRACTION is what fraction of the head-warning time has passed."
  20106. (if (equal wdays 0) (setq fraction 1.))
  20107. (let ((faces org-agenda-deadline-faces) f)
  20108. (catch 'exit
  20109. (while (setq f (pop faces))
  20110. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  20111. (defun org-agenda-get-scheduled ()
  20112. "Return the scheduled information for agenda display."
  20113. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  20114. 'org-todo-regexp org-todo-regexp
  20115. 'done-face 'org-done
  20116. 'mouse-face 'highlight
  20117. 'keymap org-agenda-keymap
  20118. 'help-echo
  20119. (format "mouse-2 or RET jump to org file %s"
  20120. (abbreviate-file-name buffer-file-name))))
  20121. (regexp org-scheduled-time-regexp)
  20122. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20123. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20124. d2 diff pos pos1 category tags
  20125. ee txt head pastschedp donep face timestr s)
  20126. (goto-char (point-min))
  20127. (while (re-search-forward regexp nil t)
  20128. (catch :skip
  20129. (org-agenda-skip)
  20130. (setq s (match-string 1)
  20131. pos (1- (match-beginning 1))
  20132. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20133. ;;; is this right?
  20134. ;;; do we need to do this for deadleine too????
  20135. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20136. diff (- d2 d1))
  20137. (setq pastschedp (and todayp (< diff 0)))
  20138. ;; When to show a scheduled item in the calendar:
  20139. ;; If it is on or past the date.
  20140. (if (or (and (< diff 0)
  20141. (and todayp (not org-agenda-only-exact-dates)))
  20142. (= diff 0))
  20143. (save-excursion
  20144. (setq category (org-get-category))
  20145. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20146. (progn
  20147. (goto-char (match-end 0))
  20148. (setq pos1 (match-beginning 0))
  20149. (setq tags (org-get-tags-at))
  20150. (setq head (buffer-substring-no-properties
  20151. (point)
  20152. (progn (skip-chars-forward "^\r\n") (point))))
  20153. (setq donep (string-match org-looking-at-done-regexp head))
  20154. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20155. (setq timestr
  20156. (concat (substring s (match-beginning 1)) " "))
  20157. (setq timestr 'time))
  20158. (if (and donep
  20159. (or org-agenda-skip-scheduled-if-done
  20160. (not (= diff 0))))
  20161. (setq txt nil)
  20162. (setq txt (org-format-agenda-item
  20163. (if (= diff 0)
  20164. (car org-agenda-scheduled-leaders)
  20165. (format (nth 1 org-agenda-scheduled-leaders)
  20166. (- 1 diff)))
  20167. head category tags timestr))))
  20168. (setq txt org-agenda-no-heading-message))
  20169. (when txt
  20170. (setq face (if pastschedp
  20171. 'org-scheduled-previously
  20172. 'org-scheduled-today))
  20173. (org-add-props txt props
  20174. 'undone-face face
  20175. 'face (if donep 'org-done face)
  20176. 'org-marker (org-agenda-new-marker pos)
  20177. 'org-hd-marker (org-agenda-new-marker pos1)
  20178. 'type (if pastschedp "past-scheduled" "scheduled")
  20179. 'date (if pastschedp d2 date)
  20180. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20181. 'org-category category)
  20182. (push txt ee))))))
  20183. (nreverse ee)))
  20184. (defun org-agenda-get-blocks ()
  20185. "Return the date-range information for agenda display."
  20186. (let* ((props (list 'face nil
  20187. 'org-not-done-regexp org-not-done-regexp
  20188. 'org-todo-regexp org-todo-regexp
  20189. 'mouse-face 'highlight
  20190. 'keymap org-agenda-keymap
  20191. 'help-echo
  20192. (format "mouse-2 or RET jump to org file %s"
  20193. (abbreviate-file-name buffer-file-name))))
  20194. (regexp org-tr-regexp)
  20195. (d0 (calendar-absolute-from-gregorian date))
  20196. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20197. donep head)
  20198. (goto-char (point-min))
  20199. (while (re-search-forward regexp nil t)
  20200. (catch :skip
  20201. (org-agenda-skip)
  20202. (setq pos (point))
  20203. (setq timestr (match-string 0)
  20204. s1 (match-string 1)
  20205. s2 (match-string 2)
  20206. d1 (time-to-days (org-time-string-to-time s1))
  20207. d2 (time-to-days (org-time-string-to-time s2)))
  20208. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20209. ;; Only allow days between the limits, because the normal
  20210. ;; date stamps will catch the limits.
  20211. (save-excursion
  20212. (setq marker (org-agenda-new-marker (point)))
  20213. (setq category (org-get-category))
  20214. (if (re-search-backward "^\\*+ " nil t)
  20215. (progn
  20216. (goto-char (match-beginning 0))
  20217. (setq hdmarker (org-agenda-new-marker (point)))
  20218. (setq tags (org-get-tags-at))
  20219. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20220. (setq head (match-string 1))
  20221. (and org-agenda-skip-timestamp-if-done
  20222. (org-entry-is-done-p)
  20223. (throw :skip t))
  20224. (setq txt (org-format-agenda-item
  20225. (format (if (= d1 d2) "" "(%d/%d): ")
  20226. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20227. head category tags
  20228. (if (= d0 d1) timestr))))
  20229. (setq txt org-agenda-no-heading-message))
  20230. (org-add-props txt props
  20231. 'org-marker marker 'org-hd-marker hdmarker
  20232. 'type "block" 'date date
  20233. 'priority (org-get-priority txt) 'org-category category)
  20234. (push txt ee)))
  20235. (goto-char pos)))
  20236. ;; Sort the entries by expiration date.
  20237. (nreverse ee)))
  20238. ;;; Agenda presentation and sorting
  20239. (defconst org-plain-time-of-day-regexp
  20240. (concat
  20241. "\\(\\<[012]?[0-9]"
  20242. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20243. "\\(--?"
  20244. "\\(\\<[012]?[0-9]"
  20245. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20246. "\\)?")
  20247. "Regular expression to match a plain time or time range.
  20248. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20249. groups carry important information:
  20250. 0 the full match
  20251. 1 the first time, range or not
  20252. 8 the second time, if it is a range.")
  20253. (defconst org-plain-time-extension-regexp
  20254. (concat
  20255. "\\(\\<[012]?[0-9]"
  20256. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20257. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20258. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20259. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20260. groups carry important information:
  20261. 0 the full match
  20262. 7 hours of duration
  20263. 9 minutes of duration")
  20264. (defconst org-stamp-time-of-day-regexp
  20265. (concat
  20266. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20267. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20268. "\\(--?"
  20269. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20270. "Regular expression to match a timestamp time or time range.
  20271. After a match, the following groups carry important information:
  20272. 0 the full match
  20273. 1 date plus weekday, for backreferencing to make sure both times on same day
  20274. 2 the first time, range or not
  20275. 4 the second time, if it is a range.")
  20276. (defvar org-prefix-has-time nil
  20277. "A flag, set by `org-compile-prefix-format'.
  20278. The flag is set if the currently compiled format contains a `%t'.")
  20279. (defvar org-prefix-has-tag nil
  20280. "A flag, set by `org-compile-prefix-format'.
  20281. The flag is set if the currently compiled format contains a `%T'.")
  20282. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20283. noprefix remove-re)
  20284. "Format TXT to be inserted into the agenda buffer.
  20285. In particular, it adds the prefix and corresponding text properties. EXTRA
  20286. must be a string and replaces the `%s' specifier in the prefix format.
  20287. CATEGORY (string, symbol or nil) may be used to overrule the default
  20288. category taken from local variable or file name. It will replace the `%c'
  20289. specifier in the format. DOTIME, when non-nil, indicates that a
  20290. time-of-day should be extracted from TXT for sorting of this entry, and for
  20291. the `%t' specifier in the format. When DOTIME is a string, this string is
  20292. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20293. only the correctly processes TXT should be returned - this is used by
  20294. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20295. Any match of REMOVE-RE will be removed from TXT."
  20296. (save-match-data
  20297. ;; Diary entries sometimes have extra whitespace at the beginning
  20298. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20299. (let* ((category (or category
  20300. org-category
  20301. (if buffer-file-name
  20302. (file-name-sans-extension
  20303. (file-name-nondirectory buffer-file-name))
  20304. "")))
  20305. (tag (if tags (nth (1- (length tags)) tags) ""))
  20306. time ; time and tag are needed for the eval of the prefix format
  20307. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20308. (time-of-day (and dotime (org-get-time-of-day ts)))
  20309. stamp plain s0 s1 s2 rtn srp)
  20310. (when (and dotime time-of-day org-prefix-has-time)
  20311. ;; Extract starting and ending time and move them to prefix
  20312. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20313. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20314. (setq s0 (match-string 0 ts)
  20315. srp (and stamp (match-end 3))
  20316. s1 (match-string (if plain 1 2) ts)
  20317. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20318. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20319. ;; them, we might want to remove them there to avoid duplication.
  20320. ;; The user can turn this off with a variable.
  20321. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20322. (string-match (concat (regexp-quote s0) " *") txt)
  20323. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20324. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20325. (= (match-beginning 0) 0)
  20326. t))
  20327. (setq txt (replace-match "" nil nil txt))))
  20328. ;; Normalize the time(s) to 24 hour
  20329. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20330. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20331. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20332. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20333. (let ((m (+ (string-to-number (match-string 2 s1))
  20334. (* 60 (string-to-number (match-string 1 s1)))
  20335. org-agenda-default-appointment-duration))
  20336. h)
  20337. (setq h (/ m 60) m (- m (* h 60)))
  20338. (setq s2 (format "%02d:%02d" h m))))
  20339. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20340. txt)
  20341. ;; Tags are in the string
  20342. (if (or (eq org-agenda-remove-tags t)
  20343. (and org-agenda-remove-tags
  20344. org-prefix-has-tag))
  20345. (setq txt (replace-match "" t t txt))
  20346. (setq txt (replace-match
  20347. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20348. (match-string 2 txt))
  20349. t t txt))))
  20350. (when remove-re
  20351. (while (string-match remove-re txt)
  20352. (setq txt (replace-match "" t t txt))))
  20353. ;; Create the final string
  20354. (if noprefix
  20355. (setq rtn txt)
  20356. ;; Prepare the variables needed in the eval of the compiled format
  20357. (setq time (cond (s2 (concat s1 "-" s2))
  20358. (s1 (concat s1 "......"))
  20359. (t ""))
  20360. extra (or extra "")
  20361. category (if (symbolp category) (symbol-name category) category))
  20362. ;; Evaluate the compiled format
  20363. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20364. ;; And finally add the text properties
  20365. (org-add-props rtn nil
  20366. 'org-category (downcase category) 'tags tags
  20367. 'org-highest-priority org-highest-priority
  20368. 'org-lowest-priority org-lowest-priority
  20369. 'prefix-length (- (length rtn) (length txt))
  20370. 'time-of-day time-of-day
  20371. 'txt txt
  20372. 'time time
  20373. 'extra extra
  20374. 'dotime dotime))))
  20375. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20376. (defvar org-agenda-sorting-strategy-selected nil)
  20377. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20378. (catch 'exit
  20379. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20380. ((and todayp (member 'today (car org-agenda-time-grid))))
  20381. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20382. ((member 'weekly (car org-agenda-time-grid)))
  20383. (t (throw 'exit list)))
  20384. (let* ((have (delq nil (mapcar
  20385. (lambda (x) (get-text-property 1 'time-of-day x))
  20386. list)))
  20387. (string (nth 1 org-agenda-time-grid))
  20388. (gridtimes (nth 2 org-agenda-time-grid))
  20389. (req (car org-agenda-time-grid))
  20390. (remove (member 'remove-match req))
  20391. new time)
  20392. (if (and (member 'require-timed req) (not have))
  20393. ;; don't show empty grid
  20394. (throw 'exit list))
  20395. (while (setq time (pop gridtimes))
  20396. (unless (and remove (member time have))
  20397. (setq time (int-to-string time))
  20398. (push (org-format-agenda-item
  20399. nil string "" nil
  20400. (concat (substring time 0 -2) ":" (substring time -2)))
  20401. new)
  20402. (put-text-property
  20403. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20404. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20405. (append new list)
  20406. (append list new)))))
  20407. (defun org-compile-prefix-format (key)
  20408. "Compile the prefix format into a Lisp form that can be evaluated.
  20409. The resulting form is returned and stored in the variable
  20410. `org-prefix-format-compiled'."
  20411. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20412. (let ((s (cond
  20413. ((stringp org-agenda-prefix-format)
  20414. org-agenda-prefix-format)
  20415. ((assq key org-agenda-prefix-format)
  20416. (cdr (assq key org-agenda-prefix-format)))
  20417. (t " %-12:c%?-12t% s")))
  20418. (start 0)
  20419. varform vars var e c f opt)
  20420. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20421. s start)
  20422. (setq var (cdr (assoc (match-string 4 s)
  20423. '(("c" . category) ("t" . time) ("s" . extra)
  20424. ("T" . tag))))
  20425. c (or (match-string 3 s) "")
  20426. opt (match-beginning 1)
  20427. start (1+ (match-beginning 0)))
  20428. (if (equal var 'time) (setq org-prefix-has-time t))
  20429. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20430. (setq f (concat "%" (match-string 2 s) "s"))
  20431. (if opt
  20432. (setq varform
  20433. `(if (equal "" ,var)
  20434. ""
  20435. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20436. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20437. (setq s (replace-match "%s" t nil s))
  20438. (push varform vars))
  20439. (setq vars (nreverse vars))
  20440. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20441. (defun org-set-sorting-strategy (key)
  20442. (if (symbolp (car org-agenda-sorting-strategy))
  20443. ;; the old format
  20444. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20445. (setq org-agenda-sorting-strategy-selected
  20446. (or (cdr (assq key org-agenda-sorting-strategy))
  20447. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20448. '(time-up category-keep priority-down)))))
  20449. (defun org-get-time-of-day (s &optional string mod24)
  20450. "Check string S for a time of day.
  20451. If found, return it as a military time number between 0 and 2400.
  20452. If not found, return nil.
  20453. The optional STRING argument forces conversion into a 5 character wide string
  20454. HH:MM."
  20455. (save-match-data
  20456. (when
  20457. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20458. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20459. (let* ((h (string-to-number (match-string 1 s)))
  20460. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20461. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20462. (am-p (equal ampm "am"))
  20463. (h1 (cond ((not ampm) h)
  20464. ((= h 12) (if am-p 0 12))
  20465. (t (+ h (if am-p 0 12)))))
  20466. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20467. (mod h1 24) h1))
  20468. (t0 (+ (* 100 h2) m))
  20469. (t1 (concat (if (>= h1 24) "+" " ")
  20470. (if (< t0 100) "0" "")
  20471. (if (< t0 10) "0" "")
  20472. (int-to-string t0))))
  20473. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20474. (defun org-finalize-agenda-entries (list &optional nosort)
  20475. "Sort and concatenate the agenda items."
  20476. (setq list (mapcar 'org-agenda-highlight-todo list))
  20477. (if nosort
  20478. list
  20479. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20480. (defun org-agenda-highlight-todo (x)
  20481. (let (re pl)
  20482. (if (eq x 'line)
  20483. (save-excursion
  20484. (beginning-of-line 1)
  20485. (setq re (get-text-property (point) 'org-todo-regexp))
  20486. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20487. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20488. (add-text-properties (match-beginning 0) (match-end 0)
  20489. (list 'face (org-get-todo-face 0)))
  20490. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20491. (delete-region (match-beginning 1) (1- (match-end 0)))
  20492. (goto-char (match-beginning 1))
  20493. (insert (format org-agenda-todo-keyword-format s)))))
  20494. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20495. pl (get-text-property 0 'prefix-length x))
  20496. (when (and re
  20497. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20498. x (or pl 0)) pl))
  20499. (add-text-properties
  20500. (or (match-end 1) (match-end 0)) (match-end 0)
  20501. (list 'face (org-get-todo-face (match-string 2 x)))
  20502. x)
  20503. (setq x (concat (substring x 0 (match-end 1))
  20504. (format org-agenda-todo-keyword-format
  20505. (match-string 2 x))
  20506. " "
  20507. (substring x (match-end 3)))))
  20508. x)))
  20509. (defsubst org-cmp-priority (a b)
  20510. "Compare the priorities of string A and B."
  20511. (let ((pa (or (get-text-property 1 'priority a) 0))
  20512. (pb (or (get-text-property 1 'priority b) 0)))
  20513. (cond ((> pa pb) +1)
  20514. ((< pa pb) -1)
  20515. (t nil))))
  20516. (defsubst org-cmp-category (a b)
  20517. "Compare the string values of categories of strings A and B."
  20518. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20519. (cb (or (get-text-property 1 'org-category b) "")))
  20520. (cond ((string-lessp ca cb) -1)
  20521. ((string-lessp cb ca) +1)
  20522. (t nil))))
  20523. (defsubst org-cmp-tag (a b)
  20524. "Compare the string values of categories of strings A and B."
  20525. (let ((ta (car (last (get-text-property 1 'tags a))))
  20526. (tb (car (last (get-text-property 1 'tags b)))))
  20527. (cond ((not ta) +1)
  20528. ((not tb) -1)
  20529. ((string-lessp ta tb) -1)
  20530. ((string-lessp tb ta) +1)
  20531. (t nil))))
  20532. (defsubst org-cmp-time (a b)
  20533. "Compare the time-of-day values of strings A and B."
  20534. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20535. (ta (or (get-text-property 1 'time-of-day a) def))
  20536. (tb (or (get-text-property 1 'time-of-day b) def)))
  20537. (cond ((< ta tb) -1)
  20538. ((< tb ta) +1)
  20539. (t nil))))
  20540. (defun org-entries-lessp (a b)
  20541. "Predicate for sorting agenda entries."
  20542. ;; The following variables will be used when the form is evaluated.
  20543. ;; So even though the compiler complains, keep them.
  20544. (let* ((time-up (org-cmp-time a b))
  20545. (time-down (if time-up (- time-up) nil))
  20546. (priority-up (org-cmp-priority a b))
  20547. (priority-down (if priority-up (- priority-up) nil))
  20548. (category-up (org-cmp-category a b))
  20549. (category-down (if category-up (- category-up) nil))
  20550. (category-keep (if category-up +1 nil))
  20551. (tag-up (org-cmp-tag a b))
  20552. (tag-down (if tag-up (- tag-up) nil)))
  20553. (cdr (assoc
  20554. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20555. '((-1 . t) (1 . nil) (nil . nil))))))
  20556. ;;; Agenda restriction lock
  20557. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20558. "Overlay to mark the headline to which arenda commands are restricted.")
  20559. (org-overlay-put org-agenda-restriction-lock-overlay
  20560. 'face 'org-agenda-restriction-lock)
  20561. (org-overlay-put org-agenda-restriction-lock-overlay
  20562. 'help-echo "Agendas are currently limited to this subtree.")
  20563. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20564. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20565. "Overlay marking the agenda restriction line in speedbar.")
  20566. (org-overlay-put org-speedbar-restriction-lock-overlay
  20567. 'face 'org-agenda-restriction-lock)
  20568. (org-overlay-put org-speedbar-restriction-lock-overlay
  20569. 'help-echo "Agendas are currently limited to this item.")
  20570. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20571. (defun org-agenda-set-restriction-lock (&optional type)
  20572. "Set restriction lock for agenda, to current subtree or file.
  20573. Restriction will be the file if TYPE is `file', or if type is the
  20574. universal prefix '(4), or if the cursor is before the first headline
  20575. in the file. Otherwise, restriction will be to the current subtree."
  20576. (interactive "P")
  20577. (and (equal type '(4)) (setq type 'file))
  20578. (setq type (cond
  20579. (type type)
  20580. ((org-at-heading-p) 'subtree)
  20581. ((condition-case nil (org-back-to-heading t) (error nil))
  20582. 'subtree)
  20583. (t 'file)))
  20584. (if (eq type 'subtree)
  20585. (progn
  20586. (setq org-agenda-restrict t)
  20587. (setq org-agenda-overriding-restriction 'subtree)
  20588. (put 'org-agenda-files 'org-restrict
  20589. (list (buffer-file-name (buffer-base-buffer))))
  20590. (org-back-to-heading t)
  20591. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20592. (move-marker org-agenda-restrict-begin (point))
  20593. (move-marker org-agenda-restrict-end
  20594. (save-excursion (org-end-of-subtree t)))
  20595. (message "Locking agenda restriction to subtree"))
  20596. (put 'org-agenda-files 'org-restrict
  20597. (list (buffer-file-name (buffer-base-buffer))))
  20598. (setq org-agenda-restrict nil)
  20599. (setq org-agenda-overriding-restriction 'file)
  20600. (move-marker org-agenda-restrict-begin nil)
  20601. (move-marker org-agenda-restrict-end nil)
  20602. (message "Locking agenda restriction to file"))
  20603. (setq current-prefix-arg nil)
  20604. (org-agenda-maybe-redo))
  20605. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20606. "Remove the agenda restriction lock."
  20607. (interactive "P")
  20608. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20609. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20610. (setq org-agenda-overriding-restriction nil)
  20611. (setq org-agenda-restrict nil)
  20612. (put 'org-agenda-files 'org-restrict nil)
  20613. (move-marker org-agenda-restrict-begin nil)
  20614. (move-marker org-agenda-restrict-end nil)
  20615. (setq current-prefix-arg nil)
  20616. (message "Agenda restriction lock removed")
  20617. (or noupdate (org-agenda-maybe-redo)))
  20618. (defun org-agenda-maybe-redo ()
  20619. "If there is any window showing the agenda view, update it."
  20620. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20621. (w0 (selected-window)))
  20622. (when w
  20623. (select-window w)
  20624. (org-agenda-redo)
  20625. (select-window w0)
  20626. (if org-agenda-overriding-restriction
  20627. (message "Agenda view shifted to new %s restriction"
  20628. org-agenda-overriding-restriction)
  20629. (message "Agenda restriction lock removed")))))
  20630. ;;; Agenda commands
  20631. (defun org-agenda-check-type (error &rest types)
  20632. "Check if agenda buffer is of allowed type.
  20633. If ERROR is non-nil, throw an error, otherwise just return nil."
  20634. (if (memq org-agenda-type types)
  20635. t
  20636. (if error
  20637. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20638. nil)))
  20639. (defun org-agenda-quit ()
  20640. "Exit agenda by removing the window or the buffer."
  20641. (interactive)
  20642. (let ((buf (current-buffer)))
  20643. (if (not (one-window-p)) (delete-window))
  20644. (kill-buffer buf)
  20645. (org-agenda-reset-markers)
  20646. (org-columns-remove-overlays))
  20647. ;; Maybe restore the pre-agenda window configuration.
  20648. (and org-agenda-restore-windows-after-quit
  20649. (not (eq org-agenda-window-setup 'other-frame))
  20650. org-pre-agenda-window-conf
  20651. (set-window-configuration org-pre-agenda-window-conf)))
  20652. (defun org-agenda-exit ()
  20653. "Exit agenda by removing the window or the buffer.
  20654. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20655. Org-mode buffers visited directly by the user will not be touched."
  20656. (interactive)
  20657. (org-release-buffers org-agenda-new-buffers)
  20658. (setq org-agenda-new-buffers nil)
  20659. (org-agenda-quit))
  20660. (defun org-agenda-execute (arg)
  20661. "Execute another agenda command, keeping same window.\\<global-map>
  20662. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20663. (interactive "P")
  20664. (let ((org-agenda-window-setup 'current-window))
  20665. (org-agenda arg)))
  20666. (defun org-save-all-org-buffers ()
  20667. "Save all Org-mode buffers without user confirmation."
  20668. (interactive)
  20669. (message "Saving all Org-mode buffers...")
  20670. (save-some-buffers t 'org-mode-p)
  20671. (message "Saving all Org-mode buffers... done"))
  20672. (defun org-agenda-redo ()
  20673. "Rebuild Agenda.
  20674. When this is the global TODO list, a prefix argument will be interpreted."
  20675. (interactive)
  20676. (let* ((org-agenda-keep-modes t)
  20677. (line (org-current-line))
  20678. (window-line (- line (org-current-line (window-start))))
  20679. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20680. (message "Rebuilding agenda buffer...")
  20681. (org-let lprops '(eval org-agenda-redo-command))
  20682. (setq org-agenda-undo-list nil
  20683. org-agenda-pending-undo-list nil)
  20684. (message "Rebuilding agenda buffer...done")
  20685. (goto-line line)
  20686. (recenter window-line)))
  20687. (defun org-agenda-manipulate-query-add ()
  20688. "Manipulate the query by adding a search term with positive selection.
  20689. Positive selection means, the term must be matched for selection of an entry."
  20690. (interactive)
  20691. (org-agenda-manipulate-query ?\[))
  20692. (defun org-agenda-manipulate-query-subtract ()
  20693. "Manipulate the query by adding a search term with negative selection.
  20694. Negative selection means, term must not be matched for selection of an entry."
  20695. (interactive)
  20696. (org-agenda-manipulate-query ?\]))
  20697. (defun org-agenda-manipulate-query-add-re ()
  20698. "Manipulate the query by adding a search regexp with positive selection.
  20699. Positive selection means, the regexp must match for selection of an entry."
  20700. (interactive)
  20701. (org-agenda-manipulate-query ?\{))
  20702. (defun org-agenda-manipulate-query-subtract-re ()
  20703. "Manipulate the query by adding a search regexp with negative selection.
  20704. Negative selection means, regexp must not match for selection of an entry."
  20705. (interactive)
  20706. (org-agenda-manipulate-query ?\}))
  20707. (defun org-agenda-manipulate-query (char)
  20708. (cond
  20709. ((eq org-agenda-type 'search)
  20710. (org-add-to-string
  20711. 'org-agenda-query-string
  20712. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  20713. (?\{ . " +{}") (?\} . " -{}")))))
  20714. (setq org-agenda-redo-command
  20715. (list 'org-search-view
  20716. org-todo-only
  20717. org-agenda-query-string
  20718. (+ (length org-agenda-query-string)
  20719. (if (member char '(?\{ ?\})) 0 1))))
  20720. (set-register org-agenda-query-register org-agenda-query-string)
  20721. (org-agenda-redo))
  20722. (t (error "Canot manipulate query for %s-type agenda buffers"
  20723. org-agenda-type))))
  20724. (defun org-add-to-string (var string)
  20725. (set var (concat (symbol-value var) string)))
  20726. (defun org-agenda-goto-date (date)
  20727. "Jump to DATE in agenda."
  20728. (interactive (list (org-read-date)))
  20729. (org-agenda-list nil date))
  20730. (defun org-agenda-goto-today ()
  20731. "Go to today."
  20732. (interactive)
  20733. (org-agenda-check-type t 'timeline 'agenda)
  20734. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20735. (cond
  20736. (tdpos (goto-char tdpos))
  20737. ((eq org-agenda-type 'agenda)
  20738. (let* ((sd (time-to-days
  20739. (time-subtract (current-time)
  20740. (list 0 (* 3600 org-extend-today-until) 0))))
  20741. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20742. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20743. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20744. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20745. (org-agenda-redo)
  20746. (org-agenda-find-same-or-today-or-agenda)))
  20747. (t (error "Cannot find today")))))
  20748. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20749. (goto-char
  20750. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20751. (text-property-any (point-min) (point-max) 'org-today t)
  20752. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20753. (point-min))))
  20754. (defun org-agenda-later (arg)
  20755. "Go forward in time by thee current span.
  20756. With prefix ARG, go forward that many times the current span."
  20757. (interactive "p")
  20758. (org-agenda-check-type t 'agenda)
  20759. (let* ((span org-agenda-span)
  20760. (sd org-starting-day)
  20761. (greg (calendar-gregorian-from-absolute sd))
  20762. (cnt (get-text-property (point) 'org-day-cnt))
  20763. greg2 nd)
  20764. (cond
  20765. ((eq span 'day)
  20766. (setq sd (+ arg sd) nd 1))
  20767. ((eq span 'week)
  20768. (setq sd (+ (* 7 arg) sd) nd 7))
  20769. ((eq span 'month)
  20770. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20771. sd (calendar-absolute-from-gregorian greg2))
  20772. (setcar greg2 (1+ (car greg2)))
  20773. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20774. ((eq span 'year)
  20775. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20776. sd (calendar-absolute-from-gregorian greg2))
  20777. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20778. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20779. (let ((org-agenda-overriding-arguments
  20780. (list (car org-agenda-last-arguments) sd nd t)))
  20781. (org-agenda-redo)
  20782. (org-agenda-find-same-or-today-or-agenda cnt))))
  20783. (defun org-agenda-earlier (arg)
  20784. "Go backward in time by the current span.
  20785. With prefix ARG, go backward that many times the current span."
  20786. (interactive "p")
  20787. (org-agenda-later (- arg)))
  20788. (defun org-agenda-day-view ()
  20789. "Switch to daily view for agenda."
  20790. (interactive)
  20791. (setq org-agenda-ndays 1)
  20792. (org-agenda-change-time-span 'day))
  20793. (defun org-agenda-week-view ()
  20794. "Switch to daily view for agenda."
  20795. (interactive)
  20796. (setq org-agenda-ndays 7)
  20797. (org-agenda-change-time-span 'week))
  20798. (defun org-agenda-month-view ()
  20799. "Switch to daily view for agenda."
  20800. (interactive)
  20801. (org-agenda-change-time-span 'month))
  20802. (defun org-agenda-year-view ()
  20803. "Switch to daily view for agenda."
  20804. (interactive)
  20805. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20806. (org-agenda-change-time-span 'year)
  20807. (error "Abort")))
  20808. (defun org-agenda-change-time-span (span)
  20809. "Change the agenda view to SPAN.
  20810. SPAN may be `day', `week', `month', `year'."
  20811. (org-agenda-check-type t 'agenda)
  20812. (if (equal org-agenda-span span)
  20813. (error "Viewing span is already \"%s\"" span))
  20814. (let* ((sd (or (get-text-property (point) 'day)
  20815. org-starting-day))
  20816. (computed (org-agenda-compute-time-span sd span))
  20817. (org-agenda-overriding-arguments
  20818. (list (car org-agenda-last-arguments)
  20819. (car computed) (cdr computed) t)))
  20820. (org-agenda-redo)
  20821. (org-agenda-find-same-or-today-or-agenda))
  20822. (org-agenda-set-mode-name)
  20823. (message "Switched to %s view" span))
  20824. (defun org-agenda-compute-time-span (sd span)
  20825. "Compute starting date and number of days for agenda.
  20826. SPAN may be `day', `week', `month', `year'. The return value
  20827. is a cons cell with the starting date and the number of days,
  20828. so that the date SD will be in that range."
  20829. (let* ((greg (calendar-gregorian-from-absolute sd))
  20830. nd)
  20831. (cond
  20832. ((eq span 'day)
  20833. (setq nd 1))
  20834. ((eq span 'week)
  20835. (let* ((nt (calendar-day-of-week
  20836. (calendar-gregorian-from-absolute sd)))
  20837. (d (if org-agenda-start-on-weekday
  20838. (- nt org-agenda-start-on-weekday)
  20839. 0)))
  20840. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20841. (setq nd 7)))
  20842. ((eq span 'month)
  20843. (setq sd (calendar-absolute-from-gregorian
  20844. (list (car greg) 1 (nth 2 greg)))
  20845. nd (- (calendar-absolute-from-gregorian
  20846. (list (1+ (car greg)) 1 (nth 2 greg)))
  20847. sd)))
  20848. ((eq span 'year)
  20849. (setq sd (calendar-absolute-from-gregorian
  20850. (list 1 1 (nth 2 greg)))
  20851. nd (- (calendar-absolute-from-gregorian
  20852. (list 1 1 (1+ (nth 2 greg))))
  20853. sd))))
  20854. (cons sd nd)))
  20855. ;; FIXME: does not work if user makes date format that starts with a blank
  20856. (defun org-agenda-next-date-line (&optional arg)
  20857. "Jump to the next line indicating a date in agenda buffer."
  20858. (interactive "p")
  20859. (org-agenda-check-type t 'agenda 'timeline)
  20860. (beginning-of-line 1)
  20861. (if (looking-at "^\\S-") (forward-char 1))
  20862. (if (not (re-search-forward "^\\S-" nil t arg))
  20863. (progn
  20864. (backward-char 1)
  20865. (error "No next date after this line in this buffer")))
  20866. (goto-char (match-beginning 0)))
  20867. (defun org-agenda-previous-date-line (&optional arg)
  20868. "Jump to the previous line indicating a date in agenda buffer."
  20869. (interactive "p")
  20870. (org-agenda-check-type t 'agenda 'timeline)
  20871. (beginning-of-line 1)
  20872. (if (not (re-search-backward "^\\S-" nil t arg))
  20873. (error "No previous date before this line in this buffer")))
  20874. ;; Initialize the highlight
  20875. (defvar org-hl (org-make-overlay 1 1))
  20876. (org-overlay-put org-hl 'face 'highlight)
  20877. (defun org-highlight (begin end &optional buffer)
  20878. "Highlight a region with overlay."
  20879. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20880. org-hl begin end (or buffer (current-buffer))))
  20881. (defun org-unhighlight ()
  20882. "Detach overlay INDEX."
  20883. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20884. ;; FIXME this is currently not used.
  20885. (defun org-highlight-until-next-command (beg end &optional buffer)
  20886. (org-highlight beg end buffer)
  20887. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20888. (defun org-unhighlight-once ()
  20889. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20890. (org-unhighlight))
  20891. (defun org-agenda-follow-mode ()
  20892. "Toggle follow mode in an agenda buffer."
  20893. (interactive)
  20894. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20895. (org-agenda-set-mode-name)
  20896. (message "Follow mode is %s"
  20897. (if org-agenda-follow-mode "on" "off")))
  20898. (defun org-agenda-log-mode ()
  20899. "Toggle log mode in an agenda buffer."
  20900. (interactive)
  20901. (org-agenda-check-type t 'agenda 'timeline)
  20902. (setq org-agenda-show-log (not org-agenda-show-log))
  20903. (org-agenda-set-mode-name)
  20904. (org-agenda-redo)
  20905. (message "Log mode is %s"
  20906. (if org-agenda-show-log "on" "off")))
  20907. (defun org-agenda-toggle-diary ()
  20908. "Toggle diary inclusion in an agenda buffer."
  20909. (interactive)
  20910. (org-agenda-check-type t 'agenda)
  20911. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20912. (org-agenda-redo)
  20913. (org-agenda-set-mode-name)
  20914. (message "Diary inclusion turned %s"
  20915. (if org-agenda-include-diary "on" "off")))
  20916. (defun org-agenda-toggle-time-grid ()
  20917. "Toggle time grid in an agenda buffer."
  20918. (interactive)
  20919. (org-agenda-check-type t 'agenda)
  20920. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20921. (org-agenda-redo)
  20922. (org-agenda-set-mode-name)
  20923. (message "Time-grid turned %s"
  20924. (if org-agenda-use-time-grid "on" "off")))
  20925. (defun org-agenda-set-mode-name ()
  20926. "Set the mode name to indicate all the small mode settings."
  20927. (setq mode-name
  20928. (concat "Org-Agenda"
  20929. (if (equal org-agenda-ndays 1) " Day" "")
  20930. (if (equal org-agenda-ndays 7) " Week" "")
  20931. (if org-agenda-follow-mode " Follow" "")
  20932. (if org-agenda-include-diary " Diary" "")
  20933. (if org-agenda-use-time-grid " Grid" "")
  20934. (if org-agenda-show-log " Log" "")))
  20935. (force-mode-line-update))
  20936. (defun org-agenda-post-command-hook ()
  20937. (and (eolp) (not (bolp)) (backward-char 1))
  20938. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20939. (if (and org-agenda-follow-mode
  20940. (get-text-property (point) 'org-marker))
  20941. (org-agenda-show)))
  20942. (defun org-agenda-show-priority ()
  20943. "Show the priority of the current item.
  20944. This priority is composed of the main priority given with the [#A] cookies,
  20945. and by additional input from the age of a schedules or deadline entry."
  20946. (interactive)
  20947. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20948. (message "Priority is %d" (if pri pri -1000))))
  20949. (defun org-agenda-show-tags ()
  20950. "Show the tags applicable to the current item."
  20951. (interactive)
  20952. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20953. (if tags
  20954. (message "Tags are :%s:"
  20955. (org-no-properties (mapconcat 'identity tags ":")))
  20956. (message "No tags associated with this line"))))
  20957. (defun org-agenda-goto (&optional highlight)
  20958. "Go to the Org-mode file which contains the item at point."
  20959. (interactive)
  20960. (let* ((marker (or (get-text-property (point) 'org-marker)
  20961. (org-agenda-error)))
  20962. (buffer (marker-buffer marker))
  20963. (pos (marker-position marker)))
  20964. (switch-to-buffer-other-window buffer)
  20965. (widen)
  20966. (goto-char pos)
  20967. (when (org-mode-p)
  20968. (org-show-context 'agenda)
  20969. (save-excursion
  20970. (and (outline-next-heading)
  20971. (org-flag-heading nil)))) ; show the next heading
  20972. (recenter (/ (window-height) 2))
  20973. (run-hooks 'org-agenda-after-show-hook)
  20974. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20975. (defvar org-agenda-after-show-hook nil
  20976. "Normal hook run after an item has been shown from the agenda.
  20977. Point is in the buffer where the item originated.")
  20978. (defun org-agenda-kill ()
  20979. "Kill the entry or subtree belonging to the current agenda entry."
  20980. (interactive)
  20981. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20982. (let* ((marker (or (get-text-property (point) 'org-marker)
  20983. (org-agenda-error)))
  20984. (buffer (marker-buffer marker))
  20985. (pos (marker-position marker))
  20986. (type (get-text-property (point) 'type))
  20987. dbeg dend (n 0) conf)
  20988. (org-with-remote-undo buffer
  20989. (with-current-buffer buffer
  20990. (save-excursion
  20991. (goto-char pos)
  20992. (if (and (org-mode-p) (not (member type '("sexp"))))
  20993. (setq dbeg (progn (org-back-to-heading t) (point))
  20994. dend (org-end-of-subtree t t))
  20995. (setq dbeg (point-at-bol)
  20996. dend (min (point-max) (1+ (point-at-eol)))))
  20997. (goto-char dbeg)
  20998. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20999. (setq conf (or (eq t org-agenda-confirm-kill)
  21000. (and (numberp org-agenda-confirm-kill)
  21001. (> n org-agenda-confirm-kill))))
  21002. (and conf
  21003. (not (y-or-n-p
  21004. (format "Delete entry with %d lines in buffer \"%s\"? "
  21005. n (buffer-name buffer))))
  21006. (error "Abort"))
  21007. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  21008. (with-current-buffer buffer (delete-region dbeg dend))
  21009. (message "Agenda item and source killed"))))
  21010. (defun org-agenda-archive ()
  21011. "Kill the entry or subtree belonging to the current agenda entry."
  21012. (interactive)
  21013. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21014. (let* ((marker (or (get-text-property (point) 'org-marker)
  21015. (org-agenda-error)))
  21016. (buffer (marker-buffer marker))
  21017. (pos (marker-position marker)))
  21018. (org-with-remote-undo buffer
  21019. (with-current-buffer buffer
  21020. (if (org-mode-p)
  21021. (save-excursion
  21022. (goto-char pos)
  21023. (org-remove-subtree-entries-from-agenda)
  21024. (org-back-to-heading t)
  21025. (org-archive-subtree))
  21026. (error "Archiving works only in Org-mode files"))))))
  21027. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  21028. "Remove all lines in the agenda that correspond to a given subtree.
  21029. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  21030. If this information is not given, the function uses the tree at point."
  21031. (let ((buf (or buf (current-buffer))) m p)
  21032. (save-excursion
  21033. (unless (and beg end)
  21034. (org-back-to-heading t)
  21035. (setq beg (point))
  21036. (org-end-of-subtree t)
  21037. (setq end (point)))
  21038. (set-buffer (get-buffer org-agenda-buffer-name))
  21039. (save-excursion
  21040. (goto-char (point-max))
  21041. (beginning-of-line 1)
  21042. (while (not (bobp))
  21043. (when (and (setq m (get-text-property (point) 'org-marker))
  21044. (equal buf (marker-buffer m))
  21045. (setq p (marker-position m))
  21046. (>= p beg)
  21047. (<= p end))
  21048. (let ((inhibit-read-only t))
  21049. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  21050. (beginning-of-line 0))))))
  21051. (defun org-agenda-open-link ()
  21052. "Follow the link in the current line, if any."
  21053. (interactive)
  21054. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  21055. (save-excursion
  21056. (save-restriction
  21057. (narrow-to-region (point-at-bol) (point-at-eol))
  21058. (org-open-at-point))))
  21059. (defun org-agenda-copy-local-variable (var)
  21060. "Get a variable from a referenced buffer and install it here."
  21061. (let ((m (get-text-property (point) 'org-marker)))
  21062. (when (and m (buffer-live-p (marker-buffer m)))
  21063. (org-set-local var (with-current-buffer (marker-buffer m)
  21064. (symbol-value var))))))
  21065. (defun org-agenda-switch-to (&optional delete-other-windows)
  21066. "Go to the Org-mode file which contains the item at point."
  21067. (interactive)
  21068. (let* ((marker (or (get-text-property (point) 'org-marker)
  21069. (org-agenda-error)))
  21070. (buffer (marker-buffer marker))
  21071. (pos (marker-position marker)))
  21072. (switch-to-buffer buffer)
  21073. (and delete-other-windows (delete-other-windows))
  21074. (widen)
  21075. (goto-char pos)
  21076. (when (org-mode-p)
  21077. (org-show-context 'agenda)
  21078. (save-excursion
  21079. (and (outline-next-heading)
  21080. (org-flag-heading nil)))))) ; show the next heading
  21081. (defun org-agenda-goto-mouse (ev)
  21082. "Go to the Org-mode file which contains the item at the mouse click."
  21083. (interactive "e")
  21084. (mouse-set-point ev)
  21085. (org-agenda-goto))
  21086. (defun org-agenda-show ()
  21087. "Display the Org-mode file which contains the item at point."
  21088. (interactive)
  21089. (let ((win (selected-window)))
  21090. (org-agenda-goto t)
  21091. (select-window win)))
  21092. (defun org-agenda-recenter (arg)
  21093. "Display the Org-mode file which contains the item at point and recenter."
  21094. (interactive "P")
  21095. (let ((win (selected-window)))
  21096. (org-agenda-goto t)
  21097. (recenter arg)
  21098. (select-window win)))
  21099. (defun org-agenda-show-mouse (ev)
  21100. "Display the Org-mode file which contains the item at the mouse click."
  21101. (interactive "e")
  21102. (mouse-set-point ev)
  21103. (org-agenda-show))
  21104. (defun org-agenda-check-no-diary ()
  21105. "Check if the entry is a diary link and abort if yes."
  21106. (if (get-text-property (point) 'org-agenda-diary-link)
  21107. (org-agenda-error)))
  21108. (defun org-agenda-error ()
  21109. (error "Command not allowed in this line"))
  21110. (defun org-agenda-tree-to-indirect-buffer ()
  21111. "Show the subtree corresponding to the current entry in an indirect buffer.
  21112. This calls the command `org-tree-to-indirect-buffer' from the original
  21113. Org-mode buffer.
  21114. With numerical prefix arg ARG, go up to this level and then take that tree.
  21115. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  21116. dedicated frame)."
  21117. (interactive)
  21118. (org-agenda-check-no-diary)
  21119. (let* ((marker (or (get-text-property (point) 'org-marker)
  21120. (org-agenda-error)))
  21121. (buffer (marker-buffer marker))
  21122. (pos (marker-position marker)))
  21123. (with-current-buffer buffer
  21124. (save-excursion
  21125. (goto-char pos)
  21126. (call-interactively 'org-tree-to-indirect-buffer)))))
  21127. (defvar org-last-heading-marker (make-marker)
  21128. "Marker pointing to the headline that last changed its TODO state
  21129. by a remote command from the agenda.")
  21130. (defun org-agenda-todo-nextset ()
  21131. "Switch TODO entry to next sequence."
  21132. (interactive)
  21133. (org-agenda-todo 'nextset))
  21134. (defun org-agenda-todo-previousset ()
  21135. "Switch TODO entry to previous sequence."
  21136. (interactive)
  21137. (org-agenda-todo 'previousset))
  21138. (defun org-agenda-todo (&optional arg)
  21139. "Cycle TODO state of line at point, also in Org-mode file.
  21140. This changes the line at point, all other lines in the agenda referring to
  21141. the same tree node, and the headline of the tree node in the Org-mode file."
  21142. (interactive "P")
  21143. (org-agenda-check-no-diary)
  21144. (let* ((col (current-column))
  21145. (marker (or (get-text-property (point) 'org-marker)
  21146. (org-agenda-error)))
  21147. (buffer (marker-buffer marker))
  21148. (pos (marker-position marker))
  21149. (hdmarker (get-text-property (point) 'org-hd-marker))
  21150. (inhibit-read-only t)
  21151. newhead)
  21152. (org-with-remote-undo buffer
  21153. (with-current-buffer buffer
  21154. (widen)
  21155. (goto-char pos)
  21156. (org-show-context 'agenda)
  21157. (save-excursion
  21158. (and (outline-next-heading)
  21159. (org-flag-heading nil))) ; show the next heading
  21160. (org-todo arg)
  21161. (and (bolp) (forward-char 1))
  21162. (setq newhead (org-get-heading))
  21163. (save-excursion
  21164. (org-back-to-heading)
  21165. (move-marker org-last-heading-marker (point))))
  21166. (beginning-of-line 1)
  21167. (save-excursion
  21168. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21169. (move-to-column col))))
  21170. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21171. "Change all lines in the agenda buffer which match HDMARKER.
  21172. The new content of the line will be NEWHEAD (as modified by
  21173. `org-format-agenda-item'). HDMARKER is checked with
  21174. `equal' against all `org-hd-marker' text properties in the file.
  21175. If FIXFACE is non-nil, the face of each item is modified acording to
  21176. the new TODO state."
  21177. (let* ((inhibit-read-only t)
  21178. props m pl undone-face done-face finish new dotime cat tags)
  21179. (save-excursion
  21180. (goto-char (point-max))
  21181. (beginning-of-line 1)
  21182. (while (not finish)
  21183. (setq finish (bobp))
  21184. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21185. (equal m hdmarker))
  21186. (setq props (text-properties-at (point))
  21187. dotime (get-text-property (point) 'dotime)
  21188. cat (get-text-property (point) 'org-category)
  21189. tags (get-text-property (point) 'tags)
  21190. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21191. pl (get-text-property (point) 'prefix-length)
  21192. undone-face (get-text-property (point) 'undone-face)
  21193. done-face (get-text-property (point) 'done-face))
  21194. (move-to-column pl)
  21195. (cond
  21196. ((equal new "")
  21197. (beginning-of-line 1)
  21198. (and (looking-at ".*\n?") (replace-match "")))
  21199. ((looking-at ".*")
  21200. (replace-match new t t)
  21201. (beginning-of-line 1)
  21202. (add-text-properties (point-at-bol) (point-at-eol) props)
  21203. (when fixface
  21204. (add-text-properties
  21205. (point-at-bol) (point-at-eol)
  21206. (list 'face
  21207. (if org-last-todo-state-is-todo
  21208. undone-face done-face))))
  21209. (org-agenda-highlight-todo 'line)
  21210. (beginning-of-line 1))
  21211. (t (error "Line update did not work"))))
  21212. (beginning-of-line 0)))
  21213. (org-finalize-agenda)))
  21214. (defun org-agenda-align-tags (&optional line)
  21215. "Align all tags in agenda items to `org-agenda-tags-column'."
  21216. (let ((inhibit-read-only t) l c)
  21217. (save-excursion
  21218. (goto-char (if line (point-at-bol) (point-min)))
  21219. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21220. (if line (point-at-eol) nil) t)
  21221. (add-text-properties
  21222. (match-beginning 2) (match-end 2)
  21223. (list 'face (delq nil (list 'org-tag (get-text-property
  21224. (match-beginning 2) 'face)))))
  21225. (setq l (- (match-end 2) (match-beginning 2))
  21226. c (if (< org-agenda-tags-column 0)
  21227. (- (abs org-agenda-tags-column) l)
  21228. org-agenda-tags-column))
  21229. (delete-region (match-beginning 1) (match-end 1))
  21230. (goto-char (match-beginning 1))
  21231. (insert (org-add-props
  21232. (make-string (max 1 (- c (current-column))) ?\ )
  21233. (text-properties-at (point))))))))
  21234. (defun org-agenda-priority-up ()
  21235. "Increase the priority of line at point, also in Org-mode file."
  21236. (interactive)
  21237. (org-agenda-priority 'up))
  21238. (defun org-agenda-priority-down ()
  21239. "Decrease the priority of line at point, also in Org-mode file."
  21240. (interactive)
  21241. (org-agenda-priority 'down))
  21242. (defun org-agenda-priority (&optional force-direction)
  21243. "Set the priority of line at point, also in Org-mode file.
  21244. This changes the line at point, all other lines in the agenda referring to
  21245. the same tree node, and the headline of the tree node in the Org-mode file."
  21246. (interactive)
  21247. (org-agenda-check-no-diary)
  21248. (let* ((marker (or (get-text-property (point) 'org-marker)
  21249. (org-agenda-error)))
  21250. (hdmarker (get-text-property (point) 'org-hd-marker))
  21251. (buffer (marker-buffer hdmarker))
  21252. (pos (marker-position hdmarker))
  21253. (inhibit-read-only t)
  21254. newhead)
  21255. (org-with-remote-undo buffer
  21256. (with-current-buffer buffer
  21257. (widen)
  21258. (goto-char pos)
  21259. (org-show-context 'agenda)
  21260. (save-excursion
  21261. (and (outline-next-heading)
  21262. (org-flag-heading nil))) ; show the next heading
  21263. (funcall 'org-priority force-direction)
  21264. (end-of-line 1)
  21265. (setq newhead (org-get-heading)))
  21266. (org-agenda-change-all-lines newhead hdmarker)
  21267. (beginning-of-line 1))))
  21268. (defun org-get-tags-at (&optional pos)
  21269. "Get a list of all headline tags applicable at POS.
  21270. POS defaults to point. If tags are inherited, the list contains
  21271. the targets in the same sequence as the headlines appear, i.e.
  21272. the tags of the current headline come last."
  21273. (interactive)
  21274. (let (tags lastpos)
  21275. (save-excursion
  21276. (save-restriction
  21277. (widen)
  21278. (goto-char (or pos (point)))
  21279. (save-match-data
  21280. (condition-case nil
  21281. (progn
  21282. (org-back-to-heading t)
  21283. (while (not (equal lastpos (point)))
  21284. (setq lastpos (point))
  21285. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21286. (setq tags (append (org-split-string
  21287. (org-match-string-no-properties 1) ":")
  21288. tags)))
  21289. (or org-use-tag-inheritance (error ""))
  21290. (org-up-heading-all 1)))
  21291. (error nil))))
  21292. tags)))
  21293. ;; FIXME: should fix the tags property of the agenda line.
  21294. (defun org-agenda-set-tags ()
  21295. "Set tags for the current headline."
  21296. (interactive)
  21297. (org-agenda-check-no-diary)
  21298. (if (and (org-region-active-p) (interactive-p))
  21299. (call-interactively 'org-change-tag-in-region)
  21300. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21301. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21302. (org-agenda-error)))
  21303. (buffer (marker-buffer hdmarker))
  21304. (pos (marker-position hdmarker))
  21305. (inhibit-read-only t)
  21306. newhead)
  21307. (org-with-remote-undo buffer
  21308. (with-current-buffer buffer
  21309. (widen)
  21310. (goto-char pos)
  21311. (save-excursion
  21312. (org-show-context 'agenda))
  21313. (save-excursion
  21314. (and (outline-next-heading)
  21315. (org-flag-heading nil))) ; show the next heading
  21316. (goto-char pos)
  21317. (call-interactively 'org-set-tags)
  21318. (end-of-line 1)
  21319. (setq newhead (org-get-heading)))
  21320. (org-agenda-change-all-lines newhead hdmarker)
  21321. (beginning-of-line 1)))))
  21322. (defun org-agenda-toggle-archive-tag ()
  21323. "Toggle the archive tag for the current entry."
  21324. (interactive)
  21325. (org-agenda-check-no-diary)
  21326. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21327. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21328. (org-agenda-error)))
  21329. (buffer (marker-buffer hdmarker))
  21330. (pos (marker-position hdmarker))
  21331. (inhibit-read-only t)
  21332. newhead)
  21333. (org-with-remote-undo buffer
  21334. (with-current-buffer buffer
  21335. (widen)
  21336. (goto-char pos)
  21337. (org-show-context 'agenda)
  21338. (save-excursion
  21339. (and (outline-next-heading)
  21340. (org-flag-heading nil))) ; show the next heading
  21341. (call-interactively 'org-toggle-archive-tag)
  21342. (end-of-line 1)
  21343. (setq newhead (org-get-heading)))
  21344. (org-agenda-change-all-lines newhead hdmarker)
  21345. (beginning-of-line 1))))
  21346. (defun org-agenda-date-later (arg &optional what)
  21347. "Change the date of this item to one day later."
  21348. (interactive "p")
  21349. (org-agenda-check-type t 'agenda 'timeline)
  21350. (org-agenda-check-no-diary)
  21351. (let* ((marker (or (get-text-property (point) 'org-marker)
  21352. (org-agenda-error)))
  21353. (buffer (marker-buffer marker))
  21354. (pos (marker-position marker)))
  21355. (org-with-remote-undo buffer
  21356. (with-current-buffer buffer
  21357. (widen)
  21358. (goto-char pos)
  21359. (if (not (org-at-timestamp-p))
  21360. (error "Cannot find time stamp"))
  21361. (org-timestamp-change arg (or what 'day)))
  21362. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21363. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21364. (defun org-agenda-date-earlier (arg &optional what)
  21365. "Change the date of this item to one day earlier."
  21366. (interactive "p")
  21367. (org-agenda-date-later (- arg) what))
  21368. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21369. "Show new date stamp via text properties."
  21370. ;; We use text properties to make this undoable
  21371. (let ((inhibit-read-only t))
  21372. (setq stamp (concat " " prefix " => " stamp))
  21373. (save-excursion
  21374. (goto-char (point-max))
  21375. (while (not (bobp))
  21376. (when (equal marker (get-text-property (point) 'org-marker))
  21377. (move-to-column (- (window-width) (length stamp)) t)
  21378. (if (featurep 'xemacs)
  21379. ;; Use `duplicable' property to trigger undo recording
  21380. (let ((ex (make-extent nil nil))
  21381. (gl (make-glyph stamp)))
  21382. (set-glyph-face gl 'secondary-selection)
  21383. (set-extent-properties
  21384. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21385. (insert-extent ex (1- (point)) (point-at-eol)))
  21386. (add-text-properties
  21387. (1- (point)) (point-at-eol)
  21388. (list 'display (org-add-props stamp nil
  21389. 'face 'secondary-selection))))
  21390. (beginning-of-line 1))
  21391. (beginning-of-line 0)))))
  21392. (defun org-agenda-date-prompt (arg)
  21393. "Change the date of this item. Date is prompted for, with default today.
  21394. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21395. be used to request time specification in the time stamp."
  21396. (interactive "P")
  21397. (org-agenda-check-type t 'agenda 'timeline)
  21398. (org-agenda-check-no-diary)
  21399. (let* ((marker (or (get-text-property (point) 'org-marker)
  21400. (org-agenda-error)))
  21401. (buffer (marker-buffer marker))
  21402. (pos (marker-position marker)))
  21403. (org-with-remote-undo buffer
  21404. (with-current-buffer buffer
  21405. (widen)
  21406. (goto-char pos)
  21407. (if (not (org-at-timestamp-p))
  21408. (error "Cannot find time stamp"))
  21409. (org-time-stamp arg)
  21410. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21411. (defun org-agenda-schedule (arg)
  21412. "Schedule the item at point."
  21413. (interactive "P")
  21414. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
  21415. (org-agenda-check-no-diary)
  21416. (let* ((marker (or (get-text-property (point) 'org-marker)
  21417. (org-agenda-error)))
  21418. (type (marker-insertion-type marker))
  21419. (buffer (marker-buffer marker))
  21420. (pos (marker-position marker))
  21421. (org-insert-labeled-timestamps-at-point nil)
  21422. ts)
  21423. (when type (message "%s" type) (sit-for 3))
  21424. (set-marker-insertion-type marker t)
  21425. (org-with-remote-undo buffer
  21426. (with-current-buffer buffer
  21427. (widen)
  21428. (goto-char pos)
  21429. (setq ts (org-schedule arg)))
  21430. (org-agenda-show-new-time marker ts "S"))
  21431. (message "Item scheduled for %s" ts)))
  21432. (defun org-agenda-deadline (arg)
  21433. "Schedule the item at point."
  21434. (interactive "P")
  21435. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
  21436. (org-agenda-check-no-diary)
  21437. (let* ((marker (or (get-text-property (point) 'org-marker)
  21438. (org-agenda-error)))
  21439. (buffer (marker-buffer marker))
  21440. (pos (marker-position marker))
  21441. (org-insert-labeled-timestamps-at-point nil)
  21442. ts)
  21443. (org-with-remote-undo buffer
  21444. (with-current-buffer buffer
  21445. (widen)
  21446. (goto-char pos)
  21447. (setq ts (org-deadline arg)))
  21448. (org-agenda-show-new-time marker ts "S"))
  21449. (message "Deadline for this item set to %s" ts)))
  21450. (defun org-get-heading (&optional no-tags)
  21451. "Return the heading of the current entry, without the stars."
  21452. (save-excursion
  21453. (org-back-to-heading t)
  21454. (if (looking-at
  21455. (if no-tags
  21456. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21457. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21458. (match-string 1) "")))
  21459. (defun org-agenda-clock-in (&optional arg)
  21460. "Start the clock on the currently selected item."
  21461. (interactive "P")
  21462. (org-agenda-check-no-diary)
  21463. (let* ((marker (or (get-text-property (point) 'org-marker)
  21464. (org-agenda-error)))
  21465. (pos (marker-position marker)))
  21466. (org-with-remote-undo (marker-buffer marker)
  21467. (with-current-buffer (marker-buffer marker)
  21468. (widen)
  21469. (goto-char pos)
  21470. (org-clock-in)))))
  21471. (defun org-agenda-clock-out (&optional arg)
  21472. "Stop the currently running clock."
  21473. (interactive "P")
  21474. (unless (marker-buffer org-clock-marker)
  21475. (error "No running clock"))
  21476. (org-with-remote-undo (marker-buffer org-clock-marker)
  21477. (org-clock-out)))
  21478. (defun org-agenda-clock-cancel (&optional arg)
  21479. "Cancel the currently running clock."
  21480. (interactive "P")
  21481. (unless (marker-buffer org-clock-marker)
  21482. (error "No running clock"))
  21483. (org-with-remote-undo (marker-buffer org-clock-marker)
  21484. (org-clock-cancel)))
  21485. (defun org-agenda-diary-entry ()
  21486. "Make a diary entry, like the `i' command from the calendar.
  21487. All the standard commands work: block, weekly etc."
  21488. (interactive)
  21489. (org-agenda-check-type t 'agenda 'timeline)
  21490. (require 'diary-lib)
  21491. (let* ((char (progn
  21492. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21493. (read-char-exclusive)))
  21494. (cmd (cdr (assoc char
  21495. '((?d . insert-diary-entry)
  21496. (?w . insert-weekly-diary-entry)
  21497. (?m . insert-monthly-diary-entry)
  21498. (?y . insert-yearly-diary-entry)
  21499. (?a . insert-anniversary-diary-entry)
  21500. (?b . insert-block-diary-entry)
  21501. (?c . insert-cyclic-diary-entry)))))
  21502. (oldf (symbol-function 'calendar-cursor-to-date))
  21503. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21504. (point (point))
  21505. (mark (or (mark t) (point))))
  21506. (unless cmd
  21507. (error "No command associated with <%c>" char))
  21508. (unless (and (get-text-property point 'day)
  21509. (or (not (equal ?b char))
  21510. (get-text-property mark 'day)))
  21511. (error "Don't know which date to use for diary entry"))
  21512. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21513. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21514. (let ((calendar-mark-ring
  21515. (list (calendar-gregorian-from-absolute
  21516. (or (get-text-property mark 'day)
  21517. (get-text-property point 'day))))))
  21518. (unwind-protect
  21519. (progn
  21520. (fset 'calendar-cursor-to-date
  21521. (lambda (&optional error)
  21522. (calendar-gregorian-from-absolute
  21523. (get-text-property point 'day))))
  21524. (call-interactively cmd))
  21525. (fset 'calendar-cursor-to-date oldf)))))
  21526. (defun org-agenda-execute-calendar-command (cmd)
  21527. "Execute a calendar command from the agenda, with the date associated to
  21528. the cursor position."
  21529. (org-agenda-check-type t 'agenda 'timeline)
  21530. (require 'diary-lib)
  21531. (unless (get-text-property (point) 'day)
  21532. (error "Don't know which date to use for calendar command"))
  21533. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21534. (point (point))
  21535. (date (calendar-gregorian-from-absolute
  21536. (get-text-property point 'day)))
  21537. ;; the following 3 vars are needed in the calendar
  21538. (displayed-day (extract-calendar-day date))
  21539. (displayed-month (extract-calendar-month date))
  21540. (displayed-year (extract-calendar-year date)))
  21541. (unwind-protect
  21542. (progn
  21543. (fset 'calendar-cursor-to-date
  21544. (lambda (&optional error)
  21545. (calendar-gregorian-from-absolute
  21546. (get-text-property point 'day))))
  21547. (call-interactively cmd))
  21548. (fset 'calendar-cursor-to-date oldf))))
  21549. (defun org-agenda-phases-of-moon ()
  21550. "Display the phases of the moon for the 3 months around the cursor date."
  21551. (interactive)
  21552. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21553. (defun org-agenda-holidays ()
  21554. "Display the holidays for the 3 months around the cursor date."
  21555. (interactive)
  21556. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21557. (defvar calendar-longitude)
  21558. (defvar calendar-latitude)
  21559. (defvar calendar-location-name)
  21560. (defun org-agenda-sunrise-sunset (arg)
  21561. "Display sunrise and sunset for the cursor date.
  21562. Latitude and longitude can be specified with the variables
  21563. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21564. argument, latitude and longitude will be prompted for."
  21565. (interactive "P")
  21566. (require 'solar)
  21567. (let ((calendar-longitude (if arg nil calendar-longitude))
  21568. (calendar-latitude (if arg nil calendar-latitude))
  21569. (calendar-location-name
  21570. (if arg "the given coordinates" calendar-location-name)))
  21571. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21572. (defun org-agenda-goto-calendar ()
  21573. "Open the Emacs calendar with the date at the cursor."
  21574. (interactive)
  21575. (org-agenda-check-type t 'agenda 'timeline)
  21576. (let* ((day (or (get-text-property (point) 'day)
  21577. (error "Don't know which date to open in calendar")))
  21578. (date (calendar-gregorian-from-absolute day))
  21579. (calendar-move-hook nil)
  21580. (view-calendar-holidays-initially nil)
  21581. (view-diary-entries-initially nil))
  21582. (calendar)
  21583. (calendar-goto-date date)))
  21584. (defun org-calendar-goto-agenda ()
  21585. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21586. This is a command that has to be installed in `calendar-mode-map'."
  21587. (interactive)
  21588. (org-agenda-list nil (calendar-absolute-from-gregorian
  21589. (calendar-cursor-to-date))
  21590. nil))
  21591. (defun org-agenda-convert-date ()
  21592. (interactive)
  21593. (org-agenda-check-type t 'agenda 'timeline)
  21594. (let ((day (get-text-property (point) 'day))
  21595. date s)
  21596. (unless day
  21597. (error "Don't know which date to convert"))
  21598. (setq date (calendar-gregorian-from-absolute day))
  21599. (setq s (concat
  21600. "Gregorian: " (calendar-date-string date) "\n"
  21601. "ISO: " (calendar-iso-date-string date) "\n"
  21602. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21603. "Julian: " (calendar-julian-date-string date) "\n"
  21604. "Astron. JD: " (calendar-astro-date-string date)
  21605. " (Julian date number at noon UTC)\n"
  21606. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21607. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21608. "French: " (calendar-french-date-string date) "\n"
  21609. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21610. "Mayan: " (calendar-mayan-date-string date) "\n"
  21611. "Coptic: " (calendar-coptic-date-string date) "\n"
  21612. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21613. "Persian: " (calendar-persian-date-string date) "\n"
  21614. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21615. (with-output-to-temp-buffer "*Dates*"
  21616. (princ s))
  21617. (if (fboundp 'fit-window-to-buffer)
  21618. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21619. ;;;; Embedded LaTeX
  21620. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21621. "Keymap for the minor `org-cdlatex-mode'.")
  21622. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21623. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21624. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21625. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21626. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21627. (defvar org-cdlatex-texmathp-advice-is-done nil
  21628. "Flag remembering if we have applied the advice to texmathp already.")
  21629. (define-minor-mode org-cdlatex-mode
  21630. "Toggle the minor `org-cdlatex-mode'.
  21631. This mode supports entering LaTeX environment and math in LaTeX fragments
  21632. in Org-mode.
  21633. \\{org-cdlatex-mode-map}"
  21634. nil " OCDL" nil
  21635. (when org-cdlatex-mode (require 'cdlatex))
  21636. (unless org-cdlatex-texmathp-advice-is-done
  21637. (setq org-cdlatex-texmathp-advice-is-done t)
  21638. (defadvice texmathp (around org-math-always-on activate)
  21639. "Always return t in org-mode buffers.
  21640. This is because we want to insert math symbols without dollars even outside
  21641. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21642. en embedded LaTeX fragement, let texmathp do its job.
  21643. \\[org-cdlatex-mode-map]"
  21644. (interactive)
  21645. (let (p)
  21646. (cond
  21647. ((not (org-mode-p)) ad-do-it)
  21648. ((eq this-command 'cdlatex-math-symbol)
  21649. (setq ad-return-value t
  21650. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21651. (t
  21652. (let ((p (org-inside-LaTeX-fragment-p)))
  21653. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21654. (setq ad-return-value t
  21655. texmathp-why '("Org-mode embedded math" . 0))
  21656. (if p ad-do-it)))))))))
  21657. (defun turn-on-org-cdlatex ()
  21658. "Unconditionally turn on `org-cdlatex-mode'."
  21659. (org-cdlatex-mode 1))
  21660. (defun org-inside-LaTeX-fragment-p ()
  21661. "Test if point is inside a LaTeX fragment.
  21662. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21663. sequence appearing also before point.
  21664. Even though the matchers for math are configurable, this function assumes
  21665. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21666. delimiters are skipped when they have been removed by customization.
  21667. The return value is nil, or a cons cell with the delimiter and
  21668. and the position of this delimiter.
  21669. This function does a reasonably good job, but can locally be fooled by
  21670. for example currency specifications. For example it will assume being in
  21671. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21672. fragments that are properly closed, but during editing, we have to live
  21673. with the uncertainty caused by missing closing delimiters. This function
  21674. looks only before point, not after."
  21675. (catch 'exit
  21676. (let ((pos (point))
  21677. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21678. (lim (progn
  21679. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21680. (point)))
  21681. dd-on str (start 0) m re)
  21682. (goto-char pos)
  21683. (when dodollar
  21684. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21685. re (nth 1 (assoc "$" org-latex-regexps)))
  21686. (while (string-match re str start)
  21687. (cond
  21688. ((= (match-end 0) (length str))
  21689. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21690. ((= (match-end 0) (- (length str) 5))
  21691. (throw 'exit nil))
  21692. (t (setq start (match-end 0))))))
  21693. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21694. (goto-char pos)
  21695. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21696. (and (match-beginning 2) (throw 'exit nil))
  21697. ;; count $$
  21698. (while (re-search-backward "\\$\\$" lim t)
  21699. (setq dd-on (not dd-on)))
  21700. (goto-char pos)
  21701. (if dd-on (cons "$$" m))))))
  21702. (defun org-try-cdlatex-tab ()
  21703. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21704. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21705. - inside a LaTeX fragment, or
  21706. - after the first word in a line, where an abbreviation expansion could
  21707. insert a LaTeX environment."
  21708. (when org-cdlatex-mode
  21709. (cond
  21710. ((save-excursion
  21711. (skip-chars-backward "a-zA-Z0-9*")
  21712. (skip-chars-backward " \t")
  21713. (bolp))
  21714. (cdlatex-tab) t)
  21715. ((org-inside-LaTeX-fragment-p)
  21716. (cdlatex-tab) t)
  21717. (t nil))))
  21718. (defun org-cdlatex-underscore-caret (&optional arg)
  21719. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21720. Revert to the normal definition outside of these fragments."
  21721. (interactive "P")
  21722. (if (org-inside-LaTeX-fragment-p)
  21723. (call-interactively 'cdlatex-sub-superscript)
  21724. (let (org-cdlatex-mode)
  21725. (call-interactively (key-binding (vector last-input-event))))))
  21726. (defun org-cdlatex-math-modify (&optional arg)
  21727. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21728. Revert to the normal definition outside of these fragments."
  21729. (interactive "P")
  21730. (if (org-inside-LaTeX-fragment-p)
  21731. (call-interactively 'cdlatex-math-modify)
  21732. (let (org-cdlatex-mode)
  21733. (call-interactively (key-binding (vector last-input-event))))))
  21734. (defvar org-latex-fragment-image-overlays nil
  21735. "List of overlays carrying the images of latex fragments.")
  21736. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21737. (defun org-remove-latex-fragment-image-overlays ()
  21738. "Remove all overlays with LaTeX fragment images in current buffer."
  21739. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21740. (setq org-latex-fragment-image-overlays nil))
  21741. (defun org-preview-latex-fragment (&optional subtree)
  21742. "Preview the LaTeX fragment at point, or all locally or globally.
  21743. If the cursor is in a LaTeX fragment, create the image and overlay
  21744. it over the source code. If there is no fragment at point, display
  21745. all fragments in the current text, from one headline to the next. With
  21746. prefix SUBTREE, display all fragments in the current subtree. With a
  21747. double prefix `C-u C-u', or when the cursor is before the first headline,
  21748. display all fragments in the buffer.
  21749. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21750. (interactive "P")
  21751. (org-remove-latex-fragment-image-overlays)
  21752. (save-excursion
  21753. (save-restriction
  21754. (let (beg end at msg)
  21755. (cond
  21756. ((or (equal subtree '(16))
  21757. (not (save-excursion
  21758. (re-search-backward (concat "^" outline-regexp) nil t))))
  21759. (setq beg (point-min) end (point-max)
  21760. msg "Creating images for buffer...%s"))
  21761. ((equal subtree '(4))
  21762. (org-back-to-heading)
  21763. (setq beg (point) end (org-end-of-subtree t)
  21764. msg "Creating images for subtree...%s"))
  21765. (t
  21766. (if (setq at (org-inside-LaTeX-fragment-p))
  21767. (goto-char (max (point-min) (- (cdr at) 2)))
  21768. (org-back-to-heading))
  21769. (setq beg (point) end (progn (outline-next-heading) (point))
  21770. msg (if at "Creating image...%s"
  21771. "Creating images for entry...%s"))))
  21772. (message msg "")
  21773. (narrow-to-region beg end)
  21774. (goto-char beg)
  21775. (org-format-latex
  21776. (concat "ltxpng/" (file-name-sans-extension
  21777. (file-name-nondirectory
  21778. buffer-file-name)))
  21779. default-directory 'overlays msg at 'forbuffer)
  21780. (message msg "done. Use `C-c C-c' to remove images.")))))
  21781. (defvar org-latex-regexps
  21782. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21783. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21784. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21785. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21786. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21787. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21788. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21789. "Regular expressions for matching embedded LaTeX.")
  21790. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21791. "Replace LaTeX fragments with links to an image, and produce images."
  21792. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21793. (let* ((prefixnodir (file-name-nondirectory prefix))
  21794. (absprefix (expand-file-name prefix dir))
  21795. (todir (file-name-directory absprefix))
  21796. (opt org-format-latex-options)
  21797. (matchers (plist-get opt :matchers))
  21798. (re-list org-latex-regexps)
  21799. (cnt 0) txt link beg end re e checkdir
  21800. m n block linkfile movefile ov)
  21801. ;; Check if there are old images files with this prefix, and remove them
  21802. (when (file-directory-p todir)
  21803. (mapc 'delete-file
  21804. (directory-files
  21805. todir 'full
  21806. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21807. ;; Check the different regular expressions
  21808. (while (setq e (pop re-list))
  21809. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21810. block (if (nth 3 e) "\n\n" ""))
  21811. (when (member m matchers)
  21812. (goto-char (point-min))
  21813. (while (re-search-forward re nil t)
  21814. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21815. (setq txt (match-string n)
  21816. beg (match-beginning n) end (match-end n)
  21817. cnt (1+ cnt)
  21818. linkfile (format "%s_%04d.png" prefix cnt)
  21819. movefile (format "%s_%04d.png" absprefix cnt)
  21820. link (concat block "[[file:" linkfile "]]" block))
  21821. (if msg (message msg cnt))
  21822. (goto-char beg)
  21823. (unless checkdir ; make sure the directory exists
  21824. (setq checkdir t)
  21825. (or (file-directory-p todir) (make-directory todir)))
  21826. (org-create-formula-image
  21827. txt movefile opt forbuffer)
  21828. (if overlays
  21829. (progn
  21830. (setq ov (org-make-overlay beg end))
  21831. (if (featurep 'xemacs)
  21832. (progn
  21833. (org-overlay-put ov 'invisible t)
  21834. (org-overlay-put
  21835. ov 'end-glyph
  21836. (make-glyph (vector 'png :file movefile))))
  21837. (org-overlay-put
  21838. ov 'display
  21839. (list 'image :type 'png :file movefile :ascent 'center)))
  21840. (push ov org-latex-fragment-image-overlays)
  21841. (goto-char end))
  21842. (delete-region beg end)
  21843. (insert link))))))))
  21844. ;; This function borrows from Ganesh Swami's latex2png.el
  21845. (defun org-create-formula-image (string tofile options buffer)
  21846. (let* ((tmpdir (if (featurep 'xemacs)
  21847. (temp-directory)
  21848. temporary-file-directory))
  21849. (texfilebase (make-temp-name
  21850. (expand-file-name "orgtex" tmpdir)))
  21851. (texfile (concat texfilebase ".tex"))
  21852. (dvifile (concat texfilebase ".dvi"))
  21853. (pngfile (concat texfilebase ".png"))
  21854. (fnh (face-attribute 'default :height nil))
  21855. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21856. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21857. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21858. "Black"))
  21859. (bg (or (plist-get options (if buffer :background :html-background))
  21860. "Transparent")))
  21861. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21862. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21863. (with-temp-file texfile
  21864. (insert org-format-latex-header
  21865. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21866. (let ((dir default-directory))
  21867. (condition-case nil
  21868. (progn
  21869. (cd tmpdir)
  21870. (call-process "latex" nil nil nil texfile))
  21871. (error nil))
  21872. (cd dir))
  21873. (if (not (file-exists-p dvifile))
  21874. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21875. (call-process "dvipng" nil nil nil
  21876. "-E" "-fg" fg "-bg" bg
  21877. "-D" dpi
  21878. ;;"-x" scale "-y" scale
  21879. "-T" "tight"
  21880. "-o" pngfile
  21881. dvifile)
  21882. (if (not (file-exists-p pngfile))
  21883. (progn (message "Failed to create png file from %s" texfile) nil)
  21884. ;; Use the requested file name and clean up
  21885. (copy-file pngfile tofile 'replace)
  21886. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21887. (delete-file (concat texfilebase e)))
  21888. pngfile))))
  21889. (defun org-dvipng-color (attr)
  21890. "Return an rgb color specification for dvipng."
  21891. (apply 'format "rgb %s %s %s"
  21892. (mapcar 'org-normalize-color
  21893. (color-values (face-attribute 'default attr nil)))))
  21894. (defun org-normalize-color (value)
  21895. "Return string to be used as color value for an RGB component."
  21896. (format "%g" (/ value 65535.0)))
  21897. ;;;; Exporting
  21898. ;;; Variables, constants, and parameter plists
  21899. (defconst org-level-max 20)
  21900. (defvar org-export-html-preamble nil
  21901. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21902. (defvar org-export-html-postamble nil
  21903. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21904. (defvar org-export-html-auto-preamble t
  21905. "Should default preamble be inserted? Set by publishing functions.")
  21906. (defvar org-export-html-auto-postamble t
  21907. "Should default postamble be inserted? Set by publishing functions.")
  21908. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21909. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21910. (defconst org-export-plist-vars
  21911. '((:language . org-export-default-language)
  21912. (:customtime . org-display-custom-times)
  21913. (:headline-levels . org-export-headline-levels)
  21914. (:section-numbers . org-export-with-section-numbers)
  21915. (:table-of-contents . org-export-with-toc)
  21916. (:preserve-breaks . org-export-preserve-breaks)
  21917. (:archived-trees . org-export-with-archived-trees)
  21918. (:emphasize . org-export-with-emphasize)
  21919. (:sub-superscript . org-export-with-sub-superscripts)
  21920. (:special-strings . org-export-with-special-strings)
  21921. (:footnotes . org-export-with-footnotes)
  21922. (:drawers . org-export-with-drawers)
  21923. (:tags . org-export-with-tags)
  21924. (:TeX-macros . org-export-with-TeX-macros)
  21925. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21926. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21927. (:fixed-width . org-export-with-fixed-width)
  21928. (:timestamps . org-export-with-timestamps)
  21929. (:author-info . org-export-author-info)
  21930. (:time-stamp-file . org-export-time-stamp-file)
  21931. (:tables . org-export-with-tables)
  21932. (:table-auto-headline . org-export-highlight-first-table-line)
  21933. (:style . org-export-html-style)
  21934. (:agenda-style . org-agenda-export-html-style)
  21935. (:convert-org-links . org-export-html-link-org-files-as-html)
  21936. (:inline-images . org-export-html-inline-images)
  21937. (:html-extension . org-export-html-extension)
  21938. (:html-table-tag . org-export-html-table-tag)
  21939. (:expand-quoted-html . org-export-html-expand)
  21940. (:timestamp . org-export-html-with-timestamp)
  21941. (:publishing-directory . org-export-publishing-directory)
  21942. (:preamble . org-export-html-preamble)
  21943. (:postamble . org-export-html-postamble)
  21944. (:auto-preamble . org-export-html-auto-preamble)
  21945. (:auto-postamble . org-export-html-auto-postamble)
  21946. (:author . user-full-name)
  21947. (:email . user-mail-address)))
  21948. (defun org-default-export-plist ()
  21949. "Return the property list with default settings for the export variables."
  21950. (let ((l org-export-plist-vars) rtn e)
  21951. (while (setq e (pop l))
  21952. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21953. rtn))
  21954. (defun org-infile-export-plist ()
  21955. "Return the property list with file-local settings for export."
  21956. (save-excursion
  21957. (save-restriction
  21958. (widen)
  21959. (goto-char 0)
  21960. (let ((re (org-make-options-regexp
  21961. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21962. p key val text options)
  21963. (while (re-search-forward re nil t)
  21964. (setq key (org-match-string-no-properties 1)
  21965. val (org-match-string-no-properties 2))
  21966. (cond
  21967. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21968. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21969. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21970. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21971. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21972. ((string-equal key "TEXT")
  21973. (setq text (if text (concat text "\n" val) val)))
  21974. ((string-equal key "OPTIONS") (setq options val))))
  21975. (setq p (plist-put p :text text))
  21976. (when options
  21977. (let ((op '(("H" . :headline-levels)
  21978. ("num" . :section-numbers)
  21979. ("toc" . :table-of-contents)
  21980. ("\\n" . :preserve-breaks)
  21981. ("@" . :expand-quoted-html)
  21982. (":" . :fixed-width)
  21983. ("|" . :tables)
  21984. ("^" . :sub-superscript)
  21985. ("-" . :special-strings)
  21986. ("f" . :footnotes)
  21987. ("d" . :drawers)
  21988. ("tags" . :tags)
  21989. ("*" . :emphasize)
  21990. ("TeX" . :TeX-macros)
  21991. ("LaTeX" . :LaTeX-fragments)
  21992. ("skip" . :skip-before-1st-heading)
  21993. ("author" . :author-info)
  21994. ("timestamp" . :time-stamp-file)))
  21995. o)
  21996. (while (setq o (pop op))
  21997. (if (string-match (concat (regexp-quote (car o))
  21998. ":\\([^ \t\n\r;,.]*\\)")
  21999. options)
  22000. (setq p (plist-put p (cdr o)
  22001. (car (read-from-string
  22002. (match-string 1 options)))))))))
  22003. p))))
  22004. (defun org-export-directory (type plist)
  22005. (let* ((val (plist-get plist :publishing-directory))
  22006. (dir (if (listp val)
  22007. (or (cdr (assoc type val)) ".")
  22008. val)))
  22009. dir))
  22010. (defun org-skip-comments (lines)
  22011. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  22012. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  22013. (re2 "^\\(\\*+\\)[ \t\n\r]")
  22014. (case-fold-search nil)
  22015. rtn line level)
  22016. (while (setq line (pop lines))
  22017. (cond
  22018. ((and (string-match re1 line)
  22019. (setq level (- (match-end 1) (match-beginning 1))))
  22020. ;; Beginning of a COMMENT subtree. Skip it.
  22021. (while (and (setq line (pop lines))
  22022. (or (not (string-match re2 line))
  22023. (> (- (match-end 1) (match-beginning 1)) level))))
  22024. (setq lines (cons line lines)))
  22025. ((string-match "^#" line)
  22026. ;; an ordinary comment line
  22027. )
  22028. ((and org-export-table-remove-special-lines
  22029. (string-match "^[ \t]*|" line)
  22030. (or (string-match "^[ \t]*| *[!_^] *|" line)
  22031. (and (string-match "| *<[0-9]+> *|" line)
  22032. (not (string-match "| *[^ <|]" line)))))
  22033. ;; a special table line that should be removed
  22034. )
  22035. (t (setq rtn (cons line rtn)))))
  22036. (nreverse rtn)))
  22037. (defun org-export (&optional arg)
  22038. (interactive)
  22039. (let ((help "[t] insert the export option template
  22040. \[v] limit export to visible part of outline tree
  22041. \[a] export as ASCII
  22042. \[h] export as HTML
  22043. \[H] export as HTML to temporary buffer
  22044. \[R] export region as HTML
  22045. \[b] export as HTML and browse immediately
  22046. \[x] export as XOXO
  22047. \[l] export as LaTeX
  22048. \[L] export as LaTeX to temporary buffer
  22049. \[i] export current file as iCalendar file
  22050. \[I] export all agenda files as iCalendar files
  22051. \[c] export agenda files into combined iCalendar file
  22052. \[F] publish current file
  22053. \[P] publish current project
  22054. \[X] publish... (project will be prompted for)
  22055. \[A] publish all projects")
  22056. (cmds
  22057. '((?t . org-insert-export-options-template)
  22058. (?v . org-export-visible)
  22059. (?a . org-export-as-ascii)
  22060. (?h . org-export-as-html)
  22061. (?b . org-export-as-html-and-open)
  22062. (?H . org-export-as-html-to-buffer)
  22063. (?R . org-export-region-as-html)
  22064. (?x . org-export-as-xoxo)
  22065. (?l . org-export-as-latex)
  22066. (?L . org-export-as-latex-to-buffer)
  22067. (?i . org-export-icalendar-this-file)
  22068. (?I . org-export-icalendar-all-agenda-files)
  22069. (?c . org-export-icalendar-combine-agenda-files)
  22070. (?F . org-publish-current-file)
  22071. (?P . org-publish-current-project)
  22072. (?X . org-publish)
  22073. (?A . org-publish-all)))
  22074. r1 r2 ass)
  22075. (save-window-excursion
  22076. (delete-other-windows)
  22077. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  22078. (princ help))
  22079. (message "Select command: ")
  22080. (setq r1 (read-char-exclusive)))
  22081. (setq r2 (if (< r1 27) (+ r1 96) r1))
  22082. (if (setq ass (assq r2 cmds))
  22083. (call-interactively (cdr ass))
  22084. (error "No command associated with key %c" r1))))
  22085. (defconst org-html-entities
  22086. '(("nbsp")
  22087. ("iexcl")
  22088. ("cent")
  22089. ("pound")
  22090. ("curren")
  22091. ("yen")
  22092. ("brvbar")
  22093. ("vert" . "&#124;")
  22094. ("sect")
  22095. ("uml")
  22096. ("copy")
  22097. ("ordf")
  22098. ("laquo")
  22099. ("not")
  22100. ("shy")
  22101. ("reg")
  22102. ("macr")
  22103. ("deg")
  22104. ("plusmn")
  22105. ("sup2")
  22106. ("sup3")
  22107. ("acute")
  22108. ("micro")
  22109. ("para")
  22110. ("middot")
  22111. ("odot"."o")
  22112. ("star"."*")
  22113. ("cedil")
  22114. ("sup1")
  22115. ("ordm")
  22116. ("raquo")
  22117. ("frac14")
  22118. ("frac12")
  22119. ("frac34")
  22120. ("iquest")
  22121. ("Agrave")
  22122. ("Aacute")
  22123. ("Acirc")
  22124. ("Atilde")
  22125. ("Auml")
  22126. ("Aring") ("AA"."&Aring;")
  22127. ("AElig")
  22128. ("Ccedil")
  22129. ("Egrave")
  22130. ("Eacute")
  22131. ("Ecirc")
  22132. ("Euml")
  22133. ("Igrave")
  22134. ("Iacute")
  22135. ("Icirc")
  22136. ("Iuml")
  22137. ("ETH")
  22138. ("Ntilde")
  22139. ("Ograve")
  22140. ("Oacute")
  22141. ("Ocirc")
  22142. ("Otilde")
  22143. ("Ouml")
  22144. ("times")
  22145. ("Oslash")
  22146. ("Ugrave")
  22147. ("Uacute")
  22148. ("Ucirc")
  22149. ("Uuml")
  22150. ("Yacute")
  22151. ("THORN")
  22152. ("szlig")
  22153. ("agrave")
  22154. ("aacute")
  22155. ("acirc")
  22156. ("atilde")
  22157. ("auml")
  22158. ("aring")
  22159. ("aelig")
  22160. ("ccedil")
  22161. ("egrave")
  22162. ("eacute")
  22163. ("ecirc")
  22164. ("euml")
  22165. ("igrave")
  22166. ("iacute")
  22167. ("icirc")
  22168. ("iuml")
  22169. ("eth")
  22170. ("ntilde")
  22171. ("ograve")
  22172. ("oacute")
  22173. ("ocirc")
  22174. ("otilde")
  22175. ("ouml")
  22176. ("divide")
  22177. ("oslash")
  22178. ("ugrave")
  22179. ("uacute")
  22180. ("ucirc")
  22181. ("uuml")
  22182. ("yacute")
  22183. ("thorn")
  22184. ("yuml")
  22185. ("fnof")
  22186. ("Alpha")
  22187. ("Beta")
  22188. ("Gamma")
  22189. ("Delta")
  22190. ("Epsilon")
  22191. ("Zeta")
  22192. ("Eta")
  22193. ("Theta")
  22194. ("Iota")
  22195. ("Kappa")
  22196. ("Lambda")
  22197. ("Mu")
  22198. ("Nu")
  22199. ("Xi")
  22200. ("Omicron")
  22201. ("Pi")
  22202. ("Rho")
  22203. ("Sigma")
  22204. ("Tau")
  22205. ("Upsilon")
  22206. ("Phi")
  22207. ("Chi")
  22208. ("Psi")
  22209. ("Omega")
  22210. ("alpha")
  22211. ("beta")
  22212. ("gamma")
  22213. ("delta")
  22214. ("epsilon")
  22215. ("varepsilon"."&epsilon;")
  22216. ("zeta")
  22217. ("eta")
  22218. ("theta")
  22219. ("iota")
  22220. ("kappa")
  22221. ("lambda")
  22222. ("mu")
  22223. ("nu")
  22224. ("xi")
  22225. ("omicron")
  22226. ("pi")
  22227. ("rho")
  22228. ("sigmaf") ("varsigma"."&sigmaf;")
  22229. ("sigma")
  22230. ("tau")
  22231. ("upsilon")
  22232. ("phi")
  22233. ("chi")
  22234. ("psi")
  22235. ("omega")
  22236. ("thetasym") ("vartheta"."&thetasym;")
  22237. ("upsih")
  22238. ("piv")
  22239. ("bull") ("bullet"."&bull;")
  22240. ("hellip") ("dots"."&hellip;")
  22241. ("prime")
  22242. ("Prime")
  22243. ("oline")
  22244. ("frasl")
  22245. ("weierp")
  22246. ("image")
  22247. ("real")
  22248. ("trade")
  22249. ("alefsym")
  22250. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22251. ("uarr") ("uparrow"."&uarr;")
  22252. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22253. ("darr")("downarrow"."&darr;")
  22254. ("harr") ("leftrightarrow"."&harr;")
  22255. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22256. ("lArr") ("Leftarrow"."&lArr;")
  22257. ("uArr") ("Uparrow"."&uArr;")
  22258. ("rArr") ("Rightarrow"."&rArr;")
  22259. ("dArr") ("Downarrow"."&dArr;")
  22260. ("hArr") ("Leftrightarrow"."&hArr;")
  22261. ("forall")
  22262. ("part") ("partial"."&part;")
  22263. ("exist") ("exists"."&exist;")
  22264. ("empty") ("emptyset"."&empty;")
  22265. ("nabla")
  22266. ("isin") ("in"."&isin;")
  22267. ("notin")
  22268. ("ni")
  22269. ("prod")
  22270. ("sum")
  22271. ("minus")
  22272. ("lowast") ("ast"."&lowast;")
  22273. ("radic")
  22274. ("prop") ("proptp"."&prop;")
  22275. ("infin") ("infty"."&infin;")
  22276. ("ang") ("angle"."&ang;")
  22277. ("and") ("wedge"."&and;")
  22278. ("or") ("vee"."&or;")
  22279. ("cap")
  22280. ("cup")
  22281. ("int")
  22282. ("there4")
  22283. ("sim")
  22284. ("cong") ("simeq"."&cong;")
  22285. ("asymp")("approx"."&asymp;")
  22286. ("ne") ("neq"."&ne;")
  22287. ("equiv")
  22288. ("le")
  22289. ("ge")
  22290. ("sub") ("subset"."&sub;")
  22291. ("sup") ("supset"."&sup;")
  22292. ("nsub")
  22293. ("sube")
  22294. ("supe")
  22295. ("oplus")
  22296. ("otimes")
  22297. ("perp")
  22298. ("sdot") ("cdot"."&sdot;")
  22299. ("lceil")
  22300. ("rceil")
  22301. ("lfloor")
  22302. ("rfloor")
  22303. ("lang")
  22304. ("rang")
  22305. ("loz") ("Diamond"."&loz;")
  22306. ("spades") ("spadesuit"."&spades;")
  22307. ("clubs") ("clubsuit"."&clubs;")
  22308. ("hearts") ("diamondsuit"."&hearts;")
  22309. ("diams") ("diamondsuit"."&diams;")
  22310. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22311. ("quot")
  22312. ("amp")
  22313. ("lt")
  22314. ("gt")
  22315. ("OElig")
  22316. ("oelig")
  22317. ("Scaron")
  22318. ("scaron")
  22319. ("Yuml")
  22320. ("circ")
  22321. ("tilde")
  22322. ("ensp")
  22323. ("emsp")
  22324. ("thinsp")
  22325. ("zwnj")
  22326. ("zwj")
  22327. ("lrm")
  22328. ("rlm")
  22329. ("ndash")
  22330. ("mdash")
  22331. ("lsquo")
  22332. ("rsquo")
  22333. ("sbquo")
  22334. ("ldquo")
  22335. ("rdquo")
  22336. ("bdquo")
  22337. ("dagger")
  22338. ("Dagger")
  22339. ("permil")
  22340. ("lsaquo")
  22341. ("rsaquo")
  22342. ("euro")
  22343. ("arccos"."arccos")
  22344. ("arcsin"."arcsin")
  22345. ("arctan"."arctan")
  22346. ("arg"."arg")
  22347. ("cos"."cos")
  22348. ("cosh"."cosh")
  22349. ("cot"."cot")
  22350. ("coth"."coth")
  22351. ("csc"."csc")
  22352. ("deg"."deg")
  22353. ("det"."det")
  22354. ("dim"."dim")
  22355. ("exp"."exp")
  22356. ("gcd"."gcd")
  22357. ("hom"."hom")
  22358. ("inf"."inf")
  22359. ("ker"."ker")
  22360. ("lg"."lg")
  22361. ("lim"."lim")
  22362. ("liminf"."liminf")
  22363. ("limsup"."limsup")
  22364. ("ln"."ln")
  22365. ("log"."log")
  22366. ("max"."max")
  22367. ("min"."min")
  22368. ("Pr"."Pr")
  22369. ("sec"."sec")
  22370. ("sin"."sin")
  22371. ("sinh"."sinh")
  22372. ("sup"."sup")
  22373. ("tan"."tan")
  22374. ("tanh"."tanh")
  22375. )
  22376. "Entities for TeX->HTML translation.
  22377. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22378. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22379. In that case, \"\\ent\" will be translated to \"&other;\".
  22380. The list contains HTML entities for Latin-1, Greek and other symbols.
  22381. It is supplemented by a number of commonly used TeX macros with appropriate
  22382. translations. There is currently no way for users to extend this.")
  22383. ;;; General functions for all backends
  22384. (defun org-cleaned-string-for-export (string &rest parameters)
  22385. "Cleanup a buffer STRING so that links can be created safely."
  22386. (interactive)
  22387. (let* ((re-radio (and org-target-link-regexp
  22388. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22389. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22390. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22391. (re-archive (concat ":" org-archive-tag ":"))
  22392. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22393. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22394. (htmlp (plist-get parameters :for-html))
  22395. (asciip (plist-get parameters :for-ascii))
  22396. (latexp (plist-get parameters :for-LaTeX))
  22397. (commentsp (plist-get parameters :comments))
  22398. (archived-trees (plist-get parameters :archived-trees))
  22399. (inhibit-read-only t)
  22400. (drawers org-drawers)
  22401. (exp-drawers (plist-get parameters :drawers))
  22402. (outline-regexp "\\*+ ")
  22403. a b xx
  22404. rtn p)
  22405. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22406. (erase-buffer)
  22407. (insert string)
  22408. ;; Remove license-to-kill stuff
  22409. (while (setq p (text-property-any (point-min) (point-max)
  22410. :org-license-to-kill t))
  22411. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22412. (let ((org-inhibit-startup t)) (org-mode))
  22413. (untabify (point-min) (point-max))
  22414. ;; Get rid of drawers
  22415. (unless (eq t exp-drawers)
  22416. (goto-char (point-min))
  22417. (let ((re (concat "^[ \t]*:\\("
  22418. (mapconcat
  22419. 'identity
  22420. (org-delete-all exp-drawers
  22421. (copy-sequence drawers))
  22422. "\\|")
  22423. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22424. (while (re-search-forward re nil t)
  22425. (replace-match ""))))
  22426. ;; Get the correct stuff before the first headline
  22427. (when (plist-get parameters :skip-before-1st-heading)
  22428. (goto-char (point-min))
  22429. (when (re-search-forward "^\\*+[ \t]" nil t)
  22430. (delete-region (point-min) (match-beginning 0))
  22431. (goto-char (point-min))
  22432. (insert "\n")))
  22433. (when (plist-get parameters :add-text)
  22434. (goto-char (point-min))
  22435. (insert (plist-get parameters :add-text) "\n"))
  22436. ;; Get rid of archived trees
  22437. (when (not (eq archived-trees t))
  22438. (goto-char (point-min))
  22439. (while (re-search-forward re-archive nil t)
  22440. (if (not (org-on-heading-p t))
  22441. (org-end-of-subtree t)
  22442. (beginning-of-line 1)
  22443. (setq a (if archived-trees
  22444. (1+ (point-at-eol)) (point))
  22445. b (org-end-of-subtree t))
  22446. (if (> b a) (delete-region a b)))))
  22447. ;; Find targets in comments and move them out of comments,
  22448. ;; but mark them as targets that should be invisible
  22449. (goto-char (point-min))
  22450. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22451. (replace-match "\\1(INVISIBLE)"))
  22452. ;; Protect backend specific stuff, throw away the others.
  22453. (let ((formatters
  22454. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22455. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22456. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22457. fmt)
  22458. (goto-char (point-min))
  22459. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22460. (goto-char (match-end 0))
  22461. (while (not (looking-at "#\\+END_EXAMPLE"))
  22462. (insert ": ")
  22463. (beginning-of-line 2)))
  22464. (goto-char (point-min))
  22465. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22466. (add-text-properties (match-beginning 0) (match-end 0)
  22467. '(org-protected t)))
  22468. (while formatters
  22469. (setq fmt (pop formatters))
  22470. (when (car fmt)
  22471. (goto-char (point-min))
  22472. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22473. ":[ \t]*\\(.*\\)") nil t)
  22474. (replace-match "\\1" t)
  22475. (add-text-properties
  22476. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22477. '(org-protected t))))
  22478. (goto-char (point-min))
  22479. (while (re-search-forward
  22480. (concat "^#\\+"
  22481. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22482. (cadddr fmt) "\\>.*\n?") nil t)
  22483. (if (car fmt)
  22484. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22485. '(org-protected t))
  22486. (delete-region (match-beginning 0) (match-end 0))))))
  22487. ;; Protect quoted subtrees
  22488. (goto-char (point-min))
  22489. (while (re-search-forward re-quote nil t)
  22490. (goto-char (match-beginning 0))
  22491. (end-of-line 1)
  22492. (add-text-properties (point) (org-end-of-subtree t)
  22493. '(org-protected t)))
  22494. ;; Protect verbatim elements
  22495. (goto-char (point-min))
  22496. (while (re-search-forward org-verbatim-re nil t)
  22497. (add-text-properties (match-beginning 4) (match-end 4)
  22498. '(org-protected t))
  22499. (goto-char (1+ (match-end 4))))
  22500. ;; Remove subtrees that are commented
  22501. (goto-char (point-min))
  22502. (while (re-search-forward re-commented nil t)
  22503. (goto-char (match-beginning 0))
  22504. (delete-region (point) (org-end-of-subtree t)))
  22505. ;; Remove special table lines
  22506. (when org-export-table-remove-special-lines
  22507. (goto-char (point-min))
  22508. (while (re-search-forward "^[ \t]*|" nil t)
  22509. (beginning-of-line 1)
  22510. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22511. (and (looking-at ".*?| *<[0-9]+> *|")
  22512. (not (looking-at ".*?| *[^ <|]"))))
  22513. (delete-region (max (point-min) (1- (point-at-bol)))
  22514. (point-at-eol))
  22515. (end-of-line 1))))
  22516. ;; Specific LaTeX stuff
  22517. (when latexp
  22518. (require 'org-export-latex nil)
  22519. (org-export-latex-cleaned-string))
  22520. (when asciip
  22521. (org-export-ascii-clean-string))
  22522. ;; Specific HTML stuff
  22523. (when htmlp
  22524. ;; Convert LaTeX fragments to images
  22525. (when (plist-get parameters :LaTeX-fragments)
  22526. (org-format-latex
  22527. (concat "ltxpng/" (file-name-sans-extension
  22528. (file-name-nondirectory
  22529. org-current-export-file)))
  22530. org-current-export-dir nil "Creating LaTeX image %s"))
  22531. (message "Exporting..."))
  22532. ;; Remove or replace comments
  22533. (goto-char (point-min))
  22534. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22535. (if commentsp
  22536. (progn (add-text-properties
  22537. (match-beginning 0) (match-end 0) '(org-protected t))
  22538. (replace-match (format commentsp (match-string 1)) t t))
  22539. (replace-match "")))
  22540. ;; Find matches for radio targets and turn them into internal links
  22541. (goto-char (point-min))
  22542. (when re-radio
  22543. (while (re-search-forward re-radio nil t)
  22544. (org-if-unprotected
  22545. (replace-match "\\1[[\\2]]"))))
  22546. ;; Find all links that contain a newline and put them into a single line
  22547. (goto-char (point-min))
  22548. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22549. (org-if-unprotected
  22550. (replace-match "\\1 \\3")
  22551. (goto-char (match-beginning 0))))
  22552. ;; Normalize links: Convert angle and plain links into bracket links
  22553. ;; Expand link abbreviations
  22554. (goto-char (point-min))
  22555. (while (re-search-forward re-plain-link nil t)
  22556. (goto-char (1- (match-end 0)))
  22557. (org-if-unprotected
  22558. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22559. ":" (match-string 3) "]]")))
  22560. ;; added 'org-link face to links
  22561. (put-text-property 0 (length s) 'face 'org-link s)
  22562. (replace-match s t t))))
  22563. (goto-char (point-min))
  22564. (while (re-search-forward re-angle-link nil t)
  22565. (goto-char (1- (match-end 0)))
  22566. (org-if-unprotected
  22567. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22568. ":" (match-string 3) "]]")))
  22569. (put-text-property 0 (length s) 'face 'org-link s)
  22570. (replace-match s t t))))
  22571. (goto-char (point-min))
  22572. (while (re-search-forward org-bracket-link-regexp nil t)
  22573. (org-if-unprotected
  22574. (let* ((s (concat "[[" (setq xx (save-match-data
  22575. (org-link-expand-abbrev (match-string 1))))
  22576. "]"
  22577. (if (match-end 3)
  22578. (match-string 2)
  22579. (concat "[" xx "]"))
  22580. "]")))
  22581. (put-text-property 0 (length s) 'face 'org-link s)
  22582. (replace-match s t t))))
  22583. ;; Find multiline emphasis and put them into single line
  22584. (when (plist-get parameters :emph-multiline)
  22585. (goto-char (point-min))
  22586. (while (re-search-forward org-emph-re nil t)
  22587. (if (not (= (char-after (match-beginning 3))
  22588. (char-after (match-beginning 4))))
  22589. (org-if-unprotected
  22590. (subst-char-in-region (match-beginning 0) (match-end 0)
  22591. ?\n ?\ t)
  22592. (goto-char (1- (match-end 0))))
  22593. (goto-char (1+ (match-beginning 0))))))
  22594. (setq rtn (buffer-string)))
  22595. (kill-buffer " org-mode-tmp")
  22596. rtn))
  22597. (defun org-export-grab-title-from-buffer ()
  22598. "Get a title for the current document, from looking at the buffer."
  22599. (let ((inhibit-read-only t))
  22600. (save-excursion
  22601. (goto-char (point-min))
  22602. (let ((end (save-excursion (outline-next-heading) (point))))
  22603. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22604. ;; Mark the line so that it will not be exported as normal text.
  22605. (org-unmodified
  22606. (add-text-properties (match-beginning 0) (match-end 0)
  22607. (list :org-license-to-kill t)))
  22608. ;; Return the title string
  22609. (org-trim (match-string 0)))))))
  22610. (defun org-export-get-title-from-subtree ()
  22611. "Return subtree title and exclude it from export."
  22612. (let (title (m (mark)))
  22613. (save-excursion
  22614. (goto-char (region-beginning))
  22615. (when (and (org-at-heading-p)
  22616. (>= (org-end-of-subtree t t) (region-end)))
  22617. ;; This is a subtree, we take the title from the first heading
  22618. (goto-char (region-beginning))
  22619. (looking-at org-todo-line-regexp)
  22620. (setq title (match-string 3))
  22621. (org-unmodified
  22622. (add-text-properties (point) (1+ (point-at-eol))
  22623. (list :org-license-to-kill t)))))
  22624. title))
  22625. (defun org-solidify-link-text (s &optional alist)
  22626. "Take link text and make a safe target out of it."
  22627. (save-match-data
  22628. (let* ((rtn
  22629. (mapconcat
  22630. 'identity
  22631. (org-split-string s "[ \t\r\n]+") "--"))
  22632. (a (assoc rtn alist)))
  22633. (or (cdr a) rtn))))
  22634. (defun org-get-min-level (lines)
  22635. "Get the minimum level in LINES."
  22636. (let ((re "^\\(\\*+\\) ") l min)
  22637. (catch 'exit
  22638. (while (setq l (pop lines))
  22639. (if (string-match re l)
  22640. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22641. 1)))
  22642. ;; Variable holding the vector with section numbers
  22643. (defvar org-section-numbers (make-vector org-level-max 0))
  22644. (defun org-init-section-numbers ()
  22645. "Initialize the vector for the section numbers."
  22646. (let* ((level -1)
  22647. (numbers (nreverse (org-split-string "" "\\.")))
  22648. (depth (1- (length org-section-numbers)))
  22649. (i depth) number-string)
  22650. (while (>= i 0)
  22651. (if (> i level)
  22652. (aset org-section-numbers i 0)
  22653. (setq number-string (or (car numbers) "0"))
  22654. (if (string-match "\\`[A-Z]\\'" number-string)
  22655. (aset org-section-numbers i
  22656. (- (string-to-char number-string) ?A -1))
  22657. (aset org-section-numbers i (string-to-number number-string)))
  22658. (pop numbers))
  22659. (setq i (1- i)))))
  22660. (defun org-section-number (&optional level)
  22661. "Return a string with the current section number.
  22662. When LEVEL is non-nil, increase section numbers on that level."
  22663. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22664. (when level
  22665. (when (> level -1)
  22666. (aset org-section-numbers
  22667. level (1+ (aref org-section-numbers level))))
  22668. (setq idx (1+ level))
  22669. (while (<= idx depth)
  22670. (if (not (= idx 1))
  22671. (aset org-section-numbers idx 0))
  22672. (setq idx (1+ idx))))
  22673. (setq idx 0)
  22674. (while (<= idx depth)
  22675. (setq n (aref org-section-numbers idx))
  22676. (setq string (concat string (if (not (string= string "")) "." "")
  22677. (int-to-string n)))
  22678. (setq idx (1+ idx)))
  22679. (save-match-data
  22680. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22681. (setq string (replace-match "" t nil string)))
  22682. (if (string-match "\\(\\.0\\)+\\'" string)
  22683. (setq string (replace-match "" t nil string))))
  22684. string))
  22685. ;;; ASCII export
  22686. (defvar org-last-level nil) ; dynamically scoped variable
  22687. (defvar org-min-level nil) ; dynamically scoped variable
  22688. (defvar org-levels-open nil) ; dynamically scoped parameter
  22689. (defvar org-ascii-current-indentation nil) ; For communication
  22690. (defun org-export-as-ascii (arg)
  22691. "Export the outline as a pretty ASCII file.
  22692. If there is an active region, export only the region.
  22693. The prefix ARG specifies how many levels of the outline should become
  22694. underlined headlines. The default is 3."
  22695. (interactive "P")
  22696. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22697. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22698. (org-infile-export-plist)))
  22699. (region-p (org-region-active-p))
  22700. (subtree-p
  22701. (when region-p
  22702. (save-excursion
  22703. (goto-char (region-beginning))
  22704. (and (org-at-heading-p)
  22705. (>= (org-end-of-subtree t t) (region-end))))))
  22706. (custom-times org-display-custom-times)
  22707. (org-ascii-current-indentation '(0 . 0))
  22708. (level 0) line txt
  22709. (umax nil)
  22710. (umax-toc nil)
  22711. (case-fold-search nil)
  22712. (filename (concat (file-name-as-directory
  22713. (org-export-directory :ascii opt-plist))
  22714. (file-name-sans-extension
  22715. (or (and subtree-p
  22716. (org-entry-get (region-beginning)
  22717. "EXPORT_FILE_NAME" t))
  22718. (file-name-nondirectory buffer-file-name)))
  22719. ".txt"))
  22720. (filename (if (equal (file-truename filename)
  22721. (file-truename buffer-file-name))
  22722. (concat filename ".txt")
  22723. filename))
  22724. (buffer (find-file-noselect filename))
  22725. (org-levels-open (make-vector org-level-max nil))
  22726. (odd org-odd-levels-only)
  22727. (date (plist-get opt-plist :date))
  22728. (author (plist-get opt-plist :author))
  22729. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22730. (plist-get opt-plist :title)
  22731. (and (not
  22732. (plist-get opt-plist :skip-before-1st-heading))
  22733. (org-export-grab-title-from-buffer))
  22734. (file-name-sans-extension
  22735. (file-name-nondirectory buffer-file-name))))
  22736. (email (plist-get opt-plist :email))
  22737. (language (plist-get opt-plist :language))
  22738. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22739. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22740. (todo nil)
  22741. (lang-words nil)
  22742. (region
  22743. (buffer-substring
  22744. (if (org-region-active-p) (region-beginning) (point-min))
  22745. (if (org-region-active-p) (region-end) (point-max))))
  22746. (lines (org-split-string
  22747. (org-cleaned-string-for-export
  22748. region
  22749. :for-ascii t
  22750. :skip-before-1st-heading
  22751. (plist-get opt-plist :skip-before-1st-heading)
  22752. :drawers (plist-get opt-plist :drawers)
  22753. :verbatim-multiline t
  22754. :archived-trees
  22755. (plist-get opt-plist :archived-trees)
  22756. :add-text (plist-get opt-plist :text))
  22757. "\n"))
  22758. thetoc have-headings first-heading-pos
  22759. table-open table-buffer)
  22760. (let ((inhibit-read-only t))
  22761. (org-unmodified
  22762. (remove-text-properties (point-min) (point-max)
  22763. '(:org-license-to-kill t))))
  22764. (setq org-min-level (org-get-min-level lines))
  22765. (setq org-last-level org-min-level)
  22766. (org-init-section-numbers)
  22767. (find-file-noselect filename)
  22768. (setq lang-words (or (assoc language org-export-language-setup)
  22769. (assoc "en" org-export-language-setup)))
  22770. (switch-to-buffer-other-window buffer)
  22771. (erase-buffer)
  22772. (fundamental-mode)
  22773. ;; create local variables for all options, to make sure all called
  22774. ;; functions get the correct information
  22775. (mapc (lambda (x)
  22776. (set (make-local-variable (cdr x))
  22777. (plist-get opt-plist (car x))))
  22778. org-export-plist-vars)
  22779. (org-set-local 'org-odd-levels-only odd)
  22780. (setq umax (if arg (prefix-numeric-value arg)
  22781. org-export-headline-levels))
  22782. (setq umax-toc (if (integerp org-export-with-toc)
  22783. (min org-export-with-toc umax)
  22784. umax))
  22785. ;; File header
  22786. (if title (org-insert-centered title ?=))
  22787. (insert "\n")
  22788. (if (and (or author email)
  22789. org-export-author-info)
  22790. (insert (concat (nth 1 lang-words) ": " (or author "")
  22791. (if email (concat " <" email ">") "")
  22792. "\n")))
  22793. (cond
  22794. ((and date (string-match "%" date))
  22795. (setq date (format-time-string date (current-time))))
  22796. (date)
  22797. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22798. (if (and date org-export-time-stamp-file)
  22799. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22800. (insert "\n\n")
  22801. (if org-export-with-toc
  22802. (progn
  22803. (push (concat (nth 3 lang-words) "\n") thetoc)
  22804. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22805. (mapc '(lambda (line)
  22806. (if (string-match org-todo-line-regexp
  22807. line)
  22808. ;; This is a headline
  22809. (progn
  22810. (setq have-headings t)
  22811. (setq level (- (match-end 1) (match-beginning 1))
  22812. level (org-tr-level level)
  22813. txt (match-string 3 line)
  22814. todo
  22815. (or (and org-export-mark-todo-in-toc
  22816. (match-beginning 2)
  22817. (not (member (match-string 2 line)
  22818. org-done-keywords)))
  22819. ; TODO, not DONE
  22820. (and org-export-mark-todo-in-toc
  22821. (= level umax-toc)
  22822. (org-search-todo-below
  22823. line lines level))))
  22824. (setq txt (org-html-expand-for-ascii txt))
  22825. (while (string-match org-bracket-link-regexp txt)
  22826. (setq txt
  22827. (replace-match
  22828. (match-string (if (match-end 2) 3 1) txt)
  22829. t t txt)))
  22830. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22831. (string-match
  22832. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22833. txt))
  22834. (setq txt (replace-match "" t t txt)))
  22835. (if (string-match quote-re0 txt)
  22836. (setq txt (replace-match "" t t txt)))
  22837. (if org-export-with-section-numbers
  22838. (setq txt (concat (org-section-number level)
  22839. " " txt)))
  22840. (if (<= level umax-toc)
  22841. (progn
  22842. (push
  22843. (concat
  22844. (make-string
  22845. (* (max 0 (- level org-min-level)) 4) ?\ )
  22846. (format (if todo "%s (*)\n" "%s\n") txt))
  22847. thetoc)
  22848. (setq org-last-level level))
  22849. ))))
  22850. lines)
  22851. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22852. (org-init-section-numbers)
  22853. (while (setq line (pop lines))
  22854. ;; Remove the quoted HTML tags.
  22855. (setq line (org-html-expand-for-ascii line))
  22856. ;; Remove targets
  22857. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22858. (setq line (replace-match "" t t line)))
  22859. ;; Replace internal links
  22860. (while (string-match org-bracket-link-regexp line)
  22861. (setq line (replace-match
  22862. (if (match-end 3) "[\\3]" "[\\1]")
  22863. t nil line)))
  22864. (when custom-times
  22865. (setq line (org-translate-time line)))
  22866. (cond
  22867. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22868. ;; a Headline
  22869. (setq first-heading-pos (or first-heading-pos (point)))
  22870. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22871. txt (match-string 2 line))
  22872. (org-ascii-level-start level txt umax lines))
  22873. ((and org-export-with-tables
  22874. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22875. (if (not table-open)
  22876. ;; New table starts
  22877. (setq table-open t table-buffer nil))
  22878. ;; Accumulate lines
  22879. (setq table-buffer (cons line table-buffer))
  22880. (when (or (not lines)
  22881. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22882. (car lines))))
  22883. (setq table-open nil
  22884. table-buffer (nreverse table-buffer))
  22885. (insert (mapconcat
  22886. (lambda (x)
  22887. (org-fix-indentation x org-ascii-current-indentation))
  22888. (org-format-table-ascii table-buffer)
  22889. "\n") "\n")))
  22890. (t
  22891. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22892. (if (and org-export-with-fixed-width
  22893. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22894. (setq line (replace-match "\\1" nil nil line)))
  22895. (insert line "\n"))))
  22896. (normal-mode)
  22897. ;; insert the table of contents
  22898. (when thetoc
  22899. (goto-char (point-min))
  22900. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22901. (progn
  22902. (goto-char (match-beginning 0))
  22903. (replace-match ""))
  22904. (goto-char first-heading-pos))
  22905. (mapc 'insert thetoc)
  22906. (or (looking-at "[ \t]*\n[ \t]*\n")
  22907. (insert "\n\n")))
  22908. ;; Convert whitespace place holders
  22909. (goto-char (point-min))
  22910. (let (beg end)
  22911. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22912. (setq end (next-single-property-change beg 'org-whitespace))
  22913. (goto-char beg)
  22914. (delete-region beg end)
  22915. (insert (make-string (- end beg) ?\ ))))
  22916. (save-buffer)
  22917. ;; remove display and invisible chars
  22918. (let (beg end)
  22919. (goto-char (point-min))
  22920. (while (setq beg (next-single-property-change (point) 'display))
  22921. (setq end (next-single-property-change beg 'display))
  22922. (delete-region beg end)
  22923. (goto-char beg)
  22924. (insert "=>"))
  22925. (goto-char (point-min))
  22926. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22927. (setq end (next-single-property-change beg 'org-cwidth))
  22928. (delete-region beg end)
  22929. (goto-char beg)))
  22930. (goto-char (point-min))))
  22931. (defun org-export-ascii-clean-string ()
  22932. "Do extra work for ASCII export"
  22933. (goto-char (point-min))
  22934. (while (re-search-forward org-verbatim-re nil t)
  22935. (goto-char (match-end 2))
  22936. (backward-delete-char 1) (insert "'")
  22937. (goto-char (match-beginning 2))
  22938. (delete-char 1) (insert "`")
  22939. (goto-char (match-end 2))))
  22940. (defun org-search-todo-below (line lines level)
  22941. "Search the subtree below LINE for any TODO entries."
  22942. (let ((rest (cdr (memq line lines)))
  22943. (re org-todo-line-regexp)
  22944. line lv todo)
  22945. (catch 'exit
  22946. (while (setq line (pop rest))
  22947. (if (string-match re line)
  22948. (progn
  22949. (setq lv (- (match-end 1) (match-beginning 1))
  22950. todo (and (match-beginning 2)
  22951. (not (member (match-string 2 line)
  22952. org-done-keywords))))
  22953. ; TODO, not DONE
  22954. (if (<= lv level) (throw 'exit nil))
  22955. (if todo (throw 'exit t))))))))
  22956. (defun org-html-expand-for-ascii (line)
  22957. "Handle quoted HTML for ASCII export."
  22958. (if org-export-html-expand
  22959. (while (string-match "@<[^<>\n]*>" line)
  22960. ;; We just remove the tags for now.
  22961. (setq line (replace-match "" nil nil line))))
  22962. line)
  22963. (defun org-insert-centered (s &optional underline)
  22964. "Insert the string S centered and underline it with character UNDERLINE."
  22965. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22966. (insert (make-string ind ?\ ) s "\n")
  22967. (if underline
  22968. (insert (make-string ind ?\ )
  22969. (make-string (string-width s) underline)
  22970. "\n"))))
  22971. (defun org-ascii-level-start (level title umax &optional lines)
  22972. "Insert a new level in ASCII export."
  22973. (let (char (n (- level umax 1)) (ind 0))
  22974. (if (> level umax)
  22975. (progn
  22976. (insert (make-string (* 2 n) ?\ )
  22977. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22978. org-export-ascii-bullets))
  22979. " " title "\n")
  22980. ;; find the indentation of the next non-empty line
  22981. (catch 'stop
  22982. (while lines
  22983. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22984. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22985. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22986. (pop lines)))
  22987. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22988. (if (or (not (equal (char-before) ?\n))
  22989. (not (equal (char-before (1- (point))) ?\n)))
  22990. (insert "\n"))
  22991. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22992. (unless org-export-with-tags
  22993. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22994. (setq title (replace-match "" t t title))))
  22995. (if org-export-with-section-numbers
  22996. (setq title (concat (org-section-number level) " " title)))
  22997. (insert title "\n" (make-string (string-width title) char) "\n")
  22998. (setq org-ascii-current-indentation '(0 . 0)))))
  22999. (defun org-export-visible (type arg)
  23000. "Create a copy of the visible part of the current buffer, and export it.
  23001. The copy is created in a temporary buffer and removed after use.
  23002. TYPE is the final key (as a string) that also select the export command in
  23003. the `C-c C-e' export dispatcher.
  23004. As a special case, if the you type SPC at the prompt, the temporary
  23005. org-mode file will not be removed but presented to you so that you can
  23006. continue to use it. The prefix arg ARG is passed through to the exporting
  23007. command."
  23008. (interactive
  23009. (list (progn
  23010. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  23011. (read-char-exclusive))
  23012. current-prefix-arg))
  23013. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  23014. (error "Invalid export key"))
  23015. (let* ((binding (cdr (assoc type
  23016. '((?a . org-export-as-ascii)
  23017. (?\C-a . org-export-as-ascii)
  23018. (?b . org-export-as-html-and-open)
  23019. (?\C-b . org-export-as-html-and-open)
  23020. (?h . org-export-as-html)
  23021. (?H . org-export-as-html-to-buffer)
  23022. (?R . org-export-region-as-html)
  23023. (?x . org-export-as-xoxo)))))
  23024. (keepp (equal type ?\ ))
  23025. (file buffer-file-name)
  23026. (buffer (get-buffer-create "*Org Export Visible*"))
  23027. s e)
  23028. ;; Need to hack the drawers here.
  23029. (save-excursion
  23030. (goto-char (point-min))
  23031. (while (re-search-forward org-drawer-regexp nil t)
  23032. (goto-char (match-beginning 1))
  23033. (or (org-invisible-p) (org-flag-drawer nil))))
  23034. (with-current-buffer buffer (erase-buffer))
  23035. (save-excursion
  23036. (setq s (goto-char (point-min)))
  23037. (while (not (= (point) (point-max)))
  23038. (goto-char (org-find-invisible))
  23039. (append-to-buffer buffer s (point))
  23040. (setq s (goto-char (org-find-visible))))
  23041. (org-cycle-hide-drawers 'all)
  23042. (goto-char (point-min))
  23043. (unless keepp
  23044. ;; Copy all comment lines to the end, to make sure #+ settings are
  23045. ;; still available for the second export step. Kind of a hack, but
  23046. ;; does do the trick.
  23047. (if (looking-at "#[^\r\n]*")
  23048. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  23049. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  23050. (append-to-buffer buffer (1+ (match-beginning 0))
  23051. (min (point-max) (1+ (match-end 0))))))
  23052. (set-buffer buffer)
  23053. (let ((buffer-file-name file)
  23054. (org-inhibit-startup t))
  23055. (org-mode)
  23056. (show-all)
  23057. (unless keepp (funcall binding arg))))
  23058. (if (not keepp)
  23059. (kill-buffer buffer)
  23060. (switch-to-buffer-other-window buffer)
  23061. (goto-char (point-min)))))
  23062. (defun org-find-visible ()
  23063. (let ((s (point)))
  23064. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23065. (get-char-property s 'invisible)))
  23066. s))
  23067. (defun org-find-invisible ()
  23068. (let ((s (point)))
  23069. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23070. (not (get-char-property s 'invisible))))
  23071. s))
  23072. ;;; HTML export
  23073. (defun org-get-current-options ()
  23074. "Return a string with current options as keyword options.
  23075. Does include HTML export options as well as TODO and CATEGORY stuff."
  23076. (format
  23077. "#+TITLE: %s
  23078. #+AUTHOR: %s
  23079. #+EMAIL: %s
  23080. #+LANGUAGE: %s
  23081. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  23082. #+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
  23083. #+CATEGORY: %s
  23084. #+SEQ_TODO: %s
  23085. #+TYP_TODO: %s
  23086. #+PRIORITIES: %c %c %c
  23087. #+DRAWERS: %s
  23088. #+STARTUP: %s %s %s %s %s
  23089. #+TAGS: %s
  23090. #+ARCHIVE: %s
  23091. #+LINK: %s
  23092. "
  23093. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  23094. org-export-headline-levels
  23095. org-export-with-section-numbers
  23096. org-export-with-toc
  23097. org-export-preserve-breaks
  23098. org-export-html-expand
  23099. org-export-with-fixed-width
  23100. org-export-with-tables
  23101. org-export-with-sub-superscripts
  23102. org-export-with-special-strings
  23103. org-export-with-footnotes
  23104. org-export-with-emphasize
  23105. org-export-with-TeX-macros
  23106. org-export-with-LaTeX-fragments
  23107. org-export-skip-text-before-1st-heading
  23108. org-export-with-drawers
  23109. org-export-with-tags
  23110. (file-name-nondirectory buffer-file-name)
  23111. "TODO FEEDBACK VERIFY DONE"
  23112. "Me Jason Marie DONE"
  23113. org-highest-priority org-lowest-priority org-default-priority
  23114. (mapconcat 'identity org-drawers " ")
  23115. (cdr (assoc org-startup-folded
  23116. '((nil . "showall") (t . "overview") (content . "content"))))
  23117. (if org-odd-levels-only "odd" "oddeven")
  23118. (if org-hide-leading-stars "hidestars" "showstars")
  23119. (if org-startup-align-all-tables "align" "noalign")
  23120. (cond ((eq org-log-done t) "logdone")
  23121. ((equal org-log-done 'note) "lognotedone")
  23122. ((not org-log-done) "nologdone"))
  23123. (or (mapconcat (lambda (x)
  23124. (cond
  23125. ((equal '(:startgroup) x) "{")
  23126. ((equal '(:endgroup) x) "}")
  23127. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  23128. (t (car x))))
  23129. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  23130. org-archive-location
  23131. "org file:~/org/%s.org"
  23132. ))
  23133. (defun org-insert-export-options-template ()
  23134. "Insert into the buffer a template with information for exporting."
  23135. (interactive)
  23136. (if (not (bolp)) (newline))
  23137. (let ((s (org-get-current-options)))
  23138. (and (string-match "#\\+CATEGORY" s)
  23139. (setq s (substring s 0 (match-beginning 0))))
  23140. (insert s)))
  23141. (defun org-toggle-fixed-width-section (arg)
  23142. "Toggle the fixed-width export.
  23143. If there is no active region, the QUOTE keyword at the current headline is
  23144. inserted or removed. When present, it causes the text between this headline
  23145. and the next to be exported as fixed-width text, and unmodified.
  23146. If there is an active region, this command adds or removes a colon as the
  23147. first character of this line. If the first character of a line is a colon,
  23148. this line is also exported in fixed-width font."
  23149. (interactive "P")
  23150. (let* ((cc 0)
  23151. (regionp (org-region-active-p))
  23152. (beg (if regionp (region-beginning) (point)))
  23153. (end (if regionp (region-end)))
  23154. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  23155. (case-fold-search nil)
  23156. (re "[ \t]*\\(:\\)")
  23157. off)
  23158. (if regionp
  23159. (save-excursion
  23160. (goto-char beg)
  23161. (setq cc (current-column))
  23162. (beginning-of-line 1)
  23163. (setq off (looking-at re))
  23164. (while (> nlines 0)
  23165. (setq nlines (1- nlines))
  23166. (beginning-of-line 1)
  23167. (cond
  23168. (arg
  23169. (move-to-column cc t)
  23170. (insert ":\n")
  23171. (forward-line -1))
  23172. ((and off (looking-at re))
  23173. (replace-match "" t t nil 1))
  23174. ((not off) (move-to-column cc t) (insert ":")))
  23175. (forward-line 1)))
  23176. (save-excursion
  23177. (org-back-to-heading)
  23178. (if (looking-at (concat outline-regexp
  23179. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23180. (replace-match "" t t nil 1)
  23181. (if (looking-at outline-regexp)
  23182. (progn
  23183. (goto-char (match-end 0))
  23184. (insert org-quote-string " "))))))))
  23185. (defun org-export-as-html-and-open (arg)
  23186. "Export the outline as HTML and immediately open it with a browser.
  23187. If there is an active region, export only the region.
  23188. The prefix ARG specifies how many levels of the outline should become
  23189. headlines. The default is 3. Lower levels will become bulleted lists."
  23190. (interactive "P")
  23191. (org-export-as-html arg 'hidden)
  23192. (org-open-file buffer-file-name))
  23193. (defun org-export-as-html-batch ()
  23194. "Call `org-export-as-html', may be used in batch processing as
  23195. emacs --batch
  23196. --load=$HOME/lib/emacs/org.el
  23197. --eval \"(setq org-export-headline-levels 2)\"
  23198. --visit=MyFile --funcall org-export-as-html-batch"
  23199. (org-export-as-html org-export-headline-levels 'hidden))
  23200. (defun org-export-as-html-to-buffer (arg)
  23201. "Call `org-exort-as-html` with output to a temporary buffer.
  23202. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23203. (interactive "P")
  23204. (org-export-as-html arg nil nil "*Org HTML Export*")
  23205. (switch-to-buffer-other-window "*Org HTML Export*"))
  23206. (defun org-replace-region-by-html (beg end)
  23207. "Assume the current region has org-mode syntax, and convert it to HTML.
  23208. This can be used in any buffer. For example, you could write an
  23209. itemized list in org-mode syntax in an HTML buffer and then use this
  23210. command to convert it."
  23211. (interactive "r")
  23212. (let (reg html buf pop-up-frames)
  23213. (save-window-excursion
  23214. (if (org-mode-p)
  23215. (setq html (org-export-region-as-html
  23216. beg end t 'string))
  23217. (setq reg (buffer-substring beg end)
  23218. buf (get-buffer-create "*Org tmp*"))
  23219. (with-current-buffer buf
  23220. (erase-buffer)
  23221. (insert reg)
  23222. (org-mode)
  23223. (setq html (org-export-region-as-html
  23224. (point-min) (point-max) t 'string)))
  23225. (kill-buffer buf)))
  23226. (delete-region beg end)
  23227. (insert html)))
  23228. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23229. "Convert region from BEG to END in org-mode buffer to HTML.
  23230. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23231. contents, and only produce the region of converted text, useful for
  23232. cut-and-paste operations.
  23233. If BUFFER is a buffer or a string, use/create that buffer as a target
  23234. of the converted HTML. If BUFFER is the symbol `string', return the
  23235. produced HTML as a string and leave not buffer behind. For example,
  23236. a Lisp program could call this function in the following way:
  23237. (setq html (org-export-region-as-html beg end t 'string))
  23238. When called interactively, the output buffer is selected, and shown
  23239. in a window. A non-interactive call will only retunr the buffer."
  23240. (interactive "r\nP")
  23241. (when (interactive-p)
  23242. (setq buffer "*Org HTML Export*"))
  23243. (let ((transient-mark-mode t) (zmacs-regions t)
  23244. rtn)
  23245. (goto-char end)
  23246. (set-mark (point)) ;; to activate the region
  23247. (goto-char beg)
  23248. (setq rtn (org-export-as-html
  23249. nil nil nil
  23250. buffer body-only))
  23251. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23252. (if (and (interactive-p) (bufferp rtn))
  23253. (switch-to-buffer-other-window rtn)
  23254. rtn)))
  23255. (defvar html-table-tag nil) ; dynamically scoped into this.
  23256. (defun org-export-as-html (arg &optional hidden ext-plist
  23257. to-buffer body-only pub-dir)
  23258. "Export the outline as a pretty HTML file.
  23259. If there is an active region, export only the region. The prefix
  23260. ARG specifies how many levels of the outline should become
  23261. headlines. The default is 3. Lower levels will become bulleted
  23262. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23263. EXT-PLIST is a property list with external parameters overriding
  23264. org-mode's default settings, but still inferior to file-local
  23265. settings. When TO-BUFFER is non-nil, create a buffer with that
  23266. name and export to that buffer. If TO-BUFFER is the symbol
  23267. `string', don't leave any buffer behind but just return the
  23268. resulting HTML as a string. When BODY-ONLY is set, don't produce
  23269. the file header and footer, simply return the content of
  23270. <body>...</body>, without even the body tags themselves. When
  23271. PUB-DIR is set, use this as the publishing directory."
  23272. (interactive "P")
  23273. ;; Make sure we have a file name when we need it.
  23274. (when (and (not (or to-buffer body-only))
  23275. (not buffer-file-name))
  23276. (if (buffer-base-buffer)
  23277. (org-set-local 'buffer-file-name
  23278. (with-current-buffer (buffer-base-buffer)
  23279. buffer-file-name))
  23280. (error "Need a file name to be able to export.")))
  23281. (message "Exporting...")
  23282. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23283. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23284. (setq-default org-done-keywords org-done-keywords)
  23285. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23286. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23287. ext-plist
  23288. (org-infile-export-plist)))
  23289. (style (plist-get opt-plist :style))
  23290. (html-extension (plist-get opt-plist :html-extension))
  23291. (link-validate (plist-get opt-plist :link-validation-function))
  23292. valid thetoc have-headings first-heading-pos
  23293. (odd org-odd-levels-only)
  23294. (region-p (org-region-active-p))
  23295. (subtree-p
  23296. (when region-p
  23297. (save-excursion
  23298. (goto-char (region-beginning))
  23299. (and (org-at-heading-p)
  23300. (>= (org-end-of-subtree t t) (region-end))))))
  23301. ;; The following two are dynamically scoped into other
  23302. ;; routines below.
  23303. (org-current-export-dir
  23304. (or pub-dir (org-export-directory :html opt-plist)))
  23305. (org-current-export-file buffer-file-name)
  23306. (level 0) (line "") (origline "") txt todo
  23307. (umax nil)
  23308. (umax-toc nil)
  23309. (filename (if to-buffer nil
  23310. (expand-file-name
  23311. (concat
  23312. (file-name-sans-extension
  23313. (or (and subtree-p
  23314. (org-entry-get (region-beginning)
  23315. "EXPORT_FILE_NAME" t))
  23316. (file-name-nondirectory buffer-file-name)))
  23317. "." html-extension)
  23318. (file-name-as-directory
  23319. (or pub-dir (org-export-directory :html opt-plist))))))
  23320. (current-dir (if buffer-file-name
  23321. (file-name-directory buffer-file-name)
  23322. default-directory))
  23323. (buffer (if to-buffer
  23324. (cond
  23325. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23326. (t (get-buffer-create to-buffer)))
  23327. (find-file-noselect filename)))
  23328. (org-levels-open (make-vector org-level-max nil))
  23329. (date (plist-get opt-plist :date))
  23330. (author (plist-get opt-plist :author))
  23331. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23332. (plist-get opt-plist :title)
  23333. (and (not
  23334. (plist-get opt-plist :skip-before-1st-heading))
  23335. (org-export-grab-title-from-buffer))
  23336. (and buffer-file-name
  23337. (file-name-sans-extension
  23338. (file-name-nondirectory buffer-file-name)))
  23339. "UNTITLED"))
  23340. (html-table-tag (plist-get opt-plist :html-table-tag))
  23341. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23342. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23343. (inquote nil)
  23344. (infixed nil)
  23345. (in-local-list nil)
  23346. (local-list-num nil)
  23347. (local-list-indent nil)
  23348. (llt org-plain-list-ordered-item-terminator)
  23349. (email (plist-get opt-plist :email))
  23350. (language (plist-get opt-plist :language))
  23351. (lang-words nil)
  23352. (target-alist nil) tg
  23353. (head-count 0) cnt
  23354. (start 0)
  23355. (coding-system (and (boundp 'buffer-file-coding-system)
  23356. buffer-file-coding-system))
  23357. (coding-system-for-write (or org-export-html-coding-system
  23358. coding-system))
  23359. (save-buffer-coding-system (or org-export-html-coding-system
  23360. coding-system))
  23361. (charset (and coding-system-for-write
  23362. (fboundp 'coding-system-get)
  23363. (coding-system-get coding-system-for-write
  23364. 'mime-charset)))
  23365. (region
  23366. (buffer-substring
  23367. (if region-p (region-beginning) (point-min))
  23368. (if region-p (region-end) (point-max))))
  23369. (lines
  23370. (org-split-string
  23371. (org-cleaned-string-for-export
  23372. region
  23373. :emph-multiline t
  23374. :for-html t
  23375. :skip-before-1st-heading
  23376. (plist-get opt-plist :skip-before-1st-heading)
  23377. :drawers (plist-get opt-plist :drawers)
  23378. :archived-trees
  23379. (plist-get opt-plist :archived-trees)
  23380. :add-text
  23381. (plist-get opt-plist :text)
  23382. :LaTeX-fragments
  23383. (plist-get opt-plist :LaTeX-fragments))
  23384. "[\r\n]"))
  23385. table-open type
  23386. table-buffer table-orig-buffer
  23387. ind start-is-num starter didclose
  23388. rpl path desc descp desc1 desc2 link
  23389. snumber fnc
  23390. )
  23391. (let ((inhibit-read-only t))
  23392. (org-unmodified
  23393. (remove-text-properties (point-min) (point-max)
  23394. '(:org-license-to-kill t))))
  23395. (message "Exporting...")
  23396. (setq org-min-level (org-get-min-level lines))
  23397. (setq org-last-level org-min-level)
  23398. (org-init-section-numbers)
  23399. (cond
  23400. ((and date (string-match "%" date))
  23401. (setq date (format-time-string date (current-time))))
  23402. (date)
  23403. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23404. ;; Get the language-dependent settings
  23405. (setq lang-words (or (assoc language org-export-language-setup)
  23406. (assoc "en" org-export-language-setup)))
  23407. ;; Switch to the output buffer
  23408. (set-buffer buffer)
  23409. (let ((inhibit-read-only t)) (erase-buffer))
  23410. (fundamental-mode)
  23411. (and (fboundp 'set-buffer-file-coding-system)
  23412. (set-buffer-file-coding-system coding-system-for-write))
  23413. (let ((case-fold-search nil)
  23414. (org-odd-levels-only odd))
  23415. ;; create local variables for all options, to make sure all called
  23416. ;; functions get the correct information
  23417. (mapc (lambda (x)
  23418. (set (make-local-variable (cdr x))
  23419. (plist-get opt-plist (car x))))
  23420. org-export-plist-vars)
  23421. (setq umax (if arg (prefix-numeric-value arg)
  23422. org-export-headline-levels))
  23423. (setq umax-toc (if (integerp org-export-with-toc)
  23424. (min org-export-with-toc umax)
  23425. umax))
  23426. (unless body-only
  23427. ;; File header
  23428. (insert (format
  23429. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23430. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23431. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23432. lang=\"%s\" xml:lang=\"%s\">
  23433. <head>
  23434. <title>%s</title>
  23435. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23436. <meta name=\"generator\" content=\"Org-mode\"/>
  23437. <meta name=\"generated\" content=\"%s\"/>
  23438. <meta name=\"author\" content=\"%s\"/>
  23439. %s
  23440. </head><body>
  23441. "
  23442. language language (org-html-expand title)
  23443. (or charset "iso-8859-1") date author style))
  23444. (insert (or (plist-get opt-plist :preamble) ""))
  23445. (when (plist-get opt-plist :auto-preamble)
  23446. (if title (insert (format org-export-html-title-format
  23447. (org-html-expand title))))))
  23448. (if (and org-export-with-toc (not body-only))
  23449. (progn
  23450. (push (format "<h%d>%s</h%d>\n"
  23451. org-export-html-toplevel-hlevel
  23452. (nth 3 lang-words)
  23453. org-export-html-toplevel-hlevel)
  23454. thetoc)
  23455. (push "<div id=\"text-table-of-contents\">\n" thetoc)
  23456. (push "<ul>\n<li>" thetoc)
  23457. (setq lines
  23458. (mapcar '(lambda (line)
  23459. (if (string-match org-todo-line-regexp line)
  23460. ;; This is a headline
  23461. (progn
  23462. (setq have-headings t)
  23463. (setq level (- (match-end 1) (match-beginning 1))
  23464. level (org-tr-level level)
  23465. txt (save-match-data
  23466. (org-html-expand
  23467. (org-export-cleanup-toc-line
  23468. (match-string 3 line))))
  23469. todo
  23470. (or (and org-export-mark-todo-in-toc
  23471. (match-beginning 2)
  23472. (not (member (match-string 2 line)
  23473. org-done-keywords)))
  23474. ; TODO, not DONE
  23475. (and org-export-mark-todo-in-toc
  23476. (= level umax-toc)
  23477. (org-search-todo-below
  23478. line lines level))))
  23479. (if (string-match
  23480. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23481. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23482. (if (string-match quote-re0 txt)
  23483. (setq txt (replace-match "" t t txt)))
  23484. (setq snumber (org-section-number level))
  23485. (if org-export-with-section-numbers
  23486. (setq txt (concat snumber " " txt)))
  23487. (if (<= level (max umax umax-toc))
  23488. (setq head-count (+ head-count 1)))
  23489. (if (<= level umax-toc)
  23490. (progn
  23491. (if (> level org-last-level)
  23492. (progn
  23493. (setq cnt (- level org-last-level))
  23494. (while (>= (setq cnt (1- cnt)) 0)
  23495. (push "\n<ul>\n<li>" thetoc))
  23496. (push "\n" thetoc)))
  23497. (if (< level org-last-level)
  23498. (progn
  23499. (setq cnt (- org-last-level level))
  23500. (while (>= (setq cnt (1- cnt)) 0)
  23501. (push "</li>\n</ul>" thetoc))
  23502. (push "\n" thetoc)))
  23503. ;; Check for targets
  23504. (while (string-match org-target-regexp line)
  23505. (setq tg (match-string 1 line)
  23506. line (replace-match
  23507. (concat "@<span class=\"target\">" tg "@</span> ")
  23508. t t line))
  23509. (push (cons (org-solidify-link-text tg)
  23510. (format "sec-%s" snumber))
  23511. target-alist))
  23512. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23513. (setq txt (replace-match "" t t txt)))
  23514. (push
  23515. (format
  23516. (if todo
  23517. "</li>\n<li><a href=\"#sec-%s\"><span class=\"todo\">%s</span></a>"
  23518. "</li>\n<li><a href=\"#sec-%s\">%s</a>")
  23519. snumber txt) thetoc)
  23520. (setq org-last-level level))
  23521. )))
  23522. line)
  23523. lines))
  23524. (while (> org-last-level (1- org-min-level))
  23525. (setq org-last-level (1- org-last-level))
  23526. (push "</li>\n</ul>\n" thetoc))
  23527. (push "</div>\n" thetoc)
  23528. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23529. (setq head-count 0)
  23530. (org-init-section-numbers)
  23531. (while (setq line (pop lines) origline line)
  23532. (catch 'nextline
  23533. ;; end of quote section?
  23534. (when (and inquote (string-match "^\\*+ " line))
  23535. (insert "</pre>\n")
  23536. (setq inquote nil))
  23537. ;; inside a quote section?
  23538. (when inquote
  23539. (insert (org-html-protect line) "\n")
  23540. (throw 'nextline nil))
  23541. ;; verbatim lines
  23542. (when (and org-export-with-fixed-width
  23543. (string-match "^[ \t]*:\\(.*\\)" line))
  23544. (when (not infixed)
  23545. (setq infixed t)
  23546. (insert "<pre>\n"))
  23547. (insert (org-html-protect (match-string 1 line)) "\n")
  23548. (when (and lines
  23549. (not (string-match "^[ \t]*\\(:.*\\)"
  23550. (car lines))))
  23551. (setq infixed nil)
  23552. (insert "</pre>\n"))
  23553. (throw 'nextline nil))
  23554. ;; Protected HTML
  23555. (when (get-text-property 0 'org-protected line)
  23556. (let (par)
  23557. (when (re-search-backward
  23558. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23559. (setq par (match-string 1))
  23560. (replace-match "\\2\n"))
  23561. (insert line "\n")
  23562. (while (and lines
  23563. (or (= (length (car lines)) 0)
  23564. (get-text-property 0 'org-protected (car lines))))
  23565. (insert (pop lines) "\n"))
  23566. (and par (insert "<p>\n")))
  23567. (throw 'nextline nil))
  23568. ;; Horizontal line
  23569. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23570. (insert "\n<hr/>\n")
  23571. (throw 'nextline nil))
  23572. ;; make targets to anchors
  23573. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23574. (cond
  23575. ((match-end 2)
  23576. (setq line (replace-match
  23577. (concat "@<a name=\""
  23578. (org-solidify-link-text (match-string 1 line))
  23579. "\">\\nbsp@</a>")
  23580. t t line)))
  23581. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23582. (setq line (replace-match
  23583. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23584. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23585. t t line)))
  23586. (t
  23587. (setq line (replace-match
  23588. (concat "@<a name=\""
  23589. (org-solidify-link-text (match-string 1 line))
  23590. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23591. t t line)))))
  23592. (setq line (org-html-handle-time-stamps line))
  23593. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23594. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23595. ;; Also handle sub_superscripts and checkboxes
  23596. (or (string-match org-table-hline-regexp line)
  23597. (setq line (org-html-expand line)))
  23598. ;; Format the links
  23599. (setq start 0)
  23600. (while (string-match org-bracket-link-analytic-regexp line start)
  23601. (setq start (match-beginning 0))
  23602. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23603. (setq path (match-string 3 line))
  23604. (setq desc1 (if (match-end 5) (match-string 5 line))
  23605. desc2 (if (match-end 2) (concat type ":" path) path)
  23606. descp (and desc1 (not (equal desc1 desc2)))
  23607. desc (or desc1 desc2))
  23608. ;; Make an image out of the description if that is so wanted
  23609. (when (and descp (org-file-image-p desc))
  23610. (save-match-data
  23611. (if (string-match "^file:" desc)
  23612. (setq desc (substring desc (match-end 0)))))
  23613. (setq desc (concat "<img src=\"" desc "\"/>")))
  23614. ;; FIXME: do we need to unescape here somewhere?
  23615. (cond
  23616. ((equal type "internal")
  23617. (setq rpl
  23618. (concat
  23619. "<a href=\"#"
  23620. (org-solidify-link-text
  23621. (save-match-data (org-link-unescape path)) target-alist)
  23622. "\">" desc "</a>")))
  23623. ((member type '("http" "https"))
  23624. ;; standard URL, just check if we need to inline an image
  23625. (if (and (or (eq t org-export-html-inline-images)
  23626. (and org-export-html-inline-images (not descp)))
  23627. (org-file-image-p path))
  23628. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23629. (setq link (concat type ":" path))
  23630. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23631. ((member type '("ftp" "mailto" "news"))
  23632. ;; standard URL
  23633. (setq link (concat type ":" path))
  23634. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23635. ((string= type "file")
  23636. ;; FILE link
  23637. (let* ((filename path)
  23638. (abs-p (file-name-absolute-p filename))
  23639. thefile file-is-image-p search)
  23640. (save-match-data
  23641. (if (string-match "::\\(.*\\)" filename)
  23642. (setq search (match-string 1 filename)
  23643. filename (replace-match "" t nil filename)))
  23644. (setq valid
  23645. (if (functionp link-validate)
  23646. (funcall link-validate filename current-dir)
  23647. t))
  23648. (setq file-is-image-p (org-file-image-p filename))
  23649. (setq thefile (if abs-p (expand-file-name filename) filename))
  23650. (when (and org-export-html-link-org-files-as-html
  23651. (string-match "\\.org$" thefile))
  23652. (setq thefile (concat (substring thefile 0
  23653. (match-beginning 0))
  23654. "." html-extension))
  23655. (if (and search
  23656. ;; make sure this is can be used as target search
  23657. (not (string-match "^[0-9]*$" search))
  23658. (not (string-match "^\\*" search))
  23659. (not (string-match "^/.*/$" search)))
  23660. (setq thefile (concat thefile "#"
  23661. (org-solidify-link-text
  23662. (org-link-unescape search)))))
  23663. (when (string-match "^file:" desc)
  23664. (setq desc (replace-match "" t t desc))
  23665. (if (string-match "\\.org$" desc)
  23666. (setq desc (replace-match "" t t desc))))))
  23667. (setq rpl (if (and file-is-image-p
  23668. (or (eq t org-export-html-inline-images)
  23669. (and org-export-html-inline-images
  23670. (not descp))))
  23671. (concat "<img src=\"" thefile "\"/>")
  23672. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23673. (if (not valid) (setq rpl desc))))
  23674. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  23675. (setq rpl
  23676. (save-match-data
  23677. (funcall fnc (org-link-unescape path) desc1 'html))))
  23678. (t
  23679. ;; just publish the path, as default
  23680. (setq rpl (concat "<i>&lt;" type ":"
  23681. (save-match-data (org-link-unescape path))
  23682. "&gt;</i>"))))
  23683. (setq line (replace-match rpl t t line)
  23684. start (+ start (length rpl))))
  23685. ;; TODO items
  23686. (if (and (string-match org-todo-line-regexp line)
  23687. (match-beginning 2))
  23688. (setq line
  23689. (concat (substring line 0 (match-beginning 2))
  23690. "<span class=\""
  23691. (if (member (match-string 2 line)
  23692. org-done-keywords)
  23693. "done" "todo")
  23694. "\">" (match-string 2 line)
  23695. "</span>" (substring line (match-end 2)))))
  23696. ;; Does this contain a reference to a footnote?
  23697. (when org-export-with-footnotes
  23698. (setq start 0)
  23699. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23700. (if (get-text-property (match-beginning 2) 'org-protected line)
  23701. (setq start (match-end 2))
  23702. (let ((n (match-string 2 line)))
  23703. (setq line
  23704. (replace-match
  23705. (format
  23706. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23707. (match-string 1 line) n n n)
  23708. t t line))))))
  23709. (cond
  23710. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23711. ;; This is a headline
  23712. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23713. txt (match-string 2 line))
  23714. (if (string-match quote-re0 txt)
  23715. (setq txt (replace-match "" t t txt)))
  23716. (if (<= level (max umax umax-toc))
  23717. (setq head-count (+ head-count 1)))
  23718. (when in-local-list
  23719. ;; Close any local lists before inserting a new header line
  23720. (while local-list-num
  23721. (org-close-li)
  23722. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23723. (pop local-list-num))
  23724. (setq local-list-indent nil
  23725. in-local-list nil))
  23726. (setq first-heading-pos (or first-heading-pos (point)))
  23727. (org-html-level-start level txt umax
  23728. (and org-export-with-toc (<= level umax))
  23729. head-count)
  23730. ;; QUOTES
  23731. (when (string-match quote-re line)
  23732. (insert "<pre>")
  23733. (setq inquote t)))
  23734. ((and org-export-with-tables
  23735. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23736. (if (not table-open)
  23737. ;; New table starts
  23738. (setq table-open t table-buffer nil table-orig-buffer nil))
  23739. ;; Accumulate lines
  23740. (setq table-buffer (cons line table-buffer)
  23741. table-orig-buffer (cons origline table-orig-buffer))
  23742. (when (or (not lines)
  23743. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23744. (car lines))))
  23745. (setq table-open nil
  23746. table-buffer (nreverse table-buffer)
  23747. table-orig-buffer (nreverse table-orig-buffer))
  23748. (org-close-par-maybe)
  23749. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23750. (t
  23751. ;; Normal lines
  23752. (when (string-match
  23753. (cond
  23754. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23755. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23756. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23757. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23758. line)
  23759. (setq ind (org-get-string-indentation line)
  23760. start-is-num (match-beginning 4)
  23761. starter (if (match-beginning 2)
  23762. (substring (match-string 2 line) 0 -1))
  23763. line (substring line (match-beginning 5)))
  23764. (unless (string-match "[^ \t]" line)
  23765. ;; empty line. Pretend indentation is large.
  23766. (setq ind (if org-empty-line-terminates-plain-lists
  23767. 0
  23768. (1+ (or (car local-list-indent) 1)))))
  23769. (setq didclose nil)
  23770. (while (and in-local-list
  23771. (or (and (= ind (car local-list-indent))
  23772. (not starter))
  23773. (< ind (car local-list-indent))))
  23774. (setq didclose t)
  23775. (org-close-li)
  23776. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23777. (pop local-list-num) (pop local-list-indent)
  23778. (setq in-local-list local-list-indent))
  23779. (cond
  23780. ((and starter
  23781. (or (not in-local-list)
  23782. (> ind (car local-list-indent))))
  23783. ;; Start new (level of) list
  23784. (org-close-par-maybe)
  23785. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23786. (push start-is-num local-list-num)
  23787. (push ind local-list-indent)
  23788. (setq in-local-list t))
  23789. (starter
  23790. ;; continue current list
  23791. (org-close-li)
  23792. (insert "<li>\n"))
  23793. (didclose
  23794. ;; we did close a list, normal text follows: need <p>
  23795. (org-open-par)))
  23796. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23797. (setq line
  23798. (replace-match
  23799. (if (equal (match-string 1 line) "X")
  23800. "<b>[X]</b>"
  23801. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23802. t t line))))
  23803. ;; Empty lines start a new paragraph. If hand-formatted lists
  23804. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23805. ;; also start a new paragraph.
  23806. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23807. ;; Is this the start of a footnote?
  23808. (when org-export-with-footnotes
  23809. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23810. (org-close-par-maybe)
  23811. (let ((n (match-string 1 line)))
  23812. (setq line (replace-match
  23813. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23814. ;; Check if the line break needs to be conserved
  23815. (cond
  23816. ((string-match "\\\\\\\\[ \t]*$" line)
  23817. (setq line (replace-match "<br/>" t t line)))
  23818. (org-export-preserve-breaks
  23819. (setq line (concat line "<br/>"))))
  23820. (insert line "\n")))))
  23821. ;; Properly close all local lists and other lists
  23822. (when inquote (insert "</pre>\n"))
  23823. (when in-local-list
  23824. ;; Close any local lists before inserting a new header line
  23825. (while local-list-num
  23826. (org-close-li)
  23827. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23828. (pop local-list-num))
  23829. (setq local-list-indent nil
  23830. in-local-list nil))
  23831. (org-html-level-start 1 nil umax
  23832. (and org-export-with-toc (<= level umax))
  23833. head-count)
  23834. ;; the </div> to lose the last text-... div.
  23835. (insert "</div>\n")
  23836. (unless body-only
  23837. (when (plist-get opt-plist :auto-postamble)
  23838. (insert "<div id=\"postamble\">")
  23839. (when (and org-export-author-info author)
  23840. (insert "<p class=\"author\"> "
  23841. (nth 1 lang-words) ": " author "\n")
  23842. (when email
  23843. (if (listp (split-string email ",+ *"))
  23844. (mapc (lambda(e)
  23845. (insert "<a href=\"mailto:" e "\">&lt;"
  23846. e "&gt;</a>\n"))
  23847. (split-string email ",+ *"))
  23848. (insert "<a href=\"mailto:" email "\">&lt;"
  23849. email "&gt;</a>\n")))
  23850. (insert "</p>\n"))
  23851. (when (and date org-export-time-stamp-file)
  23852. (insert "<p class=\"date\"> "
  23853. (nth 2 lang-words) ": "
  23854. date "</p>\n"))
  23855. (insert "</div>"))
  23856. (if org-export-html-with-timestamp
  23857. (insert org-export-html-html-helper-timestamp))
  23858. (insert (or (plist-get opt-plist :postamble) ""))
  23859. (insert "</body>\n</html>\n"))
  23860. (normal-mode)
  23861. (if (eq major-mode default-major-mode) (html-mode))
  23862. ;; insert the table of contents
  23863. (goto-char (point-min))
  23864. (when thetoc
  23865. (if (or (re-search-forward
  23866. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23867. (re-search-forward
  23868. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23869. (progn
  23870. (goto-char (match-beginning 0))
  23871. (replace-match ""))
  23872. (goto-char first-heading-pos)
  23873. (when (looking-at "\\s-*</p>")
  23874. (goto-char (match-end 0))
  23875. (insert "\n")))
  23876. (insert "<div id=\"table-of-contents\">\n")
  23877. (mapc 'insert thetoc)
  23878. (insert "</div>\n"))
  23879. ;; remove empty paragraphs and lists
  23880. (goto-char (point-min))
  23881. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23882. (replace-match ""))
  23883. (goto-char (point-min))
  23884. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23885. (replace-match ""))
  23886. (goto-char (point-min))
  23887. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23888. (replace-match ""))
  23889. ;; Convert whitespace place holders
  23890. (goto-char (point-min))
  23891. (let (beg end n)
  23892. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23893. (setq n (get-text-property beg 'org-whitespace)
  23894. end (next-single-property-change beg 'org-whitespace))
  23895. (goto-char beg)
  23896. (delete-region beg end)
  23897. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23898. (make-string n ?x)))))
  23899. (or to-buffer (save-buffer))
  23900. (goto-char (point-min))
  23901. (message "Exporting... done")
  23902. (if (eq to-buffer 'string)
  23903. (prog1 (buffer-substring (point-min) (point-max))
  23904. (kill-buffer (current-buffer)))
  23905. (current-buffer)))))
  23906. (defvar org-table-colgroup-info nil)
  23907. (defun org-format-table-ascii (lines)
  23908. "Format a table for ascii export."
  23909. (if (stringp lines)
  23910. (setq lines (org-split-string lines "\n")))
  23911. (if (not (string-match "^[ \t]*|" (car lines)))
  23912. ;; Table made by table.el - test for spanning
  23913. lines
  23914. ;; A normal org table
  23915. ;; Get rid of hlines at beginning and end
  23916. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23917. (setq lines (nreverse lines))
  23918. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23919. (setq lines (nreverse lines))
  23920. (when org-export-table-remove-special-lines
  23921. ;; Check if the table has a marking column. If yes remove the
  23922. ;; column and the special lines
  23923. (setq lines (org-table-clean-before-export lines)))
  23924. ;; Get rid of the vertical lines except for grouping
  23925. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23926. rtn line vl1 start)
  23927. (while (setq line (pop lines))
  23928. (if (string-match org-table-hline-regexp line)
  23929. (and (string-match "|\\(.*\\)|" line)
  23930. (setq line (replace-match " \\1" t nil line)))
  23931. (setq start 0 vl1 vl)
  23932. (while (string-match "|" line start)
  23933. (setq start (match-end 0))
  23934. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23935. (push line rtn))
  23936. (nreverse rtn))))
  23937. (defun org-colgroup-info-to-vline-list (info)
  23938. (let (vl new last)
  23939. (while info
  23940. (setq last new new (pop info))
  23941. (if (or (memq last '(:end :startend))
  23942. (memq new '(:start :startend)))
  23943. (push t vl)
  23944. (push nil vl)))
  23945. (setq vl (nreverse vl))
  23946. (and vl (setcar vl nil))
  23947. vl))
  23948. (defun org-format-table-html (lines olines)
  23949. "Find out which HTML converter to use and return the HTML code."
  23950. (if (stringp lines)
  23951. (setq lines (org-split-string lines "\n")))
  23952. (if (string-match "^[ \t]*|" (car lines))
  23953. ;; A normal org table
  23954. (org-format-org-table-html lines)
  23955. ;; Table made by table.el - test for spanning
  23956. (let* ((hlines (delq nil (mapcar
  23957. (lambda (x)
  23958. (if (string-match "^[ \t]*\\+-" x) x
  23959. nil))
  23960. lines)))
  23961. (first (car hlines))
  23962. (ll (and (string-match "\\S-+" first)
  23963. (match-string 0 first)))
  23964. (re (concat "^[ \t]*" (regexp-quote ll)))
  23965. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23966. hlines))))
  23967. (if (and (not spanning)
  23968. (not org-export-prefer-native-exporter-for-tables))
  23969. ;; We can use my own converter with HTML conversions
  23970. (org-format-table-table-html lines)
  23971. ;; Need to use the code generator in table.el, with the original text.
  23972. (org-format-table-table-html-using-table-generate-source olines)))))
  23973. (defun org-format-org-table-html (lines &optional splice)
  23974. "Format a table into HTML."
  23975. ;; Get rid of hlines at beginning and end
  23976. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23977. (setq lines (nreverse lines))
  23978. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23979. (setq lines (nreverse lines))
  23980. (when org-export-table-remove-special-lines
  23981. ;; Check if the table has a marking column. If yes remove the
  23982. ;; column and the special lines
  23983. (setq lines (org-table-clean-before-export lines)))
  23984. (let ((head (and org-export-highlight-first-table-line
  23985. (delq nil (mapcar
  23986. (lambda (x) (string-match "^[ \t]*|-" x))
  23987. (cdr lines)))))
  23988. (nlines 0) fnum i
  23989. tbopen line fields html gr colgropen)
  23990. (if splice (setq head nil))
  23991. (unless splice (push (if head "<thead>" "<tbody>") html))
  23992. (setq tbopen t)
  23993. (while (setq line (pop lines))
  23994. (catch 'next-line
  23995. (if (string-match "^[ \t]*|-" line)
  23996. (progn
  23997. (unless splice
  23998. (push (if head "</thead>" "</tbody>") html)
  23999. (if lines (push "<tbody>" html) (setq tbopen nil)))
  24000. (setq head nil) ;; head ends here, first time around
  24001. ;; ignore this line
  24002. (throw 'next-line t)))
  24003. ;; Break the line into fields
  24004. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24005. (unless fnum (setq fnum (make-vector (length fields) 0)))
  24006. (setq nlines (1+ nlines) i -1)
  24007. (push (concat "<tr>"
  24008. (mapconcat
  24009. (lambda (x)
  24010. (setq i (1+ i))
  24011. (if (and (< i nlines)
  24012. (string-match org-table-number-regexp x))
  24013. (incf (aref fnum i)))
  24014. (if head
  24015. (concat (car org-export-table-header-tags) x
  24016. (cdr org-export-table-header-tags))
  24017. (concat (car org-export-table-data-tags) x
  24018. (cdr org-export-table-data-tags))))
  24019. fields "")
  24020. "</tr>")
  24021. html)))
  24022. (unless splice (if tbopen (push "</tbody>" html)))
  24023. (unless splice (push "</table>\n" html))
  24024. (setq html (nreverse html))
  24025. (unless splice
  24026. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  24027. (push (mapconcat
  24028. (lambda (x)
  24029. (setq gr (pop org-table-colgroup-info))
  24030. (format "%s<col align=\"%s\"></col>%s"
  24031. (if (memq gr '(:start :startend))
  24032. (prog1
  24033. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  24034. (setq colgropen t))
  24035. "")
  24036. (if (> (/ (float x) nlines) org-table-number-fraction)
  24037. "right" "left")
  24038. (if (memq gr '(:end :startend))
  24039. (progn (setq colgropen nil) "</colgroup>")
  24040. "")))
  24041. fnum "")
  24042. html)
  24043. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  24044. (push html-table-tag html))
  24045. (concat (mapconcat 'identity html "\n") "\n")))
  24046. (defun org-table-clean-before-export (lines)
  24047. "Check if the table has a marking column.
  24048. If yes remove the column and the special lines."
  24049. (setq org-table-colgroup-info nil)
  24050. (if (memq nil
  24051. (mapcar
  24052. (lambda (x) (or (string-match "^[ \t]*|-" x)
  24053. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  24054. lines))
  24055. (progn
  24056. (setq org-table-clean-did-remove-column nil)
  24057. (delq nil
  24058. (mapcar
  24059. (lambda (x)
  24060. (cond
  24061. ((string-match "^[ \t]*| */ *|" x)
  24062. (setq org-table-colgroup-info
  24063. (mapcar (lambda (x)
  24064. (cond ((member x '("<" "&lt;")) :start)
  24065. ((member x '(">" "&gt;")) :end)
  24066. ((member x '("<>" "&lt;&gt;")) :startend)
  24067. (t nil)))
  24068. (org-split-string x "[ \t]*|[ \t]*")))
  24069. nil)
  24070. (t x)))
  24071. lines)))
  24072. (setq org-table-clean-did-remove-column t)
  24073. (delq nil
  24074. (mapcar
  24075. (lambda (x)
  24076. (cond
  24077. ((string-match "^[ \t]*| */ *|" x)
  24078. (setq org-table-colgroup-info
  24079. (mapcar (lambda (x)
  24080. (cond ((member x '("<" "&lt;")) :start)
  24081. ((member x '(">" "&gt;")) :end)
  24082. ((member x '("<>" "&lt;&gt;")) :startend)
  24083. (t nil)))
  24084. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  24085. nil)
  24086. ((string-match "^[ \t]*| *[!_^/] *|" x)
  24087. nil) ; ignore this line
  24088. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  24089. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  24090. ;; remove the first column
  24091. (replace-match "\\1|" t nil x))))
  24092. lines))))
  24093. (defun org-format-table-table-html (lines)
  24094. "Format a table generated by table.el into HTML.
  24095. This conversion does *not* use `table-generate-source' from table.el.
  24096. This has the advantage that Org-mode's HTML conversions can be used.
  24097. But it has the disadvantage, that no cell- or row-spanning is allowed."
  24098. (let (line field-buffer
  24099. (head org-export-highlight-first-table-line)
  24100. fields html empty)
  24101. (setq html (concat html-table-tag "\n"))
  24102. (while (setq line (pop lines))
  24103. (setq empty "&nbsp;")
  24104. (catch 'next-line
  24105. (if (string-match "^[ \t]*\\+-" line)
  24106. (progn
  24107. (if field-buffer
  24108. (progn
  24109. (setq
  24110. html
  24111. (concat
  24112. html
  24113. "<tr>"
  24114. (mapconcat
  24115. (lambda (x)
  24116. (if (equal x "") (setq x empty))
  24117. (if head
  24118. (concat (car org-export-table-header-tags) x
  24119. (cdr org-export-table-header-tags))
  24120. (concat (car org-export-table-data-tags) x
  24121. (cdr org-export-table-data-tags))))
  24122. field-buffer "\n")
  24123. "</tr>\n"))
  24124. (setq head nil)
  24125. (setq field-buffer nil)))
  24126. ;; Ignore this line
  24127. (throw 'next-line t)))
  24128. ;; Break the line into fields and store the fields
  24129. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24130. (if field-buffer
  24131. (setq field-buffer (mapcar
  24132. (lambda (x)
  24133. (concat x "<br/>" (pop fields)))
  24134. field-buffer))
  24135. (setq field-buffer fields))))
  24136. (setq html (concat html "</table>\n"))
  24137. html))
  24138. (defun org-format-table-table-html-using-table-generate-source (lines)
  24139. "Format a table into html, using `table-generate-source' from table.el.
  24140. This has the advantage that cell- or row-spanning is allowed.
  24141. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  24142. (require 'table)
  24143. (with-current-buffer (get-buffer-create " org-tmp1 ")
  24144. (erase-buffer)
  24145. (insert (mapconcat 'identity lines "\n"))
  24146. (goto-char (point-min))
  24147. (if (not (re-search-forward "|[^+]" nil t))
  24148. (error "Error processing table"))
  24149. (table-recognize-table)
  24150. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  24151. (table-generate-source 'html " org-tmp2 ")
  24152. (set-buffer " org-tmp2 ")
  24153. (buffer-substring (point-min) (point-max))))
  24154. (defun org-html-handle-time-stamps (s)
  24155. "Format time stamps in string S, or remove them."
  24156. (catch 'exit
  24157. (let (r b)
  24158. (while (string-match org-maybe-keyword-time-regexp s)
  24159. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  24160. ;; never export CLOCK
  24161. (throw 'exit ""))
  24162. (or b (setq b (substring s 0 (match-beginning 0))))
  24163. (if (not org-export-with-timestamps)
  24164. (setq r (concat r (substring s 0 (match-beginning 0)))
  24165. s (substring s (match-end 0)))
  24166. (setq r (concat
  24167. r (substring s 0 (match-beginning 0))
  24168. (if (match-end 1)
  24169. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  24170. (match-string 1 s)))
  24171. (format " @<span class=\"timestamp\">%s@</span>"
  24172. (substring
  24173. (org-translate-time (match-string 3 s)) 1 -1)))
  24174. s (substring s (match-end 0)))))
  24175. ;; Line break if line started and ended with time stamp stuff
  24176. (if (not r)
  24177. s
  24178. (setq r (concat r s))
  24179. (unless (string-match "\\S-" (concat b s))
  24180. (setq r (concat r "@<br/>")))
  24181. r))))
  24182. (defun org-html-protect (s)
  24183. ;; convert & to &amp;, < to &lt; and > to &gt;
  24184. (let ((start 0))
  24185. (while (string-match "&" s start)
  24186. (setq s (replace-match "&amp;" t t s)
  24187. start (1+ (match-beginning 0))))
  24188. (while (string-match "<" s)
  24189. (setq s (replace-match "&lt;" t t s)))
  24190. (while (string-match ">" s)
  24191. (setq s (replace-match "&gt;" t t s))))
  24192. s)
  24193. (defun org-export-cleanup-toc-line (s)
  24194. "Remove tags and time staps from lines going into the toc."
  24195. (when (memq org-export-with-tags '(not-in-toc nil))
  24196. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24197. (setq s (replace-match "" t t s))))
  24198. (when org-export-remove-timestamps-from-toc
  24199. (while (string-match org-maybe-keyword-time-regexp s)
  24200. (setq s (replace-match "" t t s))))
  24201. (while (string-match org-bracket-link-regexp s)
  24202. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24203. t t s)))
  24204. s)
  24205. (defun org-html-expand (string)
  24206. "Prepare STRING for HTML export. Applies all active conversions.
  24207. If there are links in the string, don't modify these."
  24208. (let* ((re (concat org-bracket-link-regexp "\\|"
  24209. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24210. m s l res)
  24211. (while (setq m (string-match re string))
  24212. (setq s (substring string 0 m)
  24213. l (match-string 0 string)
  24214. string (substring string (match-end 0)))
  24215. (push (org-html-do-expand s) res)
  24216. (push l res))
  24217. (push (org-html-do-expand string) res)
  24218. (apply 'concat (nreverse res))))
  24219. (defun org-html-do-expand (s)
  24220. "Apply all active conversions to translate special ASCII to HTML."
  24221. (setq s (org-html-protect s))
  24222. (if org-export-html-expand
  24223. (let ((start 0))
  24224. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24225. (setq s (replace-match "<\\1>" t nil s)))))
  24226. (if org-export-with-emphasize
  24227. (setq s (org-export-html-convert-emphasize s)))
  24228. (if org-export-with-special-strings
  24229. (setq s (org-export-html-convert-special-strings s)))
  24230. (if org-export-with-sub-superscripts
  24231. (setq s (org-export-html-convert-sub-super s)))
  24232. (if org-export-with-TeX-macros
  24233. (let ((start 0) wd ass)
  24234. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24235. (if (get-text-property (match-beginning 0) 'org-protected s)
  24236. (setq start (match-end 0))
  24237. (setq wd (match-string 1 s))
  24238. (if (setq ass (assoc wd org-html-entities))
  24239. (setq s (replace-match (or (cdr ass)
  24240. (concat "&" (car ass) ";"))
  24241. t t s))
  24242. (setq start (+ start (length wd))))))))
  24243. s)
  24244. (defun org-create-multibrace-regexp (left right n)
  24245. "Create a regular expression which will match a balanced sexp.
  24246. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24247. as single character strings.
  24248. The regexp returned will match the entire expression including the
  24249. delimiters. It will also define a single group which contains the
  24250. match except for the outermost delimiters. The maximum depth of
  24251. stacked delimiters is N. Escaping delimiters is not possible."
  24252. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24253. (or "\\|")
  24254. (re nothing)
  24255. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24256. (while (> n 1)
  24257. (setq n (1- n)
  24258. re (concat re or next)
  24259. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24260. (concat left "\\(" re "\\)" right)))
  24261. (defvar org-match-substring-regexp
  24262. (concat
  24263. "\\([^\\]\\)\\([_^]\\)\\("
  24264. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24265. "\\|"
  24266. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24267. "\\|"
  24268. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24269. "The regular expression matching a sub- or superscript.")
  24270. (defvar org-match-substring-with-braces-regexp
  24271. (concat
  24272. "\\([^\\]\\)\\([_^]\\)\\("
  24273. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24274. "\\)")
  24275. "The regular expression matching a sub- or superscript, forcing braces.")
  24276. (defconst org-export-html-special-string-regexps
  24277. '(("\\\\-" . "&shy;")
  24278. ("---\\([^-]\\)" . "&mdash;\\1")
  24279. ("--\\([^-]\\)" . "&ndash;\\1")
  24280. ("\\.\\.\\." . "&hellip;"))
  24281. "Regular expressions for special string conversion.")
  24282. (defun org-export-html-convert-special-strings (string)
  24283. "Convert special characters in STRING to HTML."
  24284. (let ((all org-export-html-special-string-regexps)
  24285. e a re rpl start)
  24286. (while (setq a (pop all))
  24287. (setq re (car a) rpl (cdr a) start 0)
  24288. (while (string-match re string start)
  24289. (if (get-text-property (match-beginning 0) 'org-protected string)
  24290. (setq start (match-end 0))
  24291. (setq string (replace-match rpl t nil string)))))
  24292. string))
  24293. (defun org-export-html-convert-sub-super (string)
  24294. "Convert sub- and superscripts in STRING to HTML."
  24295. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24296. (while (string-match org-match-substring-regexp string s)
  24297. (cond
  24298. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24299. ((get-text-property (match-beginning 2) 'org-protected string)
  24300. (setq s (match-end 2)))
  24301. (t
  24302. (setq s (match-end 1)
  24303. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24304. c (or (match-string 8 string)
  24305. (match-string 6 string)
  24306. (match-string 5 string))
  24307. string (replace-match
  24308. (concat (match-string 1 string)
  24309. "<" key ">" c "</" key ">")
  24310. t t string)))))
  24311. (while (string-match "\\\\\\([_^]\\)" string)
  24312. (setq string (replace-match (match-string 1 string) t t string)))
  24313. string))
  24314. (defun org-export-html-convert-emphasize (string)
  24315. "Apply emphasis."
  24316. (let ((s 0) rpl)
  24317. (while (string-match org-emph-re string s)
  24318. (if (not (equal
  24319. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24320. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24321. (setq s (match-beginning 0)
  24322. rpl
  24323. (concat
  24324. (match-string 1 string)
  24325. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24326. (match-string 4 string)
  24327. (nth 3 (assoc (match-string 3 string)
  24328. org-emphasis-alist))
  24329. (match-string 5 string))
  24330. string (replace-match rpl t t string)
  24331. s (+ s (- (length rpl) 2)))
  24332. (setq s (1+ s))))
  24333. string))
  24334. (defvar org-par-open nil)
  24335. (defun org-open-par ()
  24336. "Insert <p>, but first close previous paragraph if any."
  24337. (org-close-par-maybe)
  24338. (insert "\n<p>")
  24339. (setq org-par-open t))
  24340. (defun org-close-par-maybe ()
  24341. "Close paragraph if there is one open."
  24342. (when org-par-open
  24343. (insert "</p>")
  24344. (setq org-par-open nil)))
  24345. (defun org-close-li ()
  24346. "Close <li> if necessary."
  24347. (org-close-par-maybe)
  24348. (insert "</li>\n"))
  24349. (defvar body-only) ; dynamically scoped into this.
  24350. (defun org-html-level-start (level title umax with-toc head-count)
  24351. "Insert a new level in HTML export.
  24352. When TITLE is nil, just close all open levels."
  24353. (org-close-par-maybe)
  24354. (let ((l org-level-max) snumber)
  24355. (while (>= l level)
  24356. (if (aref org-levels-open (1- l))
  24357. (progn
  24358. (org-html-level-close l umax)
  24359. (aset org-levels-open (1- l) nil)))
  24360. (setq l (1- l)))
  24361. (when title
  24362. ;; If title is nil, this means this function is called to close
  24363. ;; all levels, so the rest is done only if title is given
  24364. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24365. (setq title (replace-match
  24366. (if org-export-with-tags
  24367. (save-match-data
  24368. (concat
  24369. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24370. (mapconcat 'identity (org-split-string
  24371. (match-string 1 title) ":")
  24372. "&nbsp;")
  24373. "</span>"))
  24374. "")
  24375. t t title)))
  24376. (if (> level umax)
  24377. (progn
  24378. (if (aref org-levels-open (1- level))
  24379. (progn
  24380. (org-close-li)
  24381. (insert "<li>" title "<br/>\n"))
  24382. (aset org-levels-open (1- level) t)
  24383. (org-close-par-maybe)
  24384. (insert "<ul>\n<li>" title "<br/>\n")))
  24385. (aset org-levels-open (1- level) t)
  24386. (setq snumber (org-section-number level))
  24387. (if (and org-export-with-section-numbers (not body-only))
  24388. (setq title (concat snumber " " title)))
  24389. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24390. (unless (= head-count 1) (insert "\n</div>\n"))
  24391. (insert (format "\n<div id=\"outline-container-%s\" class=\"outline-%d\">\n<h%d id=\"sec-%s\">%s</h%d>\n<div id=\"text-%s\">\n"
  24392. snumber level level snumber title level snumber))
  24393. (org-open-par)))))
  24394. (defun org-html-level-close (level max-outline-level)
  24395. "Terminate one level in HTML export."
  24396. (if (<= level max-outline-level)
  24397. (insert "</div>\n")
  24398. (org-close-li)
  24399. (insert "</ul>\n")))
  24400. ;;; iCalendar export
  24401. ;;;###autoload
  24402. (defun org-export-icalendar-this-file ()
  24403. "Export current file as an iCalendar file.
  24404. The iCalendar file will be located in the same directory as the Org-mode
  24405. file, but with extension `.ics'."
  24406. (interactive)
  24407. (org-export-icalendar nil buffer-file-name))
  24408. ;;;###autoload
  24409. (defun org-export-icalendar-all-agenda-files ()
  24410. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24411. Each iCalendar file will be located in the same directory as the Org-mode
  24412. file, but with extension `.ics'."
  24413. (interactive)
  24414. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24415. ;;;###autoload
  24416. (defun org-export-icalendar-combine-agenda-files ()
  24417. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24418. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24419. (interactive)
  24420. (apply 'org-export-icalendar t (org-agenda-files t)))
  24421. (defun org-export-icalendar (combine &rest files)
  24422. "Create iCalendar files for all elements of FILES.
  24423. If COMBINE is non-nil, combine all calendar entries into a single large
  24424. file and store it under the name `org-combined-agenda-icalendar-file'."
  24425. (save-excursion
  24426. (org-prepare-agenda-buffers files)
  24427. (let* ((dir (org-export-directory
  24428. :ical (list :publishing-directory
  24429. org-export-publishing-directory)))
  24430. file ical-file ical-buffer category started org-agenda-new-buffers)
  24431. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24432. (when combine
  24433. (setq ical-file
  24434. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24435. org-combined-agenda-icalendar-file
  24436. (expand-file-name org-combined-agenda-icalendar-file dir))
  24437. ical-buffer (org-get-agenda-file-buffer ical-file))
  24438. (set-buffer ical-buffer) (erase-buffer))
  24439. (while (setq file (pop files))
  24440. (catch 'nextfile
  24441. (org-check-agenda-file file)
  24442. (set-buffer (org-get-agenda-file-buffer file))
  24443. (unless combine
  24444. (setq ical-file (concat (file-name-as-directory dir)
  24445. (file-name-sans-extension
  24446. (file-name-nondirectory buffer-file-name))
  24447. ".ics"))
  24448. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24449. (with-current-buffer ical-buffer (erase-buffer)))
  24450. (setq category (or org-category
  24451. (file-name-sans-extension
  24452. (file-name-nondirectory buffer-file-name))))
  24453. (if (symbolp category) (setq category (symbol-name category)))
  24454. (let ((standard-output ical-buffer))
  24455. (if combine
  24456. (and (not started) (setq started t)
  24457. (org-start-icalendar-file org-icalendar-combined-name))
  24458. (org-start-icalendar-file category))
  24459. (org-print-icalendar-entries combine)
  24460. (when (or (and combine (not files)) (not combine))
  24461. (org-finish-icalendar-file)
  24462. (set-buffer ical-buffer)
  24463. (save-buffer)
  24464. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24465. (org-release-buffers org-agenda-new-buffers))))
  24466. (defvar org-after-save-iCalendar-file-hook nil
  24467. "Hook run after an iCalendar file has been saved.
  24468. The iCalendar buffer is still current when this hook is run.
  24469. A good way to use this is to tell a desktop calenndar application to re-read
  24470. the iCalendar file.")
  24471. (defun org-print-icalendar-entries (&optional combine)
  24472. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24473. When COMBINE is non nil, add the category to each line."
  24474. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24475. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24476. (dts (org-ical-ts-to-string
  24477. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24478. "DTSTART"))
  24479. hd ts ts2 state status (inc t) pos b sexp rrule
  24480. scheduledp deadlinep tmp pri category entry location summary desc
  24481. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24482. (org-refresh-category-properties)
  24483. (save-excursion
  24484. (goto-char (point-min))
  24485. (while (re-search-forward re1 nil t)
  24486. (catch :skip
  24487. (org-agenda-skip)
  24488. (when (boundp 'org-icalendar-verify-function)
  24489. (unless (funcall org-icalendar-verify-function)
  24490. (outline-next-heading)
  24491. (backward-char 1)
  24492. (throw :skip nil)))
  24493. (setq pos (match-beginning 0)
  24494. ts (match-string 0)
  24495. inc t
  24496. hd (org-get-heading)
  24497. summary (org-icalendar-cleanup-string
  24498. (org-entry-get nil "SUMMARY"))
  24499. desc (org-icalendar-cleanup-string
  24500. (or (org-entry-get nil "DESCRIPTION")
  24501. (and org-icalendar-include-body (org-get-entry)))
  24502. t org-icalendar-include-body)
  24503. location (org-icalendar-cleanup-string
  24504. (org-entry-get nil "LOCATION"))
  24505. category (org-get-category))
  24506. (if (looking-at re2)
  24507. (progn
  24508. (goto-char (match-end 0))
  24509. (setq ts2 (match-string 1) inc nil))
  24510. (setq tmp (buffer-substring (max (point-min)
  24511. (- pos org-ds-keyword-length))
  24512. pos)
  24513. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24514. (progn
  24515. (setq inc nil)
  24516. (replace-match "\\1" t nil ts))
  24517. ts)
  24518. deadlinep (string-match org-deadline-regexp tmp)
  24519. scheduledp (string-match org-scheduled-regexp tmp)
  24520. ;; donep (org-entry-is-done-p)
  24521. ))
  24522. (if (or (string-match org-tr-regexp hd)
  24523. (string-match org-ts-regexp hd))
  24524. (setq hd (replace-match "" t t hd)))
  24525. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24526. (setq rrule
  24527. (concat "\nRRULE:FREQ="
  24528. (cdr (assoc
  24529. (match-string 2 ts)
  24530. '(("d" . "DAILY")("w" . "WEEKLY")
  24531. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24532. ";INTERVAL=" (match-string 1 ts)))
  24533. (setq rrule ""))
  24534. (setq summary (or summary hd))
  24535. (if (string-match org-bracket-link-regexp summary)
  24536. (setq summary
  24537. (replace-match (if (match-end 3)
  24538. (match-string 3 summary)
  24539. (match-string 1 summary))
  24540. t t summary)))
  24541. (if deadlinep (setq summary (concat "DL: " summary)))
  24542. (if scheduledp (setq summary (concat "S: " summary)))
  24543. (if (string-match "\\`<%%" ts)
  24544. (with-current-buffer sexp-buffer
  24545. (insert (substring ts 1 -1) " " summary "\n"))
  24546. (princ (format "BEGIN:VEVENT
  24547. %s
  24548. %s%s
  24549. SUMMARY:%s%s%s
  24550. CATEGORIES:%s
  24551. END:VEVENT\n"
  24552. (org-ical-ts-to-string ts "DTSTART")
  24553. (org-ical-ts-to-string ts2 "DTEND" inc)
  24554. rrule summary
  24555. (if (and desc (string-match "\\S-" desc))
  24556. (concat "\nDESCRIPTION: " desc) "")
  24557. (if (and location (string-match "\\S-" location))
  24558. (concat "\nLOCATION: " location) "")
  24559. category)))))
  24560. (when (and org-icalendar-include-sexps
  24561. (condition-case nil (require 'icalendar) (error nil))
  24562. (fboundp 'icalendar-export-region))
  24563. ;; Get all the literal sexps
  24564. (goto-char (point-min))
  24565. (while (re-search-forward "^&?%%(" nil t)
  24566. (catch :skip
  24567. (org-agenda-skip)
  24568. (setq b (match-beginning 0))
  24569. (goto-char (1- (match-end 0)))
  24570. (forward-sexp 1)
  24571. (end-of-line 1)
  24572. (setq sexp (buffer-substring b (point)))
  24573. (with-current-buffer sexp-buffer
  24574. (insert sexp "\n"))
  24575. (princ (org-diary-to-ical-string sexp-buffer)))))
  24576. (when org-icalendar-include-todo
  24577. (goto-char (point-min))
  24578. (while (re-search-forward org-todo-line-regexp nil t)
  24579. (catch :skip
  24580. (org-agenda-skip)
  24581. (when (boundp 'org-icalendar-verify-function)
  24582. (unless (funcall org-icalendar-verify-function)
  24583. (outline-next-heading)
  24584. (backward-char 1)
  24585. (throw :skip nil)))
  24586. (setq state (match-string 2))
  24587. (setq status (if (member state org-done-keywords)
  24588. "COMPLETED" "NEEDS-ACTION"))
  24589. (when (and state
  24590. (or (not (member state org-done-keywords))
  24591. (eq org-icalendar-include-todo 'all))
  24592. (not (member org-archive-tag (org-get-tags-at)))
  24593. )
  24594. (setq hd (match-string 3)
  24595. summary (org-icalendar-cleanup-string
  24596. (org-entry-get nil "SUMMARY"))
  24597. desc (org-icalendar-cleanup-string
  24598. (or (org-entry-get nil "DESCRIPTION")
  24599. (and org-icalendar-include-body (org-get-entry)))
  24600. t org-icalendar-include-body)
  24601. location (org-icalendar-cleanup-string
  24602. (org-entry-get nil "LOCATION")))
  24603. (if (string-match org-bracket-link-regexp hd)
  24604. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24605. (match-string 1 hd))
  24606. t t hd)))
  24607. (if (string-match org-priority-regexp hd)
  24608. (setq pri (string-to-char (match-string 2 hd))
  24609. hd (concat (substring hd 0 (match-beginning 1))
  24610. (substring hd (match-end 1))))
  24611. (setq pri org-default-priority))
  24612. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24613. (- org-lowest-priority org-highest-priority))))))
  24614. (princ (format "BEGIN:VTODO
  24615. %s
  24616. SUMMARY:%s%s%s
  24617. CATEGORIES:%s
  24618. SEQUENCE:1
  24619. PRIORITY:%d
  24620. STATUS:%s
  24621. END:VTODO\n"
  24622. dts
  24623. (or summary hd)
  24624. (if (and location (string-match "\\S-" location))
  24625. (concat "\nLOCATION: " location) "")
  24626. (if (and desc (string-match "\\S-" desc))
  24627. (concat "\nDESCRIPTION: " desc) "")
  24628. category pri status)))))))))
  24629. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24630. "Take out stuff and quote what needs to be quoted.
  24631. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24632. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24633. characters."
  24634. (if (not s)
  24635. nil
  24636. (when is-body
  24637. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24638. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24639. (while (string-match re s) (setq s (replace-match "" t t s)))
  24640. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24641. (let ((start 0))
  24642. (while (string-match "\\([,;\\]\\)" s start)
  24643. (setq start (+ (match-beginning 0) 2)
  24644. s (replace-match "\\\\\\1" nil nil s))))
  24645. (when is-body
  24646. (while (string-match "[ \t]*\n[ \t]*" s)
  24647. (setq s (replace-match "\\n" t t s))))
  24648. (setq s (org-trim s))
  24649. (if is-body
  24650. (if maxlength
  24651. (if (and (numberp maxlength)
  24652. (> (length s) maxlength))
  24653. (setq s (substring s 0 maxlength)))))
  24654. s))
  24655. (defun org-get-entry ()
  24656. "Clean-up description string."
  24657. (save-excursion
  24658. (org-back-to-heading t)
  24659. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24660. (defun org-start-icalendar-file (name)
  24661. "Start an iCalendar file by inserting the header."
  24662. (let ((user user-full-name)
  24663. (name (or name "unknown"))
  24664. (timezone (cadr (current-time-zone))))
  24665. (princ
  24666. (format "BEGIN:VCALENDAR
  24667. VERSION:2.0
  24668. X-WR-CALNAME:%s
  24669. PRODID:-//%s//Emacs with Org-mode//EN
  24670. X-WR-TIMEZONE:%s
  24671. CALSCALE:GREGORIAN\n" name user timezone))))
  24672. (defun org-finish-icalendar-file ()
  24673. "Finish an iCalendar file by inserting the END statement."
  24674. (princ "END:VCALENDAR\n"))
  24675. (defun org-ical-ts-to-string (s keyword &optional inc)
  24676. "Take a time string S and convert it to iCalendar format.
  24677. KEYWORD is added in front, to make a complete line like DTSTART....
  24678. When INC is non-nil, increase the hour by two (if time string contains
  24679. a time), or the day by one (if it does not contain a time)."
  24680. (let ((t1 (org-parse-time-string s 'nodefault))
  24681. t2 fmt have-time time)
  24682. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24683. (setq t2 t1 have-time t)
  24684. (setq t2 (org-parse-time-string s)))
  24685. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24686. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24687. (when inc
  24688. (if have-time
  24689. (if org-agenda-default-appointment-duration
  24690. (setq mi (+ org-agenda-default-appointment-duration mi))
  24691. (setq h (+ 2 h)))
  24692. (setq d (1+ d))))
  24693. (setq time (encode-time s mi h d m y)))
  24694. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24695. (concat keyword (format-time-string fmt time))))
  24696. ;;; XOXO export
  24697. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24698. (with-current-buffer buffer
  24699. (apply 'insert output)))
  24700. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24701. (defun org-export-as-xoxo (&optional buffer)
  24702. "Export the org buffer as XOXO.
  24703. The XOXO buffer is named *xoxo-<source buffer name>*"
  24704. (interactive (list (current-buffer)))
  24705. ;; A quickie abstraction
  24706. ;; Output everything as XOXO
  24707. (with-current-buffer (get-buffer buffer)
  24708. (let* ((pos (point))
  24709. (opt-plist (org-combine-plists (org-default-export-plist)
  24710. (org-infile-export-plist)))
  24711. (filename (concat (file-name-as-directory
  24712. (org-export-directory :xoxo opt-plist))
  24713. (file-name-sans-extension
  24714. (file-name-nondirectory buffer-file-name))
  24715. ".html"))
  24716. (out (find-file-noselect filename))
  24717. (last-level 1)
  24718. (hanging-li nil))
  24719. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24720. ;; Check the output buffer is empty.
  24721. (with-current-buffer out (erase-buffer))
  24722. ;; Kick off the output
  24723. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24724. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24725. (let* ((hd (match-string-no-properties 1))
  24726. (level (length hd))
  24727. (text (concat
  24728. (match-string-no-properties 2)
  24729. (save-excursion
  24730. (goto-char (match-end 0))
  24731. (let ((str ""))
  24732. (catch 'loop
  24733. (while 't
  24734. (forward-line)
  24735. (if (looking-at "^[ \t]\\(.*\\)")
  24736. (setq str (concat str (match-string-no-properties 1)))
  24737. (throw 'loop str)))))))))
  24738. ;; Handle level rendering
  24739. (cond
  24740. ((> level last-level)
  24741. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24742. ((< level last-level)
  24743. (dotimes (- (- last-level level) 1)
  24744. (if hanging-li
  24745. (org-export-as-xoxo-insert-into out "</li>\n"))
  24746. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24747. (when hanging-li
  24748. (org-export-as-xoxo-insert-into out "</li>\n")
  24749. (setq hanging-li nil)))
  24750. ((equal level last-level)
  24751. (if hanging-li
  24752. (org-export-as-xoxo-insert-into out "</li>\n")))
  24753. )
  24754. (setq last-level level)
  24755. ;; And output the new li
  24756. (setq hanging-li 't)
  24757. (if (equal ?+ (elt text 0))
  24758. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24759. (org-export-as-xoxo-insert-into out "<li>" text))))
  24760. ;; Finally finish off the ol
  24761. (dotimes (- last-level 1)
  24762. (if hanging-li
  24763. (org-export-as-xoxo-insert-into out "</li>\n"))
  24764. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24765. (goto-char pos)
  24766. ;; Finish the buffer off and clean it up.
  24767. (switch-to-buffer-other-window out)
  24768. (indent-region (point-min) (point-max) nil)
  24769. (save-buffer)
  24770. (goto-char (point-min))
  24771. )))
  24772. ;;;; Key bindings
  24773. ;; Make `C-c C-x' a prefix key
  24774. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24775. ;; TAB key with modifiers
  24776. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24777. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24778. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24779. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24780. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24781. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24782. ;; The following line is necessary under Suse GNU/Linux
  24783. (unless (featurep 'xemacs)
  24784. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24785. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24786. (define-key org-mode-map [backtab] 'org-shifttab)
  24787. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24788. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24789. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24790. ;; Cursor keys with modifiers
  24791. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24792. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24793. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24794. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24795. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24796. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24797. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24798. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24799. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24800. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24801. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24802. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24803. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24804. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24805. ;;; Extra keys for tty access.
  24806. ;; We only set them when really needed because otherwise the
  24807. ;; menus don't show the simple keys
  24808. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24809. (not window-system))
  24810. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24811. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24812. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24813. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24814. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24815. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24816. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24817. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24818. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24819. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24820. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24821. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24822. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24823. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24824. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24825. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24826. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24827. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24828. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24829. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24830. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24831. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24832. ;; All the other keys
  24833. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24834. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24835. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24836. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24837. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24838. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24839. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24840. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24841. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24842. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24843. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24844. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24845. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24846. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24847. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24848. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24849. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24850. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24851. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24852. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24853. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24854. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24855. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24856. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24857. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24858. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24859. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24860. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24861. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24862. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24863. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24864. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24865. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24866. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24867. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24868. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24869. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24870. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24871. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24872. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  24873. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24874. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24875. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24876. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24877. (org-defkey org-mode-map "\C-m" 'org-return)
  24878. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24879. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24880. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24881. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24882. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24883. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24884. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24885. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24886. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24887. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24888. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24889. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24890. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24891. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24892. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24893. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24894. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24895. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24896. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24897. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24898. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24899. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24900. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24901. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24902. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24903. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24904. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24905. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24906. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24907. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24908. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24909. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24910. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24911. (when (featurep 'xemacs)
  24912. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24913. (defsubst org-table-p () (org-at-table-p))
  24914. (defun org-self-insert-command (N)
  24915. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24916. If the cursor is in a table looking at whitespace, the whitespace is
  24917. overwritten, and the table is not marked as requiring realignment."
  24918. (interactive "p")
  24919. (if (and (org-table-p)
  24920. (progn
  24921. ;; check if we blank the field, and if that triggers align
  24922. (and org-table-auto-blank-field
  24923. (member last-command
  24924. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24925. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24926. ;; got extra space, this field does not determine column width
  24927. (let (org-table-may-need-update) (org-table-blank-field))
  24928. ;; no extra space, this field may determine column width
  24929. (org-table-blank-field)))
  24930. t)
  24931. (eq N 1)
  24932. (looking-at "[^|\n]* |"))
  24933. (let (org-table-may-need-update)
  24934. (goto-char (1- (match-end 0)))
  24935. (delete-backward-char 1)
  24936. (goto-char (match-beginning 0))
  24937. (self-insert-command N))
  24938. (setq org-table-may-need-update t)
  24939. (self-insert-command N)
  24940. (org-fix-tags-on-the-fly)))
  24941. (defun org-fix-tags-on-the-fly ()
  24942. (when (and (equal (char-after (point-at-bol)) ?*)
  24943. (org-on-heading-p))
  24944. (org-align-tags-here org-tags-column)))
  24945. (defun org-delete-backward-char (N)
  24946. "Like `delete-backward-char', insert whitespace at field end in tables.
  24947. When deleting backwards, in tables this function will insert whitespace in
  24948. front of the next \"|\" separator, to keep the table aligned. The table will
  24949. still be marked for re-alignment if the field did fill the entire column,
  24950. because, in this case the deletion might narrow the column."
  24951. (interactive "p")
  24952. (if (and (org-table-p)
  24953. (eq N 1)
  24954. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24955. (looking-at ".*?|"))
  24956. (let ((pos (point))
  24957. (noalign (looking-at "[^|\n\r]* |"))
  24958. (c org-table-may-need-update))
  24959. (backward-delete-char N)
  24960. (skip-chars-forward "^|")
  24961. (insert " ")
  24962. (goto-char (1- pos))
  24963. ;; noalign: if there were two spaces at the end, this field
  24964. ;; does not determine the width of the column.
  24965. (if noalign (setq org-table-may-need-update c)))
  24966. (backward-delete-char N)
  24967. (org-fix-tags-on-the-fly)))
  24968. (defun org-delete-char (N)
  24969. "Like `delete-char', but insert whitespace at field end in tables.
  24970. When deleting characters, in tables this function will insert whitespace in
  24971. front of the next \"|\" separator, to keep the table aligned. The table will
  24972. still be marked for re-alignment if the field did fill the entire column,
  24973. because, in this case the deletion might narrow the column."
  24974. (interactive "p")
  24975. (if (and (org-table-p)
  24976. (not (bolp))
  24977. (not (= (char-after) ?|))
  24978. (eq N 1))
  24979. (if (looking-at ".*?|")
  24980. (let ((pos (point))
  24981. (noalign (looking-at "[^|\n\r]* |"))
  24982. (c org-table-may-need-update))
  24983. (replace-match (concat
  24984. (substring (match-string 0) 1 -1)
  24985. " |"))
  24986. (goto-char pos)
  24987. ;; noalign: if there were two spaces at the end, this field
  24988. ;; does not determine the width of the column.
  24989. (if noalign (setq org-table-may-need-update c)))
  24990. (delete-char N))
  24991. (delete-char N)
  24992. (org-fix-tags-on-the-fly)))
  24993. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24994. (put 'org-self-insert-command 'delete-selection t)
  24995. (put 'orgtbl-self-insert-command 'delete-selection t)
  24996. (put 'org-delete-char 'delete-selection 'supersede)
  24997. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24998. ;; Make `flyspell-mode' delay after some commands
  24999. (put 'org-self-insert-command 'flyspell-delayed t)
  25000. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  25001. (put 'org-delete-char 'flyspell-delayed t)
  25002. (put 'org-delete-backward-char 'flyspell-delayed t)
  25003. ;; Make pabbrev-mode expand after org-mode commands
  25004. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  25005. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  25006. ;; How to do this: Measure non-white length of current string
  25007. ;; If equal to column width, we should realign.
  25008. (defun org-remap (map &rest commands)
  25009. "In MAP, remap the functions given in COMMANDS.
  25010. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  25011. (let (new old)
  25012. (while commands
  25013. (setq old (pop commands) new (pop commands))
  25014. (if (fboundp 'command-remapping)
  25015. (org-defkey map (vector 'remap old) new)
  25016. (substitute-key-definition old new map global-map)))))
  25017. (when (eq org-enable-table-editor 'optimized)
  25018. ;; If the user wants maximum table support, we need to hijack
  25019. ;; some standard editing functions
  25020. (org-remap org-mode-map
  25021. 'self-insert-command 'org-self-insert-command
  25022. 'delete-char 'org-delete-char
  25023. 'delete-backward-char 'org-delete-backward-char)
  25024. (org-defkey org-mode-map "|" 'org-force-self-insert))
  25025. (defun org-shiftcursor-error ()
  25026. "Throw an error because Shift-Cursor command was applied in wrong context."
  25027. (error "This command is active in special context like tables, headlines or timestamps"))
  25028. (defun org-shifttab (&optional arg)
  25029. "Global visibility cycling or move to previous table field.
  25030. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  25031. on context.
  25032. See the individual commands for more information."
  25033. (interactive "P")
  25034. (cond
  25035. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  25036. (arg (message "Content view to level: ")
  25037. (org-content (prefix-numeric-value arg))
  25038. (setq org-cycle-global-status 'overview))
  25039. (t (call-interactively 'org-global-cycle))))
  25040. (defun org-shiftmetaleft ()
  25041. "Promote subtree or delete table column.
  25042. Calls `org-promote-subtree', `org-outdent-item',
  25043. or `org-table-delete-column', depending on context.
  25044. See the individual commands for more information."
  25045. (interactive)
  25046. (cond
  25047. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  25048. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  25049. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25050. (t (org-shiftcursor-error))))
  25051. (defun org-shiftmetaright ()
  25052. "Demote subtree or insert table column.
  25053. Calls `org-demote-subtree', `org-indent-item',
  25054. or `org-table-insert-column', depending on context.
  25055. See the individual commands for more information."
  25056. (interactive)
  25057. (cond
  25058. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  25059. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  25060. ((org-at-item-p) (call-interactively 'org-indent-item))
  25061. (t (org-shiftcursor-error))))
  25062. (defun org-shiftmetaup (&optional arg)
  25063. "Move subtree up or kill table row.
  25064. Calls `org-move-subtree-up' or `org-table-kill-row' or
  25065. `org-move-item-up' depending on context. See the individual commands
  25066. for more information."
  25067. (interactive "P")
  25068. (cond
  25069. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  25070. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25071. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25072. (t (org-shiftcursor-error))))
  25073. (defun org-shiftmetadown (&optional arg)
  25074. "Move subtree down or insert table row.
  25075. Calls `org-move-subtree-down' or `org-table-insert-row' or
  25076. `org-move-item-down', depending on context. See the individual
  25077. commands for more information."
  25078. (interactive "P")
  25079. (cond
  25080. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  25081. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25082. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25083. (t (org-shiftcursor-error))))
  25084. (defun org-metaleft (&optional arg)
  25085. "Promote heading or move table column to left.
  25086. Calls `org-do-promote' or `org-table-move-column', depending on context.
  25087. With no specific context, calls the Emacs default `backward-word'.
  25088. See the individual commands for more information."
  25089. (interactive "P")
  25090. (cond
  25091. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  25092. ((or (org-on-heading-p) (org-region-active-p))
  25093. (call-interactively 'org-do-promote))
  25094. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25095. (t (call-interactively 'backward-word))))
  25096. (defun org-metaright (&optional arg)
  25097. "Demote subtree or move table column to right.
  25098. Calls `org-do-demote' or `org-table-move-column', depending on context.
  25099. With no specific context, calls the Emacs default `forward-word'.
  25100. See the individual commands for more information."
  25101. (interactive "P")
  25102. (cond
  25103. ((org-at-table-p) (call-interactively 'org-table-move-column))
  25104. ((or (org-on-heading-p) (org-region-active-p))
  25105. (call-interactively 'org-do-demote))
  25106. ((org-at-item-p) (call-interactively 'org-indent-item))
  25107. (t (call-interactively 'forward-word))))
  25108. (defun org-metaup (&optional arg)
  25109. "Move subtree up or move table row up.
  25110. Calls `org-move-subtree-up' or `org-table-move-row' or
  25111. `org-move-item-up', depending on context. See the individual commands
  25112. for more information."
  25113. (interactive "P")
  25114. (cond
  25115. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  25116. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25117. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25118. (t (transpose-lines 1) (beginning-of-line -1))))
  25119. (defun org-metadown (&optional arg)
  25120. "Move subtree down or move table row down.
  25121. Calls `org-move-subtree-down' or `org-table-move-row' or
  25122. `org-move-item-down', depending on context. See the individual
  25123. commands for more information."
  25124. (interactive "P")
  25125. (cond
  25126. ((org-at-table-p) (call-interactively 'org-table-move-row))
  25127. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25128. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25129. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  25130. (defun org-shiftup (&optional arg)
  25131. "Increase item in timestamp or increase priority of current headline.
  25132. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  25133. depending on context. See the individual commands for more information."
  25134. (interactive "P")
  25135. (cond
  25136. ((org-at-timestamp-p t)
  25137. (call-interactively (if org-edit-timestamp-down-means-later
  25138. 'org-timestamp-down 'org-timestamp-up)))
  25139. ((org-on-heading-p) (call-interactively 'org-priority-up))
  25140. ((org-at-item-p) (call-interactively 'org-previous-item))
  25141. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  25142. (defun org-shiftdown (&optional arg)
  25143. "Decrease item in timestamp or decrease priority of current headline.
  25144. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  25145. depending on context. See the individual commands for more information."
  25146. (interactive "P")
  25147. (cond
  25148. ((org-at-timestamp-p t)
  25149. (call-interactively (if org-edit-timestamp-down-means-later
  25150. 'org-timestamp-up 'org-timestamp-down)))
  25151. ((org-on-heading-p) (call-interactively 'org-priority-down))
  25152. (t (call-interactively 'org-next-item))))
  25153. (defun org-shiftright ()
  25154. "Next TODO keyword or timestamp one day later, depending on context."
  25155. (interactive)
  25156. (cond
  25157. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  25158. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  25159. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  25160. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  25161. (t (org-shiftcursor-error))))
  25162. (defun org-shiftleft ()
  25163. "Previous TODO keyword or timestamp one day earlier, depending on context."
  25164. (interactive)
  25165. (cond
  25166. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  25167. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  25168. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  25169. ((org-at-property-p)
  25170. (call-interactively 'org-property-previous-allowed-value))
  25171. (t (org-shiftcursor-error))))
  25172. (defun org-shiftcontrolright ()
  25173. "Switch to next TODO set."
  25174. (interactive)
  25175. (cond
  25176. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  25177. (t (org-shiftcursor-error))))
  25178. (defun org-shiftcontrolleft ()
  25179. "Switch to previous TODO set."
  25180. (interactive)
  25181. (cond
  25182. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  25183. (t (org-shiftcursor-error))))
  25184. (defun org-ctrl-c-ret ()
  25185. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  25186. (interactive)
  25187. (cond
  25188. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  25189. (t (call-interactively 'org-insert-heading))))
  25190. (defun org-copy-special ()
  25191. "Copy region in table or copy current subtree.
  25192. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  25193. See the individual commands for more information."
  25194. (interactive)
  25195. (call-interactively
  25196. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25197. (defun org-cut-special ()
  25198. "Cut region in table or cut current subtree.
  25199. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25200. See the individual commands for more information."
  25201. (interactive)
  25202. (call-interactively
  25203. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25204. (defun org-paste-special (arg)
  25205. "Paste rectangular region into table, or past subtree relative to level.
  25206. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25207. See the individual commands for more information."
  25208. (interactive "P")
  25209. (if (org-at-table-p)
  25210. (org-table-paste-rectangle)
  25211. (org-paste-subtree arg)))
  25212. (defun org-ctrl-c-ctrl-c (&optional arg)
  25213. "Set tags in headline, or update according to changed information at point.
  25214. This command does many different things, depending on context:
  25215. - If the cursor is in a headline, prompt for tags and insert them
  25216. into the current line, aligned to `org-tags-column'. When called
  25217. with prefix arg, realign all tags in the current buffer.
  25218. - If the cursor is in one of the special #+KEYWORD lines, this
  25219. triggers scanning the buffer for these lines and updating the
  25220. information.
  25221. - If the cursor is inside a table, realign the table. This command
  25222. works even if the automatic table editor has been turned off.
  25223. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25224. the entire table.
  25225. - If the cursor is a the beginning of a dynamic block, update it.
  25226. - If the cursor is inside a table created by the table.el package,
  25227. activate that table.
  25228. - If the current buffer is a remember buffer, close note and file it.
  25229. with a prefix argument, file it without further interaction to the default
  25230. location.
  25231. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25232. links in this buffer.
  25233. - If the cursor is on a numbered item in a plain list, renumber the
  25234. ordered list.
  25235. - If the cursor is on a checkbox, toggle it."
  25236. (interactive "P")
  25237. (let ((org-enable-table-editor t))
  25238. (cond
  25239. ((or org-clock-overlays
  25240. org-occur-highlights
  25241. org-latex-fragment-image-overlays)
  25242. (org-remove-clock-overlays)
  25243. (org-remove-occur-highlights)
  25244. (org-remove-latex-fragment-image-overlays)
  25245. (message "Temporary highlights/overlays removed from current buffer"))
  25246. ((and (local-variable-p 'org-finish-function (current-buffer))
  25247. (fboundp org-finish-function))
  25248. (funcall org-finish-function))
  25249. ((org-at-property-p)
  25250. (call-interactively 'org-property-action))
  25251. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25252. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25253. ((org-at-table.el-p)
  25254. (require 'table)
  25255. (beginning-of-line 1)
  25256. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25257. (call-interactively 'table-recognize-table))
  25258. ((org-at-table-p)
  25259. (org-table-maybe-eval-formula)
  25260. (if arg
  25261. (call-interactively 'org-table-recalculate)
  25262. (org-table-maybe-recalculate-line))
  25263. (call-interactively 'org-table-align))
  25264. ((org-at-item-checkbox-p)
  25265. (call-interactively 'org-toggle-checkbox))
  25266. ((org-at-item-p)
  25267. (call-interactively 'org-maybe-renumber-ordered-list))
  25268. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25269. ;; Dynamic block
  25270. (beginning-of-line 1)
  25271. (org-update-dblock))
  25272. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25273. (cond
  25274. ((equal (match-string 1) "TBLFM")
  25275. ;; Recalculate the table before this line
  25276. (save-excursion
  25277. (beginning-of-line 1)
  25278. (skip-chars-backward " \r\n\t")
  25279. (if (org-at-table-p)
  25280. (org-call-with-arg 'org-table-recalculate t))))
  25281. (t
  25282. (call-interactively 'org-mode-restart))))
  25283. (t (error "C-c C-c can do nothing useful at this location.")))))
  25284. (defun org-mode-restart ()
  25285. "Restart Org-mode, to scan again for special lines.
  25286. Also updates the keyword regular expressions."
  25287. (interactive)
  25288. (let ((org-inhibit-startup t)) (org-mode))
  25289. (message "Org-mode restarted to refresh keyword and special line setup"))
  25290. (defun org-kill-note-or-show-branches ()
  25291. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25292. (interactive)
  25293. (if (not org-finish-function)
  25294. (call-interactively 'show-branches)
  25295. (let ((org-note-abort t))
  25296. (funcall org-finish-function))))
  25297. (defun org-return (&optional indent)
  25298. "Goto next table row or insert a newline.
  25299. Calls `org-table-next-row' or `newline', depending on context.
  25300. See the individual commands for more information."
  25301. (interactive)
  25302. (cond
  25303. ((bobp) (if indent (newline-and-indent) (newline)))
  25304. ((and (org-at-heading-p)
  25305. (looking-at
  25306. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25307. (org-show-entry)
  25308. (end-of-line 1)
  25309. (newline))
  25310. ((org-at-table-p)
  25311. (org-table-justify-field-maybe)
  25312. (call-interactively 'org-table-next-row))
  25313. (t (if indent (newline-and-indent) (newline)))))
  25314. (defun org-return-indent ()
  25315. "Goto next table row or insert a newline and indent.
  25316. Calls `org-table-next-row' or `newline-and-indent', depending on
  25317. context. See the individual commands for more information."
  25318. (interactive)
  25319. (org-return t))
  25320. (defun org-ctrl-c-star ()
  25321. "Compute table, or change heading status of lines.
  25322. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  25323. depending on context. This will also turn a plain list item or a normal
  25324. line into a subheading."
  25325. (interactive)
  25326. (cond
  25327. ((org-at-table-p)
  25328. (call-interactively 'org-table-recalculate))
  25329. ((org-region-active-p)
  25330. ;; Convert all lines in region to list items
  25331. (call-interactively 'org-toggle-region-headings))
  25332. ((org-on-heading-p)
  25333. (org-toggle-region-headings (point-at-bol)
  25334. (min (1+ (point-at-eol)) (point-max))))
  25335. ((org-at-item-p)
  25336. ;; Convert to heading
  25337. (let ((level (save-match-data
  25338. (save-excursion
  25339. (condition-case nil
  25340. (progn
  25341. (org-back-to-heading t)
  25342. (funcall outline-level))
  25343. (error 0))))))
  25344. (replace-match
  25345. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  25346. (t (org-toggle-region-headings (point-at-bol)
  25347. (min (1+ (point-at-eol)) (point-max))))))
  25348. (defun org-ctrl-c-minus ()
  25349. "Insert separator line in table or modify bullet status of line.
  25350. Also turns a plain line or a region of lines into list items.
  25351. Calls `org-table-insert-hline', `org-toggle-region-items', or
  25352. `org-cycle-list-bullet', depending on context."
  25353. (interactive)
  25354. (cond
  25355. ((org-at-table-p)
  25356. (call-interactively 'org-table-insert-hline))
  25357. ((org-on-heading-p)
  25358. ;; Convert to item
  25359. (save-excursion
  25360. (beginning-of-line 1)
  25361. (if (looking-at "\\*+ ")
  25362. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  25363. ((org-region-active-p)
  25364. ;; Convert all lines in region to list items
  25365. (call-interactively 'org-toggle-region-items))
  25366. ((org-in-item-p)
  25367. (call-interactively 'org-cycle-list-bullet))
  25368. (t (org-toggle-region-items (point-at-bol)
  25369. (min (1+ (point-at-eol)) (point-max))))))
  25370. (defun org-toggle-region-items (beg end)
  25371. "Convert all lines in region to list items.
  25372. If the first line is already an item, convert all list items in the region
  25373. to normal lines."
  25374. (interactive "r")
  25375. (let (l2 l)
  25376. (save-excursion
  25377. (goto-char end)
  25378. (setq l2 (org-current-line))
  25379. (goto-char beg)
  25380. (beginning-of-line 1)
  25381. (setq l (1- (org-current-line)))
  25382. (if (org-at-item-p)
  25383. ;; We already have items, de-itemize
  25384. (while (< (setq l (1+ l)) l2)
  25385. (when (org-at-item-p)
  25386. (goto-char (match-beginning 2))
  25387. (delete-region (match-beginning 2) (match-end 2))
  25388. (and (looking-at "[ \t]+") (replace-match "")))
  25389. (beginning-of-line 2))
  25390. (while (< (setq l (1+ l)) l2)
  25391. (unless (org-at-item-p)
  25392. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25393. (replace-match "\\1- \\2")))
  25394. (beginning-of-line 2))))))
  25395. (defun org-toggle-region-headings (beg end)
  25396. "Convert all lines in region to list items.
  25397. If the first line is already an item, convert all list items in the region
  25398. to normal lines."
  25399. (interactive "r")
  25400. (let (l2 l)
  25401. (save-excursion
  25402. (goto-char end)
  25403. (setq l2 (org-current-line))
  25404. (goto-char beg)
  25405. (beginning-of-line 1)
  25406. (setq l (1- (org-current-line)))
  25407. (if (org-on-heading-p)
  25408. ;; We already have headlines, de-star them
  25409. (while (< (setq l (1+ l)) l2)
  25410. (when (org-on-heading-p t)
  25411. (and (looking-at outline-regexp) (replace-match "")))
  25412. (beginning-of-line 2))
  25413. (let* ((stars (save-excursion
  25414. (re-search-backward org-complex-heading-regexp nil t)
  25415. (or (match-string 1) "*")))
  25416. (add-stars (if org-odd-levels-only "**" "*"))
  25417. (rpl (concat stars add-stars " \\2")))
  25418. (while (< (setq l (1+ l)) l2)
  25419. (unless (org-on-heading-p)
  25420. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25421. (replace-match rpl)))
  25422. (beginning-of-line 2)))))))
  25423. (defun org-meta-return (&optional arg)
  25424. "Insert a new heading or wrap a region in a table.
  25425. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25426. See the individual commands for more information."
  25427. (interactive "P")
  25428. (cond
  25429. ((org-at-table-p)
  25430. (call-interactively 'org-table-wrap-region))
  25431. (t (call-interactively 'org-insert-heading))))
  25432. ;;; Menu entries
  25433. ;; Define the Org-mode menus
  25434. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25435. '("Tbl"
  25436. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25437. ["Next Field" org-cycle (org-at-table-p)]
  25438. ["Previous Field" org-shifttab (org-at-table-p)]
  25439. ["Next Row" org-return (org-at-table-p)]
  25440. "--"
  25441. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25442. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25443. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25444. "--"
  25445. ("Column"
  25446. ["Move Column Left" org-metaleft (org-at-table-p)]
  25447. ["Move Column Right" org-metaright (org-at-table-p)]
  25448. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25449. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25450. ("Row"
  25451. ["Move Row Up" org-metaup (org-at-table-p)]
  25452. ["Move Row Down" org-metadown (org-at-table-p)]
  25453. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25454. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25455. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25456. "--"
  25457. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25458. ("Rectangle"
  25459. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25460. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25461. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25462. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25463. "--"
  25464. ("Calculate"
  25465. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25466. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25467. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25468. "--"
  25469. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25470. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25471. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25472. "--"
  25473. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25474. "--"
  25475. ["Sum Column/Rectangle" org-table-sum
  25476. (or (org-at-table-p) (org-region-active-p))]
  25477. ["Which Column?" org-table-current-column (org-at-table-p)])
  25478. ["Debug Formulas"
  25479. org-table-toggle-formula-debugger
  25480. :style toggle :selected org-table-formula-debug]
  25481. ["Show Col/Row Numbers"
  25482. org-table-toggle-coordinate-overlays
  25483. :style toggle :selected org-table-overlay-coordinates]
  25484. "--"
  25485. ["Create" org-table-create (and (not (org-at-table-p))
  25486. org-enable-table-editor)]
  25487. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25488. ["Import from File" org-table-import (not (org-at-table-p))]
  25489. ["Export to File" org-table-export (org-at-table-p)]
  25490. "--"
  25491. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25492. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25493. '("Org"
  25494. ("Show/Hide"
  25495. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25496. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25497. ["Sparse Tree" org-occur t]
  25498. ["Reveal Context" org-reveal t]
  25499. ["Show All" show-all t]
  25500. "--"
  25501. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25502. "--"
  25503. ["New Heading" org-insert-heading t]
  25504. ("Navigate Headings"
  25505. ["Up" outline-up-heading t]
  25506. ["Next" outline-next-visible-heading t]
  25507. ["Previous" outline-previous-visible-heading t]
  25508. ["Next Same Level" outline-forward-same-level t]
  25509. ["Previous Same Level" outline-backward-same-level t]
  25510. "--"
  25511. ["Jump" org-goto t])
  25512. ("Edit Structure"
  25513. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25514. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25515. "--"
  25516. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25517. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25518. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25519. "--"
  25520. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25521. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25522. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25523. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25524. "--"
  25525. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25526. "--"
  25527. ["Convert to odd levels" org-convert-to-odd-levels t]
  25528. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25529. ("Editing"
  25530. ["Emphasis..." org-emphasize t])
  25531. ("Archive"
  25532. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25533. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25534. ; :active t :keys "C-u C-c C-x C-a"]
  25535. ["Sparse trees open ARCHIVE trees"
  25536. (setq org-sparse-tree-open-archived-trees
  25537. (not org-sparse-tree-open-archived-trees))
  25538. :style toggle :selected org-sparse-tree-open-archived-trees]
  25539. ["Cycling opens ARCHIVE trees"
  25540. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25541. :style toggle :selected org-cycle-open-archived-trees]
  25542. ["Agenda includes ARCHIVE trees"
  25543. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25544. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25545. "--"
  25546. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25547. ; ["Check and Move Children" (org-archive-subtree '(4))
  25548. ; :active t :keys "C-u C-c C-x C-s"]
  25549. )
  25550. "--"
  25551. ("TODO Lists"
  25552. ["TODO/DONE/-" org-todo t]
  25553. ("Select keyword"
  25554. ["Next keyword" org-shiftright (org-on-heading-p)]
  25555. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25556. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25557. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25558. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25559. ["Show TODO Tree" org-show-todo-tree t]
  25560. ["Global TODO list" org-todo-list t]
  25561. "--"
  25562. ["Set Priority" org-priority t]
  25563. ["Priority Up" org-shiftup t]
  25564. ["Priority Down" org-shiftdown t])
  25565. ("TAGS and Properties"
  25566. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25567. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25568. "--"
  25569. ["Set property" 'org-set-property t]
  25570. ["Column view of properties" org-columns t]
  25571. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25572. ("Dates and Scheduling"
  25573. ["Timestamp" org-time-stamp t]
  25574. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25575. ("Change Date"
  25576. ["1 Day Later" org-shiftright t]
  25577. ["1 Day Earlier" org-shiftleft t]
  25578. ["1 ... Later" org-shiftup t]
  25579. ["1 ... Earlier" org-shiftdown t])
  25580. ["Compute Time Range" org-evaluate-time-range t]
  25581. ["Schedule Item" org-schedule t]
  25582. ["Deadline" org-deadline t]
  25583. "--"
  25584. ["Custom time format" org-toggle-time-stamp-overlays
  25585. :style radio :selected org-display-custom-times]
  25586. "--"
  25587. ["Goto Calendar" org-goto-calendar t]
  25588. ["Date from Calendar" org-date-from-calendar t])
  25589. ("Logging work"
  25590. ["Clock in" org-clock-in t]
  25591. ["Clock out" org-clock-out t]
  25592. ["Clock cancel" org-clock-cancel t]
  25593. ["Goto running clock" org-clock-goto t]
  25594. ["Display times" org-clock-display t]
  25595. ["Create clock table" org-clock-report t]
  25596. "--"
  25597. ["Record DONE time"
  25598. (progn (setq org-log-done (not org-log-done))
  25599. (message "Switching to %s will %s record a timestamp"
  25600. (car org-done-keywords)
  25601. (if org-log-done "automatically" "not")))
  25602. :style toggle :selected org-log-done])
  25603. "--"
  25604. ["Agenda Command..." org-agenda t]
  25605. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25606. ("File List for Agenda")
  25607. ("Special views current file"
  25608. ["TODO Tree" org-show-todo-tree t]
  25609. ["Check Deadlines" org-check-deadlines t]
  25610. ["Timeline" org-timeline t]
  25611. ["Tags Tree" org-tags-sparse-tree t])
  25612. "--"
  25613. ("Hyperlinks"
  25614. ["Store Link (Global)" org-store-link t]
  25615. ["Insert Link" org-insert-link t]
  25616. ["Follow Link" org-open-at-point t]
  25617. "--"
  25618. ["Next link" org-next-link t]
  25619. ["Previous link" org-previous-link t]
  25620. "--"
  25621. ["Descriptive Links"
  25622. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25623. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25624. ["Literal Links"
  25625. (progn
  25626. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25627. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25628. "--"
  25629. ["Export/Publish..." org-export t]
  25630. ("LaTeX"
  25631. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25632. :selected org-cdlatex-mode]
  25633. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25634. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25635. ["Modify math symbol" org-cdlatex-math-modify
  25636. (org-inside-LaTeX-fragment-p)]
  25637. ["Export LaTeX fragments as images"
  25638. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25639. :style toggle :selected org-export-with-LaTeX-fragments])
  25640. "--"
  25641. ("Documentation"
  25642. ["Show Version" org-version t]
  25643. ["Info Documentation" org-info t])
  25644. ("Customize"
  25645. ["Browse Org Group" org-customize t]
  25646. "--"
  25647. ["Expand This Menu" org-create-customize-menu
  25648. (fboundp 'customize-menu-create)])
  25649. "--"
  25650. ["Refresh setup" org-mode-restart t]
  25651. ))
  25652. (defun org-info (&optional node)
  25653. "Read documentation for Org-mode in the info system.
  25654. With optional NODE, go directly to that node."
  25655. (interactive)
  25656. (info (format "(org)%s" (or node ""))))
  25657. (defun org-install-agenda-files-menu ()
  25658. (let ((bl (buffer-list)))
  25659. (save-excursion
  25660. (while bl
  25661. (set-buffer (pop bl))
  25662. (if (org-mode-p) (setq bl nil)))
  25663. (when (org-mode-p)
  25664. (easy-menu-change
  25665. '("Org") "File List for Agenda"
  25666. (append
  25667. (list
  25668. ["Edit File List" (org-edit-agenda-file-list) t]
  25669. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25670. ["Remove Current File from List" org-remove-file t]
  25671. ["Cycle through agenda files" org-cycle-agenda-files t]
  25672. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25673. "--")
  25674. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25675. ;;;; Documentation
  25676. (defun org-customize ()
  25677. "Call the customize function with org as argument."
  25678. (interactive)
  25679. (customize-browse 'org))
  25680. (defun org-create-customize-menu ()
  25681. "Create a full customization menu for Org-mode, insert it into the menu."
  25682. (interactive)
  25683. (if (fboundp 'customize-menu-create)
  25684. (progn
  25685. (easy-menu-change
  25686. '("Org") "Customize"
  25687. `(["Browse Org group" org-customize t]
  25688. "--"
  25689. ,(customize-menu-create 'org)
  25690. ["Set" Custom-set t]
  25691. ["Save" Custom-save t]
  25692. ["Reset to Current" Custom-reset-current t]
  25693. ["Reset to Saved" Custom-reset-saved t]
  25694. ["Reset to Standard Settings" Custom-reset-standard t]))
  25695. (message "\"Org\"-menu now contains full customization menu"))
  25696. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25697. ;;;; Miscellaneous stuff
  25698. ;;; Generally useful functions
  25699. (defun org-context ()
  25700. "Return a list of contexts of the current cursor position.
  25701. If several contexts apply, all are returned.
  25702. Each context entry is a list with a symbol naming the context, and
  25703. two positions indicating start and end of the context. Possible
  25704. contexts are:
  25705. :headline anywhere in a headline
  25706. :headline-stars on the leading stars in a headline
  25707. :todo-keyword on a TODO keyword (including DONE) in a headline
  25708. :tags on the TAGS in a headline
  25709. :priority on the priority cookie in a headline
  25710. :item on the first line of a plain list item
  25711. :item-bullet on the bullet/number of a plain list item
  25712. :checkbox on the checkbox in a plain list item
  25713. :table in an org-mode table
  25714. :table-special on a special filed in a table
  25715. :table-table in a table.el table
  25716. :link on a hyperlink
  25717. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25718. :target on a <<target>>
  25719. :radio-target on a <<<radio-target>>>
  25720. :latex-fragment on a LaTeX fragment
  25721. :latex-preview on a LaTeX fragment with overlayed preview image
  25722. This function expects the position to be visible because it uses font-lock
  25723. faces as a help to recognize the following contexts: :table-special, :link,
  25724. and :keyword."
  25725. (let* ((f (get-text-property (point) 'face))
  25726. (faces (if (listp f) f (list f)))
  25727. (p (point)) clist o)
  25728. ;; First the large context
  25729. (cond
  25730. ((org-on-heading-p t)
  25731. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25732. (when (progn
  25733. (beginning-of-line 1)
  25734. (looking-at org-todo-line-tags-regexp))
  25735. (push (org-point-in-group p 1 :headline-stars) clist)
  25736. (push (org-point-in-group p 2 :todo-keyword) clist)
  25737. (push (org-point-in-group p 4 :tags) clist))
  25738. (goto-char p)
  25739. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25740. (if (looking-at "\\[#[A-Z0-9]\\]")
  25741. (push (org-point-in-group p 0 :priority) clist)))
  25742. ((org-at-item-p)
  25743. (push (org-point-in-group p 2 :item-bullet) clist)
  25744. (push (list :item (point-at-bol)
  25745. (save-excursion (org-end-of-item) (point)))
  25746. clist)
  25747. (and (org-at-item-checkbox-p)
  25748. (push (org-point-in-group p 0 :checkbox) clist)))
  25749. ((org-at-table-p)
  25750. (push (list :table (org-table-begin) (org-table-end)) clist)
  25751. (if (memq 'org-formula faces)
  25752. (push (list :table-special
  25753. (previous-single-property-change p 'face)
  25754. (next-single-property-change p 'face)) clist)))
  25755. ((org-at-table-p 'any)
  25756. (push (list :table-table) clist)))
  25757. (goto-char p)
  25758. ;; Now the small context
  25759. (cond
  25760. ((org-at-timestamp-p)
  25761. (push (org-point-in-group p 0 :timestamp) clist))
  25762. ((memq 'org-link faces)
  25763. (push (list :link
  25764. (previous-single-property-change p 'face)
  25765. (next-single-property-change p 'face)) clist))
  25766. ((memq 'org-special-keyword faces)
  25767. (push (list :keyword
  25768. (previous-single-property-change p 'face)
  25769. (next-single-property-change p 'face)) clist))
  25770. ((org-on-target-p)
  25771. (push (org-point-in-group p 0 :target) clist)
  25772. (goto-char (1- (match-beginning 0)))
  25773. (if (looking-at org-radio-target-regexp)
  25774. (push (org-point-in-group p 0 :radio-target) clist))
  25775. (goto-char p))
  25776. ((setq o (car (delq nil
  25777. (mapcar
  25778. (lambda (x)
  25779. (if (memq x org-latex-fragment-image-overlays) x))
  25780. (org-overlays-at (point))))))
  25781. (push (list :latex-fragment
  25782. (org-overlay-start o) (org-overlay-end o)) clist)
  25783. (push (list :latex-preview
  25784. (org-overlay-start o) (org-overlay-end o)) clist))
  25785. ((org-inside-LaTeX-fragment-p)
  25786. ;; FIXME: positions wrong.
  25787. (push (list :latex-fragment (point) (point)) clist)))
  25788. (setq clist (nreverse (delq nil clist)))
  25789. clist))
  25790. ;; FIXME: Compare with at-regexp-p Do we need both?
  25791. (defun org-in-regexp (re &optional nlines visually)
  25792. "Check if point is inside a match of regexp.
  25793. Normally only the current line is checked, but you can include NLINES extra
  25794. lines both before and after point into the search.
  25795. If VISUALLY is set, require that the cursor is not after the match but
  25796. really on, so that the block visually is on the match."
  25797. (catch 'exit
  25798. (let ((pos (point))
  25799. (eol (point-at-eol (+ 1 (or nlines 0))))
  25800. (inc (if visually 1 0)))
  25801. (save-excursion
  25802. (beginning-of-line (- 1 (or nlines 0)))
  25803. (while (re-search-forward re eol t)
  25804. (if (and (<= (match-beginning 0) pos)
  25805. (>= (+ inc (match-end 0)) pos))
  25806. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25807. (defun org-at-regexp-p (regexp)
  25808. "Is point inside a match of REGEXP in the current line?"
  25809. (catch 'exit
  25810. (save-excursion
  25811. (let ((pos (point)) (end (point-at-eol)))
  25812. (beginning-of-line 1)
  25813. (while (re-search-forward regexp end t)
  25814. (if (and (<= (match-beginning 0) pos)
  25815. (>= (match-end 0) pos))
  25816. (throw 'exit t)))
  25817. nil))))
  25818. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25819. "Call `multi-occur' with buffers for all agenda files."
  25820. (interactive "sOrg-files matching: \np")
  25821. (let* ((files (org-agenda-files))
  25822. (tnames (mapcar 'file-truename files))
  25823. (extra org-agenda-text-search-extra-files)
  25824. f)
  25825. (while (setq f (pop extra))
  25826. (unless (member (file-truename f) tnames)
  25827. (add-to-list 'files f 'append)
  25828. (add-to-list 'tnames (file-truename f) 'append)))
  25829. (multi-occur
  25830. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25831. regexp)))
  25832. (if (boundp 'occur-mode-find-occurrence-hook)
  25833. ;; Emacs 23
  25834. (add-hook 'occur-mode-find-occurrence-hook
  25835. (lambda ()
  25836. (when (org-mode-p)
  25837. (org-reveal))))
  25838. ;; Emacs 22
  25839. (defadvice occur-mode-goto-occurrence
  25840. (after org-occur-reveal activate)
  25841. (and (org-mode-p) (org-reveal)))
  25842. (defadvice occur-mode-goto-occurrence-other-window
  25843. (after org-occur-reveal activate)
  25844. (and (org-mode-p) (org-reveal)))
  25845. (defadvice occur-mode-display-occurrence
  25846. (after org-occur-reveal activate)
  25847. (when (org-mode-p)
  25848. (let ((pos (occur-mode-find-occurrence)))
  25849. (with-current-buffer (marker-buffer pos)
  25850. (save-excursion
  25851. (goto-char pos)
  25852. (org-reveal)))))))
  25853. (defun org-uniquify (list)
  25854. "Remove duplicate elements from LIST."
  25855. (let (res)
  25856. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25857. res))
  25858. (defun org-delete-all (elts list)
  25859. "Remove all elements in ELTS from LIST."
  25860. (while elts
  25861. (setq list (delete (pop elts) list)))
  25862. list)
  25863. (defun org-back-over-empty-lines ()
  25864. "Move backwards over witespace, to the beginning of the first empty line.
  25865. Returns the number o empty lines passed."
  25866. (let ((pos (point)))
  25867. (skip-chars-backward " \t\n\r")
  25868. (beginning-of-line 2)
  25869. (goto-char (min (point) pos))
  25870. (count-lines (point) pos)))
  25871. (defun org-skip-whitespace ()
  25872. (skip-chars-forward " \t\n\r"))
  25873. (defun org-point-in-group (point group &optional context)
  25874. "Check if POINT is in match-group GROUP.
  25875. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25876. match. If the match group does ot exist or point is not inside it,
  25877. return nil."
  25878. (and (match-beginning group)
  25879. (>= point (match-beginning group))
  25880. (<= point (match-end group))
  25881. (if context
  25882. (list context (match-beginning group) (match-end group))
  25883. t)))
  25884. (defun org-switch-to-buffer-other-window (&rest args)
  25885. "Switch to buffer in a second window on the current frame.
  25886. In particular, do not allow pop-up frames."
  25887. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25888. special-display-function)
  25889. (apply 'switch-to-buffer-other-window args)))
  25890. (defun org-combine-plists (&rest plists)
  25891. "Create a single property list from all plists in PLISTS.
  25892. The process starts by copying the first list, and then setting properties
  25893. from the other lists. Settings in the last list are the most significant
  25894. ones and overrule settings in the other lists."
  25895. (let ((rtn (copy-sequence (pop plists)))
  25896. p v ls)
  25897. (while plists
  25898. (setq ls (pop plists))
  25899. (while ls
  25900. (setq p (pop ls) v (pop ls))
  25901. (setq rtn (plist-put rtn p v))))
  25902. rtn))
  25903. (defun org-move-line-down (arg)
  25904. "Move the current line down. With prefix argument, move it past ARG lines."
  25905. (interactive "p")
  25906. (let ((col (current-column))
  25907. beg end pos)
  25908. (beginning-of-line 1) (setq beg (point))
  25909. (beginning-of-line 2) (setq end (point))
  25910. (beginning-of-line (+ 1 arg))
  25911. (setq pos (move-marker (make-marker) (point)))
  25912. (insert (delete-and-extract-region beg end))
  25913. (goto-char pos)
  25914. (move-to-column col)))
  25915. (defun org-move-line-up (arg)
  25916. "Move the current line up. With prefix argument, move it past ARG lines."
  25917. (interactive "p")
  25918. (let ((col (current-column))
  25919. beg end pos)
  25920. (beginning-of-line 1) (setq beg (point))
  25921. (beginning-of-line 2) (setq end (point))
  25922. (beginning-of-line (- arg))
  25923. (setq pos (move-marker (make-marker) (point)))
  25924. (insert (delete-and-extract-region beg end))
  25925. (goto-char pos)
  25926. (move-to-column col)))
  25927. (defun org-replace-escapes (string table)
  25928. "Replace %-escapes in STRING with values in TABLE.
  25929. TABLE is an association list with keys like \"%a\" and string values.
  25930. The sequences in STRING may contain normal field width and padding information,
  25931. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25932. so values can contain further %-escapes if they are define later in TABLE."
  25933. (let ((case-fold-search nil)
  25934. e re rpl)
  25935. (while (setq e (pop table))
  25936. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25937. (while (string-match re string)
  25938. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25939. (cdr e)))
  25940. (setq string (replace-match rpl t t string))))
  25941. string))
  25942. (defun org-sublist (list start end)
  25943. "Return a section of LIST, from START to END.
  25944. Counting starts at 1."
  25945. (let (rtn (c start))
  25946. (setq list (nthcdr (1- start) list))
  25947. (while (and list (<= c end))
  25948. (push (pop list) rtn)
  25949. (setq c (1+ c)))
  25950. (nreverse rtn)))
  25951. (defun org-find-base-buffer-visiting (file)
  25952. "Like `find-buffer-visiting' but alway return the base buffer and
  25953. not an indirect buffer"
  25954. (let ((buf (find-buffer-visiting file)))
  25955. (if buf
  25956. (or (buffer-base-buffer buf) buf)
  25957. nil)))
  25958. (defun org-image-file-name-regexp ()
  25959. "Return regexp matching the file names of images."
  25960. (if (fboundp 'image-file-name-regexp)
  25961. (image-file-name-regexp)
  25962. (let ((image-file-name-extensions
  25963. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25964. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25965. (concat "\\."
  25966. (regexp-opt (nconc (mapcar 'upcase
  25967. image-file-name-extensions)
  25968. image-file-name-extensions)
  25969. t)
  25970. "\\'"))))
  25971. (defun org-file-image-p (file)
  25972. "Return non-nil if FILE is an image."
  25973. (save-match-data
  25974. (string-match (org-image-file-name-regexp) file)))
  25975. ;;; Paragraph filling stuff.
  25976. ;; We want this to be just right, so use the full arsenal.
  25977. (defun org-indent-line-function ()
  25978. "Indent line like previous, but further if previous was headline or item."
  25979. (interactive)
  25980. (let* ((pos (point))
  25981. (itemp (org-at-item-p))
  25982. column bpos bcol tpos tcol bullet btype bullet-type)
  25983. ;; Find the previous relevant line
  25984. (beginning-of-line 1)
  25985. (cond
  25986. ((looking-at "#") (setq column 0))
  25987. ((looking-at "\\*+ ") (setq column 0))
  25988. (t
  25989. (beginning-of-line 0)
  25990. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25991. (beginning-of-line 0))
  25992. (cond
  25993. ((looking-at "\\*+[ \t]+")
  25994. (goto-char (match-end 0))
  25995. (setq column (current-column)))
  25996. ((org-in-item-p)
  25997. (org-beginning-of-item)
  25998. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25999. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  26000. (setq bpos (match-beginning 1) tpos (match-end 0)
  26001. bcol (progn (goto-char bpos) (current-column))
  26002. tcol (progn (goto-char tpos) (current-column))
  26003. bullet (match-string 1)
  26004. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  26005. (if (not itemp)
  26006. (setq column tcol)
  26007. (goto-char pos)
  26008. (beginning-of-line 1)
  26009. (if (looking-at "\\S-")
  26010. (progn
  26011. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26012. (setq bullet (match-string 1)
  26013. btype (if (string-match "[0-9]" bullet) "n" bullet))
  26014. (setq column (if (equal btype bullet-type) bcol tcol)))
  26015. (setq column (org-get-indentation)))))
  26016. (t (setq column (org-get-indentation))))))
  26017. (goto-char pos)
  26018. (if (<= (current-column) (current-indentation))
  26019. (indent-line-to column)
  26020. (save-excursion (indent-line-to column)))
  26021. (setq column (current-column))
  26022. (beginning-of-line 1)
  26023. (if (looking-at
  26024. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  26025. (replace-match (concat "\\1" (format org-property-format
  26026. (match-string 2) (match-string 3)))
  26027. t nil))
  26028. (move-to-column column)))
  26029. (defun org-set-autofill-regexps ()
  26030. (interactive)
  26031. ;; In the paragraph separator we include headlines, because filling
  26032. ;; text in a line directly attached to a headline would otherwise
  26033. ;; fill the headline as well.
  26034. (org-set-local 'comment-start-skip "^#+[ \t]*")
  26035. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  26036. ;; The paragraph starter includes hand-formatted lists.
  26037. (org-set-local 'paragraph-start
  26038. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  26039. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  26040. ;; But only if the user has not turned off tables or fixed-width regions
  26041. (org-set-local
  26042. 'auto-fill-inhibit-regexp
  26043. (concat "\\*+ \\|#\\+"
  26044. "\\|[ \t]*" org-keyword-time-regexp
  26045. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  26046. (concat
  26047. "\\|[ \t]*["
  26048. (if org-enable-table-editor "|" "")
  26049. (if org-enable-fixed-width-editor ":" "")
  26050. "]"))))
  26051. ;; We use our own fill-paragraph function, to make sure that tables
  26052. ;; and fixed-width regions are not wrapped. That function will pass
  26053. ;; through to `fill-paragraph' when appropriate.
  26054. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  26055. ; Adaptive filling: To get full control, first make sure that
  26056. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  26057. (org-set-local 'adaptive-fill-regexp "\000")
  26058. (org-set-local 'adaptive-fill-function
  26059. 'org-adaptive-fill-function)
  26060. (org-set-local
  26061. 'align-mode-rules-list
  26062. '((org-in-buffer-settings
  26063. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  26064. (modes . '(org-mode))))))
  26065. (defun org-fill-paragraph (&optional justify)
  26066. "Re-align a table, pass through to fill-paragraph if no table."
  26067. (let ((table-p (org-at-table-p))
  26068. (table.el-p (org-at-table.el-p)))
  26069. (cond ((and (equal (char-after (point-at-bol)) ?*)
  26070. (save-excursion (goto-char (point-at-bol))
  26071. (looking-at outline-regexp)))
  26072. t) ; skip headlines
  26073. (table.el-p t) ; skip table.el tables
  26074. (table-p (org-table-align) t) ; align org-mode tables
  26075. (t nil)))) ; call paragraph-fill
  26076. ;; For reference, this is the default value of adaptive-fill-regexp
  26077. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  26078. (defun org-adaptive-fill-function ()
  26079. "Return a fill prefix for org-mode files.
  26080. In particular, this makes sure hanging paragraphs for hand-formatted lists
  26081. work correctly."
  26082. (cond ((looking-at "#[ \t]+")
  26083. (match-string 0))
  26084. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  26085. (save-excursion
  26086. (goto-char (match-end 0))
  26087. (make-string (current-column) ?\ )))
  26088. (t nil)))
  26089. ;;;; Functions extending outline functionality
  26090. (defun org-beginning-of-line (&optional arg)
  26091. "Go to the beginning of the current line. If that is invisible, continue
  26092. to a visible line beginning. This makes the function of C-a more intuitive.
  26093. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26094. first attempt, and only move to after the tags when the cursor is already
  26095. beyond the end of the headline."
  26096. (interactive "P")
  26097. (let ((pos (point)))
  26098. (beginning-of-line 1)
  26099. (if (bobp)
  26100. nil
  26101. (backward-char 1)
  26102. (if (org-invisible-p)
  26103. (while (and (not (bobp)) (org-invisible-p))
  26104. (backward-char 1)
  26105. (beginning-of-line 1))
  26106. (forward-char 1)))
  26107. (when org-special-ctrl-a/e
  26108. (cond
  26109. ((and (looking-at org-todo-line-regexp)
  26110. (= (char-after (match-end 1)) ?\ ))
  26111. (goto-char
  26112. (if (eq org-special-ctrl-a/e t)
  26113. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  26114. ((= pos (point)) (match-beginning 3))
  26115. (t (point)))
  26116. (cond ((> pos (point)) (point))
  26117. ((not (eq last-command this-command)) (point))
  26118. (t (match-beginning 3))))))
  26119. ((org-at-item-p)
  26120. (goto-char
  26121. (if (eq org-special-ctrl-a/e t)
  26122. (cond ((> pos (match-end 4)) (match-end 4))
  26123. ((= pos (point)) (match-end 4))
  26124. (t (point)))
  26125. (cond ((> pos (point)) (point))
  26126. ((not (eq last-command this-command)) (point))
  26127. (t (match-end 4))))))))))
  26128. (defun org-end-of-line (&optional arg)
  26129. "Go to the end of the line.
  26130. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26131. first attempt, and only move to after the tags when the cursor is already
  26132. beyond the end of the headline."
  26133. (interactive "P")
  26134. (if (or (not org-special-ctrl-a/e)
  26135. (not (org-on-heading-p)))
  26136. (end-of-line arg)
  26137. (let ((pos (point)))
  26138. (beginning-of-line 1)
  26139. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  26140. (if (eq org-special-ctrl-a/e t)
  26141. (if (or (< pos (match-beginning 1))
  26142. (= pos (match-end 0)))
  26143. (goto-char (match-beginning 1))
  26144. (goto-char (match-end 0)))
  26145. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  26146. (goto-char (match-end 0))
  26147. (goto-char (match-beginning 1))))
  26148. (end-of-line arg)))))
  26149. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  26150. (define-key org-mode-map "\C-e" 'org-end-of-line)
  26151. (defun org-kill-line (&optional arg)
  26152. "Kill line, to tags or end of line."
  26153. (interactive "P")
  26154. (cond
  26155. ((or (not org-special-ctrl-k)
  26156. (bolp)
  26157. (not (org-on-heading-p)))
  26158. (call-interactively 'kill-line))
  26159. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  26160. (kill-region (point) (match-beginning 1))
  26161. (org-set-tags nil t))
  26162. (t (kill-region (point) (point-at-eol)))))
  26163. (define-key org-mode-map "\C-k" 'org-kill-line)
  26164. (defun org-invisible-p ()
  26165. "Check if point is at a character currently not visible."
  26166. ;; Early versions of noutline don't have `outline-invisible-p'.
  26167. (if (fboundp 'outline-invisible-p)
  26168. (outline-invisible-p)
  26169. (get-char-property (point) 'invisible)))
  26170. (defun org-invisible-p2 ()
  26171. "Check if point is at a character currently not visible."
  26172. (save-excursion
  26173. (if (and (eolp) (not (bobp))) (backward-char 1))
  26174. ;; Early versions of noutline don't have `outline-invisible-p'.
  26175. (if (fboundp 'outline-invisible-p)
  26176. (outline-invisible-p)
  26177. (get-char-property (point) 'invisible))))
  26178. (defalias 'org-back-to-heading 'outline-back-to-heading)
  26179. (defalias 'org-on-heading-p 'outline-on-heading-p)
  26180. (defalias 'org-at-heading-p 'outline-on-heading-p)
  26181. (defun org-at-heading-or-item-p ()
  26182. (or (org-on-heading-p) (org-at-item-p)))
  26183. (defun org-on-target-p ()
  26184. (or (org-in-regexp org-radio-target-regexp)
  26185. (org-in-regexp org-target-regexp)))
  26186. (defun org-up-heading-all (arg)
  26187. "Move to the heading line of which the present line is a subheading.
  26188. This function considers both visible and invisible heading lines.
  26189. With argument, move up ARG levels."
  26190. (if (fboundp 'outline-up-heading-all)
  26191. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  26192. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  26193. (defun org-up-heading-safe ()
  26194. "Move to the heading line of which the present line is a subheading.
  26195. This version will not throw an error. It will return the level of the
  26196. headline found, or nil if no higher level is found."
  26197. (let ((pos (point)) start-level level
  26198. (re (concat "^" outline-regexp)))
  26199. (catch 'exit
  26200. (outline-back-to-heading t)
  26201. (setq start-level (funcall outline-level))
  26202. (if (equal start-level 1) (throw 'exit nil))
  26203. (while (re-search-backward re nil t)
  26204. (setq level (funcall outline-level))
  26205. (if (< level start-level) (throw 'exit level)))
  26206. nil)))
  26207. (defun org-first-sibling-p ()
  26208. "Is this heading the first child of its parents?"
  26209. (interactive)
  26210. (let ((re (concat "^" outline-regexp))
  26211. level l)
  26212. (unless (org-at-heading-p t)
  26213. (error "Not at a heading"))
  26214. (setq level (funcall outline-level))
  26215. (save-excursion
  26216. (if (not (re-search-backward re nil t))
  26217. t
  26218. (setq l (funcall outline-level))
  26219. (< l level)))))
  26220. (defun org-goto-sibling (&optional previous)
  26221. "Goto the next sibling, even if it is invisible.
  26222. When PREVIOUS is set, go to the previous sibling instead. Returns t
  26223. when a sibling was found. When none is found, return nil and don't
  26224. move point."
  26225. (let ((fun (if previous 're-search-backward 're-search-forward))
  26226. (pos (point))
  26227. (re (concat "^" outline-regexp))
  26228. level l)
  26229. (when (condition-case nil (org-back-to-heading t) (error nil))
  26230. (setq level (funcall outline-level))
  26231. (catch 'exit
  26232. (or previous (forward-char 1))
  26233. (while (funcall fun re nil t)
  26234. (setq l (funcall outline-level))
  26235. (when (< l level) (goto-char pos) (throw 'exit nil))
  26236. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  26237. (goto-char pos)
  26238. nil))))
  26239. (defun org-show-siblings ()
  26240. "Show all siblings of the current headline."
  26241. (save-excursion
  26242. (while (org-goto-sibling) (org-flag-heading nil)))
  26243. (save-excursion
  26244. (while (org-goto-sibling 'previous)
  26245. (org-flag-heading nil))))
  26246. (defun org-show-hidden-entry ()
  26247. "Show an entry where even the heading is hidden."
  26248. (save-excursion
  26249. (org-show-entry)))
  26250. (defun org-flag-heading (flag &optional entry)
  26251. "Flag the current heading. FLAG non-nil means make invisible.
  26252. When ENTRY is non-nil, show the entire entry."
  26253. (save-excursion
  26254. (org-back-to-heading t)
  26255. ;; Check if we should show the entire entry
  26256. (if entry
  26257. (progn
  26258. (org-show-entry)
  26259. (save-excursion
  26260. (and (outline-next-heading)
  26261. (org-flag-heading nil))))
  26262. (outline-flag-region (max (point-min) (1- (point)))
  26263. (save-excursion (outline-end-of-heading) (point))
  26264. flag))))
  26265. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  26266. ;; This is an exact copy of the original function, but it uses
  26267. ;; `org-back-to-heading', to make it work also in invisible
  26268. ;; trees. And is uses an invisible-OK argument.
  26269. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  26270. (org-back-to-heading invisible-OK)
  26271. (let ((first t)
  26272. (level (funcall outline-level)))
  26273. (while (and (not (eobp))
  26274. (or first (> (funcall outline-level) level)))
  26275. (setq first nil)
  26276. (outline-next-heading))
  26277. (unless to-heading
  26278. (if (memq (preceding-char) '(?\n ?\^M))
  26279. (progn
  26280. ;; Go to end of line before heading
  26281. (forward-char -1)
  26282. (if (memq (preceding-char) '(?\n ?\^M))
  26283. ;; leave blank line before heading
  26284. (forward-char -1))))))
  26285. (point))
  26286. (defun org-show-subtree ()
  26287. "Show everything after this heading at deeper levels."
  26288. (outline-flag-region
  26289. (point)
  26290. (save-excursion
  26291. (outline-end-of-subtree) (outline-next-heading) (point))
  26292. nil))
  26293. (defun org-show-entry ()
  26294. "Show the body directly following this heading.
  26295. Show the heading too, if it is currently invisible."
  26296. (interactive)
  26297. (save-excursion
  26298. (condition-case nil
  26299. (progn
  26300. (org-back-to-heading t)
  26301. (outline-flag-region
  26302. (max (point-min) (1- (point)))
  26303. (save-excursion
  26304. (re-search-forward
  26305. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26306. (or (match-beginning 1) (point-max)))
  26307. nil))
  26308. (error nil))))
  26309. (defun org-make-options-regexp (kwds)
  26310. "Make a regular expression for keyword lines."
  26311. (concat
  26312. "^"
  26313. "#?[ \t]*\\+\\("
  26314. (mapconcat 'regexp-quote kwds "\\|")
  26315. "\\):[ \t]*"
  26316. "\\(.+\\)"))
  26317. ;; Make isearch reveal the necessary context
  26318. (defun org-isearch-end ()
  26319. "Reveal context after isearch exits."
  26320. (when isearch-success ; only if search was successful
  26321. (if (featurep 'xemacs)
  26322. ;; Under XEmacs, the hook is run in the correct place,
  26323. ;; we directly show the context.
  26324. (org-show-context 'isearch)
  26325. ;; In Emacs the hook runs *before* restoring the overlays.
  26326. ;; So we have to use a one-time post-command-hook to do this.
  26327. ;; (Emacs 22 has a special variable, see function `org-mode')
  26328. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26329. isearch-mode-end-hook-quit)
  26330. ;; Only when the isearch was not quitted.
  26331. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26332. 'append 'local)))))
  26333. (defun org-isearch-post-command ()
  26334. "Remove self from hook, and show context."
  26335. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26336. (org-show-context 'isearch))
  26337. ;;;; Integration with and fixes for other packages
  26338. ;;; Imenu support
  26339. (defvar org-imenu-markers nil
  26340. "All markers currently used by Imenu.")
  26341. (make-variable-buffer-local 'org-imenu-markers)
  26342. (defun org-imenu-new-marker (&optional pos)
  26343. "Return a new marker for use by Imenu, and remember the marker."
  26344. (let ((m (make-marker)))
  26345. (move-marker m (or pos (point)))
  26346. (push m org-imenu-markers)
  26347. m))
  26348. (defun org-imenu-get-tree ()
  26349. "Produce the index for Imenu."
  26350. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26351. (setq org-imenu-markers nil)
  26352. (let* ((n org-imenu-depth)
  26353. (re (concat "^" outline-regexp))
  26354. (subs (make-vector (1+ n) nil))
  26355. (last-level 0)
  26356. m tree level head)
  26357. (save-excursion
  26358. (save-restriction
  26359. (widen)
  26360. (goto-char (point-max))
  26361. (while (re-search-backward re nil t)
  26362. (setq level (org-reduced-level (funcall outline-level)))
  26363. (when (<= level n)
  26364. (looking-at org-complex-heading-regexp)
  26365. (setq head (org-match-string-no-properties 4)
  26366. m (org-imenu-new-marker))
  26367. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26368. (if (>= level last-level)
  26369. (push (cons head m) (aref subs level))
  26370. (push (cons head (aref subs (1+ level))) (aref subs level))
  26371. (loop for i from (1+ level) to n do (aset subs i nil)))
  26372. (setq last-level level)))))
  26373. (aref subs 1)))
  26374. (eval-after-load "imenu"
  26375. '(progn
  26376. (add-hook 'imenu-after-jump-hook
  26377. (lambda () (org-show-context 'org-goto)))))
  26378. ;; Speedbar support
  26379. (defun org-speedbar-set-agenda-restriction ()
  26380. "Restrict future agenda commands to the location at point in speedbar.
  26381. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26382. (interactive)
  26383. (let (p m tp np dir txt w)
  26384. (cond
  26385. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26386. 'org-imenu t))
  26387. (setq m (get-text-property p 'org-imenu-marker))
  26388. (save-excursion
  26389. (save-restriction
  26390. (set-buffer (marker-buffer m))
  26391. (goto-char m)
  26392. (org-agenda-set-restriction-lock 'subtree))))
  26393. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26394. 'speedbar-function 'speedbar-find-file))
  26395. (setq tp (previous-single-property-change
  26396. (1+ p) 'speedbar-function)
  26397. np (next-single-property-change
  26398. tp 'speedbar-function)
  26399. dir (speedbar-line-directory)
  26400. txt (buffer-substring-no-properties (or tp (point-min))
  26401. (or np (point-max))))
  26402. (save-excursion
  26403. (save-restriction
  26404. (set-buffer (find-file-noselect
  26405. (let ((default-directory dir))
  26406. (expand-file-name txt))))
  26407. (unless (org-mode-p)
  26408. (error "Cannot restrict to non-Org-mode file"))
  26409. (org-agenda-set-restriction-lock 'file))))
  26410. (t (error "Don't know how to restrict Org-mode's agenda")))
  26411. (org-move-overlay org-speedbar-restriction-lock-overlay
  26412. (point-at-bol) (point-at-eol))
  26413. (setq current-prefix-arg nil)
  26414. (org-agenda-maybe-redo)))
  26415. (eval-after-load "speedbar"
  26416. '(progn
  26417. (speedbar-add-supported-extension ".org")
  26418. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26419. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26420. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26421. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26422. (add-hook 'speedbar-visiting-tag-hook
  26423. (lambda () (org-show-context 'org-goto)))))
  26424. ;;; Fixes and Hacks
  26425. ;; Make flyspell not check words in links, to not mess up our keymap
  26426. (defun org-mode-flyspell-verify ()
  26427. "Don't let flyspell put overlays at active buttons."
  26428. (not (get-text-property (point) 'keymap)))
  26429. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26430. (eval-after-load "bookmark"
  26431. '(if (boundp 'bookmark-after-jump-hook)
  26432. ;; We can use the hook
  26433. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26434. ;; Hook not available, use advice
  26435. (defadvice bookmark-jump (after org-make-visible activate)
  26436. "Make the position visible."
  26437. (org-bookmark-jump-unhide))))
  26438. (defun org-bookmark-jump-unhide ()
  26439. "Unhide the current position, to show the bookmark location."
  26440. (and (org-mode-p)
  26441. (or (org-invisible-p)
  26442. (save-excursion (goto-char (max (point-min) (1- (point))))
  26443. (org-invisible-p)))
  26444. (org-show-context 'bookmark-jump)))
  26445. ;; Make session.el ignore our circular variable
  26446. (eval-after-load "session"
  26447. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26448. ;;;; Experimental code
  26449. (defun org-closed-in-range ()
  26450. "Sparse tree of items closed in a certain time range.
  26451. Still experimental, may disappear in the future."
  26452. (interactive)
  26453. ;; Get the time interval from the user.
  26454. (let* ((time1 (time-to-seconds
  26455. (org-read-date nil 'to-time nil "Starting date: ")))
  26456. (time2 (time-to-seconds
  26457. (org-read-date nil 'to-time nil "End date:")))
  26458. ;; callback function
  26459. (callback (lambda ()
  26460. (let ((time
  26461. (time-to-seconds
  26462. (apply 'encode-time
  26463. (org-parse-time-string
  26464. (match-string 1))))))
  26465. ;; check if time in interval
  26466. (and (>= time time1) (<= time time2))))))
  26467. ;; make tree, check each match with the callback
  26468. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26469. ;;;; Finish up
  26470. (provide 'org)
  26471. (run-hooks 'org-load-hook)
  26472. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26473. ;;; org.el ends here