org.el 1.0 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200
  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.21+
  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.21+"
  81. "The version number of the file org.el.")
  82. (defun org-version (&optional here)
  83. "Show the org-mode version in the echo area.
  84. With prefix arg HERE, insert it at point."
  85. (interactive "P")
  86. (let ((version (format "Org-mode version %s" org-version)))
  87. (message version)
  88. (if here
  89. (insert version))))
  90. ;;; Compatibility constants
  91. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  92. (defconst org-format-transports-properties-p
  93. (let ((x "a"))
  94. (add-text-properties 0 1 '(test t) x)
  95. (get-text-property 0 'test (format "%s" x)))
  96. "Does format transport text properties?")
  97. (defmacro org-bound-and-true-p (var)
  98. "Return the value of symbol VAR if it is bound, else nil."
  99. `(and (boundp (quote ,var)) ,var))
  100. (defmacro org-unmodified (&rest body)
  101. "Execute body without changing `buffer-modified-p'."
  102. `(set-buffer-modified-p
  103. (prog1 (buffer-modified-p) ,@body)))
  104. (defmacro org-re (s)
  105. "Replace posix classes in regular expression."
  106. (if (featurep 'xemacs)
  107. (let ((ss s))
  108. (save-match-data
  109. (while (string-match "\\[:alnum:\\]" ss)
  110. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  111. (while (string-match "\\[:alpha:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z" t t ss)))
  113. ss))
  114. s))
  115. (defmacro org-preserve-lc (&rest body)
  116. `(let ((_line (org-current-line))
  117. (_col (current-column)))
  118. (unwind-protect
  119. (progn ,@body)
  120. (goto-line _line)
  121. (move-to-column _col))))
  122. (defmacro org-without-partial-completion (&rest body)
  123. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  124. partial-completion-mode)))
  125. (unwind-protect
  126. (progn
  127. (if pc-mode (partial-completion-mode -1))
  128. ,@body)
  129. (if pc-mode (partial-completion-mode 1)))))
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'hypermedia
  136. :group 'calendar)
  137. ;; FIXME: Needs a separate group...
  138. (defcustom org-completion-fallback-command 'hippie-expand
  139. "The expansion command called by \\[org-complete] in normal context.
  140. Normal means, no org-mode-specific context."
  141. :group 'org
  142. :type 'function)
  143. (defgroup org-startup nil
  144. "Options concerning startup of Org-mode."
  145. :tag "Org Startup"
  146. :group 'org)
  147. (defcustom org-startup-folded t
  148. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  149. This can also be configured on a per-file basis by adding one of
  150. the following lines anywhere in the buffer:
  151. #+STARTUP: fold
  152. #+STARTUP: nofold
  153. #+STARTUP: content"
  154. :group 'org-startup
  155. :type '(choice
  156. (const :tag "nofold: show all" nil)
  157. (const :tag "fold: overview" t)
  158. (const :tag "content: all headlines" content)))
  159. (defcustom org-startup-truncated t
  160. "Non-nil means, entering Org-mode will set `truncate-lines'.
  161. This is useful since some lines containing links can be very long and
  162. uninteresting. Also tables look terrible when wrapped."
  163. :group 'org-startup
  164. :type 'boolean)
  165. (defcustom org-startup-align-all-tables nil
  166. "Non-nil means, align all tables when visiting a file.
  167. This is useful when the column width in tables is forced with <N> cookies
  168. in table fields. Such tables will look correct only after the first re-align.
  169. This can also be configured on a per-file basis by adding one of
  170. the following lines anywhere in the buffer:
  171. #+STARTUP: align
  172. #+STARTUP: noalign"
  173. :group 'org-startup
  174. :type 'boolean)
  175. (defcustom org-insert-mode-line-in-empty-file nil
  176. "Non-nil means insert the first line setting Org-mode in empty files.
  177. When the function `org-mode' is called interactively in an empty file, this
  178. normally means that the file name does not automatically trigger Org-mode.
  179. To ensure that the file will always be in Org-mode in the future, a
  180. line enforcing Org-mode will be inserted into the buffer, if this option
  181. has been set."
  182. :group 'org-startup
  183. :type 'boolean)
  184. (defcustom org-replace-disputed-keys nil
  185. "Non-nil means use alternative key bindings for some keys.
  186. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  187. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  188. If you want to use Org-mode together with one of these other modes,
  189. or more generally if you would like to move some Org-mode commands to
  190. other keys, set this variable and configure the keys with the variable
  191. `org-disputed-keys'.
  192. This option is only relevant at load-time of Org-mode, and must be set
  193. *before* org.el is loaded. Changing it requires a restart of Emacs to
  194. become effective."
  195. :group 'org-startup
  196. :type 'boolean)
  197. (if (fboundp 'defvaralias)
  198. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  199. (defcustom org-disputed-keys
  200. '(([(shift up)] . [(meta p)])
  201. ([(shift down)] . [(meta n)])
  202. ([(shift left)] . [(meta -)])
  203. ([(shift right)] . [(meta +)])
  204. ([(control shift right)] . [(meta shift +)])
  205. ([(control shift left)] . [(meta shift -)]))
  206. "Keys for which Org-mode and other modes compete.
  207. This is an alist, cars are the default keys, second element specifies
  208. the alternative to use when `org-replace-disputed-keys' is t.
  209. Keys can be specified in any syntax supported by `define-key'.
  210. The value of this option takes effect only at Org-mode's startup,
  211. therefore you'll have to restart Emacs to apply it after changing."
  212. :group 'org-startup
  213. :type 'alist)
  214. (defun org-key (key)
  215. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  216. Or return the original if not disputed."
  217. (if org-replace-disputed-keys
  218. (let* ((nkey (key-description key))
  219. (x (org-find-if (lambda (x)
  220. (equal (key-description (car x)) nkey))
  221. org-disputed-keys)))
  222. (if x (cdr x) key))
  223. key))
  224. (defun org-find-if (predicate seq)
  225. (catch 'exit
  226. (while seq
  227. (if (funcall predicate (car seq))
  228. (throw 'exit (car seq))
  229. (pop seq)))))
  230. (defun org-defkey (keymap key def)
  231. "Define a key, possibly translated, as returned by `org-key'."
  232. (define-key keymap (org-key key) def))
  233. (defcustom org-ellipsis nil
  234. "The ellipsis to use in the Org-mode outline.
  235. When nil, just use the standard three dots. When a string, use that instead,
  236. When a face, use the standart 3 dots, but with the specified face.
  237. The change affects only Org-mode (which will then use its own display table).
  238. Changing this requires executing `M-x org-mode' in a buffer to become
  239. effective."
  240. :group 'org-startup
  241. :type '(choice (const :tag "Default" nil)
  242. (face :tag "Face" :value org-warning)
  243. (string :tag "String" :value "...#")))
  244. (defvar org-display-table nil
  245. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  246. (defgroup org-keywords nil
  247. "Keywords in Org-mode."
  248. :tag "Org Keywords"
  249. :group 'org)
  250. (defcustom org-deadline-string "DEADLINE:"
  251. "String to mark deadline entries.
  252. A deadline is this string, followed by a time stamp. Should be a word,
  253. terminated by a colon. You can insert a schedule keyword and
  254. a timestamp with \\[org-deadline].
  255. Changes become only effective after restarting Emacs."
  256. :group 'org-keywords
  257. :type 'string)
  258. (defcustom org-scheduled-string "SCHEDULED:"
  259. "String to mark scheduled TODO entries.
  260. A schedule is this string, followed by a time stamp. Should be a word,
  261. terminated by a colon. You can insert a schedule keyword and
  262. a timestamp with \\[org-schedule].
  263. Changes become only effective after restarting Emacs."
  264. :group 'org-keywords
  265. :type 'string)
  266. (defcustom org-closed-string "CLOSED:"
  267. "String used as the prefix for timestamps logging closing a TODO entry."
  268. :group 'org-keywords
  269. :type 'string)
  270. (defcustom org-clock-string "CLOCK:"
  271. "String used as prefix for timestamps clocking work hours on an item."
  272. :group 'org-keywords
  273. :type 'string)
  274. (defcustom org-comment-string "COMMENT"
  275. "Entries starting with this keyword will never be exported.
  276. An entry can be toggled between COMMENT and normal with
  277. \\[org-toggle-comment].
  278. Changes become only effective after restarting Emacs."
  279. :group 'org-keywords
  280. :type 'string)
  281. (defcustom org-quote-string "QUOTE"
  282. "Entries starting with this keyword will be exported in fixed-width font.
  283. Quoting applies only to the text in the entry following the headline, and does
  284. not extend beyond the next headline, even if that is lower level.
  285. An entry can be toggled between QUOTE and normal with
  286. \\[org-toggle-fixed-width-section]."
  287. :group 'org-keywords
  288. :type 'string)
  289. (defconst org-repeat-re
  290. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  291. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  292. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  293. "Regular expression for specifying repeated events.
  294. After a match, group 1 contains the repeat expression.")
  295. (defgroup org-structure nil
  296. "Options concerning the general structure of Org-mode files."
  297. :tag "Org Structure"
  298. :group 'org)
  299. (defgroup org-reveal-location nil
  300. "Options about how to make context of a location visible."
  301. :tag "Org Reveal Location"
  302. :group 'org-structure)
  303. (defconst org-context-choice
  304. '(choice
  305. (const :tag "Always" t)
  306. (const :tag "Never" nil)
  307. (repeat :greedy t :tag "Individual contexts"
  308. (cons
  309. (choice :tag "Context"
  310. (const agenda)
  311. (const org-goto)
  312. (const occur-tree)
  313. (const tags-tree)
  314. (const link-search)
  315. (const mark-goto)
  316. (const bookmark-jump)
  317. (const isearch)
  318. (const default))
  319. (boolean))))
  320. "Contexts for the reveal options.")
  321. (defcustom org-show-hierarchy-above '((default . t))
  322. "Non-nil means, show full hierarchy when revealing a location.
  323. Org-mode often shows locations in an org-mode file which might have
  324. been invisible before. When this is set, the hierarchy of headings
  325. above the exposed location is shown.
  326. Turning this off for example for sparse trees makes them very compact.
  327. Instead of t, this can also be an alist specifying this option for different
  328. contexts. Valid contexts are
  329. agenda when exposing an entry from the agenda
  330. org-goto when using the command `org-goto' on key C-c C-j
  331. occur-tree when using the command `org-occur' on key C-c /
  332. tags-tree when constructing a sparse tree based on tags matches
  333. link-search when exposing search matches associated with a link
  334. mark-goto when exposing the jump goal of a mark
  335. bookmark-jump when exposing a bookmark location
  336. isearch when exiting from an incremental search
  337. default default for all contexts not set explicitly"
  338. :group 'org-reveal-location
  339. :type org-context-choice)
  340. (defcustom org-show-following-heading '((default . nil))
  341. "Non-nil means, show following heading when revealing a location.
  342. Org-mode often shows locations in an org-mode file which might have
  343. been invisible before. When this is set, the heading following the
  344. match is shown.
  345. Turning this off for example for sparse trees makes them very compact,
  346. but makes it harder to edit the location of the match. In such a case,
  347. use the command \\[org-reveal] to show more context.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. See `org-show-hierarchy-above' for valid contexts."
  350. :group 'org-reveal-location
  351. :type org-context-choice)
  352. (defcustom org-show-siblings '((default . nil) (isearch t))
  353. "Non-nil means, show all sibling heading when revealing a location.
  354. Org-mode often shows locations in an org-mode file which might have
  355. been invisible before. When this is set, the sibling of the current entry
  356. heading are all made visible. If `org-show-hierarchy-above' is t,
  357. the same happens on each level of the hierarchy above the current entry.
  358. By default this is on for the isearch context, off for all other contexts.
  359. Turning this off for example for sparse trees makes them very compact,
  360. but makes it harder to edit the location of the match. In such a case,
  361. use the command \\[org-reveal] to show more context.
  362. Instead of t, this can also be an alist specifying this option for different
  363. contexts. See `org-show-hierarchy-above' for valid contexts."
  364. :group 'org-reveal-location
  365. :type org-context-choice)
  366. (defcustom org-show-entry-below '((default . nil))
  367. "Non-nil means, show the entry below a headline when revealing a location.
  368. Org-mode often shows locations in an org-mode file which might have
  369. been invisible before. When this is set, the text below the headline that is
  370. exposed is also shown.
  371. By default this is off for all contexts.
  372. Instead of t, this can also be an alist specifying this option for different
  373. contexts. See `org-show-hierarchy-above' for valid contexts."
  374. :group 'org-reveal-location
  375. :type org-context-choice)
  376. (defgroup org-cycle nil
  377. "Options concerning visibility cycling in Org-mode."
  378. :tag "Org Cycle"
  379. :group 'org-structure)
  380. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  381. "Names of drawers. Drawers are not opened by cycling on the headline above.
  382. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  383. this:
  384. :DRAWERNAME:
  385. .....
  386. :END:
  387. The drawer \"PROPERTIES\" is special for capturing properties through
  388. the property API.
  389. Drawers can be defined on the per-file basis with a line like:
  390. #+DRAWERS: HIDDEN STATE PROPERTIES"
  391. :group 'org-structure
  392. :type '(repeat (string :tag "Drawer Name")))
  393. (defcustom org-cycle-global-at-bob nil
  394. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  395. This makes it possible to do global cycling without having to use S-TAB or
  396. C-u TAB. For this special case to work, the first line of the buffer
  397. must not be a headline - it may be empty ot some other text. When used in
  398. this way, `org-cycle-hook' is disables temporarily, to make sure the
  399. cursor stays at the beginning of the buffer.
  400. When this option is nil, don't do anything special at the beginning
  401. of the buffer."
  402. :group 'org-cycle
  403. :type 'boolean)
  404. (defcustom org-cycle-emulate-tab t
  405. "Where should `org-cycle' emulate TAB.
  406. nil Never
  407. white Only in completely white lines
  408. whitestart Only at the beginning of lines, before the first non-white char
  409. t Everywhere except in headlines
  410. exc-hl-bol Everywhere except at the start of a headline
  411. If TAB is used in a place where it does not emulate TAB, the current subtree
  412. visibility is cycled."
  413. :group 'org-cycle
  414. :type '(choice (const :tag "Never" nil)
  415. (const :tag "Only in completely white lines" white)
  416. (const :tag "Before first char in a line" whitestart)
  417. (const :tag "Everywhere except in headlines" t)
  418. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  419. ))
  420. (defcustom org-cycle-separator-lines 2
  421. "Number of empty lines needed to keep an empty line between collapsed trees.
  422. If you leave an empty line between the end of a subtree and the following
  423. headline, this empty line is hidden when the subtree is folded.
  424. Org-mode will leave (exactly) one empty line visible if the number of
  425. empty lines is equal or larger to the number given in this variable.
  426. So the default 2 means, at least 2 empty lines after the end of a subtree
  427. are needed to produce free space between a collapsed subtree and the
  428. following headline.
  429. Special case: when 0, never leave empty lines in collapsed view."
  430. :group 'org-cycle
  431. :type 'integer)
  432. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  433. org-cycle-hide-drawers
  434. org-cycle-show-empty-lines
  435. org-optimize-window-after-visibility-change)
  436. "Hook that is run after `org-cycle' has changed the buffer visibility.
  437. The function(s) in this hook must accept a single argument which indicates
  438. the new state that was set by the most recent `org-cycle' command. The
  439. argument is a symbol. After a global state change, it can have the values
  440. `overview', `content', or `all'. After a local state change, it can have
  441. the values `folded', `children', or `subtree'."
  442. :group 'org-cycle
  443. :type 'hook)
  444. (defgroup org-edit-structure nil
  445. "Options concerning structure editing in Org-mode."
  446. :tag "Org Edit Structure"
  447. :group 'org-structure)
  448. (defcustom org-special-ctrl-a/e nil
  449. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  450. When t, `C-a' will bring back the cursor to the beginning of the
  451. headline text, i.e. after the stars and after a possible TODO keyword.
  452. In an item, this will be the position after the bullet.
  453. When the cursor is already at that position, another `C-a' will bring
  454. it to the beginning of the line.
  455. `C-e' will jump to the end of the headline, ignoring the presence of tags
  456. in the headline. A second `C-e' will then jump to the true end of the
  457. line, after any tags.
  458. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  459. and only a directly following, identical keypress will bring the cursor
  460. to the special positions."
  461. :group 'org-edit-structure
  462. :type '(choice
  463. (const :tag "off" nil)
  464. (const :tag "after bullet first" t)
  465. (const :tag "border first" reversed)))
  466. (if (fboundp 'defvaralias)
  467. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  468. (defcustom org-special-ctrl-k nil
  469. "Non-nil means `C-k' will behave specially in headlines.
  470. When nil, `C-k' will call the default `kill-line' command.
  471. When t, the following will happen while the cursor is in the headline:
  472. - When the cursor is at the beginning of a headline, kill the entire
  473. line and possible the folded subtree below the line.
  474. - When in the middle of the headline text, kill the headline up to the tags.
  475. - When after the headline text, kill the tags."
  476. :group 'org-edit-structure
  477. :type 'boolean)
  478. (defcustom org-odd-levels-only nil
  479. "Non-nil means, skip even levels and only use odd levels for the outline.
  480. This has the effect that two stars are being added/taken away in
  481. promotion/demotion commands. It also influences how levels are
  482. handled by the exporters.
  483. Changing it requires restart of `font-lock-mode' to become effective
  484. for fontification also in regions already fontified.
  485. You may also set this on a per-file basis by adding one of the following
  486. lines to the buffer:
  487. #+STARTUP: odd
  488. #+STARTUP: oddeven"
  489. :group 'org-edit-structure
  490. :group 'org-font-lock
  491. :type 'boolean)
  492. (defcustom org-adapt-indentation t
  493. "Non-nil means, adapt indentation when promoting and demoting.
  494. When this is set and the *entire* text in an entry is indented, the
  495. indentation is increased by one space in a demotion command, and
  496. decreased by one in a promotion command. If any line in the entry
  497. body starts at column 0, indentation is not changed at all."
  498. :group 'org-edit-structure
  499. :type 'boolean)
  500. (defcustom org-blank-before-new-entry '((heading . nil)
  501. (plain-list-item . nil))
  502. "Should `org-insert-heading' leave a blank line before new heading/item?
  503. The value is an alist, with `heading' and `plain-list-item' as car,
  504. and a boolean flag as cdr."
  505. :group 'org-edit-structure
  506. :type '(list
  507. (cons (const heading) (boolean))
  508. (cons (const plain-list-item) (boolean))))
  509. (defcustom org-insert-heading-hook nil
  510. "Hook being run after inserting a new heading."
  511. :group 'org-edit-structure
  512. :type 'hook)
  513. (defcustom org-enable-fixed-width-editor t
  514. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  515. This currently only means, they are never auto-wrapped.
  516. When nil, such lines will be treated like ordinary lines.
  517. See also the QUOTE keyword."
  518. :group 'org-edit-structure
  519. :type 'boolean)
  520. (defcustom org-goto-auto-isearch t
  521. "Non-nil means, typing characters in org-goto starts incremental search."
  522. :group 'org-edit-structure
  523. :type 'boolean)
  524. (defgroup org-sparse-trees nil
  525. "Options concerning sparse trees in Org-mode."
  526. :tag "Org Sparse Trees"
  527. :group 'org-structure)
  528. (defcustom org-highlight-sparse-tree-matches t
  529. "Non-nil means, highlight all matches that define a sparse tree.
  530. The highlights will automatically disappear the next time the buffer is
  531. changed by an edit command."
  532. :group 'org-sparse-trees
  533. :type 'boolean)
  534. (defcustom org-remove-highlights-with-change t
  535. "Non-nil means, any change to the buffer will remove temporary highlights.
  536. Such highlights are created by `org-occur' and `org-clock-display'.
  537. When nil, `C-c C-c needs to be used to get rid of the highlights.
  538. The highlights created by `org-preview-latex-fragment' always need
  539. `C-c C-c' to be removed."
  540. :group 'org-sparse-trees
  541. :group 'org-time
  542. :type 'boolean)
  543. (defcustom org-occur-hook '(org-first-headline-recenter)
  544. "Hook that is run after `org-occur' has constructed a sparse tree.
  545. This can be used to recenter the window to show as much of the structure
  546. as possible."
  547. :group 'org-sparse-trees
  548. :type 'hook)
  549. (defgroup org-plain-lists nil
  550. "Options concerning plain lists in Org-mode."
  551. :tag "Org Plain lists"
  552. :group 'org-structure)
  553. (defcustom org-cycle-include-plain-lists nil
  554. "Non-nil means, include plain lists into visibility cycling.
  555. This means that during cycling, plain list items will *temporarily* be
  556. interpreted as outline headlines with a level given by 1000+i where i is the
  557. indentation of the bullet. In all other operations, plain list items are
  558. not seen as headlines. For example, you cannot assign a TODO keyword to
  559. such an item."
  560. :group 'org-plain-lists
  561. :type 'boolean)
  562. (defcustom org-plain-list-ordered-item-terminator t
  563. "The character that makes a line with leading number an ordered list item.
  564. Valid values are ?. and ?\). To get both terminators, use t. While
  565. ?. may look nicer, it creates the danger that a line with leading
  566. number may be incorrectly interpreted as an item. ?\) therefore is
  567. the safe choice."
  568. :group 'org-plain-lists
  569. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  570. (const :tag "paren like in \"2)\"" ?\))
  571. (const :tab "both" t)))
  572. (defcustom org-auto-renumber-ordered-lists t
  573. "Non-nil means, automatically renumber ordered plain lists.
  574. Renumbering happens when the sequence have been changed with
  575. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  576. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  577. :group 'org-plain-lists
  578. :type 'boolean)
  579. (defcustom org-provide-checkbox-statistics t
  580. "Non-nil means, update checkbox statistics after insert and toggle.
  581. When this is set, checkbox statistics is updated each time you either insert
  582. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  583. with \\[org-ctrl-c-ctrl-c\\]."
  584. :group 'org-plain-lists
  585. :type 'boolean)
  586. (defgroup org-archive nil
  587. "Options concerning archiving in Org-mode."
  588. :tag "Org Archive"
  589. :group 'org-structure)
  590. (defcustom org-archive-tag "ARCHIVE"
  591. "The tag that marks a subtree as archived.
  592. An archived subtree does not open during visibility cycling, and does
  593. not contribute to the agenda listings.
  594. After changing this, font-lock must be restarted in the relevant buffers to
  595. get the proper fontification."
  596. :group 'org-archive
  597. :group 'org-keywords
  598. :type 'string)
  599. (defcustom org-agenda-skip-archived-trees t
  600. "Non-nil means, the agenda will skip any items located in archived trees.
  601. An archived tree is a tree marked with the tag ARCHIVE."
  602. :group 'org-archive
  603. :group 'org-agenda-skip
  604. :type 'boolean)
  605. (defcustom org-cycle-open-archived-trees nil
  606. "Non-nil means, `org-cycle' will open archived trees.
  607. An archived tree is a tree marked with the tag ARCHIVE.
  608. When nil, archived trees will stay folded. You can still open them with
  609. normal outline commands like `show-all', but not with the cycling commands."
  610. :group 'org-archive
  611. :group 'org-cycle
  612. :type 'boolean)
  613. (defcustom org-sparse-tree-open-archived-trees nil
  614. "Non-nil means sparse tree construction shows matches in archived trees.
  615. When nil, matches in these trees are highlighted, but the trees are kept in
  616. collapsed state."
  617. :group 'org-archive
  618. :group 'org-sparse-trees
  619. :type 'boolean)
  620. (defcustom org-archive-location "%s_archive::"
  621. "The location where subtrees should be archived.
  622. This string consists of two parts, separated by a double-colon.
  623. The first part is a file name - when omitted, archiving happens in the same
  624. file. %s will be replaced by the current file name (without directory part).
  625. Archiving to a different file is useful to keep archived entries from
  626. contributing to the Org-mode Agenda.
  627. The part after the double colon is a headline. The archived entries will be
  628. filed under that headline. When omitted, the subtrees are simply filed away
  629. at the end of the file, as top-level entries.
  630. Here are a few examples:
  631. \"%s_archive::\"
  632. If the current file is Projects.org, archive in file
  633. Projects.org_archive, as top-level trees. This is the default.
  634. \"::* Archived Tasks\"
  635. Archive in the current file, under the top-level headline
  636. \"* Archived Tasks\".
  637. \"~/org/archive.org::\"
  638. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  639. \"basement::** Finished Tasks\"
  640. Archive in file ./basement (relative path), as level 3 trees
  641. below the level 2 heading \"** Finished Tasks\".
  642. You may set this option on a per-file basis by adding to the buffer a
  643. line like
  644. #+ARCHIVE: basement::** Finished Tasks"
  645. :group 'org-archive
  646. :type 'string)
  647. (defcustom org-archive-mark-done t
  648. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  649. This can be a string to set the keyword to use. When t, Org-mode will
  650. use the first keyword in its list that means done."
  651. :group 'org-archive
  652. :type '(choice
  653. (const :tag "No" nil)
  654. (const :tag "Yes" t)
  655. (string :tag "Use this keyword")))
  656. (defcustom org-archive-stamp-time t
  657. "Non-nil means, add a time stamp to entries moved to an archive file.
  658. This variable is obsolete and has no effect anymore, instead add ot remove
  659. `time' from the variablle `org-archive-save-context-info'."
  660. :group 'org-archive
  661. :type 'boolean)
  662. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  663. "Parts of context info that should be stored as properties when archiving.
  664. When a subtree is moved to an archive file, it looses information given by
  665. context, like inherited tags, the category, and possibly also the TODO
  666. state (depending on the variable `org-archive-mark-done').
  667. This variable can be a list of any of the following symbols:
  668. time The time of archiving.
  669. file The file where the entry originates.
  670. itags The local tags, in the headline of the subtree.
  671. ltags The tags the subtree inherits from further up the hierarchy.
  672. todo The pre-archive TODO state.
  673. category The category, taken from file name or #+CATEGORY lines.
  674. olpath The outline path to the item. These are all headlines above
  675. the current item, separated by /, like a file path.
  676. For each symbol present in the list, a property will be created in
  677. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  678. information."
  679. :group 'org-archive
  680. :type '(set :greedy t
  681. (const :tag "Time" time)
  682. (const :tag "File" file)
  683. (const :tag "Category" category)
  684. (const :tag "TODO state" todo)
  685. (const :tag "TODO state" priority)
  686. (const :tag "Inherited tags" itags)
  687. (const :tag "Outline path" olpath)
  688. (const :tag "Local tags" ltags)))
  689. (defgroup org-imenu-and-speedbar nil
  690. "Options concerning imenu and speedbar in Org-mode."
  691. :tag "Org Imenu and Speedbar"
  692. :group 'org-structure)
  693. (defcustom org-imenu-depth 2
  694. "The maximum level for Imenu access to Org-mode headlines.
  695. This also applied for speedbar access."
  696. :group 'org-imenu-and-speedbar
  697. :type 'number)
  698. (defgroup org-table nil
  699. "Options concerning tables in Org-mode."
  700. :tag "Org Table"
  701. :group 'org)
  702. (defcustom org-enable-table-editor 'optimized
  703. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  704. When nil, such lines will be treated like ordinary lines.
  705. When equal to the symbol `optimized', the table editor will be optimized to
  706. do the following:
  707. - Automatic overwrite mode in front of whitespace in table fields.
  708. This makes the structure of the table stay in tact as long as the edited
  709. field does not exceed the column width.
  710. - Minimize the number of realigns. Normally, the table is aligned each time
  711. TAB or RET are pressed to move to another field. With optimization this
  712. happens only if changes to a field might have changed the column width.
  713. Optimization requires replacing the functions `self-insert-command',
  714. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  715. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  716. very good at guessing when a re-align will be necessary, but you can always
  717. force one with \\[org-ctrl-c-ctrl-c].
  718. If you would like to use the optimized version in Org-mode, but the
  719. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  720. This variable can be used to turn on and off the table editor during a session,
  721. but in order to toggle optimization, a restart is required.
  722. See also the variable `org-table-auto-blank-field'."
  723. :group 'org-table
  724. :type '(choice
  725. (const :tag "off" nil)
  726. (const :tag "on" t)
  727. (const :tag "on, optimized" optimized)))
  728. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  729. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  730. In the optimized version, the table editor takes over all simple keys that
  731. normally just insert a character. In tables, the characters are inserted
  732. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  733. for empty fields). Outside tables, the correct binding of the keys is
  734. restored.
  735. The default for this option is t if the optimized version is also used in
  736. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  737. this variable requires a restart of Emacs to become effective."
  738. :group 'org-table
  739. :type 'boolean)
  740. (defcustom orgtbl-radio-table-templates
  741. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  742. % END RECEIVE ORGTBL %n
  743. \\begin{comment}
  744. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  745. | | |
  746. \\end{comment}\n")
  747. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  748. @c END RECEIVE ORGTBL %n
  749. @ignore
  750. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  751. | | |
  752. @end ignore\n")
  753. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  754. <!-- END RECEIVE ORGTBL %n -->
  755. <!--
  756. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  757. | | |
  758. -->\n"))
  759. "Templates for radio tables in different major modes.
  760. All occurrences of %n in a template will be replaced with the name of the
  761. table, obtained by prompting the user."
  762. :group 'org-table
  763. :type '(repeat
  764. (list (symbol :tag "Major mode")
  765. (string :tag "Format"))))
  766. (defgroup org-table-settings nil
  767. "Settings for tables in Org-mode."
  768. :tag "Org Table Settings"
  769. :group 'org-table)
  770. (defcustom org-table-default-size "5x2"
  771. "The default size for newly created tables, Columns x Rows."
  772. :group 'org-table-settings
  773. :type 'string)
  774. (defcustom org-table-number-regexp
  775. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  776. "Regular expression for recognizing numbers in table columns.
  777. If a table column contains mostly numbers, it will be aligned to the
  778. right. If not, it will be aligned to the left.
  779. The default value of this option is a regular expression which allows
  780. anything which looks remotely like a number as used in scientific
  781. context. For example, all of the following will be considered a
  782. number:
  783. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  784. Other options offered by the customize interface are more restrictive."
  785. :group 'org-table-settings
  786. :type '(choice
  787. (const :tag "Positive Integers"
  788. "^[0-9]+$")
  789. (const :tag "Integers"
  790. "^[-+]?[0-9]+$")
  791. (const :tag "Floating Point Numbers"
  792. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  793. (const :tag "Floating Point Number or Integer"
  794. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  795. (const :tag "Exponential, Floating point, Integer"
  796. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  797. (const :tag "Very General Number-Like, including hex"
  798. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  799. (string :tag "Regexp:")))
  800. (defcustom org-table-number-fraction 0.5
  801. "Fraction of numbers in a column required to make the column align right.
  802. In a column all non-white fields are considered. If at least this
  803. fraction of fields is matched by `org-table-number-fraction',
  804. alignment to the right border applies."
  805. :group 'org-table-settings
  806. :type 'number)
  807. (defgroup org-table-editing nil
  808. "Behavior of tables during editing in Org-mode."
  809. :tag "Org Table Editing"
  810. :group 'org-table)
  811. (defcustom org-table-automatic-realign t
  812. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  813. When nil, aligning is only done with \\[org-table-align], or after column
  814. removal/insertion."
  815. :group 'org-table-editing
  816. :type 'boolean)
  817. (defcustom org-table-auto-blank-field t
  818. "Non-nil means, automatically blank table field when starting to type into it.
  819. This only happens when typing immediately after a field motion
  820. command (TAB, S-TAB or RET).
  821. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  822. :group 'org-table-editing
  823. :type 'boolean)
  824. (defcustom org-table-tab-jumps-over-hlines t
  825. "Non-nil means, tab in the last column of a table with jump over a hline.
  826. If a horizontal separator line is following the current line,
  827. `org-table-next-field' can either create a new row before that line, or jump
  828. over the line. When this option is nil, a new line will be created before
  829. this line."
  830. :group 'org-table-editing
  831. :type 'boolean)
  832. (defcustom org-table-tab-recognizes-table.el t
  833. "Non-nil means, TAB will automatically notice a table.el table.
  834. When it sees such a table, it moves point into it and - if necessary -
  835. calls `table-recognize-table'."
  836. :group 'org-table-editing
  837. :type 'boolean)
  838. (defgroup org-table-calculation nil
  839. "Options concerning tables in Org-mode."
  840. :tag "Org Table Calculation"
  841. :group 'org-table)
  842. (defcustom org-table-use-standard-references t
  843. "Should org-mode work with table refrences like B3 instead of @3$2?
  844. Possible values are:
  845. nil never use them
  846. from accept as input, do not present for editing
  847. t: accept as input and present for editing"
  848. :group 'org-table-calculation
  849. :type '(choice
  850. (const :tag "Never, don't even check unser input for them" nil)
  851. (const :tag "Always, both as user input, and when editing" t)
  852. (const :tag "Convert user input, don't offer during editing" 'from)))
  853. (defcustom org-table-copy-increment t
  854. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  855. :group 'org-table-calculation
  856. :type 'boolean)
  857. (defcustom org-calc-default-modes
  858. '(calc-internal-prec 12
  859. calc-float-format (float 5)
  860. calc-angle-mode deg
  861. calc-prefer-frac nil
  862. calc-symbolic-mode nil
  863. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  864. calc-display-working-message t
  865. )
  866. "List with Calc mode settings for use in calc-eval for table formulas.
  867. The list must contain alternating symbols (Calc modes variables and values).
  868. Don't remove any of the default settings, just change the values. Org-mode
  869. relies on the variables to be present in the list."
  870. :group 'org-table-calculation
  871. :type 'plist)
  872. (defcustom org-table-formula-evaluate-inline t
  873. "Non-nil means, TAB and RET evaluate a formula in current table field.
  874. If the current field starts with an equal sign, it is assumed to be a formula
  875. which should be evaluated as described in the manual and in the documentation
  876. string of the command `org-table-eval-formula'. This feature requires the
  877. Emacs calc package.
  878. When this variable is nil, formula calculation is only available through
  879. the command \\[org-table-eval-formula]."
  880. :group 'org-table-calculation
  881. :type 'boolean)
  882. (defcustom org-table-formula-use-constants t
  883. "Non-nil means, interpret constants in formulas in tables.
  884. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  885. by the value given in `org-table-formula-constants', or by a value obtained
  886. from the `constants.el' package."
  887. :group 'org-table-calculation
  888. :type 'boolean)
  889. (defcustom org-table-formula-constants nil
  890. "Alist with constant names and values, for use in table formulas.
  891. The car of each element is a name of a constant, without the `$' before it.
  892. The cdr is the value as a string. For example, if you'd like to use the
  893. speed of light in a formula, you would configure
  894. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  895. and then use it in an equation like `$1*$c'.
  896. Constants can also be defined on a per-file basis using a line like
  897. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  898. :group 'org-table-calculation
  899. :type '(repeat
  900. (cons (string :tag "name")
  901. (string :tag "value"))))
  902. (defvar org-table-formula-constants-local nil
  903. "Local version of `org-table-formula-constants'.")
  904. (make-variable-buffer-local 'org-table-formula-constants-local)
  905. (defcustom org-table-allow-automatic-line-recalculation t
  906. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  907. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  908. :group 'org-table-calculation
  909. :type 'boolean)
  910. (defgroup org-link nil
  911. "Options concerning links in Org-mode."
  912. :tag "Org Link"
  913. :group 'org)
  914. (defvar org-link-abbrev-alist-local nil
  915. "Buffer-local version of `org-link-abbrev-alist', which see.
  916. The value of this is taken from the #+LINK lines.")
  917. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  918. (defcustom org-link-abbrev-alist nil
  919. "Alist of link abbreviations.
  920. The car of each element is a string, to be replaced at the start of a link.
  921. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  922. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  923. [[linkkey:tag][description]]
  924. If REPLACE is a string, the tag will simply be appended to create the link.
  925. If the string contains \"%s\", the tag will be inserted there.
  926. REPLACE may also be a function that will be called with the tag as the
  927. only argument to create the link, which should be returned as a string.
  928. See the manual for examples."
  929. :group 'org-link
  930. :type 'alist)
  931. (defcustom org-descriptive-links t
  932. "Non-nil means, hide link part and only show description of bracket links.
  933. Bracket links are like [[link][descritpion]]. This variable sets the initial
  934. state in new org-mode buffers. The setting can then be toggled on a
  935. per-buffer basis from the Org->Hyperlinks menu."
  936. :group 'org-link
  937. :type 'boolean)
  938. (defcustom org-link-file-path-type 'adaptive
  939. "How the path name in file links should be stored.
  940. Valid values are:
  941. relative Relative to the current directory, i.e. the directory of the file
  942. into which the link is being inserted.
  943. absolute Absolute path, if possible with ~ for home directory.
  944. noabbrev Absolute path, no abbreviation of home directory.
  945. adaptive Use relative path for files in the current directory and sub-
  946. directories of it. For other files, use an absolute path."
  947. :group 'org-link
  948. :type '(choice
  949. (const relative)
  950. (const absolute)
  951. (const noabbrev)
  952. (const adaptive)))
  953. (defcustom org-activate-links '(bracket angle plain radio tag date)
  954. "Types of links that should be activated in Org-mode files.
  955. This is a list of symbols, each leading to the activation of a certain link
  956. type. In principle, it does not hurt to turn on most link types - there may
  957. be a small gain when turning off unused link types. The types are:
  958. bracket The recommended [[link][description]] or [[link]] links with hiding.
  959. angular Links in angular brackes that may contain whitespace like
  960. <bbdb:Carsten Dominik>.
  961. plain Plain links in normal text, no whitespace, like http://google.com.
  962. radio Text that is matched by a radio target, see manual for details.
  963. tag Tag settings in a headline (link to tag search).
  964. date Time stamps (link to calendar).
  965. Changing this variable requires a restart of Emacs to become effective."
  966. :group 'org-link
  967. :type '(set (const :tag "Double bracket links (new style)" bracket)
  968. (const :tag "Angular bracket links (old style)" angular)
  969. (const :tag "plain text links" plain)
  970. (const :tag "Radio target matches" radio)
  971. (const :tag "Tags" tag)
  972. (const :tag "Tags" target)
  973. (const :tag "Timestamps" date)))
  974. (defgroup org-link-store nil
  975. "Options concerning storing links in Org-mode"
  976. :tag "Org Store Link"
  977. :group 'org-link)
  978. (defcustom org-email-link-description-format "Email %c: %.30s"
  979. "Format of the description part of a link to an email or usenet message.
  980. The following %-excapes will be replaced by corresponding information:
  981. %F full \"From\" field
  982. %f name, taken from \"From\" field, address if no name
  983. %T full \"To\" field
  984. %t first name in \"To\" field, address if no name
  985. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  986. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  987. %s subject
  988. %m message-id.
  989. You may use normal field width specification between the % and the letter.
  990. This is for example useful to limit the length of the subject.
  991. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  992. :group 'org-link-store
  993. :type 'string)
  994. (defcustom org-from-is-user-regexp
  995. (let (r1 r2)
  996. (when (and user-mail-address (not (string= user-mail-address "")))
  997. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  998. (when (and user-full-name (not (string= user-full-name "")))
  999. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1000. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1001. "Regexp mached against the \"From:\" header of an email or usenet message.
  1002. It should match if the message is from the user him/herself."
  1003. :group 'org-link-store
  1004. :type 'regexp)
  1005. (defcustom org-context-in-file-links t
  1006. "Non-nil means, file links from `org-store-link' contain context.
  1007. A search string will be added to the file name with :: as separator and
  1008. used to find the context when the link is activated by the command
  1009. `org-open-at-point'.
  1010. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1011. negates this setting for the duration of the command."
  1012. :group 'org-link-store
  1013. :type 'boolean)
  1014. (defcustom org-keep-stored-link-after-insertion nil
  1015. "Non-nil means, keep link in list for entire session.
  1016. The command `org-store-link' adds a link pointing to the current
  1017. location to an internal list. These links accumulate during a session.
  1018. The command `org-insert-link' can be used to insert links into any
  1019. Org-mode file (offering completion for all stored links). When this
  1020. option is nil, every link which has been inserted once using \\[org-insert-link]
  1021. will be removed from the list, to make completing the unused links
  1022. more efficient."
  1023. :group 'org-link-store
  1024. :type 'boolean)
  1025. (defcustom org-usenet-links-prefer-google nil
  1026. "Non-nil means, `org-store-link' will create web links to Google groups.
  1027. When nil, Gnus will be used for such links.
  1028. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1029. negates this setting for the duration of the command."
  1030. :group 'org-link-store
  1031. :type 'boolean)
  1032. (defgroup org-link-follow nil
  1033. "Options concerning following links in Org-mode"
  1034. :tag "Org Follow Link"
  1035. :group 'org-link)
  1036. (defcustom org-tab-follows-link nil
  1037. "Non-nil means, on links TAB will follow the link.
  1038. Needs to be set before org.el is loaded."
  1039. :group 'org-link-follow
  1040. :type 'boolean)
  1041. (defcustom org-return-follows-link nil
  1042. "Non-nil means, on links RET will follow the link.
  1043. Needs to be set before org.el is loaded."
  1044. :group 'org-link-follow
  1045. :type 'boolean)
  1046. (defcustom org-mouse-1-follows-link t
  1047. "Non-nil means, mouse-1 on a link will follow the link.
  1048. A longer mouse click will still set point. Does not wortk on XEmacs.
  1049. Needs to be set before org.el is loaded."
  1050. :group 'org-link-follow
  1051. :type 'boolean)
  1052. (defcustom org-mark-ring-length 4
  1053. "Number of different positions to be recorded in the ring
  1054. Changing this requires a restart of Emacs to work correctly."
  1055. :group 'org-link-follow
  1056. :type 'interger)
  1057. (defcustom org-link-frame-setup
  1058. '((vm . vm-visit-folder-other-frame)
  1059. (gnus . gnus-other-frame)
  1060. (file . find-file-other-window))
  1061. "Setup the frame configuration for following links.
  1062. When following a link with Emacs, it may often be useful to display
  1063. this link in another window or frame. This variable can be used to
  1064. set this up for the different types of links.
  1065. For VM, use any of
  1066. `vm-visit-folder'
  1067. `vm-visit-folder-other-frame'
  1068. For Gnus, use any of
  1069. `gnus'
  1070. `gnus-other-frame'
  1071. For FILE, use any of
  1072. `find-file'
  1073. `find-file-other-window'
  1074. `find-file-other-frame'
  1075. For the calendar, use the variable `calendar-setup'.
  1076. For BBDB, it is currently only possible to display the matches in
  1077. another window."
  1078. :group 'org-link-follow
  1079. :type '(list
  1080. (cons (const vm)
  1081. (choice
  1082. (const vm-visit-folder)
  1083. (const vm-visit-folder-other-window)
  1084. (const vm-visit-folder-other-frame)))
  1085. (cons (const gnus)
  1086. (choice
  1087. (const gnus)
  1088. (const gnus-other-frame)))
  1089. (cons (const file)
  1090. (choice
  1091. (const find-file)
  1092. (const find-file-other-window)
  1093. (const find-file-other-frame)))))
  1094. (defcustom org-display-internal-link-with-indirect-buffer nil
  1095. "Non-nil means, use indirect buffer to display infile links.
  1096. Activating internal links (from one location in a file to another location
  1097. in the same file) normally just jumps to the location. When the link is
  1098. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1099. another window. When this option is set, the other window actually displays
  1100. an indirect buffer clone of the current buffer, to avoid any visibility
  1101. changes to the current buffer."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-open-non-existing-files nil
  1105. "Non-nil means, `org-open-file' will open non-existing files.
  1106. When nil, an error will be generated."
  1107. :group 'org-link-follow
  1108. :type 'boolean)
  1109. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1110. "Function and arguments to call for following mailto links.
  1111. This is a list with the first element being a lisp function, and the
  1112. remaining elements being arguments to the function. In string arguments,
  1113. %a will be replaced by the address, and %s will be replaced by the subject
  1114. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1115. :group 'org-link-follow
  1116. :type '(choice
  1117. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1118. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1119. (const :tag "message-mail" (message-mail "%a" "%s"))
  1120. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1121. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1122. "Non-nil means, ask for confirmation before executing shell links.
  1123. Shell links can be dangerous: just think about a link
  1124. [[shell:rm -rf ~/*][Google Search]]
  1125. This link would show up in your Org-mode document as \"Google Search\",
  1126. but really it would remove your entire home directory.
  1127. Therefore we advise against setting this variable to nil.
  1128. Just change it to `y-or-n-p' of you want to confirm with a
  1129. single keystroke rather than having to type \"yes\"."
  1130. :group 'org-link-follow
  1131. :type '(choice
  1132. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1133. (const :tag "with y-or-n (faster)" y-or-n-p)
  1134. (const :tag "no confirmation (dangerous)" nil)))
  1135. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1136. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1137. Elisp links can be dangerous: just think about a link
  1138. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1139. This link would show up in your Org-mode document as \"Google Search\",
  1140. but really it would remove your entire home directory.
  1141. Therefore we advise against setting this variable to nil.
  1142. Just change it to `y-or-n-p' of you want to confirm with a
  1143. single keystroke rather than having to type \"yes\"."
  1144. :group 'org-link-follow
  1145. :type '(choice
  1146. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1147. (const :tag "with y-or-n (faster)" y-or-n-p)
  1148. (const :tag "no confirmation (dangerous)" nil)))
  1149. (defconst org-file-apps-defaults-gnu
  1150. '((remote . emacs)
  1151. (t . mailcap))
  1152. "Default file applications on a UNIX or GNU/Linux system.
  1153. See `org-file-apps'.")
  1154. (defconst org-file-apps-defaults-macosx
  1155. '((remote . emacs)
  1156. (t . "open %s")
  1157. ("ps" . "gv %s")
  1158. ("ps.gz" . "gv %s")
  1159. ("eps" . "gv %s")
  1160. ("eps.gz" . "gv %s")
  1161. ("dvi" . "xdvi %s")
  1162. ("fig" . "xfig %s"))
  1163. "Default file applications on a MacOS X system.
  1164. The system \"open\" is known as a default, but we use X11 applications
  1165. for some files for which the OS does not have a good default.
  1166. See `org-file-apps'.")
  1167. (defconst org-file-apps-defaults-windowsnt
  1168. (list
  1169. '(remote . emacs)
  1170. (cons t
  1171. (list (if (featurep 'xemacs)
  1172. 'mswindows-shell-execute
  1173. 'w32-shell-execute)
  1174. "open" 'file)))
  1175. "Default file applications on a Windows NT system.
  1176. The system \"open\" is used for most files.
  1177. See `org-file-apps'.")
  1178. (defcustom org-file-apps
  1179. '(
  1180. ("txt" . emacs)
  1181. ("tex" . emacs)
  1182. ("ltx" . emacs)
  1183. ("org" . emacs)
  1184. ("el" . emacs)
  1185. ("bib" . emacs)
  1186. )
  1187. "External applications for opening `file:path' items in a document.
  1188. Org-mode uses system defaults for different file types, but
  1189. you can use this variable to set the application for a given file
  1190. extension. The entries in this list are cons cells where the car identifies
  1191. files and the cdr the corresponding command. Possible values for the
  1192. file identifier are
  1193. \"ext\" A string identifying an extension
  1194. `directory' Matches a directory
  1195. `remote' Matches a remote file, accessible through tramp or efs.
  1196. Remote files most likely should be visited through Emacs
  1197. because external applications cannot handle such paths.
  1198. t Default for all remaining files
  1199. Possible values for the command are:
  1200. `emacs' The file will be visited by the current Emacs process.
  1201. `default' Use the default application for this file type.
  1202. string A command to be executed by a shell; %s will be replaced
  1203. by the path to the file.
  1204. sexp A Lisp form which will be evaluated. The file path will
  1205. be available in the Lisp variable `file'.
  1206. For more examples, see the system specific constants
  1207. `org-file-apps-defaults-macosx'
  1208. `org-file-apps-defaults-windowsnt'
  1209. `org-file-apps-defaults-gnu'."
  1210. :group 'org-link-follow
  1211. :type '(repeat
  1212. (cons (choice :value ""
  1213. (string :tag "Extension")
  1214. (const :tag "Default for unrecognized files" t)
  1215. (const :tag "Remote file" remote)
  1216. (const :tag "Links to a directory" directory))
  1217. (choice :value ""
  1218. (const :tag "Visit with Emacs" emacs)
  1219. (const :tag "Use system default" default)
  1220. (string :tag "Command")
  1221. (sexp :tag "Lisp form")))))
  1222. (defcustom org-mhe-search-all-folders nil
  1223. "Non-nil means, that the search for the mh-message will be extended to
  1224. all folders if the message cannot be found in the folder given in the link.
  1225. Searching all folders is very efficient with one of the search engines
  1226. supported by MH-E, but will be slow with pick."
  1227. :group 'org-link-follow
  1228. :type 'boolean)
  1229. (defgroup org-remember nil
  1230. "Options concerning interaction with remember.el."
  1231. :tag "Org Remember"
  1232. :group 'org)
  1233. (defcustom org-directory "~/org"
  1234. "Directory with org files.
  1235. This directory will be used as default to prompt for org files.
  1236. Used by the hooks for remember.el."
  1237. :group 'org-remember
  1238. :type 'directory)
  1239. (defcustom org-default-notes-file "~/.notes"
  1240. "Default target for storing notes.
  1241. Used by the hooks for remember.el. This can be a string, or nil to mean
  1242. the value of `remember-data-file'.
  1243. You can set this on a per-template basis with the variable
  1244. `org-remember-templates'."
  1245. :group 'org-remember
  1246. :type '(choice
  1247. (const :tag "Default from remember-data-file" nil)
  1248. file))
  1249. (defcustom org-remember-store-without-prompt t
  1250. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1251. In this case, you need `C-u C-c C-c' to get the prompts for
  1252. note file and headline.
  1253. When this variable is nil, `C-c C-c' give you the prompts, and
  1254. `C-u C-c C-c' trigger the fasttrack."
  1255. :group 'org-remember
  1256. :type 'boolean)
  1257. (defcustom org-remember-interactive-interface 'refile
  1258. "The interface to be used for interactive filing of remember notes.
  1259. This is only used when the interactive mode for selecting a filing
  1260. location is used (see the variable `org-remember-store-without-prompt').
  1261. Allowed vaues are:
  1262. outline The interface shows an outline of the relevant file
  1263. and the correct heading is found by moving through
  1264. the outline or by searching with incremental search.
  1265. outline-path-completion Headlines in the current buffer are offered via
  1266. completion.
  1267. refile Use the refile interface, and offer headlines,
  1268. possibly from different buffers."
  1269. :group 'org-remember
  1270. :type '(choice
  1271. (const :tag "Refile" refile)
  1272. (const :tag "Outline" outline)
  1273. (const :tag "Outline-path-completion" outline-path-completion)))
  1274. (defcustom org-goto-interface 'outline
  1275. "The default interface to be used for `org-goto'.
  1276. Allowed vaues are:
  1277. outline The interface shows an outline of the relevant file
  1278. and the correct heading is found by moving through
  1279. the outline or by searching with incremental search.
  1280. outline-path-completion Headlines in the current buffer are offered via
  1281. completion."
  1282. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1283. :type '(choice
  1284. (const :tag "Outline" outline)
  1285. (const :tag "Outline-path-completion" outline-path-completion)))
  1286. (defcustom org-remember-default-headline ""
  1287. "The headline that should be the default location in the notes file.
  1288. When filing remember notes, the cursor will start at that position.
  1289. You can set this on a per-template basis with the variable
  1290. `org-remember-templates'."
  1291. :group 'org-remember
  1292. :type 'string)
  1293. (defcustom org-remember-templates nil
  1294. "Templates for the creation of remember buffers.
  1295. When nil, just let remember make the buffer.
  1296. When not nil, this is a list of 5-element lists. In each entry, the first
  1297. element is the name of the template, which should be a single short word.
  1298. The second element is a character, a unique key to select this template.
  1299. The third element is the template. The fourth element is optional and can
  1300. specify a destination file for remember items created with this template.
  1301. The default file is given by `org-default-notes-file'. An optional fifth
  1302. element can specify the headline in that file that should be offered
  1303. first when the user is asked to file the entry. The default headline is
  1304. given in the variable `org-remember-default-headline'.
  1305. The template specifies the structure of the remember buffer. It should have
  1306. a first line starting with a star, to act as the org-mode headline.
  1307. Furthermore, the following %-escapes will be replaced with content:
  1308. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1309. A default value and a completion table ca be specified like this:
  1310. %^{prompt|default|completion2|completion3|...}
  1311. %t time stamp, date only
  1312. %T time stamp with date and time
  1313. %u, %U like the above, but inactive time stamps
  1314. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1315. You may define a prompt like %^{Please specify birthday}t
  1316. %n user name (taken from `user-full-name')
  1317. %a annotation, normally the link created with org-store-link
  1318. %i initial content, the region active. If %i is indented,
  1319. the entire inserted text will be indented as well.
  1320. %c content of the clipboard, or current kill ring head
  1321. %^g prompt for tags, with completion on tags in target file
  1322. %^G prompt for tags, with completion all tags in all agenda files
  1323. %:keyword specific information for certain link types, see below
  1324. %[pathname] insert the contents of the file given by `pathname'
  1325. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1326. %! Store this note immediately after filling the template
  1327. %? After completing the template, position cursor here.
  1328. Apart from these general escapes, you can access information specific to the
  1329. link type that is created. For example, calling `remember' in emails or gnus
  1330. will record the author and the subject of the message, which you can access
  1331. with %:author and %:subject, respectively. Here is a complete list of what
  1332. is recorded for each link type.
  1333. Link type | Available information
  1334. -------------------+------------------------------------------------------
  1335. bbdb | %:type %:name %:company
  1336. vm, wl, mh, rmail | %:type %:subject %:message-id
  1337. | %:from %:fromname %:fromaddress
  1338. | %:to %:toname %:toaddress
  1339. | %:fromto (either \"to NAME\" or \"from NAME\")
  1340. gnus | %:group, for messages also all email fields
  1341. w3, w3m | %:type %:url
  1342. info | %:type %:file %:node
  1343. calendar | %:type %:date"
  1344. :group 'org-remember
  1345. :get (lambda (var) ; Make sure all entries have 5 elements
  1346. (mapcar (lambda (x)
  1347. (if (not (stringp (car x))) (setq x (cons "" x)))
  1348. (cond ((= (length x) 4) (append x '("")))
  1349. ((= (length x) 3) (append x '("" "")))
  1350. (t x)))
  1351. (default-value var)))
  1352. :type '(repeat
  1353. :tag "enabled"
  1354. (list :value ("" ?a "\n" nil nil)
  1355. (string :tag "Name")
  1356. (character :tag "Selection Key")
  1357. (string :tag "Template")
  1358. (choice
  1359. (file :tag "Destination file")
  1360. (const :tag "Prompt for file" nil))
  1361. (choice
  1362. (string :tag "Destination headline")
  1363. (const :tag "Selection interface for heading")))))
  1364. (defcustom org-reverse-note-order nil
  1365. "Non-nil means, store new notes at the beginning of a file or entry.
  1366. When nil, new notes will be filed to the end of a file or entry.
  1367. This can also be a list with cons cells of regular expressions that
  1368. are matched against file names, and values."
  1369. :group 'org-remember
  1370. :type '(choice
  1371. (const :tag "Reverse always" t)
  1372. (const :tag "Reverse never" nil)
  1373. (repeat :tag "By file name regexp"
  1374. (cons regexp boolean))))
  1375. (defcustom org-refile-targets nil
  1376. "Targets for refiling entries with \\[org-refile].
  1377. This is list of cons cells. Each cell contains:
  1378. - a specification of the files to be considered, either a list of files,
  1379. or a symbol whose function or value fields will be used to retrieve
  1380. a file name or a list of file names. Nil means, refile to a different
  1381. heading in the current buffer.
  1382. - A specification of how to find candidate refile targets. This may be
  1383. any of
  1384. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1385. This tag has to be present in all target headlines, inheritance will
  1386. not be considered.
  1387. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1388. todo keyword.
  1389. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1390. headlines that are refiling targets.
  1391. - a cons cell (:level . N). Any headline of level N is considered a target.
  1392. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1393. ;; FIXME: what if there are a var and func with same name???
  1394. :group 'org-remember
  1395. :type '(repeat
  1396. (cons
  1397. (choice :value org-agenda-files
  1398. (const :tag "All agenda files" org-agenda-files)
  1399. (const :tag "Current buffer" nil)
  1400. (function) (variable) (file))
  1401. (choice :tag "Identify target headline by"
  1402. (cons :tag "Specific tag" (const :tag) (string))
  1403. (cons :tag "TODO keyword" (const :todo) (string))
  1404. (cons :tag "Regular expression" (const :regexp) (regexp))
  1405. (cons :tag "Level number" (const :level) (integer))
  1406. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1407. (defcustom org-refile-use-outline-path nil
  1408. "Non-nil means, provide refile targets as paths.
  1409. So a level 3 headline will be available as level1/level2/level3.
  1410. When the value is `file', also include the file name (without directory)
  1411. into the path. When `full-file-path', include the full file path."
  1412. :group 'org-remember
  1413. :type '(choice
  1414. (const :tag "Not" nil)
  1415. (const :tag "Yes" t)
  1416. (const :tag "Start with file name" file)
  1417. (const :tag "Start with full file path" full-file-path)))
  1418. (defgroup org-todo nil
  1419. "Options concerning TODO items in Org-mode."
  1420. :tag "Org TODO"
  1421. :group 'org)
  1422. (defgroup org-progress nil
  1423. "Options concerning Progress logging in Org-mode."
  1424. :tag "Org Progress"
  1425. :group 'org-time)
  1426. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1427. "List of TODO entry keyword sequences and their interpretation.
  1428. \\<org-mode-map>This is a list of sequences.
  1429. Each sequence starts with a symbol, either `sequence' or `type',
  1430. indicating if the keywords should be interpreted as a sequence of
  1431. action steps, or as different types of TODO items. The first
  1432. keywords are states requiring action - these states will select a headline
  1433. for inclusion into the global TODO list Org-mode produces. If one of
  1434. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1435. signify that no further action is necessary. If \"|\" is not found,
  1436. the last keyword is treated as the only DONE state of the sequence.
  1437. The command \\[org-todo] cycles an entry through these states, and one
  1438. additional state where no keyword is present. For details about this
  1439. cycling, see the manual.
  1440. TODO keywords and interpretation can also be set on a per-file basis with
  1441. the special #+SEQ_TODO and #+TYP_TODO lines.
  1442. For backward compatibility, this variable may also be just a list
  1443. of keywords - in this case the interptetation (sequence or type) will be
  1444. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1445. :group 'org-todo
  1446. :group 'org-keywords
  1447. :type '(choice
  1448. (repeat :tag "Old syntax, just keywords"
  1449. (string :tag "Keyword"))
  1450. (repeat :tag "New syntax"
  1451. (cons
  1452. (choice
  1453. :tag "Interpretation"
  1454. (const :tag "Sequence (cycling hits every state)" sequence)
  1455. (const :tag "Type (cycling directly to DONE)" type))
  1456. (repeat
  1457. (string :tag "Keyword"))))))
  1458. (defvar org-todo-keywords-1 nil)
  1459. (make-variable-buffer-local 'org-todo-keywords-1)
  1460. (defvar org-todo-keywords-for-agenda nil)
  1461. (defvar org-done-keywords-for-agenda nil)
  1462. (defvar org-not-done-keywords nil)
  1463. (make-variable-buffer-local 'org-not-done-keywords)
  1464. (defvar org-done-keywords nil)
  1465. (make-variable-buffer-local 'org-done-keywords)
  1466. (defvar org-todo-heads nil)
  1467. (make-variable-buffer-local 'org-todo-heads)
  1468. (defvar org-todo-sets nil)
  1469. (make-variable-buffer-local 'org-todo-sets)
  1470. (defvar org-todo-log-states nil)
  1471. (make-variable-buffer-local 'org-todo-log-states)
  1472. (defvar org-todo-kwd-alist nil)
  1473. (make-variable-buffer-local 'org-todo-kwd-alist)
  1474. (defvar org-todo-key-alist nil)
  1475. (make-variable-buffer-local 'org-todo-key-alist)
  1476. (defvar org-todo-key-trigger nil)
  1477. (make-variable-buffer-local 'org-todo-key-trigger)
  1478. (defcustom org-todo-interpretation 'sequence
  1479. "Controls how TODO keywords are interpreted.
  1480. This variable is in principle obsolete and is only used for
  1481. backward compatibility, if the interpretation of todo keywords is
  1482. not given already in `org-todo-keywords'. See that variable for
  1483. more information."
  1484. :group 'org-todo
  1485. :group 'org-keywords
  1486. :type '(choice (const sequence)
  1487. (const type)))
  1488. (defcustom org-use-fast-todo-selection 'prefix
  1489. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1490. This variable describes if and under what circumstances the cycling
  1491. mechanism for TODO keywords will be replaced by a single-key, direct
  1492. selection scheme.
  1493. When nil, fast selection is never used.
  1494. When the symbol `prefix', it will be used when `org-todo' is called with
  1495. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1496. in an agenda buffer.
  1497. When t, fast selection is used by default. In this case, the prefix
  1498. argument forces cycling instead.
  1499. In all cases, the special interface is only used if access keys have actually
  1500. been assigned by the user, i.e. if keywords in the configuration are followed
  1501. by a letter in parenthesis, like TODO(t)."
  1502. :group 'org-todo
  1503. :type '(choice
  1504. (const :tag "Never" nil)
  1505. (const :tag "By default" t)
  1506. (const :tag "Only with C-u C-c C-t" prefix)))
  1507. (defcustom org-after-todo-state-change-hook nil
  1508. "Hook which is run after the state of a TODO item was changed.
  1509. The new state (a string with a TODO keyword, or nil) is available in the
  1510. Lisp variable `state'."
  1511. :group 'org-todo
  1512. :type 'hook)
  1513. (defcustom org-log-done nil
  1514. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1515. When the state of an entry is changed from nothing or a DONE state to
  1516. a not-done TODO state, remove a previous closing date.
  1517. This can also be a list of symbols indicating under which conditions
  1518. the time stamp recording the action should be annotated with a short note.
  1519. Valid members of this list are
  1520. done Offer to record a note when marking entries done
  1521. state Offer to record a note whenever changing the TODO state
  1522. of an item. This is only relevant if TODO keywords are
  1523. interpreted as sequence, see variable `org-todo-interpretation'.
  1524. When `state' is set, this includes tracking `done'.
  1525. clock-out Offer to record a note when clocking out of an item.
  1526. A separate window will then pop up and allow you to type a note.
  1527. After finishing with C-c C-c, the note will be added directly after the
  1528. timestamp, as a plain list item. See also the variable
  1529. `org-log-note-headings'.
  1530. Logging can also be configured on a per-file basis by adding one of
  1531. the following lines anywhere in the buffer:
  1532. #+STARTUP: logdone
  1533. #+STARTUP: nologging
  1534. #+STARTUP: lognotedone
  1535. #+STARTUP: lognotestate
  1536. #+STARTUP: lognoteclock-out
  1537. You can have local logging settings for a subtree by setting the LOGGING
  1538. property to one or more of these keywords."
  1539. :group 'org-todo
  1540. :group 'org-progress
  1541. :type '(choice
  1542. (const :tag "off" nil)
  1543. (const :tag "on" t)
  1544. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1545. (const :tag "when item is marked DONE" done)
  1546. (const :tag "when TODO state changes" state)
  1547. (const :tag "when clocking out" clock-out))))
  1548. (defcustom org-log-done-with-time t
  1549. "Non-nil means, the CLOSED time stamp will contain date and time.
  1550. When nil, only the date will be recorded."
  1551. :group 'org-progress
  1552. :type 'boolean)
  1553. (defcustom org-log-note-headings
  1554. '((done . "CLOSING NOTE %t")
  1555. (state . "State %-12s %t")
  1556. (clock-out . ""))
  1557. "Headings for notes added when clocking out or closing TODO items.
  1558. The value is an alist, with the car being a symbol indicating the note
  1559. context, and the cdr is the heading to be used. The heading may also be the
  1560. empty string.
  1561. %t in the heading will be replaced by a time stamp.
  1562. %s will be replaced by the new TODO state, in double quotes.
  1563. %u will be replaced by the user name.
  1564. %U will be replaced by the full user name."
  1565. :group 'org-todo
  1566. :group 'org-progress
  1567. :type '(list :greedy t
  1568. (cons (const :tag "Heading when closing an item" done) string)
  1569. (cons (const :tag
  1570. "Heading when changing todo state (todo sequence only)"
  1571. state) string)
  1572. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1573. (defcustom org-log-states-order-reversed t
  1574. "Non-nil means, the latest state change note will be directly after heading.
  1575. When nil, the notes will be orderer according to time."
  1576. :group 'org-todo
  1577. :group 'org-progress
  1578. :type 'boolean)
  1579. (defcustom org-log-repeat t
  1580. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1581. When nil, no note will be taken.
  1582. This option can also be set with on a per-file-basis with
  1583. #+STARTUP: logrepeat
  1584. #+STARTUP: nologrepeat
  1585. You can have local logging settings for a subtree by setting the LOGGING
  1586. property to one or more of these keywords."
  1587. :group 'org-todo
  1588. :group 'org-progress
  1589. :type 'boolean)
  1590. (defcustom org-clock-into-drawer 2
  1591. "Should clocking info be wrapped into a drawer?
  1592. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1593. If necessary, the drawer will be created.
  1594. When nil, the drawer will not be created, but used when present.
  1595. When an integer and the number of clocking entries in an item
  1596. reaches or exceeds this number, a drawer will be created."
  1597. :group 'org-todo
  1598. :group 'org-progress
  1599. :type '(choice
  1600. (const :tag "Always" t)
  1601. (const :tag "Only when drawer exists" nil)
  1602. (integer :tag "When at least N clock entries")))
  1603. (defcustom org-clock-out-when-done t
  1604. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1605. Nil means, clock will keep running until stopped explicitly with
  1606. `C-c C-x C-o', or until the clock is started in a different item."
  1607. :group 'org-progress
  1608. :type 'boolean)
  1609. (defcustom org-clock-in-switch-to-state nil
  1610. "Set task to a special todo state while clocking it.
  1611. The value should be the state to which the entry should be switched."
  1612. :group 'org-progress
  1613. :group 'org-todo
  1614. :type '(choice
  1615. (const :tag "Don't force a state" nil)
  1616. (string :tag "State")))
  1617. (defgroup org-priorities nil
  1618. "Priorities in Org-mode."
  1619. :tag "Org Priorities"
  1620. :group 'org-todo)
  1621. (defcustom org-highest-priority ?A
  1622. "The highest priority of TODO items. A character like ?A, ?B etc.
  1623. Must have a smaller ASCII number than `org-lowest-priority'."
  1624. :group 'org-priorities
  1625. :type 'character)
  1626. (defcustom org-lowest-priority ?C
  1627. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1628. Must have a larger ASCII number than `org-highest-priority'."
  1629. :group 'org-priorities
  1630. :type 'character)
  1631. (defcustom org-default-priority ?B
  1632. "The default priority of TODO items.
  1633. This is the priority an item get if no explicit priority is given."
  1634. :group 'org-priorities
  1635. :type 'character)
  1636. (defcustom org-priority-start-cycle-with-default t
  1637. "Non-nil means, start with default priority when starting to cycle.
  1638. When this is nil, the first step in the cycle will be (depending on the
  1639. command used) one higher or lower that the default priority."
  1640. :group 'org-priorities
  1641. :type 'boolean)
  1642. (defgroup org-time nil
  1643. "Options concerning time stamps and deadlines in Org-mode."
  1644. :tag "Org Time"
  1645. :group 'org)
  1646. (defcustom org-insert-labeled-timestamps-at-point nil
  1647. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1648. When nil, these labeled time stamps are forces into the second line of an
  1649. entry, just after the headline. When scheduling from the global TODO list,
  1650. the time stamp will always be forced into the second line."
  1651. :group 'org-time
  1652. :type 'boolean)
  1653. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1654. "Formats for `format-time-string' which are used for time stamps.
  1655. It is not recommended to change this constant.")
  1656. (defcustom org-time-stamp-rounding-minutes 0
  1657. "Number of minutes to round time stamps to upon insertion.
  1658. When zero, insert the time unmodified. Useful rounding numbers
  1659. should be factors of 60, so for example 5, 10, 15.
  1660. When this is not zero, you can still force an exact time-stamp by using
  1661. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1662. :group 'org-time
  1663. :type 'integer)
  1664. (defcustom org-display-custom-times nil
  1665. "Non-nil means, overlay custom formats over all time stamps.
  1666. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1667. To turn this on on a per-file basis, insert anywhere in the file:
  1668. #+STARTUP: customtime"
  1669. :group 'org-time
  1670. :set 'set-default
  1671. :type 'sexp)
  1672. (make-variable-buffer-local 'org-display-custom-times)
  1673. (defcustom org-time-stamp-custom-formats
  1674. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1675. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1676. These are overlayed over the default ISO format if the variable
  1677. `org-display-custom-times' is set. Time like %H:%M should be at the
  1678. end of the second format."
  1679. :group 'org-time
  1680. :type 'sexp)
  1681. (defun org-time-stamp-format (&optional long inactive)
  1682. "Get the right format for a time string."
  1683. (let ((f (if long (cdr org-time-stamp-formats)
  1684. (car org-time-stamp-formats))))
  1685. (if inactive
  1686. (concat "[" (substring f 1 -1) "]")
  1687. f)))
  1688. (defcustom org-read-date-prefer-future t
  1689. "Non-nil means, assume future for incomplete date input from user.
  1690. This affects the following situations:
  1691. 1. The user gives a day, but no month.
  1692. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1693. read this as the third of *next* month. However, if you enter \"17\",
  1694. it will be considered as *this* month.
  1695. 2. The user gives a month but not a year.
  1696. For example, if it is april and you enter \"feb 2\", this will be read
  1697. as feb 2, *next* year. \"May 5\", however, will be this year.
  1698. When this option is nil, the current month and year will always be used
  1699. as defaults."
  1700. :group 'org-time
  1701. :type 'boolean)
  1702. (defcustom org-read-date-display-live t
  1703. "Non-nil means, display current interpretation of date prompt live.
  1704. This display will be in an overlay, in the minibuffer."
  1705. :group 'org-time
  1706. :type 'boolean)
  1707. (defcustom org-read-date-popup-calendar t
  1708. "Non-nil means, pop up a calendar when prompting for a date.
  1709. In the calendar, the date can be selected with mouse-1. However, the
  1710. minibuffer will also be active, and you can simply enter the date as well.
  1711. When nil, only the minibuffer will be available."
  1712. :group 'org-time
  1713. :type 'boolean)
  1714. (if (fboundp 'defvaralias)
  1715. (defvaralias 'org-popup-calendar-for-date-prompt
  1716. 'org-read-date-popup-calendar))
  1717. (defcustom org-extend-today-until 0
  1718. "The hour when your day really ends.
  1719. This has influence for the following applications:
  1720. - When switching the agenda to \"today\". It it is still earlier than
  1721. the time given here, the day recognized as TODAY is actually yesterday.
  1722. - When a date is read from the user and it is still before the time given
  1723. here, the current date and time will be assumed to be yesterday, 23:59.
  1724. FIXME:
  1725. IMPORTANT: This is still a very experimental feature, it may disappear
  1726. again or it may be extended to mean more things."
  1727. :group 'org-time
  1728. :type 'number)
  1729. (defcustom org-edit-timestamp-down-means-later nil
  1730. "Non-nil means, S-down will increase the time in a time stamp.
  1731. When nil, S-up will increase."
  1732. :group 'org-time
  1733. :type 'boolean)
  1734. (defcustom org-calendar-follow-timestamp-change t
  1735. "Non-nil means, make the calendar window follow timestamp changes.
  1736. When a timestamp is modified and the calendar window is visible, it will be
  1737. moved to the new date."
  1738. :group 'org-time
  1739. :type 'boolean)
  1740. (defcustom org-clock-heading-function nil
  1741. "When non-nil, should be a function to create `org-clock-heading'.
  1742. This is the string shown in the mode line when a clock is running.
  1743. The function is called with point at the beginning of the headline."
  1744. :group 'org-time ; FIXME: Should we have a separate group????
  1745. :type 'function)
  1746. (defgroup org-tags nil
  1747. "Options concerning tags in Org-mode."
  1748. :tag "Org Tags"
  1749. :group 'org)
  1750. (defcustom org-tag-alist nil
  1751. "List of tags allowed in Org-mode files.
  1752. When this list is nil, Org-mode will base TAG input on what is already in the
  1753. buffer.
  1754. The value of this variable is an alist, the car of each entry must be a
  1755. keyword as a string, the cdr may be a character that is used to select
  1756. that tag through the fast-tag-selection interface.
  1757. See the manual for details."
  1758. :group 'org-tags
  1759. :type '(repeat
  1760. (choice
  1761. (cons (string :tag "Tag name")
  1762. (character :tag "Access char"))
  1763. (const :tag "Start radio group" (:startgroup))
  1764. (const :tag "End radio group" (:endgroup)))))
  1765. (defcustom org-use-fast-tag-selection 'auto
  1766. "Non-nil means, use fast tag selection scheme.
  1767. This is a special interface to select and deselect tags with single keys.
  1768. When nil, fast selection is never used.
  1769. When the symbol `auto', fast selection is used if and only if selection
  1770. characters for tags have been configured, either through the variable
  1771. `org-tag-alist' or through a #+TAGS line in the buffer.
  1772. When t, fast selection is always used and selection keys are assigned
  1773. automatically if necessary."
  1774. :group 'org-tags
  1775. :type '(choice
  1776. (const :tag "Always" t)
  1777. (const :tag "Never" nil)
  1778. (const :tag "When selection characters are configured" 'auto)))
  1779. (defcustom org-fast-tag-selection-single-key nil
  1780. "Non-nil means, fast tag selection exits after first change.
  1781. When nil, you have to press RET to exit it.
  1782. During fast tag selection, you can toggle this flag with `C-c'.
  1783. This variable can also have the value `expert'. In this case, the window
  1784. displaying the tags menu is not even shown, until you press C-c again."
  1785. :group 'org-tags
  1786. :type '(choice
  1787. (const :tag "No" nil)
  1788. (const :tag "Yes" t)
  1789. (const :tag "Expert" expert)))
  1790. (defvar org-fast-tag-selection-include-todo nil
  1791. "Non-nil means, fast tags selection interface will also offer TODO states.
  1792. This is an undocumented feature, you should not rely on it.")
  1793. (defcustom org-tags-column -80
  1794. "The column to which tags should be indented in a headline.
  1795. If this number is positive, it specifies the column. If it is negative,
  1796. it means that the tags should be flushright to that column. For example,
  1797. -80 works well for a normal 80 character screen."
  1798. :group 'org-tags
  1799. :type 'integer)
  1800. (defcustom org-auto-align-tags t
  1801. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1802. These operations change the length of a headline and therefore shift
  1803. the tags around. With this options turned on, after each such operation
  1804. the tags are again aligned to `org-tags-column'."
  1805. :group 'org-tags
  1806. :type 'boolean)
  1807. (defcustom org-use-tag-inheritance t
  1808. "Non-nil means, tags in levels apply also for sublevels.
  1809. When nil, only the tags directly given in a specific line apply there.
  1810. If you turn off this option, you very likely want to turn on the
  1811. companion option `org-tags-match-list-sublevels'."
  1812. :group 'org-tags
  1813. :type 'boolean)
  1814. (defcustom org-tags-match-list-sublevels nil
  1815. "Non-nil means list also sublevels of headlines matching tag search.
  1816. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1817. the sublevels of a headline matching a tag search often also match
  1818. the same search. Listing all of them can create very long lists.
  1819. Setting this variable to nil causes subtrees of a match to be skipped.
  1820. This option is off by default, because inheritance in on. If you turn
  1821. inheritance off, you very likely want to turn this option on.
  1822. As a special case, if the tag search is restricted to TODO items, the
  1823. value of this variable is ignored and sublevels are always checked, to
  1824. make sure all corresponding TODO items find their way into the list."
  1825. :group 'org-tags
  1826. :type 'boolean)
  1827. (defvar org-tags-history nil
  1828. "History of minibuffer reads for tags.")
  1829. (defvar org-last-tags-completion-table nil
  1830. "The last used completion table for tags.")
  1831. (defvar org-after-tags-change-hook nil
  1832. "Hook that is run after the tags in a line have changed.")
  1833. (defgroup org-properties nil
  1834. "Options concerning properties in Org-mode."
  1835. :tag "Org Properties"
  1836. :group 'org)
  1837. (defcustom org-property-format "%-10s %s"
  1838. "How property key/value pairs should be formatted by `indent-line'.
  1839. When `indent-line' hits a property definition, it will format the line
  1840. according to this format, mainly to make sure that the values are
  1841. lined-up with respect to each other."
  1842. :group 'org-properties
  1843. :type 'string)
  1844. (defcustom org-use-property-inheritance nil
  1845. "Non-nil means, properties apply also for sublevels.
  1846. This setting is only relevant during property searches, not when querying
  1847. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1848. you need to call `org-entry-get' with the inheritance flag.
  1849. Turning this on can cause significant overhead when doing a search, so
  1850. this is turned off by default.
  1851. When nil, only the properties directly given in the current entry count.
  1852. The value may also be a list of properties that shouldhave inheritance.
  1853. However, note that some special properties use inheritance under special
  1854. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1855. and the properties ending in \"_ALL\" when they are used as descriptor
  1856. for valid values of a property."
  1857. :group 'org-properties
  1858. :type '(choice
  1859. (const :tag "Not" nil)
  1860. (const :tag "Always" nil)
  1861. (repeat :tag "Specific properties" (string :tag "Property"))))
  1862. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1863. "The default column format, if no other format has been defined.
  1864. This variable can be set on the per-file basis by inserting a line
  1865. #+COLUMNS: %25ITEM ....."
  1866. :group 'org-properties
  1867. :type 'string)
  1868. (defcustom org-global-properties nil
  1869. "List of property/value pairs that can be inherited by any entry.
  1870. You can set buffer-local values for this by adding lines like
  1871. #+PROPERTY: NAME VALUE"
  1872. :group 'org-properties
  1873. :type '(repeat
  1874. (cons (string :tag "Property")
  1875. (string :tag "Value"))))
  1876. (defvar org-local-properties nil
  1877. "List of property/value pairs that can be inherited by any entry.
  1878. Valid for the current buffer.
  1879. This variable is populated from #+PROPERTY lines.")
  1880. (defgroup org-agenda nil
  1881. "Options concerning agenda views in Org-mode."
  1882. :tag "Org Agenda"
  1883. :group 'org)
  1884. (defvar org-category nil
  1885. "Variable used by org files to set a category for agenda display.
  1886. Such files should use a file variable to set it, for example
  1887. # -*- mode: org; org-category: \"ELisp\"
  1888. or contain a special line
  1889. #+CATEGORY: ELisp
  1890. If the file does not specify a category, then file's base name
  1891. is used instead.")
  1892. (make-variable-buffer-local 'org-category)
  1893. (defcustom org-agenda-files nil
  1894. "The files to be used for agenda display.
  1895. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1896. \\[org-remove-file]. You can also use customize to edit the list.
  1897. If an entry is a directory, all files in that directory that are matched by
  1898. `org-agenda-file-regexp' will be part of the file list.
  1899. If the value of the variable is not a list but a single file name, then
  1900. the list of agenda files is actually stored and maintained in that file, one
  1901. agenda file per line."
  1902. :group 'org-agenda
  1903. :type '(choice
  1904. (repeat :tag "List of files and directories" file)
  1905. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1906. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1907. "Regular expression to match files for `org-agenda-files'.
  1908. If any element in the list in that variable contains a directory instead
  1909. of a normal file, all files in that directory that are matched by this
  1910. regular expression will be included."
  1911. :group 'org-agenda
  1912. :type 'regexp)
  1913. (defcustom org-agenda-skip-unavailable-files nil
  1914. "t means to just skip non-reachable files in `org-agenda-files'.
  1915. Nil means to remove them, after a query, from the list."
  1916. :group 'org-agenda
  1917. :type 'boolean)
  1918. (defcustom org-agenda-multi-occur-extra-files nil
  1919. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1920. The files in `org-agenda-files' are always searched."
  1921. :group 'org-agenda
  1922. :type '(repeat file))
  1923. (defcustom org-agenda-confirm-kill 1
  1924. "When set, remote killing from the agenda buffer needs confirmation.
  1925. When t, a confirmation is always needed. When a number N, confirmation is
  1926. only needed when the text to be killed contains more than N non-white lines."
  1927. :group 'org-agenda
  1928. :type '(choice
  1929. (const :tag "Never" nil)
  1930. (const :tag "Always" t)
  1931. (number :tag "When more than N lines")))
  1932. (defcustom org-calendar-to-agenda-key [?c]
  1933. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1934. The command `org-calendar-goto-agenda' will be bound to this key. The
  1935. default is the character `c' because then `c' can be used to switch back and
  1936. forth between agenda and calendar."
  1937. :group 'org-agenda
  1938. :type 'sexp)
  1939. (defcustom org-agenda-compact-blocks nil
  1940. "Non-nil means, make the block agenda more compact.
  1941. This is done by leaving out unnecessary lines."
  1942. :group 'org-agenda
  1943. :type nil)
  1944. (defgroup org-agenda-export nil
  1945. "Options concerning exporting agenda views in Org-mode."
  1946. :tag "Org Agenda Export"
  1947. :group 'org-agenda)
  1948. (defcustom org-agenda-with-colors t
  1949. "Non-nil means, use colors in agenda views."
  1950. :group 'org-agenda-export
  1951. :type 'boolean)
  1952. (defcustom org-agenda-exporter-settings nil
  1953. "Alist of variable/value pairs that should be active during agenda export.
  1954. This is a good place to set uptions for ps-print and for htmlize."
  1955. :group 'org-agenda-export
  1956. :type '(repeat
  1957. (list
  1958. (variable)
  1959. (sexp :tag "Value"))))
  1960. (defcustom org-agenda-export-html-style ""
  1961. "The style specification for exported HTML Agenda files.
  1962. If this variable contains a string, it will replace the default <style>
  1963. section as produced by `htmlize'.
  1964. Since there are different ways of setting style information, this variable
  1965. needs to contain the full HTML structure to provide a style, including the
  1966. surrounding HTML tags. The style specifications should include definitions
  1967. the fonts used by the agenda, here is an example:
  1968. <style type=\"text/css\">
  1969. p { font-weight: normal; color: gray; }
  1970. .org-agenda-structure {
  1971. font-size: 110%;
  1972. color: #003399;
  1973. font-weight: 600;
  1974. }
  1975. .org-todo {
  1976. color: #cc6666;Week-agenda:
  1977. font-weight: bold;
  1978. }
  1979. .org-done {
  1980. color: #339933;
  1981. }
  1982. .title { text-align: center; }
  1983. .todo, .deadline { color: red; }
  1984. .done { color: green; }
  1985. </style>
  1986. or, if you want to keep the style in a file,
  1987. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1988. As the value of this option simply gets inserted into the HTML <head> header,
  1989. you can \"misuse\" it to also add other text to the header. However,
  1990. <style>...</style> is required, if not present the variable will be ignored."
  1991. :group 'org-agenda-export
  1992. :group 'org-export-html
  1993. :type 'string)
  1994. (defgroup org-agenda-custom-commands nil
  1995. "Options concerning agenda views in Org-mode."
  1996. :tag "Org Agenda Custom Commands"
  1997. :group 'org-agenda)
  1998. (defcustom org-agenda-custom-commands nil
  1999. "Custom commands for the agenda.
  2000. These commands will be offered on the splash screen displayed by the
  2001. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2002. (key desc type match options files)
  2003. key The key (one or more characters as a string) to be associated
  2004. with the command.
  2005. desc A description of the commend, when omitted or nil, a default
  2006. description is built using MATCH.
  2007. type The command type, any of the following symbols:
  2008. todo Entries with a specific TODO keyword, in all agenda files.
  2009. tags Tags match in all agenda files.
  2010. tags-todo Tags match in all agenda files, TODO entries only.
  2011. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2012. tags-tree Sparse tree with all tags matches in *current* file.
  2013. occur-tree Occur sparse tree for *current* file.
  2014. ... A user-defined function.
  2015. match What to search for:
  2016. - a single keyword for TODO keyword searches
  2017. - a tags match expression for tags searches
  2018. - a regular expression for occur searches
  2019. options A list of option settings, similar to that in a let form, so like
  2020. this: ((opt1 val1) (opt2 val2) ...)
  2021. files A list of files file to write the produced agenda buffer to
  2022. with the command `org-store-agenda-views'.
  2023. If a file name ends in \".html\", an HTML version of the buffer
  2024. is written out. If it ends in \".ps\", a postscript version is
  2025. produced. Otherwide, only the plain text is written to the file.
  2026. You can also define a set of commands, to create a composite agenda buffer.
  2027. In this case, an entry looks like this:
  2028. (key desc (cmd1 cmd2 ...) general-options file)
  2029. where
  2030. desc A description string to be displayed in the dispatcher menu.
  2031. cmd An agenda command, similar to the above. However, tree commands
  2032. are no allowed, but instead you can get agenda and global todo list.
  2033. So valid commands for a set are:
  2034. (agenda)
  2035. (alltodo)
  2036. (stuck)
  2037. (todo \"match\" options files)
  2038. (tags \"match\" options files)
  2039. (tags-todo \"match\" options files)
  2040. Each command can carry a list of options, and another set of options can be
  2041. given for the whole set of commands. Individual command options take
  2042. precedence over the general options.
  2043. When using several characters as key to a command, the first characters
  2044. are prefix commands. For the dispatcher to display useful information, you
  2045. should provide a description for the prefix, like
  2046. (setq org-agenda-custom-commands
  2047. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2048. (\"hl\" tags \"+HOME+Lisa\")
  2049. (\"hp\" tags \"+HOME+Peter\")
  2050. (\"hk\" tags \"+HOME+Kim\")))"
  2051. :group 'org-agenda-custom-commands
  2052. :type '(repeat
  2053. (choice :value ("a" "" tags "" nil)
  2054. (list :tag "Single command"
  2055. (string :tag "Access Key(s) ")
  2056. (option (string :tag "Description"))
  2057. (choice
  2058. (const :tag "Agenda" agenda)
  2059. (const :tag "TODO list" alltodo)
  2060. (const :tag "Stuck projects" stuck)
  2061. (const :tag "Tags search (all agenda files)" tags)
  2062. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2063. (const :tag "TODO keyword search (all agenda files)" todo)
  2064. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2065. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2066. (const :tag "Occur tree (current buffer)" occur-tree)
  2067. (sexp :tag "Other, user-defined function"))
  2068. (string :tag "Match")
  2069. (repeat :tag "Local options"
  2070. (list (variable :tag "Option") (sexp :tag "Value")))
  2071. (option (repeat :tag "Export" (file :tag "Export to"))))
  2072. (list :tag "Command series, all agenda files"
  2073. (string :tag "Access Key(s)")
  2074. (string :tag "Description ")
  2075. (repeat
  2076. (choice
  2077. (const :tag "Agenda" (agenda))
  2078. (const :tag "TODO list" (alltodo))
  2079. (const :tag "Stuck projects" (stuck))
  2080. (list :tag "Tags search"
  2081. (const :format "" tags)
  2082. (string :tag "Match")
  2083. (repeat :tag "Local options"
  2084. (list (variable :tag "Option")
  2085. (sexp :tag "Value"))))
  2086. (list :tag "Tags search, TODO entries only"
  2087. (const :format "" tags-todo)
  2088. (string :tag "Match")
  2089. (repeat :tag "Local options"
  2090. (list (variable :tag "Option")
  2091. (sexp :tag "Value"))))
  2092. (list :tag "TODO keyword search"
  2093. (const :format "" todo)
  2094. (string :tag "Match")
  2095. (repeat :tag "Local options"
  2096. (list (variable :tag "Option")
  2097. (sexp :tag "Value"))))
  2098. (list :tag "Other, user-defined function"
  2099. (symbol :tag "function")
  2100. (string :tag "Match")
  2101. (repeat :tag "Local options"
  2102. (list (variable :tag "Option")
  2103. (sexp :tag "Value"))))))
  2104. (repeat :tag "General options"
  2105. (list (variable :tag "Option")
  2106. (sexp :tag "Value")))
  2107. (option (repeat :tag "Export" (file :tag "Export to"))))
  2108. (cons :tag "Prefix key documentation"
  2109. (string :tag "Access Key(s)")
  2110. (string :tag "Description ")))))
  2111. (defcustom org-stuck-projects
  2112. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2113. "How to identify stuck projects.
  2114. This is a list of four items:
  2115. 1. A tags/todo matcher string that is used to identify a project.
  2116. The entire tree below a headline matched by this is considered one project.
  2117. 2. A list of TODO keywords identifying non-stuck projects.
  2118. If the project subtree contains any headline with one of these todo
  2119. keywords, the project is considered to be not stuck. If you specify
  2120. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2121. 3. A list of tags identifying non-stuck projects.
  2122. If the project subtree contains any headline with one of these tags,
  2123. the project is considered to be not stuck. If you specify \"*\" as
  2124. a tag, any tag will mark the project unstuck.
  2125. 4. An arbitrary regular expression matching non-stuck projects.
  2126. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2127. or `C-c a #' to produce the list."
  2128. :group 'org-agenda-custom-commands
  2129. :type '(list
  2130. (string :tag "Tags/TODO match to identify a project")
  2131. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2132. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2133. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2134. (defgroup org-agenda-skip nil
  2135. "Options concerning skipping parts of agenda files."
  2136. :tag "Org Agenda Skip"
  2137. :group 'org-agenda)
  2138. (defcustom org-agenda-todo-list-sublevels t
  2139. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2140. When nil, the sublevels of a TODO entry are not checked, resulting in
  2141. potentially much shorter TODO lists."
  2142. :group 'org-agenda-skip
  2143. :group 'org-todo
  2144. :type 'boolean)
  2145. (defcustom org-agenda-todo-ignore-with-date nil
  2146. "Non-nil means, don't show entries with a date in the global todo list.
  2147. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2148. but don't want them to show up in the TODO list.
  2149. When this is set, it also covers deadlines and scheduled items, the settings
  2150. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2151. will be ignored."
  2152. :group 'org-agenda-skip
  2153. :group 'org-todo
  2154. :type 'boolean)
  2155. (defcustom org-agenda-todo-ignore-scheduled nil
  2156. "Non-nil means, don't show scheduled entries in the global todo list.
  2157. The idea behind this is that by scheduling it, you have already taken care
  2158. of this item.
  2159. See also `org-agenda-todo-ignore-with-date'."
  2160. :group 'org-agenda-skip
  2161. :group 'org-todo
  2162. :type 'boolean)
  2163. (defcustom org-agenda-todo-ignore-deadlines nil
  2164. "Non-nil means, don't show near deadline entries in the global todo list.
  2165. Near means closer than `org-deadline-warning-days' days.
  2166. The idea behind this is that such items will appear in the agenda anyway.
  2167. See also `org-agenda-todo-ignore-with-date'."
  2168. :group 'org-agenda-skip
  2169. :group 'org-todo
  2170. :type 'boolean)
  2171. (defcustom org-agenda-skip-scheduled-if-done nil
  2172. "Non-nil means don't show scheduled items in agenda when they are done.
  2173. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2174. it applies only to the actual date of the scheduling. Warnings about
  2175. an item with a past scheduling dates are always turned off when the item
  2176. is DONE."
  2177. :group 'org-agenda-skip
  2178. :type 'boolean)
  2179. (defcustom org-agenda-skip-deadline-if-done nil
  2180. "Non-nil means don't show deadines when the corresponding item is done.
  2181. When nil, the deadline is still shown and should give you a happy feeling.
  2182. This is relevant for the daily/weekly agenda. And it applied only to the
  2183. actualy date of the deadline. Warnings about approching and past-due
  2184. deadlines are always turned off when the item is DONE."
  2185. :group 'org-agenda-skip
  2186. :type 'boolean)
  2187. (defcustom org-agenda-skip-timestamp-if-done nil
  2188. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2189. :group 'org-agenda-skip
  2190. :type 'boolean)
  2191. (defcustom org-timeline-show-empty-dates 3
  2192. "Non-nil means, `org-timeline' also shows dates without an entry.
  2193. When nil, only the days which actually have entries are shown.
  2194. When t, all days between the first and the last date are shown.
  2195. When an integer, show also empty dates, but if there is a gap of more than
  2196. N days, just insert a special line indicating the size of the gap."
  2197. :group 'org-agenda-skip
  2198. :type '(choice
  2199. (const :tag "None" nil)
  2200. (const :tag "All" t)
  2201. (number :tag "at most")))
  2202. (defgroup org-agenda-startup nil
  2203. "Options concerning initial settings in the Agenda in Org Mode."
  2204. :tag "Org Agenda Startup"
  2205. :group 'org-agenda)
  2206. (defcustom org-finalize-agenda-hook nil
  2207. "Hook run just before displaying an agenda buffer."
  2208. :group 'org-agenda-startup
  2209. :type 'hook)
  2210. (defcustom org-agenda-mouse-1-follows-link nil
  2211. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2212. A longer mouse click will still set point. Does not wortk on XEmacs.
  2213. Needs to be set before org.el is loaded."
  2214. :group 'org-agenda-startup
  2215. :type 'boolean)
  2216. (defcustom org-agenda-start-with-follow-mode nil
  2217. "The initial value of follow-mode in a newly created agenda window."
  2218. :group 'org-agenda-startup
  2219. :type 'boolean)
  2220. (defgroup org-agenda-windows nil
  2221. "Options concerning the windows used by the Agenda in Org Mode."
  2222. :tag "Org Agenda Windows"
  2223. :group 'org-agenda)
  2224. (defcustom org-agenda-window-setup 'reorganize-frame
  2225. "How the agenda buffer should be displayed.
  2226. Possible values for this option are:
  2227. current-window Show agenda in the current window, keeping all other windows.
  2228. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2229. other-window Use `switch-to-buffer-other-window' to display agenda.
  2230. reorganize-frame Show only two windows on the current frame, the current
  2231. window and the agenda.
  2232. See also the variable `org-agenda-restore-windows-after-quit'."
  2233. :group 'org-agenda-windows
  2234. :type '(choice
  2235. (const current-window)
  2236. (const other-frame)
  2237. (const other-window)
  2238. (const reorganize-frame)))
  2239. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2240. "The min and max height of the agenda window as a fraction of frame height.
  2241. The value of the variable is a cons cell with two numbers between 0 and 1.
  2242. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2243. :group 'org-agenda-windows
  2244. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2245. (defcustom org-agenda-restore-windows-after-quit nil
  2246. "Non-nil means, restore window configuration open exiting agenda.
  2247. Before the window configuration is changed for displaying the agenda,
  2248. the current status is recorded. When the agenda is exited with
  2249. `q' or `x' and this option is set, the old state is restored. If
  2250. `org-agenda-window-setup' is `other-frame', the value of this
  2251. option will be ignored.."
  2252. :group 'org-agenda-windows
  2253. :type 'boolean)
  2254. (defcustom org-indirect-buffer-display 'other-window
  2255. "How should indirect tree buffers be displayed?
  2256. This applies to indirect buffers created with the commands
  2257. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2258. Valid values are:
  2259. current-window Display in the current window
  2260. other-window Just display in another window.
  2261. dedicated-frame Create one new frame, and re-use it each time.
  2262. new-frame Make a new frame each time. Note that in this case
  2263. previously-made indirect buffers are kept, and you need to
  2264. kill these buffers yourself."
  2265. :group 'org-structure
  2266. :group 'org-agenda-windows
  2267. :type '(choice
  2268. (const :tag "In current window" current-window)
  2269. (const :tag "In current frame, other window" other-window)
  2270. (const :tag "Each time a new frame" new-frame)
  2271. (const :tag "One dedicated frame" dedicated-frame)))
  2272. (defgroup org-agenda-daily/weekly nil
  2273. "Options concerning the daily/weekly agenda."
  2274. :tag "Org Agenda Daily/Weekly"
  2275. :group 'org-agenda)
  2276. (defcustom org-agenda-ndays 7
  2277. "Number of days to include in overview display.
  2278. Should be 1 or 7."
  2279. :group 'org-agenda-daily/weekly
  2280. :type 'number)
  2281. (defcustom org-agenda-start-on-weekday 1
  2282. "Non-nil means, start the overview always on the specified weekday.
  2283. 0 denotes Sunday, 1 denotes Monday etc.
  2284. When nil, always start on the current day."
  2285. :group 'org-agenda-daily/weekly
  2286. :type '(choice (const :tag "Today" nil)
  2287. (number :tag "Weekday No.")))
  2288. (defcustom org-agenda-show-all-dates t
  2289. "Non-nil means, `org-agenda' shows every day in the selected range.
  2290. When nil, only the days which actually have entries are shown."
  2291. :group 'org-agenda-daily/weekly
  2292. :type 'boolean)
  2293. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2294. "Format string for displaying dates in the agenda.
  2295. Used by the daily/weekly agenda and by the timeline. This should be
  2296. a format string understood by `format-time-string', or a function returning
  2297. the formatted date as a string. The function must take a single argument,
  2298. a calendar-style date list like (month day year)."
  2299. :group 'org-agenda-daily/weekly
  2300. :type '(choice
  2301. (string :tag "Format string")
  2302. (function :tag "Function")))
  2303. (defun org-agenda-format-date-aligned (date)
  2304. "Format a date string for display in the daily/weekly agenda, or timeline.
  2305. This function makes sure that dates are aligned for easy reading."
  2306. (format "%-9s %2d %s %4d"
  2307. (calendar-day-name date)
  2308. (extract-calendar-day date)
  2309. (calendar-month-name (extract-calendar-month date))
  2310. (extract-calendar-year date)))
  2311. (defcustom org-agenda-include-diary nil
  2312. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2313. :group 'org-agenda-daily/weekly
  2314. :type 'boolean)
  2315. (defcustom org-agenda-include-all-todo nil
  2316. "Set means weekly/daily agenda will always contain all TODO entries.
  2317. The TODO entries will be listed at the top of the agenda, before
  2318. the entries for specific days."
  2319. :group 'org-agenda-daily/weekly
  2320. :type 'boolean)
  2321. (defcustom org-agenda-repeating-timestamp-show-all t
  2322. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2323. When nil, only one occurence is shown, either today or the
  2324. nearest into the future."
  2325. :group 'org-agenda-daily/weekly
  2326. :type 'boolean)
  2327. (defcustom org-deadline-warning-days 14
  2328. "No. of days before expiration during which a deadline becomes active.
  2329. This variable governs the display in sparse trees and in the agenda.
  2330. When negative, it means use this number (the absolute value of it)
  2331. even if a deadline has a different individual lead time specified."
  2332. :group 'org-time
  2333. :group 'org-agenda-daily/weekly
  2334. :type 'number)
  2335. (defcustom org-scheduled-past-days 10000
  2336. "No. of days to continue listing scheduled items that are not marked DONE.
  2337. When an item is scheduled on a date, it shows up in the agenda on this
  2338. day and will be listed until it is marked done for the number of days
  2339. given here."
  2340. :group 'org-agenda-daily/weekly
  2341. :type 'number)
  2342. (defgroup org-agenda-time-grid nil
  2343. "Options concerning the time grid in the Org-mode Agenda."
  2344. :tag "Org Agenda Time Grid"
  2345. :group 'org-agenda)
  2346. (defcustom org-agenda-use-time-grid t
  2347. "Non-nil means, show a time grid in the agenda schedule.
  2348. A time grid is a set of lines for specific times (like every two hours between
  2349. 8:00 and 20:00). The items scheduled for a day at specific times are
  2350. sorted in between these lines.
  2351. For details about when the grid will be shown, and what it will look like, see
  2352. the variable `org-agenda-time-grid'."
  2353. :group 'org-agenda-time-grid
  2354. :type 'boolean)
  2355. (defcustom org-agenda-time-grid
  2356. '((daily today require-timed)
  2357. "----------------"
  2358. (800 1000 1200 1400 1600 1800 2000))
  2359. "The settings for time grid for agenda display.
  2360. This is a list of three items. The first item is again a list. It contains
  2361. symbols specifying conditions when the grid should be displayed:
  2362. daily if the agenda shows a single day
  2363. weekly if the agenda shows an entire week
  2364. today show grid on current date, independent of daily/weekly display
  2365. require-timed show grid only if at least one item has a time specification
  2366. The second item is a string which will be places behing the grid time.
  2367. The third item is a list of integers, indicating the times that should have
  2368. a grid line."
  2369. :group 'org-agenda-time-grid
  2370. :type
  2371. '(list
  2372. (set :greedy t :tag "Grid Display Options"
  2373. (const :tag "Show grid in single day agenda display" daily)
  2374. (const :tag "Show grid in weekly agenda display" weekly)
  2375. (const :tag "Always show grid for today" today)
  2376. (const :tag "Show grid only if any timed entries are present"
  2377. require-timed)
  2378. (const :tag "Skip grid times already present in an entry"
  2379. remove-match))
  2380. (string :tag "Grid String")
  2381. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2382. (defgroup org-agenda-sorting nil
  2383. "Options concerning sorting in the Org-mode Agenda."
  2384. :tag "Org Agenda Sorting"
  2385. :group 'org-agenda)
  2386. (defconst org-sorting-choice
  2387. '(choice
  2388. (const time-up) (const time-down)
  2389. (const category-keep) (const category-up) (const category-down)
  2390. (const tag-down) (const tag-up)
  2391. (const priority-up) (const priority-down))
  2392. "Sorting choices.")
  2393. (defcustom org-agenda-sorting-strategy
  2394. '((agenda time-up category-keep priority-down)
  2395. (todo category-keep priority-down)
  2396. (tags category-keep priority-down))
  2397. "Sorting structure for the agenda items of a single day.
  2398. This is a list of symbols which will be used in sequence to determine
  2399. if an entry should be listed before another entry. The following
  2400. symbols are recognized:
  2401. time-up Put entries with time-of-day indications first, early first
  2402. time-down Put entries with time-of-day indications first, late first
  2403. category-keep Keep the default order of categories, corresponding to the
  2404. sequence in `org-agenda-files'.
  2405. category-up Sort alphabetically by category, A-Z.
  2406. category-down Sort alphabetically by category, Z-A.
  2407. tag-up Sort alphabetically by last tag, A-Z.
  2408. tag-down Sort alphabetically by last tag, Z-A.
  2409. priority-up Sort numerically by priority, high priority last.
  2410. priority-down Sort numerically by priority, high priority first.
  2411. The different possibilities will be tried in sequence, and testing stops
  2412. if one comparison returns a \"not-equal\". For example, the default
  2413. '(time-up category-keep priority-down)
  2414. means: Pull out all entries having a specified time of day and sort them,
  2415. in order to make a time schedule for the current day the first thing in the
  2416. agenda listing for the day. Of the entries without a time indication, keep
  2417. the grouped in categories, don't sort the categories, but keep them in
  2418. the sequence given in `org-agenda-files'. Within each category sort by
  2419. priority.
  2420. Leaving out `category-keep' would mean that items will be sorted across
  2421. categories by priority.
  2422. Instead of a single list, this can also be a set of list for specific
  2423. contents, with a context symbol in the car of the list, any of
  2424. `agenda', `todo', `tags' for the corresponding agenda views."
  2425. :group 'org-agenda-sorting
  2426. :type `(choice
  2427. (repeat :tag "General" ,org-sorting-choice)
  2428. (list :tag "Individually"
  2429. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2430. (repeat ,org-sorting-choice))
  2431. (cons (const :tag "Strategy for TODO lists" todo)
  2432. (repeat ,org-sorting-choice))
  2433. (cons (const :tag "Strategy for Tags matches" tags)
  2434. (repeat ,org-sorting-choice)))))
  2435. (defcustom org-sort-agenda-notime-is-late t
  2436. "Non-nil means, items without time are considered late.
  2437. This is only relevant for sorting. When t, items which have no explicit
  2438. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2439. do have a time. When nil, the default time is before 0:00. You can use this
  2440. option to decide if the schedule for today should come before or after timeless
  2441. agenda entries."
  2442. :group 'org-agenda-sorting
  2443. :type 'boolean)
  2444. (defgroup org-agenda-line-format nil
  2445. "Options concerning the entry prefix in the Org-mode agenda display."
  2446. :tag "Org Agenda Line Format"
  2447. :group 'org-agenda)
  2448. (defcustom org-agenda-prefix-format
  2449. '((agenda . " %-12:c%?-12t% s")
  2450. (timeline . " % s")
  2451. (todo . " %-12:c")
  2452. (tags . " %-12:c"))
  2453. "Format specifications for the prefix of items in the agenda views.
  2454. An alist with four entries, for the different agenda types. The keys to the
  2455. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2456. are format strings.
  2457. This format works similar to a printf format, with the following meaning:
  2458. %c the category of the item, \"Diary\" for entries from the diary, or
  2459. as given by the CATEGORY keyword or derived from the file name.
  2460. %T the *last* tag of the item. Last because inherited tags come
  2461. first in the list.
  2462. %t the time-of-day specification if one applies to the entry, in the
  2463. format HH:MM
  2464. %s Scheduling/Deadline information, a short string
  2465. All specifiers work basically like the standard `%s' of printf, but may
  2466. contain two additional characters: A question mark just after the `%' and
  2467. a whitespace/punctuation character just before the final letter.
  2468. If the first character after `%' is a question mark, the entire field
  2469. will only be included if the corresponding value applies to the
  2470. current entry. This is useful for fields which should have fixed
  2471. width when present, but zero width when absent. For example,
  2472. \"%?-12t\" will result in a 12 character time field if a time of the
  2473. day is specified, but will completely disappear in entries which do
  2474. not contain a time.
  2475. If there is punctuation or whitespace character just before the final
  2476. format letter, this character will be appended to the field value if
  2477. the value is not empty. For example, the format \"%-12:c\" leads to
  2478. \"Diary: \" if the category is \"Diary\". If the category were be
  2479. empty, no additional colon would be interted.
  2480. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2481. - Indent the line with two space characters
  2482. - Give the category in a 12 chars wide field, padded with whitespace on
  2483. the right (because of `-'). Append a colon if there is a category
  2484. (because of `:').
  2485. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2486. time, don't put in an empty field, just skip it (because of '?').
  2487. - Finally, put the scheduling information and append a whitespace.
  2488. As another example, if you don't want the time-of-day of entries in
  2489. the prefix, you could use:
  2490. (setq org-agenda-prefix-format \" %-11:c% s\")
  2491. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2492. `org-agenda-remove-tags'."
  2493. :type '(choice
  2494. (string :tag "General format")
  2495. (list :greedy t :tag "View dependent"
  2496. (cons (const agenda) (string :tag "Format"))
  2497. (cons (const timeline) (string :tag "Format"))
  2498. (cons (const todo) (string :tag "Format"))
  2499. (cons (const tags) (string :tag "Format"))))
  2500. :group 'org-agenda-line-format)
  2501. (defvar org-prefix-format-compiled nil
  2502. "The compiled version of the most recently used prefix format.
  2503. See the variable `org-agenda-prefix-format'.")
  2504. (defcustom org-agenda-todo-keyword-format "%-1s"
  2505. "Format for the TODO keyword in agenda lines.
  2506. Set this to something like \"%-12s\" if you want all TODO keywords
  2507. to occupy a fixed space in the agenda display."
  2508. :group 'org-agenda-line-format
  2509. :type 'string)
  2510. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2511. "Text preceeding scheduled items in the agenda view.
  2512. This is a list with two strings. The first applies when the item is
  2513. scheduled on the current day. The second applies when it has been scheduled
  2514. previously, it may contain a %d to capture how many days ago the item was
  2515. scheduled."
  2516. :group 'org-agenda-line-format
  2517. :type '(list
  2518. (string :tag "Scheduled today ")
  2519. (string :tag "Scheduled previously")))
  2520. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2521. "Text preceeding deadline items in the agenda view.
  2522. This is a list with two strings. The first applies when the item has its
  2523. deadline on the current day. The second applies when it is in the past or
  2524. in the future, it may contain %d to capture how many days away the deadline
  2525. is (was)."
  2526. :group 'org-agenda-line-format
  2527. :type '(list
  2528. (string :tag "Deadline today ")
  2529. (string :tag "Deadline relative")))
  2530. (defcustom org-agenda-remove-times-when-in-prefix t
  2531. "Non-nil means, remove duplicate time specifications in agenda items.
  2532. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2533. time-of-day specification in a headline or diary entry is extracted and
  2534. placed into the prefix. If this option is non-nil, the original specification
  2535. \(a timestamp or -range, or just a plain time(range) specification like
  2536. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2537. cluttered.
  2538. The option can be t or nil. It may also be the symbol `beg', indicating
  2539. that the time should only be removed what it is located at the beginning of
  2540. the headline/diary entry."
  2541. :group 'org-agenda-line-format
  2542. :type '(choice
  2543. (const :tag "Always" t)
  2544. (const :tag "Never" nil)
  2545. (const :tag "When at beginning of entry" beg)))
  2546. (defcustom org-agenda-default-appointment-duration nil
  2547. "Default duration for appointments that only have a starting time.
  2548. When nil, no duration is specified in such cases.
  2549. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2550. :group 'org-agenda-line-format
  2551. :type '(choice
  2552. (integer :tag "Minutes")
  2553. (const :tag "No default duration")))
  2554. (defcustom org-agenda-remove-tags nil
  2555. "Non-nil means, remove the tags from the headline copy in the agenda.
  2556. When this is the symbol `prefix', only remove tags when
  2557. `org-agenda-prefix-format' contains a `%T' specifier."
  2558. :group 'org-agenda-line-format
  2559. :type '(choice
  2560. (const :tag "Always" t)
  2561. (const :tag "Never" nil)
  2562. (const :tag "When prefix format contains %T" prefix)))
  2563. (if (fboundp 'defvaralias)
  2564. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2565. 'org-agenda-remove-tags))
  2566. (defcustom org-agenda-tags-column -80
  2567. "Shift tags in agenda items to this column.
  2568. If this number is positive, it specifies the column. If it is negative,
  2569. it means that the tags should be flushright to that column. For example,
  2570. -80 works well for a normal 80 character screen."
  2571. :group 'org-agenda-line-format
  2572. :type 'integer)
  2573. (if (fboundp 'defvaralias)
  2574. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2575. (defcustom org-agenda-fontify-priorities t
  2576. "Non-nil means, highlight low and high priorities in agenda.
  2577. When t, the highest priority entries are bold, lowest priority italic.
  2578. This may also be an association list of priority faces. The face may be
  2579. a names face, or a list like `(:background \"Red\")'."
  2580. :group 'org-agenda-line-format
  2581. :type '(choice
  2582. (const :tag "Never" nil)
  2583. (const :tag "Defaults" t)
  2584. (repeat :tag "Specify"
  2585. (list (character :tag "Priority" :value ?A)
  2586. (sexp :tag "face")))))
  2587. (defgroup org-latex nil
  2588. "Options for embedding LaTeX code into Org-mode"
  2589. :tag "Org LaTeX"
  2590. :group 'org)
  2591. (defcustom org-format-latex-options
  2592. '(:foreground default :background default :scale 1.0
  2593. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2594. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2595. "Options for creating images from LaTeX fragments.
  2596. This is a property list with the following properties:
  2597. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2598. `default' means use the forground of the default face.
  2599. :background the background color, or \"Transparent\".
  2600. `default' means use the background of the default face.
  2601. :scale a scaling factor for the size of the images
  2602. :html-foreground, :html-background, :html-scale
  2603. The same numbers for HTML export.
  2604. :matchers a list indicating which matchers should be used to
  2605. find LaTeX fragments. Valid members of this list are:
  2606. \"begin\" find environments
  2607. \"$\" find math expressions surrounded by $...$
  2608. \"$$\" find math expressions surrounded by $$....$$
  2609. \"\\(\" find math expressions surrounded by \\(...\\)
  2610. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2611. :group 'org-latex
  2612. :type 'plist)
  2613. (defcustom org-format-latex-header "\\documentclass{article}
  2614. \\usepackage{fullpage} % do not remove
  2615. \\usepackage{amssymb}
  2616. \\usepackage[usenames]{color}
  2617. \\usepackage{amsmath}
  2618. \\usepackage{latexsym}
  2619. \\usepackage[mathscr]{eucal}
  2620. \\pagestyle{empty} % do not remove"
  2621. "The document header used for processing LaTeX fragments."
  2622. :group 'org-latex
  2623. :type 'string)
  2624. (defgroup org-export nil
  2625. "Options for exporting org-listings."
  2626. :tag "Org Export"
  2627. :group 'org)
  2628. (defgroup org-export-general nil
  2629. "General options for exporting Org-mode files."
  2630. :tag "Org Export General"
  2631. :group 'org-export)
  2632. ;; FIXME
  2633. (defvar org-export-publishing-directory nil)
  2634. (defcustom org-export-with-special-strings t
  2635. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2636. When this option is turned on, these strings will be exported as:
  2637. Org HTML LaTeX
  2638. -----+----------+--------
  2639. \\- &shy; \\-
  2640. -- &ndash; --
  2641. --- &mdash; ---
  2642. ... &hellip; \ldots
  2643. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2644. :group 'org-export-translation
  2645. :type 'boolean)
  2646. (defcustom org-export-language-setup
  2647. '(("en" "Author" "Date" "Table of Contents")
  2648. ("cs" "Autor" "Datum" "Obsah")
  2649. ("da" "Ophavsmand" "Dato" "Indhold")
  2650. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2651. ("es" "Autor" "Fecha" "\xcdndice")
  2652. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2653. ("it" "Autore" "Data" "Indice")
  2654. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2655. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2656. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2657. "Terms used in export text, translated to different languages.
  2658. Use the variable `org-export-default-language' to set the language,
  2659. or use the +OPTION lines for a per-file setting."
  2660. :group 'org-export-general
  2661. :type '(repeat
  2662. (list
  2663. (string :tag "HTML language tag")
  2664. (string :tag "Author")
  2665. (string :tag "Date")
  2666. (string :tag "Table of Contents"))))
  2667. (defcustom org-export-default-language "en"
  2668. "The default language of HTML export, as a string.
  2669. This should have an association in `org-export-language-setup'."
  2670. :group 'org-export-general
  2671. :type 'string)
  2672. (defcustom org-export-skip-text-before-1st-heading t
  2673. "Non-nil means, skip all text before the first headline when exporting.
  2674. When nil, that text is exported as well."
  2675. :group 'org-export-general
  2676. :type 'boolean)
  2677. (defcustom org-export-headline-levels 3
  2678. "The last level which is still exported as a headline.
  2679. Inferior levels will produce itemize lists when exported.
  2680. Note that a numeric prefix argument to an exporter function overrides
  2681. this setting.
  2682. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2683. :group 'org-export-general
  2684. :type 'number)
  2685. (defcustom org-export-with-section-numbers t
  2686. "Non-nil means, add section numbers to headlines when exporting.
  2687. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2688. :group 'org-export-general
  2689. :type 'boolean)
  2690. (defcustom org-export-with-toc t
  2691. "Non-nil means, create a table of contents in exported files.
  2692. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2693. When an integer, include levels up to N in the toc, this may then be
  2694. different from `org-export-headline-levels', but it will not be allowed
  2695. to be larger than the number of headline levels.
  2696. When nil, no table of contents is made.
  2697. Headlines which contain any TODO items will be marked with \"(*)\" in
  2698. ASCII export, and with red color in HTML output, if the option
  2699. `org-export-mark-todo-in-toc' is set.
  2700. In HTML output, the TOC will be clickable.
  2701. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2702. or \"toc:3\"."
  2703. :group 'org-export-general
  2704. :type '(choice
  2705. (const :tag "No Table of Contents" nil)
  2706. (const :tag "Full Table of Contents" t)
  2707. (integer :tag "TOC to level")))
  2708. (defcustom org-export-mark-todo-in-toc nil
  2709. "Non-nil means, mark TOC lines that contain any open TODO items."
  2710. :group 'org-export-general
  2711. :type 'boolean)
  2712. (defcustom org-export-preserve-breaks nil
  2713. "Non-nil means, preserve all line breaks when exporting.
  2714. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2715. export, line breaks will always be preserved, regardless of this variable.
  2716. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2717. :group 'org-export-general
  2718. :type 'boolean)
  2719. (defcustom org-export-with-archived-trees 'headline
  2720. "Whether subtrees with the ARCHIVE tag should be exported.
  2721. This can have three different values
  2722. nil Do not export, pretend this tree is not present
  2723. t Do export the entire tree
  2724. headline Only export the headline, but skip the tree below it."
  2725. :group 'org-export-general
  2726. :group 'org-archive
  2727. :type '(choice
  2728. (const :tag "not at all" nil)
  2729. (const :tag "headline only" 'headline)
  2730. (const :tag "entirely" t)))
  2731. (defcustom org-export-author-info t
  2732. "Non-nil means, insert author name and email into the exported file.
  2733. This option can also be set with the +OPTIONS line,
  2734. e.g. \"author-info:nil\"."
  2735. :group 'org-export-general
  2736. :type 'boolean)
  2737. (defcustom org-export-time-stamp-file t
  2738. "Non-nil means, insert a time stamp into the exported file.
  2739. The time stamp shows when the file was created.
  2740. This option can also be set with the +OPTIONS line,
  2741. e.g. \"timestamp:nil\"."
  2742. :group 'org-export-general
  2743. :type 'boolean)
  2744. (defcustom org-export-with-timestamps t
  2745. "If nil, do not export time stamps and associated keywords."
  2746. :group 'org-export-general
  2747. :type 'boolean)
  2748. (defcustom org-export-remove-timestamps-from-toc t
  2749. "If nil, remove timestamps from the table of contents entries."
  2750. :group 'org-export-general
  2751. :type 'boolean)
  2752. (defcustom org-export-with-tags 'not-in-toc
  2753. "If nil, do not export tags, just remove them from headlines.
  2754. If this is the symbol `not-in-toc', tags will be removed from table of
  2755. contents entries, but still be shown in the headlines of the document.
  2756. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2757. :group 'org-export-general
  2758. :type '(choice
  2759. (const :tag "Off" nil)
  2760. (const :tag "Not in TOC" not-in-toc)
  2761. (const :tag "On" t)))
  2762. (defcustom org-export-with-drawers nil
  2763. "Non-nil means, export with drawers like the property drawer.
  2764. When t, all drawers are exported. This may also be a list of
  2765. drawer names to export."
  2766. :group 'org-export-general
  2767. :type '(choice
  2768. (const :tag "All drawers" t)
  2769. (const :tag "None" nil)
  2770. (repeat :tag "Selected drawers"
  2771. (string :tag "Drawer name"))))
  2772. (defgroup org-export-translation nil
  2773. "Options for translating special ascii sequences for the export backends."
  2774. :tag "Org Export Translation"
  2775. :group 'org-export)
  2776. (defcustom org-export-with-emphasize t
  2777. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2778. If the export target supports emphasizing text, the word will be
  2779. typeset in bold, italic, or underlined, respectively. Works only for
  2780. single words, but you can say: I *really* *mean* *this*.
  2781. Not all export backends support this.
  2782. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2783. :group 'org-export-translation
  2784. :type 'boolean)
  2785. (defcustom org-export-with-footnotes t
  2786. "If nil, export [1] as a footnote marker.
  2787. Lines starting with [1] will be formatted as footnotes.
  2788. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2789. :group 'org-export-translation
  2790. :type 'boolean)
  2791. (defcustom org-export-with-sub-superscripts t
  2792. "Non-nil means, interpret \"_\" and \"^\" for export.
  2793. When this option is turned on, you can use TeX-like syntax for sub- and
  2794. superscripts. Several characters after \"_\" or \"^\" will be
  2795. considered as a single item - so grouping with {} is normally not
  2796. needed. For example, the following things will be parsed as single
  2797. sub- or superscripts.
  2798. 10^24 or 10^tau several digits will be considered 1 item.
  2799. 10^-12 or 10^-tau a leading sign with digits or a word
  2800. x^2-y^3 will be read as x^2 - y^3, because items are
  2801. terminated by almost any nonword/nondigit char.
  2802. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2803. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2804. sub/superscript. If you set this variable to the symbol `{}',
  2805. the braces are *required* in order to trigger interpretations as
  2806. sub/superscript. This can be helpful in documents that need \"_\"
  2807. frequently in plain text.
  2808. Not all export backends support this, but HTML does.
  2809. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2810. :group 'org-export-translation
  2811. :type '(choice
  2812. (const :tag "Always interpret" t)
  2813. (const :tag "Only with braces" {})
  2814. (const :tag "Never interpret" nil)))
  2815. (defcustom org-export-with-special-strings t
  2816. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2817. When this option is turned on, these strings will be exported as:
  2818. \\- : &shy;
  2819. -- : &ndash;
  2820. --- : &mdash;
  2821. Not all export backends support this, but HTML does.
  2822. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2823. :group 'org-export-translation
  2824. :type 'boolean)
  2825. (defcustom org-export-with-TeX-macros t
  2826. "Non-nil means, interpret simple TeX-like macros when exporting.
  2827. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2828. No only real TeX macros will work here, but the standard HTML entities
  2829. for math can be used as macro names as well. For a list of supported
  2830. names in HTML export, see the constant `org-html-entities'.
  2831. Not all export backends support this.
  2832. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2833. :group 'org-export-translation
  2834. :group 'org-export-latex
  2835. :type 'boolean)
  2836. (defcustom org-export-with-LaTeX-fragments nil
  2837. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2838. When set, the exporter will find LaTeX environments if the \\begin line is
  2839. the first non-white thing on a line. It will also find the math delimiters
  2840. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2841. display math.
  2842. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2843. :group 'org-export-translation
  2844. :group 'org-export-latex
  2845. :type 'boolean)
  2846. (defcustom org-export-with-fixed-width t
  2847. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2848. This can be used to have pre-formatted text, fragments of code etc. For
  2849. example:
  2850. : ;; Some Lisp examples
  2851. : (while (defc cnt)
  2852. : (ding))
  2853. will be looking just like this in also HTML. See also the QUOTE keyword.
  2854. Not all export backends support this.
  2855. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2856. :group 'org-export-translation
  2857. :type 'boolean)
  2858. (defcustom org-match-sexp-depth 3
  2859. "Number of stacked braces for sub/superscript matching.
  2860. This has to be set before loading org.el to be effective."
  2861. :group 'org-export-translation
  2862. :type 'integer)
  2863. (defgroup org-export-tables nil
  2864. "Options for exporting tables in Org-mode."
  2865. :tag "Org Export Tables"
  2866. :group 'org-export)
  2867. (defcustom org-export-with-tables t
  2868. "If non-nil, lines starting with \"|\" define a table.
  2869. For example:
  2870. | Name | Address | Birthday |
  2871. |-------------+----------+-----------|
  2872. | Arthur Dent | England | 29.2.2100 |
  2873. Not all export backends support this.
  2874. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2875. :group 'org-export-tables
  2876. :type 'boolean)
  2877. (defcustom org-export-highlight-first-table-line t
  2878. "Non-nil means, highlight the first table line.
  2879. In HTML export, this means use <th> instead of <td>.
  2880. In tables created with table.el, this applies to the first table line.
  2881. In Org-mode tables, all lines before the first horizontal separator
  2882. line will be formatted with <th> tags."
  2883. :group 'org-export-tables
  2884. :type 'boolean)
  2885. (defcustom org-export-table-remove-special-lines t
  2886. "Remove special lines and marking characters in calculating tables.
  2887. This removes the special marking character column from tables that are set
  2888. up for spreadsheet calculations. It also removes the entire lines
  2889. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2890. the values of constants may be useful to have."
  2891. :group 'org-export-tables
  2892. :type 'boolean)
  2893. (defcustom org-export-prefer-native-exporter-for-tables nil
  2894. "Non-nil means, always export tables created with table.el natively.
  2895. Natively means, use the HTML code generator in table.el.
  2896. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2897. the table does not use row- or column-spanning). This has the
  2898. advantage, that the automatic HTML conversions for math symbols and
  2899. sub/superscripts can be applied. Org-mode's HTML generator is also
  2900. much faster."
  2901. :group 'org-export-tables
  2902. :type 'boolean)
  2903. (defgroup org-export-ascii nil
  2904. "Options specific for ASCII export of Org-mode files."
  2905. :tag "Org Export ASCII"
  2906. :group 'org-export)
  2907. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2908. "Characters for underlining headings in ASCII export.
  2909. In the given sequence, these characters will be used for level 1, 2, ..."
  2910. :group 'org-export-ascii
  2911. :type '(repeat character))
  2912. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2913. "Bullet characters for headlines converted to lists in ASCII export.
  2914. The first character is used for the first lest level generated in this
  2915. way, and so on. If there are more levels than characters given here,
  2916. the list will be repeated.
  2917. Note that plain lists will keep the same bullets as the have in the
  2918. Org-mode file."
  2919. :group 'org-export-ascii
  2920. :type '(repeat character))
  2921. (defgroup org-export-xml nil
  2922. "Options specific for XML export of Org-mode files."
  2923. :tag "Org Export XML"
  2924. :group 'org-export)
  2925. (defgroup org-export-html nil
  2926. "Options specific for HTML export of Org-mode files."
  2927. :tag "Org Export HTML"
  2928. :group 'org-export)
  2929. (defcustom org-export-html-coding-system nil
  2930. ""
  2931. :group 'org-export-html
  2932. :type 'coding-system)
  2933. (defcustom org-export-html-extension "html"
  2934. "The extension for exported HTML files."
  2935. :group 'org-export-html
  2936. :type 'string)
  2937. (defcustom org-export-html-style
  2938. "<style type=\"text/css\">
  2939. html {
  2940. font-family: Times, serif;
  2941. font-size: 12pt;
  2942. }
  2943. .title { text-align: center; }
  2944. .todo { color: red; }
  2945. .done { color: green; }
  2946. .timestamp { color: grey }
  2947. .timestamp-kwd { color: CadetBlue }
  2948. .tag { background-color:lightblue; font-weight:normal }
  2949. .target { background-color: lavender; }
  2950. pre {
  2951. border: 1pt solid #AEBDCC;
  2952. background-color: #F3F5F7;
  2953. padding: 5pt;
  2954. font-family: courier, monospace;
  2955. }
  2956. table { border-collapse: collapse; }
  2957. td, th {
  2958. vertical-align: top;
  2959. <!--border: 1pt solid #ADB9CC;-->
  2960. }
  2961. </style>"
  2962. "The default style specification for exported HTML files.
  2963. Since there are different ways of setting style information, this variable
  2964. needs to contain the full HTML structure to provide a style, including the
  2965. surrounding HTML tags. The style specifications should include definitions
  2966. for new classes todo, done, title, and deadline. For example, legal values
  2967. would be:
  2968. <style type=\"text/css\">
  2969. p { font-weight: normal; color: gray; }
  2970. h1 { color: black; }
  2971. .title { text-align: center; }
  2972. .todo, .deadline { color: red; }
  2973. .done { color: green; }
  2974. </style>
  2975. or, if you want to keep the style in a file,
  2976. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2977. As the value of this option simply gets inserted into the HTML <head> header,
  2978. you can \"misuse\" it to add arbitrary text to the header."
  2979. :group 'org-export-html
  2980. :type 'string)
  2981. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2982. "Format for typesetting the document title in HTML export."
  2983. :group 'org-export-html
  2984. :type 'string)
  2985. (defcustom org-export-html-toplevel-hlevel 2
  2986. "The <H> level for level 1 headings in HTML export."
  2987. :group 'org-export-html
  2988. :type 'string)
  2989. (defcustom org-export-html-link-org-files-as-html t
  2990. "Non-nil means, make file links to `file.org' point to `file.html'.
  2991. When org-mode is exporting an org-mode file to HTML, links to
  2992. non-html files are directly put into a href tag in HTML.
  2993. However, links to other Org-mode files (recognized by the
  2994. extension `.org.) should become links to the corresponding html
  2995. file, assuming that the linked org-mode file will also be
  2996. converted to HTML.
  2997. When nil, the links still point to the plain `.org' file."
  2998. :group 'org-export-html
  2999. :type 'boolean)
  3000. (defcustom org-export-html-inline-images 'maybe
  3001. "Non-nil means, inline images into exported HTML pages.
  3002. This is done using an <img> tag. When nil, an anchor with href is used to
  3003. link to the image. If this option is `maybe', then images in links with
  3004. an empty description will be inlined, while images with a description will
  3005. be linked only."
  3006. :group 'org-export-html
  3007. :type '(choice (const :tag "Never" nil)
  3008. (const :tag "Always" t)
  3009. (const :tag "When there is no description" maybe)))
  3010. ;; FIXME: rename
  3011. (defcustom org-export-html-expand t
  3012. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3013. When nil, these tags will be exported as plain text and therefore
  3014. not be interpreted by a browser.
  3015. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3016. :group 'org-export-html
  3017. :type 'boolean)
  3018. (defcustom org-export-html-table-tag
  3019. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3020. "The HTML tag that is used to start a table.
  3021. This must be a <table> tag, but you may change the options like
  3022. borders and spacing."
  3023. :group 'org-export-html
  3024. :type 'string)
  3025. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3026. "The opening tag for table header fields.
  3027. This is customizable so that alignment options can be specified."
  3028. :group 'org-export-tables
  3029. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3030. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3031. "The opening tag for table data fields.
  3032. This is customizable so that alignment options can be specified."
  3033. :group 'org-export-tables
  3034. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3035. (defcustom org-export-html-with-timestamp nil
  3036. "If non-nil, write `org-export-html-html-helper-timestamp'
  3037. into the exported HTML text. Otherwise, the buffer will just be saved
  3038. to a file."
  3039. :group 'org-export-html
  3040. :type 'boolean)
  3041. (defcustom org-export-html-html-helper-timestamp
  3042. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3043. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3044. :group 'org-export-html
  3045. :type 'string)
  3046. (defgroup org-export-icalendar nil
  3047. "Options specific for iCalendar export of Org-mode files."
  3048. :tag "Org Export iCalendar"
  3049. :group 'org-export)
  3050. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3051. "The file name for the iCalendar file covering all agenda files.
  3052. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3053. The file name should be absolute, the file will be overwritten without warning."
  3054. :group 'org-export-icalendar
  3055. :type 'file)
  3056. (defcustom org-icalendar-include-todo nil
  3057. "Non-nil means, export to iCalendar files should also cover TODO items."
  3058. :group 'org-export-icalendar
  3059. :type '(choice
  3060. (const :tag "None" nil)
  3061. (const :tag "Unfinished" t)
  3062. (const :tag "All" all)))
  3063. (defcustom org-icalendar-include-sexps t
  3064. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3065. These are entries like in the diary, but directly in an Org-mode file."
  3066. :group 'org-export-icalendar
  3067. :type 'boolean)
  3068. (defcustom org-icalendar-include-body 100
  3069. "Amount of text below headline to be included in iCalendar export.
  3070. This is a number of characters that should maximally be included.
  3071. Properties, scheduling and clocking lines will always be removed.
  3072. The text will be inserted into the DESCRIPTION field."
  3073. :group 'org-export-icalendar
  3074. :type '(choice
  3075. (const :tag "Nothing" nil)
  3076. (const :tag "Everything" t)
  3077. (integer :tag "Max characters")))
  3078. (defcustom org-icalendar-combined-name "OrgMode"
  3079. "Calendar name for the combined iCalendar representing all agenda files."
  3080. :group 'org-export-icalendar
  3081. :type 'string)
  3082. (defgroup org-font-lock nil
  3083. "Font-lock settings for highlighting in Org-mode."
  3084. :tag "Org Font Lock"
  3085. :group 'org)
  3086. (defcustom org-level-color-stars-only nil
  3087. "Non-nil means fontify only the stars in each headline.
  3088. When nil, the entire headline is fontified.
  3089. Changing it requires restart of `font-lock-mode' to become effective
  3090. also in regions already fontified."
  3091. :group 'org-font-lock
  3092. :type 'boolean)
  3093. (defcustom org-hide-leading-stars nil
  3094. "Non-nil means, hide the first N-1 stars in a headline.
  3095. This works by using the face `org-hide' for these stars. This
  3096. face is white for a light background, and black for a dark
  3097. background. You may have to customize the face `org-hide' to
  3098. make this work.
  3099. Changing it requires restart of `font-lock-mode' to become effective
  3100. also in regions already fontified.
  3101. You may also set this on a per-file basis by adding one of the following
  3102. lines to the buffer:
  3103. #+STARTUP: hidestars
  3104. #+STARTUP: showstars"
  3105. :group 'org-font-lock
  3106. :type 'boolean)
  3107. (defcustom org-fontify-done-headline nil
  3108. "Non-nil means, change the face of a headline if it is marked DONE.
  3109. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3110. When this is non-nil, the headline after the keyword is set to the
  3111. `org-headline-done' as an additional indication."
  3112. :group 'org-font-lock
  3113. :type 'boolean)
  3114. (defcustom org-fontify-emphasized-text t
  3115. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3116. Changing this variable requires a restart of Emacs to take effect."
  3117. :group 'org-font-lock
  3118. :type 'boolean)
  3119. (defcustom org-highlight-latex-fragments-and-specials nil
  3120. "Non-nil means, fontify what is treated specially by the exporters."
  3121. :group 'org-font-lock
  3122. :type 'boolean)
  3123. (defcustom org-hide-emphasis-markers nil
  3124. "Non-nil mean font-lock should hide the emphasis marker characters."
  3125. :group 'org-font-lock
  3126. :type 'boolean)
  3127. (defvar org-emph-re nil
  3128. "Regular expression for matching emphasis.")
  3129. (defvar org-verbatim-re nil
  3130. "Regular expression for matching verbatim text.")
  3131. (defvar org-emphasis-regexp-components) ; defined just below
  3132. (defvar org-emphasis-alist) ; defined just below
  3133. (defun org-set-emph-re (var val)
  3134. "Set variable and compute the emphasis regular expression."
  3135. (set var val)
  3136. (when (and (boundp 'org-emphasis-alist)
  3137. (boundp 'org-emphasis-regexp-components)
  3138. org-emphasis-alist org-emphasis-regexp-components)
  3139. (let* ((e org-emphasis-regexp-components)
  3140. (pre (car e))
  3141. (post (nth 1 e))
  3142. (border (nth 2 e))
  3143. (body (nth 3 e))
  3144. (nl (nth 4 e))
  3145. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3146. (body1 (concat body "*?"))
  3147. (markers (mapconcat 'car org-emphasis-alist ""))
  3148. (vmarkers (mapconcat
  3149. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3150. org-emphasis-alist "")))
  3151. ;; make sure special characters appear at the right position in the class
  3152. (if (string-match "\\^" markers)
  3153. (setq markers (concat (replace-match "" t t markers) "^")))
  3154. (if (string-match "-" markers)
  3155. (setq markers (concat (replace-match "" t t markers) "-")))
  3156. (if (string-match "\\^" vmarkers)
  3157. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3158. (if (string-match "-" vmarkers)
  3159. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3160. (if (> nl 0)
  3161. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3162. (int-to-string nl) "\\}")))
  3163. ;; Make the regexp
  3164. (setq org-emph-re
  3165. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3166. "\\("
  3167. "\\([" markers "]\\)"
  3168. "\\("
  3169. "[^" border "]\\|"
  3170. "[^" border (if (and nil stacked) markers) "]"
  3171. body1
  3172. "[^" border (if (and nil stacked) markers) "]"
  3173. "\\)"
  3174. "\\3\\)"
  3175. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3176. (setq org-verbatim-re
  3177. (concat "\\([" pre "]\\|^\\)"
  3178. "\\("
  3179. "\\([" vmarkers "]\\)"
  3180. "\\("
  3181. "[^" border "]\\|"
  3182. "[^" border "]"
  3183. body1
  3184. "[^" border "]"
  3185. "\\)"
  3186. "\\3\\)"
  3187. "\\([" post "]\\|$\\)")))))
  3188. (defcustom org-emphasis-regexp-components
  3189. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3190. "Components used to build the regular expression for emphasis.
  3191. This is a list with 6 entries. Terminology: In an emphasis string
  3192. like \" *strong word* \", we call the initial space PREMATCH, the final
  3193. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3194. and \"trong wor\" is the body. The different components in this variable
  3195. specify what is allowed/forbidden in each part:
  3196. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3197. post Chars allowed as postmatch. End of line will be allowed too.
  3198. border The chars *forbidden* as border characters.
  3199. body-regexp A regexp like \".\" to match a body character. Don't use
  3200. non-shy groups here, and don't allow newline here.
  3201. newline The maximum number of newlines allowed in an emphasis exp.
  3202. Use customize to modify this, or restart Emacs after changing it."
  3203. :group 'org-font-lock
  3204. :set 'org-set-emph-re
  3205. :type '(list
  3206. (sexp :tag "Allowed chars in pre ")
  3207. (sexp :tag "Allowed chars in post ")
  3208. (sexp :tag "Forbidden chars in border ")
  3209. (sexp :tag "Regexp for body ")
  3210. (integer :tag "number of newlines allowed")
  3211. (option (boolean :tag "Stacking (DISABLED) "))))
  3212. (defcustom org-emphasis-alist
  3213. '(("*" bold "<b>" "</b>")
  3214. ("/" italic "<i>" "</i>")
  3215. ("_" underline "<u>" "</u>")
  3216. ("=" org-code "<code>" "</code>" verbatim)
  3217. ("~" org-verbatim "" "" verbatim)
  3218. ("+" (:strike-through t) "<del>" "</del>")
  3219. )
  3220. "Special syntax for emphasized text.
  3221. Text starting and ending with a special character will be emphasized, for
  3222. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3223. characters, the face to be used by font-lock for highlighting in Org-mode
  3224. Emacs buffers, and the HTML tags to be used for this.
  3225. Use customize to modify this, or restart Emacs after changing it."
  3226. :group 'org-font-lock
  3227. :set 'org-set-emph-re
  3228. :type '(repeat
  3229. (list
  3230. (string :tag "Marker character")
  3231. (choice
  3232. (face :tag "Font-lock-face")
  3233. (plist :tag "Face property list"))
  3234. (string :tag "HTML start tag")
  3235. (string :tag "HTML end tag")
  3236. (option (const verbatim)))))
  3237. ;;; The faces
  3238. (defgroup org-faces nil
  3239. "Faces in Org-mode."
  3240. :tag "Org Faces"
  3241. :group 'org-font-lock)
  3242. (defun org-compatible-face (inherits specs)
  3243. "Make a compatible face specification.
  3244. If INHERITS is an existing face and if the Emacs version supports it,
  3245. just inherit the face. If not, use SPECS to define the face.
  3246. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3247. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3248. to the top of the list. The `min-colors' attribute will be removed from
  3249. any other entries, and any resulting duplicates will be removed entirely."
  3250. (cond
  3251. ((and inherits (facep inherits)
  3252. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3253. ;; In Emacs 23, we use inheritance where possible.
  3254. ;; We only do this in Emacs 23, because only there the outline
  3255. ;; faces have been changed to the original org-mode-level-faces.
  3256. (list (list t :inherit inherits)))
  3257. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3258. ;; These do not understand the `min-colors' attribute.
  3259. (let (r e a)
  3260. (while (setq e (pop specs))
  3261. (cond
  3262. ((memq (car e) '(t default)) (push e r))
  3263. ((setq a (member '(min-colors 8) (car e)))
  3264. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3265. (cdr e)))))
  3266. ((setq a (assq 'min-colors (car e)))
  3267. (setq e (cons (delq a (car e)) (cdr e)))
  3268. (or (assoc (car e) r) (push e r)))
  3269. (t (or (assoc (car e) r) (push e r)))))
  3270. (nreverse r)))
  3271. (t specs)))
  3272. (put 'org-compatible-face 'lisp-indent-function 1)
  3273. (defface org-hide
  3274. '((((background light)) (:foreground "white"))
  3275. (((background dark)) (:foreground "black")))
  3276. "Face used to hide leading stars in headlines.
  3277. The forground color of this face should be equal to the background
  3278. color of the frame."
  3279. :group 'org-faces)
  3280. (defface org-level-1 ;; font-lock-function-name-face
  3281. (org-compatible-face 'outline-1
  3282. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3283. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3284. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3285. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3286. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3287. (t (:bold t))))
  3288. "Face used for level 1 headlines."
  3289. :group 'org-faces)
  3290. (defface org-level-2 ;; font-lock-variable-name-face
  3291. (org-compatible-face 'outline-2
  3292. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3293. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3294. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3295. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3296. (t (:bold t))))
  3297. "Face used for level 2 headlines."
  3298. :group 'org-faces)
  3299. (defface org-level-3 ;; font-lock-keyword-face
  3300. (org-compatible-face 'outline-3
  3301. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3302. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3303. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3304. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3305. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3306. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3307. (t (:bold t))))
  3308. "Face used for level 3 headlines."
  3309. :group 'org-faces)
  3310. (defface org-level-4 ;; font-lock-comment-face
  3311. (org-compatible-face 'outline-4
  3312. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3313. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3314. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3315. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3316. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3317. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3318. (t (:bold t))))
  3319. "Face used for level 4 headlines."
  3320. :group 'org-faces)
  3321. (defface org-level-5 ;; font-lock-type-face
  3322. (org-compatible-face 'outline-5
  3323. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3324. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3325. (((class color) (min-colors 8)) (:foreground "green"))))
  3326. "Face used for level 5 headlines."
  3327. :group 'org-faces)
  3328. (defface org-level-6 ;; font-lock-constant-face
  3329. (org-compatible-face 'outline-6
  3330. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3331. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3332. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3333. "Face used for level 6 headlines."
  3334. :group 'org-faces)
  3335. (defface org-level-7 ;; font-lock-builtin-face
  3336. (org-compatible-face 'outline-7
  3337. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3338. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3339. (((class color) (min-colors 8)) (:foreground "blue"))))
  3340. "Face used for level 7 headlines."
  3341. :group 'org-faces)
  3342. (defface org-level-8 ;; font-lock-string-face
  3343. (org-compatible-face 'outline-8
  3344. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3345. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3346. (((class color) (min-colors 8)) (:foreground "green"))))
  3347. "Face used for level 8 headlines."
  3348. :group 'org-faces)
  3349. (defface org-special-keyword ;; font-lock-string-face
  3350. (org-compatible-face nil
  3351. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3352. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3353. (t (:italic t))))
  3354. "Face used for special keywords."
  3355. :group 'org-faces)
  3356. (defface org-drawer ;; font-lock-function-name-face
  3357. (org-compatible-face nil
  3358. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3359. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3360. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3361. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3362. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3363. (t (:bold t))))
  3364. "Face used for drawers."
  3365. :group 'org-faces)
  3366. (defface org-property-value nil
  3367. "Face used for the value of a property."
  3368. :group 'org-faces)
  3369. (defface org-column
  3370. (org-compatible-face nil
  3371. '((((class color) (min-colors 16) (background light))
  3372. (:background "grey90"))
  3373. (((class color) (min-colors 16) (background dark))
  3374. (:background "grey30"))
  3375. (((class color) (min-colors 8))
  3376. (:background "cyan" :foreground "black"))
  3377. (t (:inverse-video t))))
  3378. "Face for column display of entry properties."
  3379. :group 'org-faces)
  3380. (when (fboundp 'set-face-attribute)
  3381. ;; Make sure that a fixed-width face is used when we have a column table.
  3382. (set-face-attribute 'org-column nil
  3383. :height (face-attribute 'default :height)
  3384. :family (face-attribute 'default :family)))
  3385. (defface org-warning
  3386. (org-compatible-face 'font-lock-warning-face
  3387. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3388. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3389. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3390. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3391. (t (:bold t))))
  3392. "Face for deadlines and TODO keywords."
  3393. :group 'org-faces)
  3394. (defface org-archived ; similar to shadow
  3395. (org-compatible-face 'shadow
  3396. '((((class color grayscale) (min-colors 88) (background light))
  3397. (:foreground "grey50"))
  3398. (((class color grayscale) (min-colors 88) (background dark))
  3399. (:foreground "grey70"))
  3400. (((class color) (min-colors 8) (background light))
  3401. (:foreground "green"))
  3402. (((class color) (min-colors 8) (background dark))
  3403. (:foreground "yellow"))))
  3404. "Face for headline with the ARCHIVE tag."
  3405. :group 'org-faces)
  3406. (defface org-link
  3407. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3408. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3409. (t (:underline t)))
  3410. "Face for links."
  3411. :group 'org-faces)
  3412. (defface org-ellipsis
  3413. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3414. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3415. (t (:strike-through t)))
  3416. "Face for the ellipsis in folded text."
  3417. :group 'org-faces)
  3418. (defface org-target
  3419. '((((class color) (background light)) (:underline t))
  3420. (((class color) (background dark)) (:underline t))
  3421. (t (:underline t)))
  3422. "Face for links."
  3423. :group 'org-faces)
  3424. (defface org-date
  3425. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3426. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3427. (t (:underline t)))
  3428. "Face for links."
  3429. :group 'org-faces)
  3430. (defface org-sexp-date
  3431. '((((class color) (background light)) (:foreground "Purple"))
  3432. (((class color) (background dark)) (:foreground "Cyan"))
  3433. (t (:underline t)))
  3434. "Face for links."
  3435. :group 'org-faces)
  3436. (defface org-tag
  3437. '((t (:bold t)))
  3438. "Face for tags."
  3439. :group 'org-faces)
  3440. (defface org-todo ; font-lock-warning-face
  3441. (org-compatible-face nil
  3442. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3443. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3444. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3445. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3446. (t (:inverse-video t :bold t))))
  3447. "Face for TODO keywords."
  3448. :group 'org-faces)
  3449. (defface org-done ;; font-lock-type-face
  3450. (org-compatible-face nil
  3451. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3452. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3453. (((class color) (min-colors 8)) (:foreground "green"))
  3454. (t (:bold t))))
  3455. "Face used for todo keywords that indicate DONE items."
  3456. :group 'org-faces)
  3457. (defface org-headline-done ;; font-lock-string-face
  3458. (org-compatible-face nil
  3459. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3460. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3461. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3462. "Face used to indicate that a headline is DONE.
  3463. This face is only used if `org-fontify-done-headline' is set. If applies
  3464. to the part of the headline after the DONE keyword."
  3465. :group 'org-faces)
  3466. (defcustom org-todo-keyword-faces nil
  3467. "Faces for specific TODO keywords.
  3468. This is a list of cons cells, with TODO keywords in the car
  3469. and faces in the cdr. The face can be a symbol, or a property
  3470. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3471. :group 'org-faces
  3472. :group 'org-todo
  3473. :type '(repeat
  3474. (cons
  3475. (string :tag "keyword")
  3476. (sexp :tag "face"))))
  3477. (defface org-table ;; font-lock-function-name-face
  3478. (org-compatible-face nil
  3479. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3480. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3481. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3482. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3483. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3484. (((class color) (min-colors 8) (background dark)))))
  3485. "Face used for tables."
  3486. :group 'org-faces)
  3487. (defface org-formula
  3488. (org-compatible-face nil
  3489. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3490. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3491. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3492. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3493. (t (:bold t :italic t))))
  3494. "Face for formulas."
  3495. :group 'org-faces)
  3496. (defface org-code
  3497. (org-compatible-face nil
  3498. '((((class color grayscale) (min-colors 88) (background light))
  3499. (:foreground "grey50"))
  3500. (((class color grayscale) (min-colors 88) (background dark))
  3501. (:foreground "grey70"))
  3502. (((class color) (min-colors 8) (background light))
  3503. (:foreground "green"))
  3504. (((class color) (min-colors 8) (background dark))
  3505. (:foreground "yellow"))))
  3506. "Face for fixed-with text like code snippets."
  3507. :group 'org-faces
  3508. :version "22.1")
  3509. (defface org-verbatim
  3510. (org-compatible-face nil
  3511. '((((class color grayscale) (min-colors 88) (background light))
  3512. (:foreground "grey50" :underline t))
  3513. (((class color grayscale) (min-colors 88) (background dark))
  3514. (:foreground "grey70" :underline t))
  3515. (((class color) (min-colors 8) (background light))
  3516. (:foreground "green" :underline t))
  3517. (((class color) (min-colors 8) (background dark))
  3518. (:foreground "yellow" :underline t))))
  3519. "Face for fixed-with text like code snippets."
  3520. :group 'org-faces
  3521. :version "22.1")
  3522. (defface org-agenda-structure ;; font-lock-function-name-face
  3523. (org-compatible-face nil
  3524. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3525. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3526. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3527. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3528. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3529. (t (:bold t))))
  3530. "Face used in agenda for captions and dates."
  3531. :group 'org-faces)
  3532. (defface org-scheduled-today
  3533. (org-compatible-face nil
  3534. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3535. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3536. (((class color) (min-colors 8)) (:foreground "green"))
  3537. (t (:bold t :italic t))))
  3538. "Face for items scheduled for a certain day."
  3539. :group 'org-faces)
  3540. (defface org-scheduled-previously
  3541. (org-compatible-face nil
  3542. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3543. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3544. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3545. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3546. (t (:bold t))))
  3547. "Face for items scheduled previously, and not yet done."
  3548. :group 'org-faces)
  3549. (defface org-upcoming-deadline
  3550. (org-compatible-face nil
  3551. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3552. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3553. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3554. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3555. (t (:bold t))))
  3556. "Face for items scheduled previously, and not yet done."
  3557. :group 'org-faces)
  3558. (defcustom org-agenda-deadline-faces
  3559. '((1.0 . org-warning)
  3560. (0.5 . org-upcoming-deadline)
  3561. (0.0 . default))
  3562. "Faces for showing deadlines in the agenda.
  3563. This is a list of cons cells. The cdr of each cell is a face to be used,
  3564. and it can also just be like '(:foreground \"yellow\").
  3565. Each car is a fraction of the head-warning time that must have passed for
  3566. this the face in the cdr to be used for display. The numbers must be
  3567. given in descending order. The head-warning time is normally taken
  3568. from `org-deadline-warning-days', but can also be specified in the deadline
  3569. timestamp itself, like this:
  3570. DEADLINE: <2007-08-13 Mon -8d>
  3571. You may use d for days, w for weeks, m for months and y for years. Months
  3572. and years will only be treated in an approximate fashion (30.4 days for a
  3573. month and 365.24 days for a year)."
  3574. :group 'org-faces
  3575. :group 'org-agenda-daily/weekly
  3576. :type '(repeat
  3577. (cons
  3578. (number :tag "Fraction of head-warning time passed")
  3579. (sexp :tag "Face"))))
  3580. ;; FIXME: this is not a good face yet.
  3581. (defface org-agenda-restriction-lock
  3582. (org-compatible-face nil
  3583. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3584. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3585. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3586. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3587. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3588. (t (:inverse-video t))))
  3589. "Face for showing the agenda restriction lock."
  3590. :group 'org-faces)
  3591. (defface org-time-grid ;; font-lock-variable-name-face
  3592. (org-compatible-face nil
  3593. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3594. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3595. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3596. "Face used for time grids."
  3597. :group 'org-faces)
  3598. (defconst org-level-faces
  3599. '(org-level-1 org-level-2 org-level-3 org-level-4
  3600. org-level-5 org-level-6 org-level-7 org-level-8
  3601. ))
  3602. (defcustom org-n-level-faces (length org-level-faces)
  3603. "The number different faces to be used for headlines.
  3604. Org-mode defines 8 different headline faces, so this can be at most 8.
  3605. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3606. :type 'number
  3607. :group 'org-faces)
  3608. ;;; Functions and variables from ther packages
  3609. ;; Declared here to avoid compiler warnings
  3610. (eval-and-compile
  3611. (unless (fboundp 'declare-function)
  3612. (defmacro declare-function (fn file &optional arglist fileonly))))
  3613. ;; XEmacs only
  3614. (defvar outline-mode-menu-heading)
  3615. (defvar outline-mode-menu-show)
  3616. (defvar outline-mode-menu-hide)
  3617. (defvar zmacs-regions) ; XEmacs regions
  3618. ;; Emacs only
  3619. (defvar mark-active)
  3620. ;; Various packages
  3621. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3622. (declare-function add-to-diary-list "diary-lib"
  3623. (date string specifier &optional marker globcolor literal))
  3624. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3625. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3626. (declare-function Info-goto-node "info" (nodename &optional fork))
  3627. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3628. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3629. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3630. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3631. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3632. (declare-function bbdb-record-name "ext:bbdb" (record))
  3633. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3634. (declare-function bibtex-generate-autokey "bibtex" ())
  3635. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3636. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3637. (defvar calc-embedded-close-formula)
  3638. (defvar calc-embedded-open-formula)
  3639. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3640. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3641. (declare-function calendar-check-holidays "holidays" (date))
  3642. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3643. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3644. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3645. (declare-function calendar-forward-day "cal-move" (arg))
  3646. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3647. (declare-function calendar-goto-date "cal-move" (date))
  3648. (declare-function calendar-goto-today "cal-move" ())
  3649. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3650. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3651. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3652. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3653. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3654. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3655. (defvar calendar-mode-map)
  3656. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3657. (declare-function cdlatex-tab "ext:cdlatex" ())
  3658. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3659. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3660. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3661. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3662. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3663. (defvar font-lock-unfontify-region-function)
  3664. (declare-function gnus-article-show-summary "gnus-art" ())
  3665. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3666. (defvar gnus-other-frame-object)
  3667. (defvar gnus-group-name)
  3668. (defvar gnus-article-current)
  3669. (defvar Info-current-file)
  3670. (defvar Info-current-node)
  3671. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3672. (declare-function mh-find-path "mh-utils" ())
  3673. (declare-function mh-get-header-field "mh-utils" (field))
  3674. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3675. (declare-function mh-header-display "mh-show" ())
  3676. (declare-function mh-index-previous-folder "mh-search" ())
  3677. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3678. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3679. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3680. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3681. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3682. (declare-function mh-show-header-display "mh-show" t t)
  3683. (declare-function mh-show-msg "mh-show" (msg))
  3684. (declare-function mh-show-show "mh-show" t t)
  3685. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3686. (defvar mh-progs)
  3687. (defvar mh-current-folder)
  3688. (defvar mh-show-folder-buffer)
  3689. (defvar mh-index-folder)
  3690. (defvar mh-searcher)
  3691. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3692. (declare-function parse-time-string "parse-time" (string))
  3693. (declare-function remember "remember" (&optional initial))
  3694. (declare-function remember-buffer-desc "remember" ())
  3695. (declare-function remember-finalize "remember" ())
  3696. (defvar remember-save-after-remembering)
  3697. (defvar remember-data-file)
  3698. (defvar remember-register)
  3699. (defvar remember-buffer)
  3700. (defvar remember-handler-functions)
  3701. (defvar remember-annotation-functions)
  3702. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3703. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3704. (declare-function rmail-what-message "rmail" ())
  3705. (defvar texmathp-why)
  3706. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3707. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3708. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3709. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3710. (declare-function vm-isearch-update "ext:vm-search" ())
  3711. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3712. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3713. (declare-function vm-su-subject "ext:vm-summary" (m))
  3714. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3715. (defvar vm-message-pointer)
  3716. (defvar vm-folder-directory)
  3717. (defvar w3m-current-url)
  3718. (defvar w3m-current-title)
  3719. ;; backward compatibility to old version of wl
  3720. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3721. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3722. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3723. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3724. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3725. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3726. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3727. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3728. (defvar wl-summary-buffer-elmo-folder)
  3729. (defvar wl-summary-buffer-folder-name)
  3730. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3731. (defvar org-latex-regexps)
  3732. (defvar constants-unit-system)
  3733. ;;; Variables for pre-computed regular expressions, all buffer local
  3734. (defvar org-drawer-regexp nil
  3735. "Matches first line of a hidden block.")
  3736. (make-variable-buffer-local 'org-drawer-regexp)
  3737. (defvar org-todo-regexp nil
  3738. "Matches any of the TODO state keywords.")
  3739. (make-variable-buffer-local 'org-todo-regexp)
  3740. (defvar org-not-done-regexp nil
  3741. "Matches any of the TODO state keywords except the last one.")
  3742. (make-variable-buffer-local 'org-not-done-regexp)
  3743. (defvar org-todo-line-regexp nil
  3744. "Matches a headline and puts TODO state into group 2 if present.")
  3745. (make-variable-buffer-local 'org-todo-line-regexp)
  3746. (defvar org-complex-heading-regexp nil
  3747. "Matches a headline and puts everything into groups:
  3748. group 1: the stars
  3749. group 2: The todo keyword, maybe
  3750. group 3: Priority cookie
  3751. group 4: True headline
  3752. group 5: Tags")
  3753. (make-variable-buffer-local 'org-complex-heading-regexp)
  3754. (defvar org-todo-line-tags-regexp nil
  3755. "Matches a headline and puts TODO state into group 2 if present.
  3756. Also put tags into group 4 if tags are present.")
  3757. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3758. (defvar org-nl-done-regexp nil
  3759. "Matches newline followed by a headline with the DONE keyword.")
  3760. (make-variable-buffer-local 'org-nl-done-regexp)
  3761. (defvar org-looking-at-done-regexp nil
  3762. "Matches the DONE keyword a point.")
  3763. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3764. (defvar org-ds-keyword-length 12
  3765. "Maximum length of the Deadline and SCHEDULED keywords.")
  3766. (make-variable-buffer-local 'org-ds-keyword-length)
  3767. (defvar org-deadline-regexp nil
  3768. "Matches the DEADLINE keyword.")
  3769. (make-variable-buffer-local 'org-deadline-regexp)
  3770. (defvar org-deadline-time-regexp nil
  3771. "Matches the DEADLINE keyword together with a time stamp.")
  3772. (make-variable-buffer-local 'org-deadline-time-regexp)
  3773. (defvar org-deadline-line-regexp nil
  3774. "Matches the DEADLINE keyword and the rest of the line.")
  3775. (make-variable-buffer-local 'org-deadline-line-regexp)
  3776. (defvar org-scheduled-regexp nil
  3777. "Matches the SCHEDULED keyword.")
  3778. (make-variable-buffer-local 'org-scheduled-regexp)
  3779. (defvar org-scheduled-time-regexp nil
  3780. "Matches the SCHEDULED keyword together with a time stamp.")
  3781. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3782. (defvar org-closed-time-regexp nil
  3783. "Matches the CLOSED keyword together with a time stamp.")
  3784. (make-variable-buffer-local 'org-closed-time-regexp)
  3785. (defvar org-keyword-time-regexp nil
  3786. "Matches any of the 4 keywords, together with the time stamp.")
  3787. (make-variable-buffer-local 'org-keyword-time-regexp)
  3788. (defvar org-keyword-time-not-clock-regexp nil
  3789. "Matches any of the 3 keywords, together with the time stamp.")
  3790. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3791. (defvar org-maybe-keyword-time-regexp nil
  3792. "Matches a timestamp, possibly preceeded by a keyword.")
  3793. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3794. (defvar org-planning-or-clock-line-re nil
  3795. "Matches a line with planning or clock info.")
  3796. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3797. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3798. rear-nonsticky t mouse-map t fontified t)
  3799. "Properties to remove when a string without properties is wanted.")
  3800. (defsubst org-match-string-no-properties (num &optional string)
  3801. (if (featurep 'xemacs)
  3802. (let ((s (match-string num string)))
  3803. (remove-text-properties 0 (length s) org-rm-props s)
  3804. s)
  3805. (match-string-no-properties num string)))
  3806. (defsubst org-no-properties (s)
  3807. (if (fboundp 'set-text-properties)
  3808. (set-text-properties 0 (length s) nil s)
  3809. (remove-text-properties 0 (length s) org-rm-props s))
  3810. s)
  3811. (defsubst org-get-alist-option (option key)
  3812. (cond ((eq key t) t)
  3813. ((eq option t) t)
  3814. ((assoc key option) (cdr (assoc key option)))
  3815. (t (cdr (assq 'default option)))))
  3816. (defsubst org-inhibit-invisibility ()
  3817. "Modified `buffer-invisibility-spec' for Emacs 21.
  3818. Some ops with invisible text do not work correctly on Emacs 21. For these
  3819. we turn off invisibility temporarily. Use this in a `let' form."
  3820. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3821. (defsubst org-set-local (var value)
  3822. "Make VAR local in current buffer and set it to VALUE."
  3823. (set (make-variable-buffer-local var) value))
  3824. (defsubst org-mode-p ()
  3825. "Check if the current buffer is in Org-mode."
  3826. (eq major-mode 'org-mode))
  3827. (defsubst org-last (list)
  3828. "Return the last element of LIST."
  3829. (car (last list)))
  3830. (defun org-let (list &rest body)
  3831. (eval (cons 'let (cons list body))))
  3832. (put 'org-let 'lisp-indent-function 1)
  3833. (defun org-let2 (list1 list2 &rest body)
  3834. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3835. (put 'org-let2 'lisp-indent-function 2)
  3836. (defconst org-startup-options
  3837. '(("fold" org-startup-folded t)
  3838. ("overview" org-startup-folded t)
  3839. ("nofold" org-startup-folded nil)
  3840. ("showall" org-startup-folded nil)
  3841. ("content" org-startup-folded content)
  3842. ("hidestars" org-hide-leading-stars t)
  3843. ("showstars" org-hide-leading-stars nil)
  3844. ("odd" org-odd-levels-only t)
  3845. ("oddeven" org-odd-levels-only nil)
  3846. ("align" org-startup-align-all-tables t)
  3847. ("noalign" org-startup-align-all-tables nil)
  3848. ("customtime" org-display-custom-times t)
  3849. ("logging" org-log-done t)
  3850. ("logdone" org-log-done t)
  3851. ("nologging" org-log-done nil)
  3852. ("lognotedone" org-log-done done push)
  3853. ("lognotestate" org-log-done state push)
  3854. ("lognoteclock-out" org-log-done clock-out push)
  3855. ("logrepeat" org-log-repeat t)
  3856. ("nologrepeat" org-log-repeat nil)
  3857. ("constcgs" constants-unit-system cgs)
  3858. ("constSI" constants-unit-system SI))
  3859. "Variable associated with STARTUP options for org-mode.
  3860. Each element is a list of three items: The startup options as written
  3861. in the #+STARTUP line, the corresponding variable, and the value to
  3862. set this variable to if the option is found. An optional forth element PUSH
  3863. means to push this value onto the list in the variable.")
  3864. (defun org-set-regexps-and-options ()
  3865. "Precompute regular expressions for current buffer."
  3866. (when (org-mode-p)
  3867. (org-set-local 'org-todo-kwd-alist nil)
  3868. (org-set-local 'org-todo-key-alist nil)
  3869. (org-set-local 'org-todo-key-trigger nil)
  3870. (org-set-local 'org-todo-keywords-1 nil)
  3871. (org-set-local 'org-done-keywords nil)
  3872. (org-set-local 'org-todo-heads nil)
  3873. (org-set-local 'org-todo-sets nil)
  3874. (org-set-local 'org-todo-log-states nil)
  3875. (let ((re (org-make-options-regexp
  3876. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3877. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3878. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3879. (splitre "[ \t]+")
  3880. kwds kws0 kwsa key value cat arch tags const links hw dws
  3881. tail sep kws1 prio props drawers
  3882. ex log)
  3883. (save-excursion
  3884. (save-restriction
  3885. (widen)
  3886. (goto-char (point-min))
  3887. (while (re-search-forward re nil t)
  3888. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3889. (cond
  3890. ((equal key "CATEGORY")
  3891. (if (string-match "[ \t]+$" value)
  3892. (setq value (replace-match "" t t value)))
  3893. (setq cat value))
  3894. ((member key '("SEQ_TODO" "TODO"))
  3895. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3896. ((equal key "TYP_TODO")
  3897. (push (cons 'type (org-split-string value splitre)) kwds))
  3898. ((equal key "TAGS")
  3899. (setq tags (append tags (org-split-string value splitre))))
  3900. ((equal key "COLUMNS")
  3901. (org-set-local 'org-columns-default-format value))
  3902. ((equal key "LINK")
  3903. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3904. (push (cons (match-string 1 value)
  3905. (org-trim (match-string 2 value)))
  3906. links)))
  3907. ((equal key "PRIORITIES")
  3908. (setq prio (org-split-string value " +")))
  3909. ((equal key "PROPERTY")
  3910. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3911. (push (cons (match-string 1 value) (match-string 2 value))
  3912. props)))
  3913. ((equal key "DRAWERS")
  3914. (setq drawers (org-split-string value splitre)))
  3915. ((equal key "CONSTANTS")
  3916. (setq const (append const (org-split-string value splitre))))
  3917. ((equal key "STARTUP")
  3918. (let ((opts (org-split-string value splitre))
  3919. l var val)
  3920. (while (setq l (pop opts))
  3921. (when (setq l (assoc l org-startup-options))
  3922. (setq var (nth 1 l) val (nth 2 l))
  3923. (if (not (nth 3 l))
  3924. (set (make-local-variable var) val)
  3925. (if (not (listp (symbol-value var)))
  3926. (set (make-local-variable var) nil))
  3927. (set (make-local-variable var) (symbol-value var))
  3928. (add-to-list var val))))))
  3929. ((equal key "ARCHIVE")
  3930. (string-match " *$" value)
  3931. (setq arch (replace-match "" t t value))
  3932. (remove-text-properties 0 (length arch)
  3933. '(face t fontified t) arch)))
  3934. )))
  3935. (when cat
  3936. (org-set-local 'org-category (intern cat))
  3937. (push (cons "CATEGORY" cat) props))
  3938. (when prio
  3939. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3940. (setq prio (mapcar 'string-to-char prio))
  3941. (org-set-local 'org-highest-priority (nth 0 prio))
  3942. (org-set-local 'org-lowest-priority (nth 1 prio))
  3943. (org-set-local 'org-default-priority (nth 2 prio)))
  3944. (and props (org-set-local 'org-local-properties (nreverse props)))
  3945. (and drawers (org-set-local 'org-drawers drawers))
  3946. (and arch (org-set-local 'org-archive-location arch))
  3947. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3948. ;; Process the TODO keywords
  3949. (unless kwds
  3950. ;; Use the global values as if they had been given locally.
  3951. (setq kwds (default-value 'org-todo-keywords))
  3952. (if (stringp (car kwds))
  3953. (setq kwds (list (cons org-todo-interpretation
  3954. (default-value 'org-todo-keywords)))))
  3955. (setq kwds (reverse kwds)))
  3956. (setq kwds (nreverse kwds))
  3957. (let (inter kws kw)
  3958. (while (setq kws (pop kwds))
  3959. (setq inter (pop kws) sep (member "|" kws)
  3960. kws0 (delete "|" (copy-sequence kws))
  3961. kwsa nil
  3962. kws1 (mapcar
  3963. (lambda (x)
  3964. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3965. (progn
  3966. (setq kw (match-string 1 x)
  3967. ex (and (match-end 2) (match-string 2 x))
  3968. log (and ex (string-match "@" ex))
  3969. key (and ex (substring ex 0 1)))
  3970. (if (equal key "@") (setq key nil))
  3971. (push (cons kw (and key (string-to-char key))) kwsa)
  3972. (and log (push kw org-todo-log-states))
  3973. kw)
  3974. (error "Invalid TODO keyword %s" x)))
  3975. kws0)
  3976. kwsa (if kwsa (append '((:startgroup))
  3977. (nreverse kwsa)
  3978. '((:endgroup))))
  3979. hw (car kws1)
  3980. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3981. tail (list inter hw (car dws) (org-last dws)))
  3982. (add-to-list 'org-todo-heads hw 'append)
  3983. (push kws1 org-todo-sets)
  3984. (setq org-done-keywords (append org-done-keywords dws nil))
  3985. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3986. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3987. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3988. (setq org-todo-sets (nreverse org-todo-sets)
  3989. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3990. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3991. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3992. ;; Process the constants
  3993. (when const
  3994. (let (e cst)
  3995. (while (setq e (pop const))
  3996. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3997. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3998. (setq org-table-formula-constants-local cst)))
  3999. ;; Process the tags.
  4000. (when tags
  4001. (let (e tgs)
  4002. (while (setq e (pop tags))
  4003. (cond
  4004. ((equal e "{") (push '(:startgroup) tgs))
  4005. ((equal e "}") (push '(:endgroup) tgs))
  4006. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4007. (push (cons (match-string 1 e)
  4008. (string-to-char (match-string 2 e)))
  4009. tgs))
  4010. (t (push (list e) tgs))))
  4011. (org-set-local 'org-tag-alist nil)
  4012. (while (setq e (pop tgs))
  4013. (or (and (stringp (car e))
  4014. (assoc (car e) org-tag-alist))
  4015. (push e org-tag-alist))))))
  4016. ;; Compute the regular expressions and other local variables
  4017. (if (not org-done-keywords)
  4018. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4019. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4020. (length org-scheduled-string)))
  4021. org-drawer-regexp
  4022. (concat "^[ \t]*:\\("
  4023. (mapconcat 'regexp-quote org-drawers "\\|")
  4024. "\\):[ \t]*$")
  4025. org-not-done-keywords
  4026. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4027. org-todo-regexp
  4028. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4029. "\\|") "\\)\\>")
  4030. org-not-done-regexp
  4031. (concat "\\<\\("
  4032. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4033. "\\)\\>")
  4034. org-todo-line-regexp
  4035. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4036. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4037. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4038. org-complex-heading-regexp
  4039. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4040. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4041. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4042. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4043. org-nl-done-regexp
  4044. (concat "\n\\*+[ \t]+"
  4045. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4046. "\\)" "\\>")
  4047. org-todo-line-tags-regexp
  4048. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4049. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4050. (org-re
  4051. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4052. org-looking-at-done-regexp
  4053. (concat "^" "\\(?:"
  4054. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4055. "\\>")
  4056. org-deadline-regexp (concat "\\<" org-deadline-string)
  4057. org-deadline-time-regexp
  4058. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4059. org-deadline-line-regexp
  4060. (concat "\\<\\(" org-deadline-string "\\).*")
  4061. org-scheduled-regexp
  4062. (concat "\\<" org-scheduled-string)
  4063. org-scheduled-time-regexp
  4064. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4065. org-closed-time-regexp
  4066. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4067. org-keyword-time-regexp
  4068. (concat "\\<\\(" org-scheduled-string
  4069. "\\|" org-deadline-string
  4070. "\\|" org-closed-string
  4071. "\\|" org-clock-string "\\)"
  4072. " *[[<]\\([^]>]+\\)[]>]")
  4073. org-keyword-time-not-clock-regexp
  4074. (concat "\\<\\(" org-scheduled-string
  4075. "\\|" org-deadline-string
  4076. "\\|" org-closed-string
  4077. "\\)"
  4078. " *[[<]\\([^]>]+\\)[]>]")
  4079. org-maybe-keyword-time-regexp
  4080. (concat "\\(\\<\\(" org-scheduled-string
  4081. "\\|" org-deadline-string
  4082. "\\|" org-closed-string
  4083. "\\|" org-clock-string "\\)\\)?"
  4084. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4085. org-planning-or-clock-line-re
  4086. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4087. "\\|" org-deadline-string
  4088. "\\|" org-closed-string "\\|" org-clock-string
  4089. "\\)\\>\\)")
  4090. )
  4091. (org-compute-latex-and-specials-regexp)
  4092. (org-set-font-lock-defaults)))
  4093. (defun org-remove-keyword-keys (list)
  4094. (mapcar (lambda (x)
  4095. (if (string-match "(..?)$" x)
  4096. (substring x 0 (match-beginning 0))
  4097. x))
  4098. list))
  4099. ;; FIXME: this could be done much better, using second characters etc.
  4100. (defun org-assign-fast-keys (alist)
  4101. "Assign fast keys to a keyword-key alist.
  4102. Respect keys that are already there."
  4103. (let (new e k c c1 c2 (char ?a))
  4104. (while (setq e (pop alist))
  4105. (cond
  4106. ((equal e '(:startgroup)) (push e new))
  4107. ((equal e '(:endgroup)) (push e new))
  4108. (t
  4109. (setq k (car e) c2 nil)
  4110. (if (cdr e)
  4111. (setq c (cdr e))
  4112. ;; automatically assign a character.
  4113. (setq c1 (string-to-char
  4114. (downcase (substring
  4115. k (if (= (string-to-char k) ?@) 1 0)))))
  4116. (if (or (rassoc c1 new) (rassoc c1 alist))
  4117. (while (or (rassoc char new) (rassoc char alist))
  4118. (setq char (1+ char)))
  4119. (setq c2 c1))
  4120. (setq c (or c2 char)))
  4121. (push (cons k c) new))))
  4122. (nreverse new)))
  4123. ;;; Some variables ujsed in various places
  4124. (defvar org-window-configuration nil
  4125. "Used in various places to store a window configuration.")
  4126. (defvar org-finish-function nil
  4127. "Function to be called when `C-c C-c' is used.
  4128. This is for getting out of special buffers like remember.")
  4129. ;; FIXME: Occasionally check by commenting these, to make sure
  4130. ;; no other functions uses these, forgetting to let-bind them.
  4131. (defvar entry)
  4132. (defvar state)
  4133. (defvar last-state)
  4134. (defvar date)
  4135. (defvar description)
  4136. ;; Defined somewhere in this file, but used before definition.
  4137. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4138. (defvar org-agenda-buffer-name)
  4139. (defvar org-agenda-undo-list)
  4140. (defvar org-agenda-pending-undo-list)
  4141. (defvar org-agenda-overriding-header)
  4142. (defvar orgtbl-mode)
  4143. (defvar org-html-entities)
  4144. (defvar org-struct-menu)
  4145. (defvar org-org-menu)
  4146. (defvar org-tbl-menu)
  4147. (defvar org-agenda-keymap)
  4148. ;;;; Emacs/XEmacs compatibility
  4149. ;; Overlay compatibility functions
  4150. (defun org-make-overlay (beg end &optional buffer)
  4151. (if (featurep 'xemacs)
  4152. (make-extent beg end buffer)
  4153. (make-overlay beg end buffer)))
  4154. (defun org-delete-overlay (ovl)
  4155. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4156. (defun org-detach-overlay (ovl)
  4157. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4158. (defun org-move-overlay (ovl beg end &optional buffer)
  4159. (if (featurep 'xemacs)
  4160. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4161. (move-overlay ovl beg end buffer)))
  4162. (defun org-overlay-put (ovl prop value)
  4163. (if (featurep 'xemacs)
  4164. (set-extent-property ovl prop value)
  4165. (overlay-put ovl prop value)))
  4166. (defun org-overlay-display (ovl text &optional face evap)
  4167. "Make overlay OVL display TEXT with face FACE."
  4168. (if (featurep 'xemacs)
  4169. (let ((gl (make-glyph text)))
  4170. (and face (set-glyph-face gl face))
  4171. (set-extent-property ovl 'invisible t)
  4172. (set-extent-property ovl 'end-glyph gl))
  4173. (overlay-put ovl 'display text)
  4174. (if face (overlay-put ovl 'face face))
  4175. (if evap (overlay-put ovl 'evaporate t))))
  4176. (defun org-overlay-before-string (ovl text &optional face evap)
  4177. "Make overlay OVL display TEXT with face FACE."
  4178. (if (featurep 'xemacs)
  4179. (let ((gl (make-glyph text)))
  4180. (and face (set-glyph-face gl face))
  4181. (set-extent-property ovl 'begin-glyph gl))
  4182. (if face (org-add-props text nil 'face face))
  4183. (overlay-put ovl 'before-string text)
  4184. (if evap (overlay-put ovl 'evaporate t))))
  4185. (defun org-overlay-get (ovl prop)
  4186. (if (featurep 'xemacs)
  4187. (extent-property ovl prop)
  4188. (overlay-get ovl prop)))
  4189. (defun org-overlays-at (pos)
  4190. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4191. (defun org-overlays-in (&optional start end)
  4192. (if (featurep 'xemacs)
  4193. (extent-list nil start end)
  4194. (overlays-in start end)))
  4195. (defun org-overlay-start (o)
  4196. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4197. (defun org-overlay-end (o)
  4198. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4199. (defun org-find-overlays (prop &optional pos delete)
  4200. "Find all overlays specifying PROP at POS or point.
  4201. If DELETE is non-nil, delete all those overlays."
  4202. (let ((overlays (org-overlays-at (or pos (point))))
  4203. ov found)
  4204. (while (setq ov (pop overlays))
  4205. (if (org-overlay-get ov prop)
  4206. (if delete (org-delete-overlay ov) (push ov found))))
  4207. found))
  4208. ;; Region compatibility
  4209. (defun org-add-hook (hook function &optional append local)
  4210. "Add-hook, compatible with both Emacsen."
  4211. (if (and local (featurep 'xemacs))
  4212. (add-local-hook hook function append)
  4213. (add-hook hook function append local)))
  4214. (defvar org-ignore-region nil
  4215. "To temporarily disable the active region.")
  4216. (defun org-region-active-p ()
  4217. "Is `transient-mark-mode' on and the region active?
  4218. Works on both Emacs and XEmacs."
  4219. (if org-ignore-region
  4220. nil
  4221. (if (featurep 'xemacs)
  4222. (and zmacs-regions (region-active-p))
  4223. (if (fboundp 'use-region-p)
  4224. (use-region-p)
  4225. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4226. ;; Invisibility compatibility
  4227. (defun org-add-to-invisibility-spec (arg)
  4228. "Add elements to `buffer-invisibility-spec'.
  4229. See documentation for `buffer-invisibility-spec' for the kind of elements
  4230. that can be added."
  4231. (cond
  4232. ((fboundp 'add-to-invisibility-spec)
  4233. (add-to-invisibility-spec arg))
  4234. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4235. (setq buffer-invisibility-spec (list arg)))
  4236. (t
  4237. (setq buffer-invisibility-spec
  4238. (cons arg buffer-invisibility-spec)))))
  4239. (defun org-remove-from-invisibility-spec (arg)
  4240. "Remove elements from `buffer-invisibility-spec'."
  4241. (if (fboundp 'remove-from-invisibility-spec)
  4242. (remove-from-invisibility-spec arg)
  4243. (if (consp buffer-invisibility-spec)
  4244. (setq buffer-invisibility-spec
  4245. (delete arg buffer-invisibility-spec)))))
  4246. (defun org-in-invisibility-spec-p (arg)
  4247. "Is ARG a member of `buffer-invisibility-spec'?"
  4248. (if (consp buffer-invisibility-spec)
  4249. (member arg buffer-invisibility-spec)
  4250. nil))
  4251. ;;;; Define the Org-mode
  4252. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4253. (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."))
  4254. ;; We use a before-change function to check if a table might need
  4255. ;; an update.
  4256. (defvar org-table-may-need-update t
  4257. "Indicates that a table might need an update.
  4258. This variable is set by `org-before-change-function'.
  4259. `org-table-align' sets it back to nil.")
  4260. (defvar org-mode-map)
  4261. (defvar org-mode-hook nil)
  4262. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4263. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4264. (defvar org-table-buffer-is-an nil)
  4265. (defconst org-outline-regexp "\\*+ ")
  4266. ;;;###autoload
  4267. (define-derived-mode org-mode outline-mode "Org"
  4268. "Outline-based notes management and organizer, alias
  4269. \"Carsten's outline-mode for keeping track of everything.\"
  4270. Org-mode develops organizational tasks around a NOTES file which
  4271. contains information about projects as plain text. Org-mode is
  4272. implemented on top of outline-mode, which is ideal to keep the content
  4273. of large files well structured. It supports ToDo items, deadlines and
  4274. time stamps, which magically appear in the diary listing of the Emacs
  4275. calendar. Tables are easily created with a built-in table editor.
  4276. Plain text URL-like links connect to websites, emails (VM), Usenet
  4277. messages (Gnus), BBDB entries, and any files related to the project.
  4278. For printing and sharing of notes, an Org-mode file (or a part of it)
  4279. can be exported as a structured ASCII or HTML file.
  4280. The following commands are available:
  4281. \\{org-mode-map}"
  4282. ;; Get rid of Outline menus, they are not needed
  4283. ;; Need to do this here because define-derived-mode sets up
  4284. ;; the keymap so late. Still, it is a waste to call this each time
  4285. ;; we switch another buffer into org-mode.
  4286. (if (featurep 'xemacs)
  4287. (when (boundp 'outline-mode-menu-heading)
  4288. ;; Assume this is Greg's port, it used easymenu
  4289. (easy-menu-remove outline-mode-menu-heading)
  4290. (easy-menu-remove outline-mode-menu-show)
  4291. (easy-menu-remove outline-mode-menu-hide))
  4292. (define-key org-mode-map [menu-bar headings] 'undefined)
  4293. (define-key org-mode-map [menu-bar hide] 'undefined)
  4294. (define-key org-mode-map [menu-bar show] 'undefined))
  4295. (easy-menu-add org-org-menu)
  4296. (easy-menu-add org-tbl-menu)
  4297. (org-install-agenda-files-menu)
  4298. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4299. (org-add-to-invisibility-spec '(org-cwidth))
  4300. (when (featurep 'xemacs)
  4301. (org-set-local 'line-move-ignore-invisible t))
  4302. (org-set-local 'outline-regexp org-outline-regexp)
  4303. (org-set-local 'outline-level 'org-outline-level)
  4304. (when (and org-ellipsis
  4305. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4306. (fboundp 'make-glyph-code))
  4307. (unless org-display-table
  4308. (setq org-display-table (make-display-table)))
  4309. (set-display-table-slot
  4310. org-display-table 4
  4311. (vconcat (mapcar
  4312. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4313. org-ellipsis)))
  4314. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4315. (setq buffer-display-table org-display-table))
  4316. (org-set-regexps-and-options)
  4317. ;; Calc embedded
  4318. (org-set-local 'calc-embedded-open-mode "# ")
  4319. (modify-syntax-entry ?# "<")
  4320. (modify-syntax-entry ?@ "w")
  4321. (if org-startup-truncated (setq truncate-lines t))
  4322. (org-set-local 'font-lock-unfontify-region-function
  4323. 'org-unfontify-region)
  4324. ;; Activate before-change-function
  4325. (org-set-local 'org-table-may-need-update t)
  4326. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4327. 'local)
  4328. ;; Check for running clock before killing a buffer
  4329. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4330. ;; Paragraphs and auto-filling
  4331. (org-set-autofill-regexps)
  4332. (setq indent-line-function 'org-indent-line-function)
  4333. (org-update-radio-target-regexp)
  4334. ;; Comment characters
  4335. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4336. (org-set-local 'comment-padding " ")
  4337. ;; Align options lines
  4338. (org-set-local
  4339. 'align-mode-rules-list
  4340. '((org-in-buffer-settings
  4341. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4342. (modes . '(org-mode)))))
  4343. ;; Imenu
  4344. (org-set-local 'imenu-create-index-function
  4345. 'org-imenu-get-tree)
  4346. ;; Make isearch reveal context
  4347. (if (or (featurep 'xemacs)
  4348. (not (boundp 'outline-isearch-open-invisible-function)))
  4349. ;; Emacs 21 and XEmacs make use of the hook
  4350. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4351. ;; Emacs 22 deals with this through a special variable
  4352. (org-set-local 'outline-isearch-open-invisible-function
  4353. (lambda (&rest ignore) (org-show-context 'isearch))))
  4354. ;; If empty file that did not turn on org-mode automatically, make it to.
  4355. (if (and org-insert-mode-line-in-empty-file
  4356. (interactive-p)
  4357. (= (point-min) (point-max)))
  4358. (insert "# -*- mode: org -*-\n\n"))
  4359. (unless org-inhibit-startup
  4360. (when org-startup-align-all-tables
  4361. (let ((bmp (buffer-modified-p)))
  4362. (org-table-map-tables 'org-table-align)
  4363. (set-buffer-modified-p bmp)))
  4364. (org-cycle-hide-drawers 'all)
  4365. (cond
  4366. ((eq org-startup-folded t)
  4367. (org-cycle '(4)))
  4368. ((eq org-startup-folded 'content)
  4369. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4370. (org-cycle '(4)) (org-cycle '(4)))))))
  4371. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4372. (defsubst org-call-with-arg (command arg)
  4373. "Call COMMAND interactively, but pretend prefix are was ARG."
  4374. (let ((current-prefix-arg arg)) (call-interactively command)))
  4375. (defsubst org-current-line (&optional pos)
  4376. (save-excursion
  4377. (and pos (goto-char pos))
  4378. ;; works also in narrowed buffer, because we start at 1, not point-min
  4379. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4380. (defun org-current-time ()
  4381. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4382. (if (> org-time-stamp-rounding-minutes 0)
  4383. (let ((r org-time-stamp-rounding-minutes)
  4384. (time (decode-time)))
  4385. (apply 'encode-time
  4386. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4387. (nthcdr 2 time))))
  4388. (current-time)))
  4389. (defun org-add-props (string plist &rest props)
  4390. "Add text properties to entire string, from beginning to end.
  4391. PLIST may be a list of properties, PROPS are individual properties and values
  4392. that will be added to PLIST. Returns the string that was modified."
  4393. (add-text-properties
  4394. 0 (length string) (if props (append plist props) plist) string)
  4395. string)
  4396. (put 'org-add-props 'lisp-indent-function 2)
  4397. ;;;; Font-Lock stuff, including the activators
  4398. (defvar org-mouse-map (make-sparse-keymap))
  4399. (org-defkey org-mouse-map
  4400. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4401. (org-defkey org-mouse-map
  4402. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4403. (when org-mouse-1-follows-link
  4404. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4405. (when org-tab-follows-link
  4406. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4407. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4408. (when org-return-follows-link
  4409. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4410. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4411. (require 'font-lock)
  4412. (defconst org-non-link-chars "]\t\n\r<>")
  4413. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4414. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4415. (defvar org-link-re-with-space nil
  4416. "Matches a link with spaces, optional angular brackets around it.")
  4417. (defvar org-link-re-with-space2 nil
  4418. "Matches a link with spaces, optional angular brackets around it.")
  4419. (defvar org-angle-link-re nil
  4420. "Matches link with angular brackets, spaces are allowed.")
  4421. (defvar org-plain-link-re nil
  4422. "Matches plain link, without spaces.")
  4423. (defvar org-bracket-link-regexp nil
  4424. "Matches a link in double brackets.")
  4425. (defvar org-bracket-link-analytic-regexp nil
  4426. "Regular expression used to analyze links.
  4427. Here is what the match groups contain after a match:
  4428. 1: http:
  4429. 2: http
  4430. 3: path
  4431. 4: [desc]
  4432. 5: desc")
  4433. (defvar org-any-link-re nil
  4434. "Regular expression matching any link.")
  4435. (defun org-make-link-regexps ()
  4436. "Update the link regular expressions.
  4437. This should be called after the variable `org-link-types' has changed."
  4438. (setq org-link-re-with-space
  4439. (concat
  4440. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4441. "\\([^" org-non-link-chars " ]"
  4442. "[^" org-non-link-chars "]*"
  4443. "[^" org-non-link-chars " ]\\)>?")
  4444. org-link-re-with-space2
  4445. (concat
  4446. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4447. "\\([^" org-non-link-chars " ]"
  4448. "[^]\t\n\r]*"
  4449. "[^" org-non-link-chars " ]\\)>?")
  4450. org-angle-link-re
  4451. (concat
  4452. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4453. "\\([^" org-non-link-chars " ]"
  4454. "[^" org-non-link-chars "]*"
  4455. "\\)>")
  4456. org-plain-link-re
  4457. (concat
  4458. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4459. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4460. org-bracket-link-regexp
  4461. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4462. org-bracket-link-analytic-regexp
  4463. (concat
  4464. "\\[\\["
  4465. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4466. "\\([^]]+\\)"
  4467. "\\]"
  4468. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4469. "\\]")
  4470. org-any-link-re
  4471. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4472. org-angle-link-re "\\)\\|\\("
  4473. org-plain-link-re "\\)")))
  4474. (org-make-link-regexps)
  4475. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4476. "Regular expression for fast time stamp matching.")
  4477. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4478. "Regular expression for fast time stamp matching.")
  4479. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4480. "Regular expression matching time strings for analysis.
  4481. This one does not require the space after the date.")
  4482. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4483. "Regular expression matching time strings for analysis.")
  4484. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4485. "Regular expression matching time stamps, with groups.")
  4486. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4487. "Regular expression matching time stamps (also [..]), with groups.")
  4488. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4489. "Regular expression matching a time stamp range.")
  4490. (defconst org-tr-regexp-both
  4491. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4492. "Regular expression matching a time stamp range.")
  4493. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4494. org-ts-regexp "\\)?")
  4495. "Regular expression matching a time stamp or time stamp range.")
  4496. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4497. org-ts-regexp-both "\\)?")
  4498. "Regular expression matching a time stamp or time stamp range.
  4499. The time stamps may be either active or inactive.")
  4500. (defvar org-emph-face nil)
  4501. (defun org-do-emphasis-faces (limit)
  4502. "Run through the buffer and add overlays to links."
  4503. (let (rtn)
  4504. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4505. (if (not (= (char-after (match-beginning 3))
  4506. (char-after (match-beginning 4))))
  4507. (progn
  4508. (setq rtn t)
  4509. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4510. 'face
  4511. (nth 1 (assoc (match-string 3)
  4512. org-emphasis-alist)))
  4513. (add-text-properties (match-beginning 2) (match-end 2)
  4514. '(font-lock-multiline t))
  4515. (when org-hide-emphasis-markers
  4516. (add-text-properties (match-end 4) (match-beginning 5)
  4517. '(invisible org-link))
  4518. (add-text-properties (match-beginning 3) (match-end 3)
  4519. '(invisible org-link)))))
  4520. (backward-char 1))
  4521. rtn))
  4522. (defun org-emphasize (&optional char)
  4523. "Insert or change an emphasis, i.e. a font like bold or italic.
  4524. If there is an active region, change that region to a new emphasis.
  4525. If there is no region, just insert the marker characters and position
  4526. the cursor between them.
  4527. CHAR should be either the marker character, or the first character of the
  4528. HTML tag associated with that emphasis. If CHAR is a space, the means
  4529. to remove the emphasis of the selected region.
  4530. If char is not given (for example in an interactive call) it
  4531. will be prompted for."
  4532. (interactive)
  4533. (let ((eal org-emphasis-alist) e det
  4534. (erc org-emphasis-regexp-components)
  4535. (prompt "")
  4536. (string "") beg end move tag c s)
  4537. (if (org-region-active-p)
  4538. (setq beg (region-beginning) end (region-end)
  4539. string (buffer-substring beg end))
  4540. (setq move t))
  4541. (while (setq e (pop eal))
  4542. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4543. c (aref tag 0))
  4544. (push (cons c (string-to-char (car e))) det)
  4545. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4546. (substring tag 1)))))
  4547. (unless char
  4548. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4549. (setq char (read-char-exclusive)))
  4550. (setq char (or (cdr (assoc char det)) char))
  4551. (if (equal char ?\ )
  4552. (setq s "" move nil)
  4553. (unless (assoc (char-to-string char) org-emphasis-alist)
  4554. (error "No such emphasis marker: \"%c\"" char))
  4555. (setq s (char-to-string char)))
  4556. (while (and (> (length string) 1)
  4557. (equal (substring string 0 1) (substring string -1))
  4558. (assoc (substring string 0 1) org-emphasis-alist))
  4559. (setq string (substring string 1 -1)))
  4560. (setq string (concat s string s))
  4561. (if beg (delete-region beg end))
  4562. (unless (or (bolp)
  4563. (string-match (concat "[" (nth 0 erc) "\n]")
  4564. (char-to-string (char-before (point)))))
  4565. (insert " "))
  4566. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4567. (char-to-string (char-after (point))))
  4568. (insert " ") (backward-char 1))
  4569. (insert string)
  4570. (and move (backward-char 1))))
  4571. (defconst org-nonsticky-props
  4572. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4573. (defun org-activate-plain-links (limit)
  4574. "Run through the buffer and add overlays to links."
  4575. (catch 'exit
  4576. (let (f)
  4577. (while (re-search-forward org-plain-link-re limit t)
  4578. (setq f (get-text-property (match-beginning 0) 'face))
  4579. (if (or (eq f 'org-tag)
  4580. (and (listp f) (memq 'org-tag f)))
  4581. nil
  4582. (add-text-properties (match-beginning 0) (match-end 0)
  4583. (list 'mouse-face 'highlight
  4584. 'rear-nonsticky org-nonsticky-props
  4585. 'keymap org-mouse-map
  4586. ))
  4587. (throw 'exit t))))))
  4588. (defun org-activate-code (limit)
  4589. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4590. (unless (get-text-property (match-beginning 1) 'face)
  4591. (remove-text-properties (match-beginning 0) (match-end 0)
  4592. '(display t invisible t intangible t))
  4593. t)))
  4594. (defun org-activate-angle-links (limit)
  4595. "Run through the buffer and add overlays to links."
  4596. (if (re-search-forward org-angle-link-re limit t)
  4597. (progn
  4598. (add-text-properties (match-beginning 0) (match-end 0)
  4599. (list 'mouse-face 'highlight
  4600. 'rear-nonsticky org-nonsticky-props
  4601. 'keymap org-mouse-map
  4602. ))
  4603. t)))
  4604. (defmacro org-maybe-intangible (props)
  4605. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4606. In emacs 21, invisible text is not avoided by the command loop, so the
  4607. intangible property is needed to make sure point skips this text.
  4608. In Emacs 22, this is not necessary. The intangible text property has
  4609. led to problems with flyspell. These problems are fixed in flyspell.el,
  4610. but we still avoid setting the property in Emacs 22 and later.
  4611. We use a macro so that the test can happen at compilation time."
  4612. (if (< emacs-major-version 22)
  4613. `(append '(intangible t) ,props)
  4614. props))
  4615. (defun org-activate-bracket-links (limit)
  4616. "Run through the buffer and add overlays to bracketed links."
  4617. (if (re-search-forward org-bracket-link-regexp limit t)
  4618. (let* ((help (concat "LINK: "
  4619. (org-match-string-no-properties 1)))
  4620. ;; FIXME: above we should remove the escapes.
  4621. ;; but that requires another match, protecting match data,
  4622. ;; a lot of overhead for font-lock.
  4623. (ip (org-maybe-intangible
  4624. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4625. 'keymap org-mouse-map 'mouse-face 'highlight
  4626. 'font-lock-multiline t 'help-echo help)))
  4627. (vp (list 'rear-nonsticky org-nonsticky-props
  4628. 'keymap org-mouse-map 'mouse-face 'highlight
  4629. ' font-lock-multiline t 'help-echo help)))
  4630. ;; We need to remove the invisible property here. Table narrowing
  4631. ;; may have made some of this invisible.
  4632. (remove-text-properties (match-beginning 0) (match-end 0)
  4633. '(invisible nil))
  4634. (if (match-end 3)
  4635. (progn
  4636. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4637. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4638. (add-text-properties (match-end 3) (match-end 0) ip))
  4639. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4640. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4641. (add-text-properties (match-end 1) (match-end 0) ip))
  4642. t)))
  4643. (defun org-activate-dates (limit)
  4644. "Run through the buffer and add overlays to dates."
  4645. (if (re-search-forward org-tsr-regexp-both limit t)
  4646. (progn
  4647. (add-text-properties (match-beginning 0) (match-end 0)
  4648. (list 'mouse-face 'highlight
  4649. 'rear-nonsticky org-nonsticky-props
  4650. 'keymap org-mouse-map))
  4651. (when org-display-custom-times
  4652. (if (match-end 3)
  4653. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4654. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4655. t)))
  4656. (defvar org-target-link-regexp nil
  4657. "Regular expression matching radio targets in plain text.")
  4658. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4659. "Regular expression matching a link target.")
  4660. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4661. "Regular expression matching a radio target.")
  4662. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4663. "Regular expression matching any target.")
  4664. (defun org-activate-target-links (limit)
  4665. "Run through the buffer and add overlays to target matches."
  4666. (when org-target-link-regexp
  4667. (let ((case-fold-search t))
  4668. (if (re-search-forward org-target-link-regexp limit t)
  4669. (progn
  4670. (add-text-properties (match-beginning 0) (match-end 0)
  4671. (list 'mouse-face 'highlight
  4672. 'rear-nonsticky org-nonsticky-props
  4673. 'keymap org-mouse-map
  4674. 'help-echo "Radio target link"
  4675. 'org-linked-text t))
  4676. t)))))
  4677. (defun org-update-radio-target-regexp ()
  4678. "Find all radio targets in this file and update the regular expression."
  4679. (interactive)
  4680. (when (memq 'radio org-activate-links)
  4681. (setq org-target-link-regexp
  4682. (org-make-target-link-regexp (org-all-targets 'radio)))
  4683. (org-restart-font-lock)))
  4684. (defun org-hide-wide-columns (limit)
  4685. (let (s e)
  4686. (setq s (text-property-any (point) (or limit (point-max))
  4687. 'org-cwidth t))
  4688. (when s
  4689. (setq e (next-single-property-change s 'org-cwidth))
  4690. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4691. (goto-char e)
  4692. t)))
  4693. (defvar org-latex-and-specials-regexp nil
  4694. "Regular expression for highlighting export special stuff.")
  4695. (defvar org-match-substring-regexp)
  4696. (defvar org-match-substring-with-braces-regexp)
  4697. (defvar org-export-html-special-string-regexps)
  4698. (defun org-compute-latex-and-specials-regexp ()
  4699. "Compute regular expression for stuff treated specially by exporters."
  4700. (if (not org-highlight-latex-fragments-and-specials)
  4701. (org-set-local 'org-latex-and-specials-regexp nil)
  4702. (let*
  4703. ((matchers (plist-get org-format-latex-options :matchers))
  4704. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4705. org-latex-regexps)))
  4706. (options (org-combine-plists (org-default-export-plist)
  4707. (org-infile-export-plist)))
  4708. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4709. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4710. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4711. (org-export-html-expand (plist-get options :expand-quoted-html))
  4712. (org-export-with-special-strings (plist-get options :special-strings))
  4713. (re-sub
  4714. (cond
  4715. ((equal org-export-with-sub-superscripts '{})
  4716. (list org-match-substring-with-braces-regexp))
  4717. (org-export-with-sub-superscripts
  4718. (list org-match-substring-regexp))
  4719. (t nil)))
  4720. (re-latex
  4721. (if org-export-with-LaTeX-fragments
  4722. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4723. (re-macros
  4724. (if org-export-with-TeX-macros
  4725. (list (concat "\\\\"
  4726. (regexp-opt
  4727. (append (mapcar 'car org-html-entities)
  4728. (if (boundp 'org-latex-entities)
  4729. org-latex-entities nil))
  4730. 'words))) ; FIXME
  4731. ))
  4732. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4733. (re-special (if org-export-with-special-strings
  4734. (mapcar (lambda (x) (car x))
  4735. org-export-html-special-string-regexps)))
  4736. (re-rest
  4737. (delq nil
  4738. (list
  4739. (if org-export-html-expand "@<[^>\n]+>")
  4740. ))))
  4741. (org-set-local
  4742. 'org-latex-and-specials-regexp
  4743. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4744. re-rest) "\\|")))))
  4745. (defface org-latex-and-export-specials
  4746. (let ((font (cond ((assq :inherit custom-face-attributes)
  4747. '(:inherit underline))
  4748. (t '(:underline t)))))
  4749. `((((class grayscale) (background light))
  4750. (:foreground "DimGray" ,@font))
  4751. (((class grayscale) (background dark))
  4752. (:foreground "LightGray" ,@font))
  4753. (((class color) (background light))
  4754. (:foreground "SaddleBrown"))
  4755. (((class color) (background dark))
  4756. (:foreground "burlywood"))
  4757. (t (,@font))))
  4758. "Face used to highlight math latex and other special exporter stuff."
  4759. :group 'org-faces)
  4760. (defun org-do-latex-and-special-faces (limit)
  4761. "Run through the buffer and add overlays to links."
  4762. (when org-latex-and-specials-regexp
  4763. (let (rtn d)
  4764. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4765. limit t))
  4766. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4767. 'face))
  4768. '(org-code org-verbatim underline)))
  4769. (progn
  4770. (setq rtn t
  4771. d (cond ((member (char-after (1+ (match-beginning 0)))
  4772. '(?_ ?^)) 1)
  4773. (t 0)))
  4774. (font-lock-prepend-text-property
  4775. (+ d (match-beginning 0)) (match-end 0)
  4776. 'face 'org-latex-and-export-specials)
  4777. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4778. '(font-lock-multiline t)))))
  4779. rtn)))
  4780. (defun org-restart-font-lock ()
  4781. "Restart font-lock-mode, to force refontification."
  4782. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4783. (font-lock-mode -1)
  4784. (font-lock-mode 1)))
  4785. (defun org-all-targets (&optional radio)
  4786. "Return a list of all targets in this file.
  4787. With optional argument RADIO, only find radio targets."
  4788. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4789. rtn)
  4790. (save-excursion
  4791. (goto-char (point-min))
  4792. (while (re-search-forward re nil t)
  4793. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4794. rtn)))
  4795. (defun org-make-target-link-regexp (targets)
  4796. "Make regular expression matching all strings in TARGETS.
  4797. The regular expression finds the targets also if there is a line break
  4798. between words."
  4799. (and targets
  4800. (concat
  4801. "\\<\\("
  4802. (mapconcat
  4803. (lambda (x)
  4804. (while (string-match " +" x)
  4805. (setq x (replace-match "\\s-+" t t x)))
  4806. x)
  4807. targets
  4808. "\\|")
  4809. "\\)\\>")))
  4810. (defun org-activate-tags (limit)
  4811. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4812. (progn
  4813. (add-text-properties (match-beginning 1) (match-end 1)
  4814. (list 'mouse-face 'highlight
  4815. 'rear-nonsticky org-nonsticky-props
  4816. 'keymap org-mouse-map))
  4817. t)))
  4818. (defun org-outline-level ()
  4819. (save-excursion
  4820. (looking-at outline-regexp)
  4821. (if (match-beginning 1)
  4822. (+ (org-get-string-indentation (match-string 1)) 1000)
  4823. (1- (- (match-end 0) (match-beginning 0))))))
  4824. (defvar org-font-lock-keywords nil)
  4825. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4826. "Regular expression matching a property line.")
  4827. (defun org-set-font-lock-defaults ()
  4828. (let* ((em org-fontify-emphasized-text)
  4829. (lk org-activate-links)
  4830. (org-font-lock-extra-keywords
  4831. (list
  4832. ;; Headlines
  4833. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4834. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4835. ;; Table lines
  4836. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4837. (1 'org-table t))
  4838. ;; Table internals
  4839. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4840. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4841. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4842. ;; Drawers
  4843. (list org-drawer-regexp '(0 'org-special-keyword t))
  4844. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4845. ;; Properties
  4846. (list org-property-re
  4847. '(1 'org-special-keyword t)
  4848. '(3 'org-property-value t))
  4849. (if org-format-transports-properties-p
  4850. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4851. ;; Links
  4852. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4853. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4854. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4855. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4856. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4857. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4858. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4859. '(org-hide-wide-columns (0 nil append))
  4860. ;; TODO lines
  4861. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4862. '(1 (org-get-todo-face 1) t))
  4863. ;; DONE
  4864. (if org-fontify-done-headline
  4865. (list (concat "^[*]+ +\\<\\("
  4866. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4867. "\\)\\(.*\\)")
  4868. '(2 'org-headline-done t))
  4869. nil)
  4870. ;; Priorities
  4871. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4872. ;; Special keywords
  4873. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4874. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4875. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4876. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4877. ;; Emphasis
  4878. (if em
  4879. (if (featurep 'xemacs)
  4880. '(org-do-emphasis-faces (0 nil append))
  4881. '(org-do-emphasis-faces)))
  4882. ;; Checkboxes
  4883. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4884. 2 'bold prepend)
  4885. (if org-provide-checkbox-statistics
  4886. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4887. (0 (org-get-checkbox-statistics-face) t)))
  4888. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4889. '(1 'org-archived prepend))
  4890. ;; Specials
  4891. '(org-do-latex-and-special-faces)
  4892. ;; Code
  4893. '(org-activate-code (1 'org-code t))
  4894. ;; COMMENT
  4895. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4896. "\\|" org-quote-string "\\)\\>")
  4897. '(1 'org-special-keyword t))
  4898. '("^#.*" (0 'font-lock-comment-face t))
  4899. )))
  4900. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4901. ;; Now set the full font-lock-keywords
  4902. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4903. (org-set-local 'font-lock-defaults
  4904. '(org-font-lock-keywords t nil nil backward-paragraph))
  4905. (kill-local-variable 'font-lock-keywords) nil))
  4906. (defvar org-m nil)
  4907. (defvar org-l nil)
  4908. (defvar org-f nil)
  4909. (defun org-get-level-face (n)
  4910. "Get the right face for match N in font-lock matching of healdines."
  4911. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4912. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4913. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4914. (cond
  4915. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4916. ((eq n 2) org-f)
  4917. (t (if org-level-color-stars-only nil org-f))))
  4918. (defun org-get-todo-face (kwd)
  4919. "Get the right face for a TODO keyword KWD.
  4920. If KWD is a number, get the corresponding match group."
  4921. (if (numberp kwd) (setq kwd (match-string kwd)))
  4922. (or (cdr (assoc kwd org-todo-keyword-faces))
  4923. (and (member kwd org-done-keywords) 'org-done)
  4924. 'org-todo))
  4925. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4926. "Remove fontification and activation overlays from links."
  4927. (font-lock-default-unfontify-region beg end)
  4928. (let* ((buffer-undo-list t)
  4929. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4930. (inhibit-modification-hooks t)
  4931. deactivate-mark buffer-file-name buffer-file-truename)
  4932. (remove-text-properties beg end
  4933. '(mouse-face t keymap t org-linked-text t
  4934. invisible t intangible t))))
  4935. ;;;; Visibility cycling, including org-goto and indirect buffer
  4936. ;;; Cycling
  4937. (defvar org-cycle-global-status nil)
  4938. (make-variable-buffer-local 'org-cycle-global-status)
  4939. (defvar org-cycle-subtree-status nil)
  4940. (make-variable-buffer-local 'org-cycle-subtree-status)
  4941. ;;;###autoload
  4942. (defun org-cycle (&optional arg)
  4943. "Visibility cycling for Org-mode.
  4944. - When this function is called with a prefix argument, rotate the entire
  4945. buffer through 3 states (global cycling)
  4946. 1. OVERVIEW: Show only top-level headlines.
  4947. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4948. 3. SHOW ALL: Show everything.
  4949. - When point is at the beginning of a headline, rotate the subtree started
  4950. by this line through 3 different states (local cycling)
  4951. 1. FOLDED: Only the main headline is shown.
  4952. 2. CHILDREN: The main headline and the direct children are shown.
  4953. From this state, you can move to one of the children
  4954. and zoom in further.
  4955. 3. SUBTREE: Show the entire subtree, including body text.
  4956. - When there is a numeric prefix, go up to a heading with level ARG, do
  4957. a `show-subtree' and return to the previous cursor position. If ARG
  4958. is negative, go up that many levels.
  4959. - When point is not at the beginning of a headline, execute
  4960. `indent-relative', like TAB normally does. See the option
  4961. `org-cycle-emulate-tab' for details.
  4962. - Special case: if point is at the beginning of the buffer and there is
  4963. no headline in line 1, this function will act as if called with prefix arg.
  4964. But only if also the variable `org-cycle-global-at-bob' is t."
  4965. (interactive "P")
  4966. (let* ((outline-regexp
  4967. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4968. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4969. outline-regexp))
  4970. (bob-special (and org-cycle-global-at-bob (bobp)
  4971. (not (looking-at outline-regexp))))
  4972. (org-cycle-hook
  4973. (if bob-special
  4974. (delq 'org-optimize-window-after-visibility-change
  4975. (copy-sequence org-cycle-hook))
  4976. org-cycle-hook))
  4977. (pos (point)))
  4978. (if (or bob-special (equal arg '(4)))
  4979. ;; special case: use global cycling
  4980. (setq arg t))
  4981. (cond
  4982. ((org-at-table-p 'any)
  4983. ;; Enter the table or move to the next field in the table
  4984. (or (org-table-recognize-table.el)
  4985. (progn
  4986. (if arg (org-table-edit-field t)
  4987. (org-table-justify-field-maybe)
  4988. (call-interactively 'org-table-next-field)))))
  4989. ((eq arg t) ;; Global cycling
  4990. (cond
  4991. ((and (eq last-command this-command)
  4992. (eq org-cycle-global-status 'overview))
  4993. ;; We just created the overview - now do table of contents
  4994. ;; This can be slow in very large buffers, so indicate action
  4995. (message "CONTENTS...")
  4996. (org-content)
  4997. (message "CONTENTS...done")
  4998. (setq org-cycle-global-status 'contents)
  4999. (run-hook-with-args 'org-cycle-hook 'contents))
  5000. ((and (eq last-command this-command)
  5001. (eq org-cycle-global-status 'contents))
  5002. ;; We just showed the table of contents - now show everything
  5003. (show-all)
  5004. (message "SHOW ALL")
  5005. (setq org-cycle-global-status 'all)
  5006. (run-hook-with-args 'org-cycle-hook 'all))
  5007. (t
  5008. ;; Default action: go to overview
  5009. (org-overview)
  5010. (message "OVERVIEW")
  5011. (setq org-cycle-global-status 'overview)
  5012. (run-hook-with-args 'org-cycle-hook 'overview))))
  5013. ((and org-drawers org-drawer-regexp
  5014. (save-excursion
  5015. (beginning-of-line 1)
  5016. (looking-at org-drawer-regexp)))
  5017. ;; Toggle block visibility
  5018. (org-flag-drawer
  5019. (not (get-char-property (match-end 0) 'invisible))))
  5020. ((integerp arg)
  5021. ;; Show-subtree, ARG levels up from here.
  5022. (save-excursion
  5023. (org-back-to-heading)
  5024. (outline-up-heading (if (< arg 0) (- arg)
  5025. (- (funcall outline-level) arg)))
  5026. (org-show-subtree)))
  5027. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5028. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5029. ;; At a heading: rotate between three different views
  5030. (org-back-to-heading)
  5031. (let ((goal-column 0) eoh eol eos)
  5032. ;; First, some boundaries
  5033. (save-excursion
  5034. (org-back-to-heading)
  5035. (save-excursion
  5036. (beginning-of-line 2)
  5037. (while (and (not (eobp)) ;; this is like `next-line'
  5038. (get-char-property (1- (point)) 'invisible))
  5039. (beginning-of-line 2)) (setq eol (point)))
  5040. (outline-end-of-heading) (setq eoh (point))
  5041. (org-end-of-subtree t)
  5042. (unless (eobp)
  5043. (skip-chars-forward " \t\n")
  5044. (beginning-of-line 1) ; in case this is an item
  5045. )
  5046. (setq eos (1- (point))))
  5047. ;; Find out what to do next and set `this-command'
  5048. (cond
  5049. ((= eos eoh)
  5050. ;; Nothing is hidden behind this heading
  5051. (message "EMPTY ENTRY")
  5052. (setq org-cycle-subtree-status nil)
  5053. (save-excursion
  5054. (goto-char eos)
  5055. (outline-next-heading)
  5056. (if (org-invisible-p) (org-flag-heading nil))))
  5057. ((or (>= eol eos)
  5058. (not (string-match "\\S-" (buffer-substring eol eos))))
  5059. ;; Entire subtree is hidden in one line: open it
  5060. (org-show-entry)
  5061. (show-children)
  5062. (message "CHILDREN")
  5063. (save-excursion
  5064. (goto-char eos)
  5065. (outline-next-heading)
  5066. (if (org-invisible-p) (org-flag-heading nil)))
  5067. (setq org-cycle-subtree-status 'children)
  5068. (run-hook-with-args 'org-cycle-hook 'children))
  5069. ((and (eq last-command this-command)
  5070. (eq org-cycle-subtree-status 'children))
  5071. ;; We just showed the children, now show everything.
  5072. (org-show-subtree)
  5073. (message "SUBTREE")
  5074. (setq org-cycle-subtree-status 'subtree)
  5075. (run-hook-with-args 'org-cycle-hook 'subtree))
  5076. (t
  5077. ;; Default action: hide the subtree.
  5078. (hide-subtree)
  5079. (message "FOLDED")
  5080. (setq org-cycle-subtree-status 'folded)
  5081. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5082. ;; TAB emulation
  5083. (buffer-read-only (org-back-to-heading))
  5084. ((org-try-cdlatex-tab))
  5085. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5086. (or (not (bolp))
  5087. (not (looking-at outline-regexp))))
  5088. (call-interactively (global-key-binding "\t")))
  5089. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5090. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5091. (or (and (eq org-cycle-emulate-tab 'white)
  5092. (= (match-end 0) (point-at-eol)))
  5093. (and (eq org-cycle-emulate-tab 'whitestart)
  5094. (>= (match-end 0) pos))))
  5095. t
  5096. (eq org-cycle-emulate-tab t))
  5097. ; (if (and (looking-at "[ \n\r\t]")
  5098. ; (string-match "^[ \t]*$" (buffer-substring
  5099. ; (point-at-bol) (point))))
  5100. ; (progn
  5101. ; (beginning-of-line 1)
  5102. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5103. (call-interactively (global-key-binding "\t")))
  5104. (t (save-excursion
  5105. (org-back-to-heading)
  5106. (org-cycle))))))
  5107. ;;;###autoload
  5108. (defun org-global-cycle (&optional arg)
  5109. "Cycle the global visibility. For details see `org-cycle'."
  5110. (interactive "P")
  5111. (let ((org-cycle-include-plain-lists
  5112. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5113. (if (integerp arg)
  5114. (progn
  5115. (show-all)
  5116. (hide-sublevels arg)
  5117. (setq org-cycle-global-status 'contents))
  5118. (org-cycle '(4)))))
  5119. (defun org-overview ()
  5120. "Switch to overview mode, shoing only top-level headlines.
  5121. Really, this shows all headlines with level equal or greater than the level
  5122. of the first headline in the buffer. This is important, because if the
  5123. first headline is not level one, then (hide-sublevels 1) gives confusing
  5124. results."
  5125. (interactive)
  5126. (let ((level (save-excursion
  5127. (goto-char (point-min))
  5128. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5129. (progn
  5130. (goto-char (match-beginning 0))
  5131. (funcall outline-level))))))
  5132. (and level (hide-sublevels level))))
  5133. (defun org-content (&optional arg)
  5134. "Show all headlines in the buffer, like a table of contents.
  5135. With numerical argument N, show content up to level N."
  5136. (interactive "P")
  5137. (save-excursion
  5138. ;; Visit all headings and show their offspring
  5139. (and (integerp arg) (org-overview))
  5140. (goto-char (point-max))
  5141. (catch 'exit
  5142. (while (and (progn (condition-case nil
  5143. (outline-previous-visible-heading 1)
  5144. (error (goto-char (point-min))))
  5145. t)
  5146. (looking-at outline-regexp))
  5147. (if (integerp arg)
  5148. (show-children (1- arg))
  5149. (show-branches))
  5150. (if (bobp) (throw 'exit nil))))))
  5151. (defun org-optimize-window-after-visibility-change (state)
  5152. "Adjust the window after a change in outline visibility.
  5153. This function is the default value of the hook `org-cycle-hook'."
  5154. (when (get-buffer-window (current-buffer))
  5155. (cond
  5156. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5157. ; ((eq state 'overview) (org-beginning-of-line))
  5158. ((eq state 'content) nil)
  5159. ((eq state 'all) nil)
  5160. ((eq state 'folded) nil)
  5161. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5162. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5163. (defun org-compact-display-after-subtree-move ()
  5164. (let (beg end)
  5165. (save-excursion
  5166. (if (org-up-heading-safe)
  5167. (progn
  5168. (hide-subtree)
  5169. (show-entry)
  5170. (show-children)
  5171. (org-cycle-show-empty-lines 'children)
  5172. (org-cycle-hide-drawers 'children))
  5173. (org-overview)))))
  5174. (defun org-cycle-show-empty-lines (state)
  5175. "Show empty lines above all visible headlines.
  5176. The region to be covered depends on STATE when called through
  5177. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5178. entire buffer covered. Note that an empty line is only shown if there
  5179. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5180. (when (> org-cycle-separator-lines 0)
  5181. (save-excursion
  5182. (let* ((n org-cycle-separator-lines)
  5183. (re (cond
  5184. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5185. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5186. (t (let ((ns (number-to-string (- n 2))))
  5187. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5188. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5189. beg end)
  5190. (cond
  5191. ((memq state '(overview contents t))
  5192. (setq beg (point-min) end (point-max)))
  5193. ((memq state '(children folded))
  5194. (setq beg (point) end (progn (org-end-of-subtree t t)
  5195. (beginning-of-line 2)
  5196. (point)))))
  5197. (when beg
  5198. (goto-char beg)
  5199. (while (re-search-forward re end t)
  5200. (if (not (get-char-property (match-end 1) 'invisible))
  5201. (outline-flag-region
  5202. (match-beginning 1) (match-end 1) nil)))))))
  5203. ;; Never hide empty lines at the end of the file.
  5204. (save-excursion
  5205. (goto-char (point-max))
  5206. (outline-previous-heading)
  5207. (outline-end-of-heading)
  5208. (if (and (looking-at "[ \t\n]+")
  5209. (= (match-end 0) (point-max)))
  5210. (outline-flag-region (point) (match-end 0) nil))))
  5211. (defun org-subtree-end-visible-p ()
  5212. "Is the end of the current subtree visible?"
  5213. (pos-visible-in-window-p
  5214. (save-excursion (org-end-of-subtree t) (point))))
  5215. (defun org-first-headline-recenter (&optional N)
  5216. "Move cursor to the first headline and recenter the headline.
  5217. Optional argument N means, put the headline into the Nth line of the window."
  5218. (goto-char (point-min))
  5219. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5220. (beginning-of-line)
  5221. (recenter (prefix-numeric-value N))))
  5222. ;;; Org-goto
  5223. (defvar org-goto-window-configuration nil)
  5224. (defvar org-goto-marker nil)
  5225. (defvar org-goto-map
  5226. (let ((map (make-sparse-keymap)))
  5227. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5228. (while (setq cmd (pop cmds))
  5229. (substitute-key-definition cmd cmd map global-map)))
  5230. (suppress-keymap map)
  5231. (org-defkey map "\C-m" 'org-goto-ret)
  5232. (org-defkey map [(return)] 'org-goto-ret)
  5233. (org-defkey map [(left)] 'org-goto-left)
  5234. (org-defkey map [(right)] 'org-goto-right)
  5235. (org-defkey map [(control ?g)] 'org-goto-quit)
  5236. (org-defkey map "\C-i" 'org-cycle)
  5237. (org-defkey map [(tab)] 'org-cycle)
  5238. (org-defkey map [(down)] 'outline-next-visible-heading)
  5239. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5240. (if org-goto-auto-isearch
  5241. (if (fboundp 'define-key-after)
  5242. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5243. nil)
  5244. (org-defkey map "q" 'org-goto-quit)
  5245. (org-defkey map "n" 'outline-next-visible-heading)
  5246. (org-defkey map "p" 'outline-previous-visible-heading)
  5247. (org-defkey map "f" 'outline-forward-same-level)
  5248. (org-defkey map "b" 'outline-backward-same-level)
  5249. (org-defkey map "u" 'outline-up-heading))
  5250. (org-defkey map "/" 'org-occur)
  5251. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5252. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5253. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5254. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5255. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5256. map))
  5257. (defconst org-goto-help
  5258. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5259. RET=jump to location [Q]uit and return to previous location
  5260. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5261. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5262. (defun org-goto (&optional alternative-interface)
  5263. "Look up a different location in the current file, keeping current visibility.
  5264. When you want look-up or go to a different location in a document, the
  5265. fastest way is often to fold the entire buffer and then dive into the tree.
  5266. This method has the disadvantage, that the previous location will be folded,
  5267. which may not be what you want.
  5268. This command works around this by showing a copy of the current buffer
  5269. in an indirect buffer, in overview mode. You can dive into the tree in
  5270. that copy, use org-occur and incremental search to find a location.
  5271. When pressing RET or `Q', the command returns to the original buffer in
  5272. which the visibility is still unchanged. After RET is will also jump to
  5273. the location selected in the indirect buffer and expose the
  5274. the headline hierarchy above."
  5275. (interactive "P")
  5276. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5277. (org-refile-use-outline-path t)
  5278. (interface
  5279. (if (not alternative-interface)
  5280. org-goto-interface
  5281. (if (eq org-goto-interface 'outline)
  5282. 'outline-path-completion
  5283. 'outline)))
  5284. (org-goto-start-pos (point))
  5285. (selected-point
  5286. (if (eq interface 'outline)
  5287. (car (org-get-location (current-buffer) org-goto-help))
  5288. (nth 3 (org-refile-get-location "Goto: ")))))
  5289. (if selected-point
  5290. (progn
  5291. (org-mark-ring-push org-goto-start-pos)
  5292. (goto-char selected-point)
  5293. (if (or (org-invisible-p) (org-invisible-p2))
  5294. (org-show-context 'org-goto)))
  5295. (message "Quit"))))
  5296. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5297. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5298. (defvar org-goto-local-auto-isearch-map) ; defined below
  5299. (defun org-get-location (buf help)
  5300. "Let the user select a location in the Org-mode buffer BUF.
  5301. This function uses a recursive edit. It returns the selected position
  5302. or nil."
  5303. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5304. (isearch-hide-immediately nil)
  5305. (isearch-search-fun-function
  5306. (lambda () 'org-goto-local-search-forward-headings))
  5307. (org-goto-selected-point org-goto-exit-command))
  5308. (save-excursion
  5309. (save-window-excursion
  5310. (delete-other-windows)
  5311. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5312. (switch-to-buffer
  5313. (condition-case nil
  5314. (make-indirect-buffer (current-buffer) "*org-goto*")
  5315. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5316. (with-output-to-temp-buffer "*Help*"
  5317. (princ help))
  5318. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5319. (setq buffer-read-only nil)
  5320. (let ((org-startup-truncated t)
  5321. (org-startup-folded nil)
  5322. (org-startup-align-all-tables nil))
  5323. (org-mode)
  5324. (org-overview))
  5325. (setq buffer-read-only t)
  5326. (if (and (boundp 'org-goto-start-pos)
  5327. (integer-or-marker-p org-goto-start-pos))
  5328. (let ((org-show-hierarchy-above t)
  5329. (org-show-siblings t)
  5330. (org-show-following-heading t))
  5331. (goto-char org-goto-start-pos)
  5332. (and (org-invisible-p) (org-show-context)))
  5333. (goto-char (point-min)))
  5334. (org-beginning-of-line)
  5335. (message "Select location and press RET")
  5336. (use-local-map org-goto-map)
  5337. (recursive-edit)
  5338. ))
  5339. (kill-buffer "*org-goto*")
  5340. (cons org-goto-selected-point org-goto-exit-command)))
  5341. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5342. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5343. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5344. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5345. (defun org-goto-local-search-forward-headings (string bound noerror)
  5346. "Search and make sure that anu matches are in headlines."
  5347. (catch 'return
  5348. (while (search-forward string bound noerror)
  5349. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5350. (and (member :headline context)
  5351. (not (member :tags context))))
  5352. (throw 'return (point))))))
  5353. (defun org-goto-local-auto-isearch ()
  5354. "Start isearch."
  5355. (interactive)
  5356. (goto-char (point-min))
  5357. (let ((keys (this-command-keys)))
  5358. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5359. (isearch-mode t)
  5360. (isearch-process-search-char (string-to-char keys)))))
  5361. (defun org-goto-ret (&optional arg)
  5362. "Finish `org-goto' by going to the new location."
  5363. (interactive "P")
  5364. (setq org-goto-selected-point (point)
  5365. org-goto-exit-command 'return)
  5366. (throw 'exit nil))
  5367. (defun org-goto-left ()
  5368. "Finish `org-goto' by going to the new location."
  5369. (interactive)
  5370. (if (org-on-heading-p)
  5371. (progn
  5372. (beginning-of-line 1)
  5373. (setq org-goto-selected-point (point)
  5374. org-goto-exit-command 'left)
  5375. (throw 'exit nil))
  5376. (error "Not on a heading")))
  5377. (defun org-goto-right ()
  5378. "Finish `org-goto' by going to the new location."
  5379. (interactive)
  5380. (if (org-on-heading-p)
  5381. (progn
  5382. (setq org-goto-selected-point (point)
  5383. org-goto-exit-command 'right)
  5384. (throw 'exit nil))
  5385. (error "Not on a heading")))
  5386. (defun org-goto-quit ()
  5387. "Finish `org-goto' without cursor motion."
  5388. (interactive)
  5389. (setq org-goto-selected-point nil)
  5390. (setq org-goto-exit-command 'quit)
  5391. (throw 'exit nil))
  5392. ;;; Indirect buffer display of subtrees
  5393. (defvar org-indirect-dedicated-frame nil
  5394. "This is the frame being used for indirect tree display.")
  5395. (defvar org-last-indirect-buffer nil)
  5396. (defun org-tree-to-indirect-buffer (&optional arg)
  5397. "Create indirect buffer and narrow it to current subtree.
  5398. With numerical prefix ARG, go up to this level and then take that tree.
  5399. If ARG is negative, go up that many levels.
  5400. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5401. indirect buffer previously made with this command, to avoid proliferation of
  5402. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5403. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5404. is kept so that you can work with several indirect buffers at the same time.
  5405. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5406. requests that a new frame be made for the new buffer, so that the dedicated
  5407. frame is not changed."
  5408. (interactive "P")
  5409. (let ((cbuf (current-buffer))
  5410. (cwin (selected-window))
  5411. (pos (point))
  5412. beg end level heading ibuf)
  5413. (save-excursion
  5414. (org-back-to-heading t)
  5415. (when (numberp arg)
  5416. (setq level (org-outline-level))
  5417. (if (< arg 0) (setq arg (+ level arg)))
  5418. (while (> (setq level (org-outline-level)) arg)
  5419. (outline-up-heading 1 t)))
  5420. (setq beg (point)
  5421. heading (org-get-heading))
  5422. (org-end-of-subtree t) (setq end (point)))
  5423. (if (and (buffer-live-p org-last-indirect-buffer)
  5424. (not (eq org-indirect-buffer-display 'new-frame))
  5425. (not arg))
  5426. (kill-buffer org-last-indirect-buffer))
  5427. (setq ibuf (org-get-indirect-buffer cbuf)
  5428. org-last-indirect-buffer ibuf)
  5429. (cond
  5430. ((or (eq org-indirect-buffer-display 'new-frame)
  5431. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5432. (select-frame (make-frame))
  5433. (delete-other-windows)
  5434. (switch-to-buffer ibuf)
  5435. (org-set-frame-title heading))
  5436. ((eq org-indirect-buffer-display 'dedicated-frame)
  5437. (raise-frame
  5438. (select-frame (or (and org-indirect-dedicated-frame
  5439. (frame-live-p org-indirect-dedicated-frame)
  5440. org-indirect-dedicated-frame)
  5441. (setq org-indirect-dedicated-frame (make-frame)))))
  5442. (delete-other-windows)
  5443. (switch-to-buffer ibuf)
  5444. (org-set-frame-title (concat "Indirect: " heading)))
  5445. ((eq org-indirect-buffer-display 'current-window)
  5446. (switch-to-buffer ibuf))
  5447. ((eq org-indirect-buffer-display 'other-window)
  5448. (pop-to-buffer ibuf))
  5449. (t (error "Invalid value.")))
  5450. (if (featurep 'xemacs)
  5451. (save-excursion (org-mode) (turn-on-font-lock)))
  5452. (narrow-to-region beg end)
  5453. (show-all)
  5454. (goto-char pos)
  5455. (and (window-live-p cwin) (select-window cwin))))
  5456. (defun org-get-indirect-buffer (&optional buffer)
  5457. (setq buffer (or buffer (current-buffer)))
  5458. (let ((n 1) (base (buffer-name buffer)) bname)
  5459. (while (buffer-live-p
  5460. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5461. (setq n (1+ n)))
  5462. (condition-case nil
  5463. (make-indirect-buffer buffer bname 'clone)
  5464. (error (make-indirect-buffer buffer bname)))))
  5465. (defun org-set-frame-title (title)
  5466. "Set the title of the current frame to the string TITLE."
  5467. ;; FIXME: how to name a single frame in XEmacs???
  5468. (unless (featurep 'xemacs)
  5469. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5470. ;;;; Structure editing
  5471. ;;; Inserting headlines
  5472. (defun org-insert-heading (&optional force-heading)
  5473. "Insert a new heading or item with same depth at point.
  5474. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5475. If point is at the beginning of a headline, insert a sibling before the
  5476. current headline. If point is not at the beginning, do not split the line,
  5477. but create the new hedline after the current line."
  5478. (interactive "P")
  5479. (if (= (buffer-size) 0)
  5480. (insert "\n* ")
  5481. (when (or force-heading (not (org-insert-item)))
  5482. (let* ((head (save-excursion
  5483. (condition-case nil
  5484. (progn
  5485. (org-back-to-heading)
  5486. (match-string 0))
  5487. (error "*"))))
  5488. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5489. pos)
  5490. (cond
  5491. ((and (org-on-heading-p) (bolp)
  5492. (or (bobp)
  5493. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5494. ;; insert before the current line
  5495. (open-line (if blank 2 1)))
  5496. ((and (bolp)
  5497. (or (bobp)
  5498. (save-excursion
  5499. (backward-char 1) (not (org-invisible-p)))))
  5500. ;; insert right here
  5501. nil)
  5502. (t
  5503. ;; in the middle of the line
  5504. (org-show-entry)
  5505. (end-of-line 1)
  5506. (newline (if blank 2 1))))
  5507. (insert head) (just-one-space)
  5508. (setq pos (point))
  5509. (end-of-line 1)
  5510. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5511. (run-hooks 'org-insert-heading-hook)))))
  5512. (defun org-insert-heading-after-current ()
  5513. "Insert a new heading with same level as current, after current subtree."
  5514. (interactive)
  5515. (org-back-to-heading)
  5516. (org-insert-heading)
  5517. (org-move-subtree-down)
  5518. (end-of-line 1))
  5519. (defun org-insert-todo-heading (arg)
  5520. "Insert a new heading with the same level and TODO state as current heading.
  5521. If the heading has no TODO state, or if the state is DONE, use the first
  5522. state (TODO by default). Also with prefix arg, force first state."
  5523. (interactive "P")
  5524. (when (not (org-insert-item 'checkbox))
  5525. (org-insert-heading)
  5526. (save-excursion
  5527. (org-back-to-heading)
  5528. (outline-previous-heading)
  5529. (looking-at org-todo-line-regexp))
  5530. (if (or arg
  5531. (not (match-beginning 2))
  5532. (member (match-string 2) org-done-keywords))
  5533. (insert (car org-todo-keywords-1) " ")
  5534. (insert (match-string 2) " "))))
  5535. (defun org-insert-subheading (arg)
  5536. "Insert a new subheading and demote it.
  5537. Works for outline headings and for plain lists alike."
  5538. (interactive "P")
  5539. (org-insert-heading arg)
  5540. (cond
  5541. ((org-on-heading-p) (org-do-demote))
  5542. ((org-at-item-p) (org-indent-item 1))))
  5543. (defun org-insert-todo-subheading (arg)
  5544. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5545. Works for outline headings and for plain lists alike."
  5546. (interactive "P")
  5547. (org-insert-todo-heading arg)
  5548. (cond
  5549. ((org-on-heading-p) (org-do-demote))
  5550. ((org-at-item-p) (org-indent-item 1))))
  5551. ;;; Promotion and Demotion
  5552. (defun org-promote-subtree ()
  5553. "Promote the entire subtree.
  5554. See also `org-promote'."
  5555. (interactive)
  5556. (save-excursion
  5557. (org-map-tree 'org-promote))
  5558. (org-fix-position-after-promote))
  5559. (defun org-demote-subtree ()
  5560. "Demote the entire subtree. See `org-demote'.
  5561. See also `org-promote'."
  5562. (interactive)
  5563. (save-excursion
  5564. (org-map-tree 'org-demote))
  5565. (org-fix-position-after-promote))
  5566. (defun org-do-promote ()
  5567. "Promote the current heading higher up the tree.
  5568. If the region is active in `transient-mark-mode', promote all headings
  5569. in the region."
  5570. (interactive)
  5571. (save-excursion
  5572. (if (org-region-active-p)
  5573. (org-map-region 'org-promote (region-beginning) (region-end))
  5574. (org-promote)))
  5575. (org-fix-position-after-promote))
  5576. (defun org-do-demote ()
  5577. "Demote the current heading lower down the tree.
  5578. If the region is active in `transient-mark-mode', demote all headings
  5579. in the region."
  5580. (interactive)
  5581. (save-excursion
  5582. (if (org-region-active-p)
  5583. (org-map-region 'org-demote (region-beginning) (region-end))
  5584. (org-demote)))
  5585. (org-fix-position-after-promote))
  5586. (defun org-fix-position-after-promote ()
  5587. "Make sure that after pro/demotion cursor position is right."
  5588. (let ((pos (point)))
  5589. (when (save-excursion
  5590. (beginning-of-line 1)
  5591. (looking-at org-todo-line-regexp)
  5592. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5593. (cond ((eobp) (insert " "))
  5594. ((eolp) (insert " "))
  5595. ((equal (char-after) ?\ ) (forward-char 1))))))
  5596. (defun org-reduced-level (l)
  5597. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5598. (defun org-get-legal-level (level &optional change)
  5599. "Rectify a level change under the influence of `org-odd-levels-only'
  5600. LEVEL is a current level, CHANGE is by how much the level should be
  5601. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5602. even level numbers will become the next higher odd number."
  5603. (if org-odd-levels-only
  5604. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5605. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5606. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5607. (max 1 (+ level change))))
  5608. (defun org-promote ()
  5609. "Promote the current heading higher up the tree.
  5610. If the region is active in `transient-mark-mode', promote all headings
  5611. in the region."
  5612. (org-back-to-heading t)
  5613. (let* ((level (save-match-data (funcall outline-level)))
  5614. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5615. (diff (abs (- level (length up-head) -1))))
  5616. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5617. (replace-match up-head nil t)
  5618. ;; Fixup tag positioning
  5619. (and org-auto-align-tags (org-set-tags nil t))
  5620. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5621. (defun org-demote ()
  5622. "Demote the current heading lower down the tree.
  5623. If the region is active in `transient-mark-mode', demote all headings
  5624. in the region."
  5625. (org-back-to-heading t)
  5626. (let* ((level (save-match-data (funcall outline-level)))
  5627. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5628. (diff (abs (- level (length down-head) -1))))
  5629. (replace-match down-head nil t)
  5630. ;; Fixup tag positioning
  5631. (and org-auto-align-tags (org-set-tags nil t))
  5632. (if org-adapt-indentation (org-fixup-indentation diff))))
  5633. (defun org-map-tree (fun)
  5634. "Call FUN for every heading underneath the current one."
  5635. (org-back-to-heading)
  5636. (let ((level (funcall outline-level)))
  5637. (save-excursion
  5638. (funcall fun)
  5639. (while (and (progn
  5640. (outline-next-heading)
  5641. (> (funcall outline-level) level))
  5642. (not (eobp)))
  5643. (funcall fun)))))
  5644. (defun org-map-region (fun beg end)
  5645. "Call FUN for every heading between BEG and END."
  5646. (let ((org-ignore-region t))
  5647. (save-excursion
  5648. (setq end (copy-marker end))
  5649. (goto-char beg)
  5650. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5651. (< (point) end))
  5652. (funcall fun))
  5653. (while (and (progn
  5654. (outline-next-heading)
  5655. (< (point) end))
  5656. (not (eobp)))
  5657. (funcall fun)))))
  5658. (defun org-fixup-indentation (diff)
  5659. "Change the indentation in the current entry by DIFF
  5660. However, if any line in the current entry has no indentation, or if it
  5661. would end up with no indentation after the change, nothing at all is done."
  5662. (save-excursion
  5663. (let ((end (save-excursion (outline-next-heading)
  5664. (point-marker)))
  5665. (prohibit (if (> diff 0)
  5666. "^\\S-"
  5667. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5668. col)
  5669. (unless (save-excursion (end-of-line 1)
  5670. (re-search-forward prohibit end t))
  5671. (while (and (< (point) end)
  5672. (re-search-forward "^[ \t]+" end t))
  5673. (goto-char (match-end 0))
  5674. (setq col (current-column))
  5675. (if (< diff 0) (replace-match ""))
  5676. (indent-to (+ diff col))))
  5677. (move-marker end nil))))
  5678. (defun org-convert-to-odd-levels ()
  5679. "Convert an org-mode file with all levels allowed to one with odd levels.
  5680. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5681. level 5 etc."
  5682. (interactive)
  5683. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5684. (let ((org-odd-levels-only nil) n)
  5685. (save-excursion
  5686. (goto-char (point-min))
  5687. (while (re-search-forward "^\\*\\*+ " nil t)
  5688. (setq n (- (length (match-string 0)) 2))
  5689. (while (>= (setq n (1- n)) 0)
  5690. (org-demote))
  5691. (end-of-line 1))))))
  5692. (defun org-convert-to-oddeven-levels ()
  5693. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5694. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5695. section with an even level, conversion would destroy the structure of the file. An error
  5696. is signaled in this case."
  5697. (interactive)
  5698. (goto-char (point-min))
  5699. ;; First check if there are no even levels
  5700. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5701. (org-show-context t)
  5702. (error "Not all levels are odd in this file. Conversion not possible."))
  5703. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5704. (let ((org-odd-levels-only nil) n)
  5705. (save-excursion
  5706. (goto-char (point-min))
  5707. (while (re-search-forward "^\\*\\*+ " nil t)
  5708. (setq n (/ (1- (length (match-string 0))) 2))
  5709. (while (>= (setq n (1- n)) 0)
  5710. (org-promote))
  5711. (end-of-line 1))))))
  5712. (defun org-tr-level (n)
  5713. "Make N odd if required."
  5714. (if org-odd-levels-only (1+ (/ n 2)) n))
  5715. ;;; Vertical tree motion, cutting and pasting of subtrees
  5716. (defun org-move-subtree-up (&optional arg)
  5717. "Move the current subtree up past ARG headlines of the same level."
  5718. (interactive "p")
  5719. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5720. (defun org-move-subtree-down (&optional arg)
  5721. "Move the current subtree down past ARG headlines of the same level."
  5722. (interactive "p")
  5723. (setq arg (prefix-numeric-value arg))
  5724. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5725. 'outline-get-last-sibling))
  5726. (ins-point (make-marker))
  5727. (cnt (abs arg))
  5728. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5729. ;; Select the tree
  5730. (org-back-to-heading)
  5731. (setq beg0 (point))
  5732. (save-excursion
  5733. (setq ne-beg (org-back-over-empty-lines))
  5734. (setq beg (point)))
  5735. (save-match-data
  5736. (save-excursion (outline-end-of-heading)
  5737. (setq folded (org-invisible-p)))
  5738. (outline-end-of-subtree))
  5739. (outline-next-heading)
  5740. (setq ne-end (org-back-over-empty-lines))
  5741. (setq end (point))
  5742. (goto-char beg0)
  5743. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5744. ;; include less whitespace
  5745. (save-excursion
  5746. (goto-char beg)
  5747. (forward-line (- ne-beg ne-end))
  5748. (setq beg (point))))
  5749. ;; Find insertion point, with error handling
  5750. (while (> cnt 0)
  5751. (or (and (funcall movfunc) (looking-at outline-regexp))
  5752. (progn (goto-char beg0)
  5753. (error "Cannot move past superior level or buffer limit")))
  5754. (setq cnt (1- cnt)))
  5755. (if (> arg 0)
  5756. ;; Moving forward - still need to move over subtree
  5757. (progn (org-end-of-subtree t t)
  5758. (save-excursion
  5759. (org-back-over-empty-lines)
  5760. (or (bolp) (newline)))))
  5761. (setq ne-ins (org-back-over-empty-lines))
  5762. (move-marker ins-point (point))
  5763. (setq txt (buffer-substring beg end))
  5764. (delete-region beg end)
  5765. (outline-flag-region (1- beg) beg nil)
  5766. (outline-flag-region (1- (point)) (point) nil)
  5767. (insert txt)
  5768. (or (bolp) (insert "\n"))
  5769. (setq ins-end (point))
  5770. (goto-char ins-point)
  5771. (org-skip-whitespace)
  5772. (when (and (< arg 0)
  5773. (org-first-sibling-p)
  5774. (> ne-ins ne-beg))
  5775. ;; Move whitespace back to beginning
  5776. (save-excursion
  5777. (goto-char ins-end)
  5778. (let ((kill-whole-line t))
  5779. (kill-line (- ne-ins ne-beg)) (point)))
  5780. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5781. (move-marker ins-point nil)
  5782. (org-compact-display-after-subtree-move)
  5783. (unless folded
  5784. (org-show-entry)
  5785. (show-children)
  5786. (org-cycle-hide-drawers 'children))))
  5787. (defvar org-subtree-clip ""
  5788. "Clipboard for cut and paste of subtrees.
  5789. This is actually only a copy of the kill, because we use the normal kill
  5790. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5791. (defvar org-subtree-clip-folded nil
  5792. "Was the last copied subtree folded?
  5793. This is used to fold the tree back after pasting.")
  5794. (defun org-cut-subtree (&optional n)
  5795. "Cut the current subtree into the clipboard.
  5796. With prefix arg N, cut this many sequential subtrees.
  5797. This is a short-hand for marking the subtree and then cutting it."
  5798. (interactive "p")
  5799. (org-copy-subtree n 'cut))
  5800. (defun org-copy-subtree (&optional n cut)
  5801. "Cut the current subtree into the clipboard.
  5802. With prefix arg N, cut this many sequential subtrees.
  5803. This is a short-hand for marking the subtree and then copying it.
  5804. If CUT is non-nil, actually cut the subtree."
  5805. (interactive "p")
  5806. (let (beg end folded (beg0 (point)))
  5807. (if (interactive-p)
  5808. (org-back-to-heading nil) ; take what looks like a subtree
  5809. (org-back-to-heading t)) ; take what is really there
  5810. (org-back-over-empty-lines)
  5811. (setq beg (point))
  5812. (skip-chars-forward " \t\r\n")
  5813. (save-match-data
  5814. (save-excursion (outline-end-of-heading)
  5815. (setq folded (org-invisible-p)))
  5816. (condition-case nil
  5817. (outline-forward-same-level (1- n))
  5818. (error nil))
  5819. (org-end-of-subtree t t))
  5820. (org-back-over-empty-lines)
  5821. (setq end (point))
  5822. (goto-char beg0)
  5823. (when (> end beg)
  5824. (setq org-subtree-clip-folded folded)
  5825. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5826. (setq org-subtree-clip (current-kill 0))
  5827. (message "%s: Subtree(s) with %d characters"
  5828. (if cut "Cut" "Copied")
  5829. (length org-subtree-clip)))))
  5830. (defun org-paste-subtree (&optional level tree)
  5831. "Paste the clipboard as a subtree, with modification of headline level.
  5832. The entire subtree is promoted or demoted in order to match a new headline
  5833. level. By default, the new level is derived from the visible headings
  5834. before and after the insertion point, and taken to be the inferior headline
  5835. level of the two. So if the previous visible heading is level 3 and the
  5836. next is level 4 (or vice versa), level 4 will be used for insertion.
  5837. This makes sure that the subtree remains an independent subtree and does
  5838. not swallow low level entries.
  5839. You can also force a different level, either by using a numeric prefix
  5840. argument, or by inserting the heading marker by hand. For example, if the
  5841. cursor is after \"*****\", then the tree will be shifted to level 5.
  5842. If you want to insert the tree as is, just use \\[yank].
  5843. If optional TREE is given, use this text instead of the kill ring."
  5844. (interactive "P")
  5845. (unless (org-kill-is-subtree-p tree)
  5846. (error "%s"
  5847. (substitute-command-keys
  5848. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5849. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5850. (^re (concat "^\\(" outline-regexp "\\)"))
  5851. (re (concat "\\(" outline-regexp "\\)"))
  5852. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5853. (old-level (if (string-match ^re txt)
  5854. (- (match-end 0) (match-beginning 0) 1)
  5855. -1))
  5856. (force-level (cond (level (prefix-numeric-value level))
  5857. ((string-match
  5858. ^re_ (buffer-substring (point-at-bol) (point)))
  5859. (- (match-end 1) (match-beginning 1)))
  5860. (t nil)))
  5861. (previous-level (save-excursion
  5862. (condition-case nil
  5863. (progn
  5864. (outline-previous-visible-heading 1)
  5865. (if (looking-at re)
  5866. (- (match-end 0) (match-beginning 0) 1)
  5867. 1))
  5868. (error 1))))
  5869. (next-level (save-excursion
  5870. (condition-case nil
  5871. (progn
  5872. (or (looking-at outline-regexp)
  5873. (outline-next-visible-heading 1))
  5874. (if (looking-at re)
  5875. (- (match-end 0) (match-beginning 0) 1)
  5876. 1))
  5877. (error 1))))
  5878. (new-level (or force-level (max previous-level next-level)))
  5879. (shift (if (or (= old-level -1)
  5880. (= new-level -1)
  5881. (= old-level new-level))
  5882. 0
  5883. (- new-level old-level)))
  5884. (delta (if (> shift 0) -1 1))
  5885. (func (if (> shift 0) 'org-demote 'org-promote))
  5886. (org-odd-levels-only nil)
  5887. beg end)
  5888. ;; Remove the forced level indicator
  5889. (if force-level
  5890. (delete-region (point-at-bol) (point)))
  5891. ;; Paste
  5892. (beginning-of-line 1)
  5893. (org-back-over-empty-lines) ;; FIXME: correct fix????
  5894. (setq beg (point))
  5895. (insert-before-markers txt) ;; FIXME: correct fix????
  5896. (unless (string-match "\n\\'" txt) (insert "\n"))
  5897. (setq end (point))
  5898. (goto-char beg)
  5899. (skip-chars-forward " \t\n\r")
  5900. (setq beg (point))
  5901. ;; Shift if necessary
  5902. (unless (= shift 0)
  5903. (save-restriction
  5904. (narrow-to-region beg end)
  5905. (while (not (= shift 0))
  5906. (org-map-region func (point-min) (point-max))
  5907. (setq shift (+ delta shift)))
  5908. (goto-char (point-min))))
  5909. (when (interactive-p)
  5910. (message "Clipboard pasted as level %d subtree" new-level))
  5911. (if (and kill-ring
  5912. (eq org-subtree-clip (current-kill 0))
  5913. org-subtree-clip-folded)
  5914. ;; The tree was folded before it was killed/copied
  5915. (hide-subtree))))
  5916. (defun org-kill-is-subtree-p (&optional txt)
  5917. "Check if the current kill is an outline subtree, or a set of trees.
  5918. Returns nil if kill does not start with a headline, or if the first
  5919. headline level is not the largest headline level in the tree.
  5920. So this will actually accept several entries of equal levels as well,
  5921. which is OK for `org-paste-subtree'.
  5922. If optional TXT is given, check this string instead of the current kill."
  5923. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5924. (start-level (and kill
  5925. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5926. org-outline-regexp "\\)")
  5927. kill)
  5928. (- (match-end 2) (match-beginning 2) 1)))
  5929. (re (concat "^" org-outline-regexp))
  5930. (start (1+ (match-beginning 2))))
  5931. (if (not start-level)
  5932. (progn
  5933. nil) ;; does not even start with a heading
  5934. (catch 'exit
  5935. (while (setq start (string-match re kill (1+ start)))
  5936. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5937. (throw 'exit nil)))
  5938. t))))
  5939. (defun org-narrow-to-subtree ()
  5940. "Narrow buffer to the current subtree."
  5941. (interactive)
  5942. (save-excursion
  5943. (narrow-to-region
  5944. (progn (org-back-to-heading) (point))
  5945. (progn (org-end-of-subtree t t) (point)))))
  5946. ;;; Outline Sorting
  5947. (defun org-sort (with-case)
  5948. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5949. Optional argument WITH-CASE means sort case-sensitively."
  5950. (interactive "P")
  5951. (if (org-at-table-p)
  5952. (org-call-with-arg 'org-table-sort-lines with-case)
  5953. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5954. (defvar org-priority-regexp) ; defined later in the file
  5955. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5956. "Sort entries on a certain level of an outline tree.
  5957. If there is an active region, the entries in the region are sorted.
  5958. Else, if the cursor is before the first entry, sort the top-level items.
  5959. Else, the children of the entry at point are sorted.
  5960. Sorting can be alphabetically, numerically, and by date/time as given by
  5961. the first time stamp in the entry. The command prompts for the sorting
  5962. type unless it has been given to the function through the SORTING-TYPE
  5963. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5964. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5965. called with point at the beginning of the record. It must return either
  5966. a string or a number that should serve as the sorting key for that record.
  5967. Comparing entries ignores case by default. However, with an optional argument
  5968. WITH-CASE, the sorting considers case as well."
  5969. (interactive "P")
  5970. (let ((case-func (if with-case 'identity 'downcase))
  5971. start beg end stars re re2
  5972. txt what tmp plain-list-p)
  5973. ;; Find beginning and end of region to sort
  5974. (cond
  5975. ((org-region-active-p)
  5976. ;; we will sort the region
  5977. (setq end (region-end)
  5978. what "region")
  5979. (goto-char (region-beginning))
  5980. (if (not (org-on-heading-p)) (outline-next-heading))
  5981. (setq start (point)))
  5982. ((org-at-item-p)
  5983. ;; we will sort this plain list
  5984. (org-beginning-of-item-list) (setq start (point))
  5985. (org-end-of-item-list) (setq end (point))
  5986. (goto-char start)
  5987. (setq plain-list-p t
  5988. what "plain list"))
  5989. ((or (org-on-heading-p)
  5990. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5991. ;; we will sort the children of the current headline
  5992. (org-back-to-heading)
  5993. (setq start (point)
  5994. end (progn (org-end-of-subtree t t)
  5995. (org-back-over-empty-lines)
  5996. (point))
  5997. what "children")
  5998. (goto-char start)
  5999. (show-subtree)
  6000. (outline-next-heading))
  6001. (t
  6002. ;; we will sort the top-level entries in this file
  6003. (goto-char (point-min))
  6004. (or (org-on-heading-p) (outline-next-heading))
  6005. (setq start (point) end (point-max) what "top-level")
  6006. (goto-char start)
  6007. (show-all)))
  6008. (setq beg (point))
  6009. (if (>= beg end) (error "Nothing to sort"))
  6010. (unless plain-list-p
  6011. (looking-at "\\(\\*+\\)")
  6012. (setq stars (match-string 1)
  6013. re (concat "^" (regexp-quote stars) " +")
  6014. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6015. txt (buffer-substring beg end))
  6016. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6017. (if (and (not (equal stars "*")) (string-match re2 txt))
  6018. (error "Region to sort contains a level above the first entry")))
  6019. (unless sorting-type
  6020. (message
  6021. (if plain-list-p
  6022. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6023. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6024. what)
  6025. (setq sorting-type (read-char-exclusive))
  6026. (and (= (downcase sorting-type) ?f)
  6027. (setq getkey-func
  6028. (completing-read "Sort using function: "
  6029. obarray 'fboundp t nil nil))
  6030. (setq getkey-func (intern getkey-func)))
  6031. (and (= (downcase sorting-type) ?r)
  6032. (setq property
  6033. (completing-read "Property: "
  6034. (mapcar 'list (org-buffer-property-keys t))
  6035. nil t))))
  6036. (message "Sorting entries...")
  6037. (save-restriction
  6038. (narrow-to-region start end)
  6039. (let ((dcst (downcase sorting-type))
  6040. (now (current-time)))
  6041. (sort-subr
  6042. (/= dcst sorting-type)
  6043. ;; This function moves to the beginning character of the "record" to
  6044. ;; be sorted.
  6045. (if plain-list-p
  6046. (lambda nil
  6047. (if (org-at-item-p) t (goto-char (point-max))))
  6048. (lambda nil
  6049. (if (re-search-forward re nil t)
  6050. (goto-char (match-beginning 0))
  6051. (goto-char (point-max)))))
  6052. ;; This function moves to the last character of the "record" being
  6053. ;; sorted.
  6054. (if plain-list-p
  6055. 'org-end-of-item
  6056. (lambda nil
  6057. (save-match-data
  6058. (condition-case nil
  6059. (outline-forward-same-level 1)
  6060. (error
  6061. (goto-char (point-max)))))))
  6062. ;; This function returns the value that gets sorted against.
  6063. (if plain-list-p
  6064. (lambda nil
  6065. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6066. (cond
  6067. ((= dcst ?n)
  6068. (string-to-number (buffer-substring (match-end 0)
  6069. (point-at-eol))))
  6070. ((= dcst ?a)
  6071. (buffer-substring (match-end 0) (point-at-eol)))
  6072. ((= dcst ?t)
  6073. (if (re-search-forward org-ts-regexp
  6074. (point-at-eol) t)
  6075. (org-time-string-to-time (match-string 0))
  6076. now))
  6077. ((= dcst ?f)
  6078. (if getkey-func
  6079. (progn
  6080. (setq tmp (funcall getkey-func))
  6081. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6082. tmp)
  6083. (error "Invalid key function `%s'" getkey-func)))
  6084. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6085. (lambda nil
  6086. (cond
  6087. ((= dcst ?n)
  6088. (if (looking-at outline-regexp)
  6089. (string-to-number (buffer-substring (match-end 0)
  6090. (point-at-eol)))
  6091. nil))
  6092. ((= dcst ?a)
  6093. (funcall case-func (buffer-substring (point-at-bol)
  6094. (point-at-eol))))
  6095. ((= dcst ?t)
  6096. (if (re-search-forward org-ts-regexp
  6097. (save-excursion
  6098. (forward-line 2)
  6099. (point)) t)
  6100. (org-time-string-to-time (match-string 0))
  6101. now))
  6102. ((= dcst ?p)
  6103. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6104. (string-to-char (match-string 2))
  6105. org-default-priority))
  6106. ((= dcst ?r)
  6107. (or (org-entry-get nil property) ""))
  6108. ((= dcst ?f)
  6109. (if getkey-func
  6110. (progn
  6111. (setq tmp (funcall getkey-func))
  6112. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6113. tmp)
  6114. (error "Invalid key function `%s'" getkey-func)))
  6115. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6116. nil
  6117. (cond
  6118. ((= dcst ?a) 'string<)
  6119. ((= dcst ?t) 'time-less-p)
  6120. (t nil)))))
  6121. (message "Sorting entries...done")))
  6122. (defun org-do-sort (table what &optional with-case sorting-type)
  6123. "Sort TABLE of WHAT according to SORTING-TYPE.
  6124. The user will be prompted for the SORTING-TYPE if the call to this
  6125. function does not specify it. WHAT is only for the prompt, to indicate
  6126. what is being sorted. The sorting key will be extracted from
  6127. the car of the elements of the table.
  6128. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6129. (unless sorting-type
  6130. (message
  6131. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6132. what)
  6133. (setq sorting-type (read-char-exclusive)))
  6134. (let ((dcst (downcase sorting-type))
  6135. extractfun comparefun)
  6136. ;; Define the appropriate functions
  6137. (cond
  6138. ((= dcst ?n)
  6139. (setq extractfun 'string-to-number
  6140. comparefun (if (= dcst sorting-type) '< '>)))
  6141. ((= dcst ?a)
  6142. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6143. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6144. comparefun (if (= dcst sorting-type)
  6145. 'string<
  6146. (lambda (a b) (and (not (string< a b))
  6147. (not (string= a b)))))))
  6148. ((= dcst ?t)
  6149. (setq extractfun
  6150. (lambda (x)
  6151. (if (string-match org-ts-regexp x)
  6152. (time-to-seconds
  6153. (org-time-string-to-time (match-string 0 x)))
  6154. 0))
  6155. comparefun (if (= dcst sorting-type) '< '>)))
  6156. (t (error "Invalid sorting type `%c'" sorting-type)))
  6157. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6158. table)
  6159. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6160. ;;;; Plain list items, including checkboxes
  6161. ;;; Plain list items
  6162. (defun org-at-item-p ()
  6163. "Is point in a line starting a hand-formatted item?"
  6164. (let ((llt org-plain-list-ordered-item-terminator))
  6165. (save-excursion
  6166. (goto-char (point-at-bol))
  6167. (looking-at
  6168. (cond
  6169. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6170. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6171. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6172. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6173. (defun org-in-item-p ()
  6174. "It the cursor inside a plain list item.
  6175. Does not have to be the first line."
  6176. (save-excursion
  6177. (condition-case nil
  6178. (progn
  6179. (org-beginning-of-item)
  6180. (org-at-item-p)
  6181. t)
  6182. (error nil))))
  6183. (defun org-insert-item (&optional checkbox)
  6184. "Insert a new item at the current level.
  6185. Return t when things worked, nil when we are not in an item."
  6186. (when (save-excursion
  6187. (condition-case nil
  6188. (progn
  6189. (org-beginning-of-item)
  6190. (org-at-item-p)
  6191. (if (org-invisible-p) (error "Invisible item"))
  6192. t)
  6193. (error nil)))
  6194. (let* ((bul (match-string 0))
  6195. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6196. (match-end 0)))
  6197. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6198. pos)
  6199. (cond
  6200. ((and (org-at-item-p) (<= (point) eow))
  6201. ;; before the bullet
  6202. (beginning-of-line 1)
  6203. (open-line (if blank 2 1)))
  6204. ((<= (point) eow)
  6205. (beginning-of-line 1))
  6206. (t (newline (if blank 2 1))))
  6207. (insert bul (if checkbox "[ ]" ""))
  6208. (just-one-space)
  6209. (setq pos (point))
  6210. (end-of-line 1)
  6211. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6212. (org-maybe-renumber-ordered-list)
  6213. (and checkbox (org-update-checkbox-count-maybe))
  6214. t))
  6215. ;;; Checkboxes
  6216. (defun org-at-item-checkbox-p ()
  6217. "Is point at a line starting a plain-list item with a checklet?"
  6218. (and (org-at-item-p)
  6219. (save-excursion
  6220. (goto-char (match-end 0))
  6221. (skip-chars-forward " \t")
  6222. (looking-at "\\[[- X]\\]"))))
  6223. (defun org-toggle-checkbox (&optional arg)
  6224. "Toggle the checkbox in the current line."
  6225. (interactive "P")
  6226. (catch 'exit
  6227. (let (beg end status (firstnew 'unknown))
  6228. (cond
  6229. ((org-region-active-p)
  6230. (setq beg (region-beginning) end (region-end)))
  6231. ((org-on-heading-p)
  6232. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6233. ((org-at-item-checkbox-p)
  6234. (let ((pos (point)))
  6235. (replace-match
  6236. (cond (arg "[-]")
  6237. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6238. (t "[ ]"))
  6239. t t)
  6240. (goto-char pos))
  6241. (throw 'exit t))
  6242. (t (error "Not at a checkbox or heading, and no active region")))
  6243. (save-excursion
  6244. (goto-char beg)
  6245. (while (< (point) end)
  6246. (when (org-at-item-checkbox-p)
  6247. (setq status (equal (match-string 0) "[X]"))
  6248. (when (eq firstnew 'unknown)
  6249. (setq firstnew (not status)))
  6250. (replace-match
  6251. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6252. (beginning-of-line 2)))))
  6253. (org-update-checkbox-count-maybe))
  6254. (defun org-update-checkbox-count-maybe ()
  6255. "Update checkbox statistics unless turned off by user."
  6256. (when org-provide-checkbox-statistics
  6257. (org-update-checkbox-count)))
  6258. (defun org-update-checkbox-count (&optional all)
  6259. "Update the checkbox statistics in the current section.
  6260. This will find all statistic cookies like [57%] and [6/12] and update them
  6261. with the current numbers. With optional prefix argument ALL, do this for
  6262. the whole buffer."
  6263. (interactive "P")
  6264. (save-excursion
  6265. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6266. (beg (condition-case nil
  6267. (progn (outline-back-to-heading) (point))
  6268. (error (point-min))))
  6269. (end (move-marker (make-marker)
  6270. (progn (outline-next-heading) (point))))
  6271. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6272. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6273. (re-find (concat re "\\|" re-box))
  6274. beg-cookie end-cookie is-percent c-on c-off lim
  6275. eline curr-ind next-ind continue-from startsearch
  6276. (cstat 0)
  6277. )
  6278. (when all
  6279. (goto-char (point-min))
  6280. (outline-next-heading)
  6281. (setq beg (point) end (point-max)))
  6282. (goto-char end)
  6283. ;; find each statistic cookie
  6284. (while (re-search-backward re-find beg t)
  6285. (setq beg-cookie (match-beginning 1)
  6286. end-cookie (match-end 1)
  6287. cstat (+ cstat (if end-cookie 1 0))
  6288. startsearch (point-at-eol)
  6289. continue-from (point-at-bol)
  6290. is-percent (match-beginning 2)
  6291. lim (cond
  6292. ((org-on-heading-p) (outline-next-heading) (point))
  6293. ((org-at-item-p) (org-end-of-item) (point))
  6294. (t nil))
  6295. c-on 0
  6296. c-off 0)
  6297. (when lim
  6298. ;; find first checkbox for this cookie and gather
  6299. ;; statistics from all that are at this indentation level
  6300. (goto-char startsearch)
  6301. (if (re-search-forward re-box lim t)
  6302. (progn
  6303. (org-beginning-of-item)
  6304. (setq curr-ind (org-get-indentation))
  6305. (setq next-ind curr-ind)
  6306. (while (= curr-ind next-ind)
  6307. (save-excursion (end-of-line) (setq eline (point)))
  6308. (if (re-search-forward re-box eline t)
  6309. (if (member (match-string 2) '("[ ]" "[-]"))
  6310. (setq c-off (1+ c-off))
  6311. (setq c-on (1+ c-on))
  6312. )
  6313. )
  6314. (org-end-of-item)
  6315. (setq next-ind (org-get-indentation))
  6316. )))
  6317. (goto-char continue-from)
  6318. ;; update cookie
  6319. (when end-cookie
  6320. (delete-region beg-cookie end-cookie)
  6321. (goto-char beg-cookie)
  6322. (insert
  6323. (if is-percent
  6324. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6325. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6326. ;; update items checkbox if it has one
  6327. (when (org-at-item-p)
  6328. (org-beginning-of-item)
  6329. (when (and (> (+ c-on c-off) 0)
  6330. (re-search-forward re-box (point-at-eol) t))
  6331. (setq beg-cookie (match-beginning 2)
  6332. end-cookie (match-end 2))
  6333. (delete-region beg-cookie end-cookie)
  6334. (goto-char beg-cookie)
  6335. (cond ((= c-off 0) (insert "[X]"))
  6336. ((= c-on 0) (insert "[ ]"))
  6337. (t (insert "[-]")))
  6338. )))
  6339. (goto-char continue-from))
  6340. (when (interactive-p)
  6341. (message "Checkbox satistics updated %s (%d places)"
  6342. (if all "in entire file" "in current outline entry") cstat)))))
  6343. (defun org-get-checkbox-statistics-face ()
  6344. "Select the face for checkbox statistics.
  6345. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6346. it will be `org-todo'."
  6347. (if (match-end 1)
  6348. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6349. (if (and (> (match-end 2) (match-beginning 2))
  6350. (equal (match-string 2) (match-string 3)))
  6351. 'org-done
  6352. 'org-todo)))
  6353. (defun org-get-indentation (&optional line)
  6354. "Get the indentation of the current line, interpreting tabs.
  6355. When LINE is given, assume it represents a line and compute its indentation."
  6356. (if line
  6357. (if (string-match "^ *" (org-remove-tabs line))
  6358. (match-end 0))
  6359. (save-excursion
  6360. (beginning-of-line 1)
  6361. (skip-chars-forward " \t")
  6362. (current-column))))
  6363. (defun org-remove-tabs (s &optional width)
  6364. "Replace tabulators in S with spaces.
  6365. Assumes that s is a single line, starting in column 0."
  6366. (setq width (or width tab-width))
  6367. (while (string-match "\t" s)
  6368. (setq s (replace-match
  6369. (make-string
  6370. (- (* width (/ (+ (match-beginning 0) width) width))
  6371. (match-beginning 0)) ?\ )
  6372. t t s)))
  6373. s)
  6374. (defun org-fix-indentation (line ind)
  6375. "Fix indentation in LINE.
  6376. IND is a cons cell with target and minimum indentation.
  6377. If the current indenation in LINE is smaller than the minimum,
  6378. leave it alone. If it is larger than ind, set it to the target."
  6379. (let* ((l (org-remove-tabs line))
  6380. (i (org-get-indentation l))
  6381. (i1 (car ind)) (i2 (cdr ind)))
  6382. (if (>= i i2) (setq l (substring line i2)))
  6383. (if (> i1 0)
  6384. (concat (make-string i1 ?\ ) l)
  6385. l)))
  6386. (defcustom org-empty-line-terminates-plain-lists nil
  6387. "Non-nil means, an empty line ends all plain list levels.
  6388. When nil, empty lines are part of the preceeding item."
  6389. :group 'org-plain-lists
  6390. :type 'boolean)
  6391. (defun org-beginning-of-item ()
  6392. "Go to the beginning of the current hand-formatted item.
  6393. If the cursor is not in an item, throw an error."
  6394. (interactive)
  6395. (let ((pos (point))
  6396. (limit (save-excursion
  6397. (condition-case nil
  6398. (progn
  6399. (org-back-to-heading)
  6400. (beginning-of-line 2) (point))
  6401. (error (point-min)))))
  6402. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6403. ind ind1)
  6404. (if (org-at-item-p)
  6405. (beginning-of-line 1)
  6406. (beginning-of-line 1)
  6407. (skip-chars-forward " \t")
  6408. (setq ind (current-column))
  6409. (if (catch 'exit
  6410. (while t
  6411. (beginning-of-line 0)
  6412. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6413. (if (looking-at "[ \t]*$")
  6414. (setq ind1 ind-empty)
  6415. (skip-chars-forward " \t")
  6416. (setq ind1 (current-column)))
  6417. (if (< ind1 ind)
  6418. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6419. nil
  6420. (goto-char pos)
  6421. (error "Not in an item")))))
  6422. (defun org-end-of-item ()
  6423. "Go to the end of the current hand-formatted item.
  6424. If the cursor is not in an item, throw an error."
  6425. (interactive)
  6426. (let* ((pos (point))
  6427. ind1
  6428. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6429. (limit (save-excursion (outline-next-heading) (point)))
  6430. (ind (save-excursion
  6431. (org-beginning-of-item)
  6432. (skip-chars-forward " \t")
  6433. (current-column)))
  6434. (end (catch 'exit
  6435. (while t
  6436. (beginning-of-line 2)
  6437. (if (eobp) (throw 'exit (point)))
  6438. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6439. (if (looking-at "[ \t]*$")
  6440. (setq ind1 ind-empty)
  6441. (skip-chars-forward " \t")
  6442. (setq ind1 (current-column)))
  6443. (if (<= ind1 ind)
  6444. (throw 'exit (point-at-bol)))))))
  6445. (if end
  6446. (goto-char end)
  6447. (goto-char pos)
  6448. (error "Not in an item"))))
  6449. (defun org-next-item ()
  6450. "Move to the beginning of the next item in the current plain list.
  6451. Error if not at a plain list, or if this is the last item in the list."
  6452. (interactive)
  6453. (let (ind ind1 (pos (point)))
  6454. (org-beginning-of-item)
  6455. (setq ind (org-get-indentation))
  6456. (org-end-of-item)
  6457. (setq ind1 (org-get-indentation))
  6458. (unless (and (org-at-item-p) (= ind ind1))
  6459. (goto-char pos)
  6460. (error "On last item"))))
  6461. (defun org-previous-item ()
  6462. "Move to the beginning of the previous item in the current plain list.
  6463. Error if not at a plain list, or if this is the first item in the list."
  6464. (interactive)
  6465. (let (beg ind ind1 (pos (point)))
  6466. (org-beginning-of-item)
  6467. (setq beg (point))
  6468. (setq ind (org-get-indentation))
  6469. (goto-char beg)
  6470. (catch 'exit
  6471. (while t
  6472. (beginning-of-line 0)
  6473. (if (looking-at "[ \t]*$")
  6474. nil
  6475. (if (<= (setq ind1 (org-get-indentation)) ind)
  6476. (throw 'exit t)))))
  6477. (condition-case nil
  6478. (if (or (not (org-at-item-p))
  6479. (< ind1 (1- ind)))
  6480. (error "")
  6481. (org-beginning-of-item))
  6482. (error (goto-char pos)
  6483. (error "On first item")))))
  6484. (defun org-first-list-item-p ()
  6485. "Is this heading the item in a plain list?"
  6486. (unless (org-at-item-p)
  6487. (error "Not at a plain list item"))
  6488. (org-beginning-of-item)
  6489. (= (point) (save-excursion (org-beginning-of-item-list))))
  6490. (defun org-move-item-down ()
  6491. "Move the plain list item at point down, i.e. swap with following item.
  6492. Subitems (items with larger indentation) are considered part of the item,
  6493. so this really moves item trees."
  6494. (interactive)
  6495. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6496. (org-beginning-of-item)
  6497. (setq beg0 (point))
  6498. (save-excursion
  6499. (setq ne-beg (org-back-over-empty-lines))
  6500. (setq beg (point)))
  6501. (goto-char beg0)
  6502. (setq ind (org-get-indentation))
  6503. (org-end-of-item)
  6504. (setq end0 (point))
  6505. (setq ind1 (org-get-indentation))
  6506. (setq ne-end (org-back-over-empty-lines))
  6507. (setq end (point))
  6508. (goto-char beg0)
  6509. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6510. ;; include less whitespace
  6511. (save-excursion
  6512. (goto-char beg)
  6513. (forward-line (- ne-beg ne-end))
  6514. (setq beg (point))))
  6515. (goto-char end0)
  6516. (if (and (org-at-item-p) (= ind ind1))
  6517. (progn
  6518. (org-end-of-item)
  6519. (org-back-over-empty-lines)
  6520. (setq txt (buffer-substring beg end))
  6521. (save-excursion
  6522. (delete-region beg end))
  6523. (setq pos (point))
  6524. (insert txt)
  6525. (goto-char pos) (org-skip-whitespace)
  6526. (org-maybe-renumber-ordered-list))
  6527. (goto-char pos)
  6528. (error "Cannot move this item further down"))))
  6529. (defun org-move-item-up (arg)
  6530. "Move the plain list item at point up, i.e. swap with previous item.
  6531. Subitems (items with larger indentation) are considered part of the item,
  6532. so this really moves item trees."
  6533. (interactive "p")
  6534. (let (beg beg0 end end0 ind ind1 (pos (point)) txt
  6535. ne-beg ne-end ne-ins ins-end)
  6536. (org-beginning-of-item)
  6537. (setq beg0 (point))
  6538. (setq ind (org-get-indentation))
  6539. (save-excursion
  6540. (setq ne-beg (org-back-over-empty-lines))
  6541. (setq beg (point)))
  6542. (goto-char beg0)
  6543. (org-end-of-item)
  6544. (setq ne-end (org-back-over-empty-lines))
  6545. (setq end (point))
  6546. (goto-char beg0)
  6547. (catch 'exit
  6548. (while t
  6549. (beginning-of-line 0)
  6550. (if (looking-at "[ \t]*$")
  6551. (if org-empty-line-terminates-plain-lists
  6552. (progn
  6553. (goto-char pos)
  6554. (error "Cannot move this item further up"))
  6555. nil)
  6556. (if (<= (setq ind1 (org-get-indentation)) ind)
  6557. (throw 'exit t)))))
  6558. (condition-case nil
  6559. (org-beginning-of-item)
  6560. (error (goto-char beg)
  6561. (error "Cannot move this item further up")))
  6562. (setq ind1 (org-get-indentation))
  6563. (if (and (org-at-item-p) (= ind ind1))
  6564. (progn
  6565. (setq ne-ins (org-back-over-empty-lines))
  6566. (setq txt (buffer-substring beg end))
  6567. (save-excursion
  6568. (delete-region beg end))
  6569. (setq pos (point))
  6570. (insert txt)
  6571. (setq ins-end (point))
  6572. (goto-char pos) (org-skip-whitespace)
  6573. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6574. ;; Move whitespace back to beginning
  6575. (save-excursion
  6576. (goto-char ins-end)
  6577. (let ((kill-whole-line t))
  6578. (kill-line (- ne-ins ne-beg)) (point)))
  6579. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6580. (org-maybe-renumber-ordered-list))
  6581. (goto-char pos)
  6582. (error "Cannot move this item further up"))))
  6583. (defun org-maybe-renumber-ordered-list ()
  6584. "Renumber the ordered list at point if setup allows it.
  6585. This tests the user option `org-auto-renumber-ordered-lists' before
  6586. doing the renumbering."
  6587. (interactive)
  6588. (when (and org-auto-renumber-ordered-lists
  6589. (org-at-item-p))
  6590. (if (match-beginning 3)
  6591. (org-renumber-ordered-list 1)
  6592. (org-fix-bullet-type))))
  6593. (defun org-maybe-renumber-ordered-list-safe ()
  6594. (condition-case nil
  6595. (save-excursion
  6596. (org-maybe-renumber-ordered-list))
  6597. (error nil)))
  6598. (defun org-cycle-list-bullet (&optional which)
  6599. "Cycle through the different itemize/enumerate bullets.
  6600. This cycle the entire list level through the sequence:
  6601. `-' -> `+' -> `*' -> `1.' -> `1)'
  6602. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6603. 0 meand `-', 1 means `+' etc."
  6604. (interactive "P")
  6605. (org-preserve-lc
  6606. (org-beginning-of-item-list)
  6607. (org-at-item-p)
  6608. (beginning-of-line 1)
  6609. (let ((current (match-string 0))
  6610. (prevp (eq which 'previous))
  6611. new)
  6612. (setq new (cond
  6613. ((and (numberp which)
  6614. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6615. ((string-match "-" current) (if prevp "1)" "+"))
  6616. ((string-match "\\+" current)
  6617. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6618. ((string-match "\\*" current) (if prevp "+" "1."))
  6619. ((string-match "\\." current) (if prevp "*" "1)"))
  6620. ((string-match ")" current) (if prevp "1." "-"))
  6621. (t (error "This should not happen"))))
  6622. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6623. (org-fix-bullet-type)
  6624. (org-maybe-renumber-ordered-list))))
  6625. (defun org-get-string-indentation (s)
  6626. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6627. (let ((n -1) (i 0) (w tab-width) c)
  6628. (catch 'exit
  6629. (while (< (setq n (1+ n)) (length s))
  6630. (setq c (aref s n))
  6631. (cond ((= c ?\ ) (setq i (1+ i)))
  6632. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6633. (t (throw 'exit t)))))
  6634. i))
  6635. (defun org-renumber-ordered-list (arg)
  6636. "Renumber an ordered plain list.
  6637. Cursor needs to be in the first line of an item, the line that starts
  6638. with something like \"1.\" or \"2)\"."
  6639. (interactive "p")
  6640. (unless (and (org-at-item-p)
  6641. (match-beginning 3))
  6642. (error "This is not an ordered list"))
  6643. (let ((line (org-current-line))
  6644. (col (current-column))
  6645. (ind (org-get-string-indentation
  6646. (buffer-substring (point-at-bol) (match-beginning 3))))
  6647. ;; (term (substring (match-string 3) -1))
  6648. ind1 (n (1- arg))
  6649. fmt)
  6650. ;; find where this list begins
  6651. (org-beginning-of-item-list)
  6652. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6653. (setq fmt (concat "%d" (match-string 1)))
  6654. (beginning-of-line 0)
  6655. ;; walk forward and replace these numbers
  6656. (catch 'exit
  6657. (while t
  6658. (catch 'next
  6659. (beginning-of-line 2)
  6660. (if (eobp) (throw 'exit nil))
  6661. (if (looking-at "[ \t]*$") (throw 'next nil))
  6662. (skip-chars-forward " \t") (setq ind1 (current-column))
  6663. (if (> ind1 ind) (throw 'next t))
  6664. (if (< ind1 ind) (throw 'exit t))
  6665. (if (not (org-at-item-p)) (throw 'exit nil))
  6666. (delete-region (match-beginning 2) (match-end 2))
  6667. (goto-char (match-beginning 2))
  6668. (insert (format fmt (setq n (1+ n)))))))
  6669. (goto-line line)
  6670. (move-to-column col)))
  6671. (defun org-fix-bullet-type ()
  6672. "Make sure all items in this list have the same bullet as the firsst item."
  6673. (interactive)
  6674. (unless (org-at-item-p) (error "This is not a list"))
  6675. (let ((line (org-current-line))
  6676. (col (current-column))
  6677. (ind (current-indentation))
  6678. ind1 bullet)
  6679. ;; find where this list begins
  6680. (org-beginning-of-item-list)
  6681. (beginning-of-line 1)
  6682. ;; find out what the bullet type is
  6683. (looking-at "[ \t]*\\(\\S-+\\)")
  6684. (setq bullet (match-string 1))
  6685. ;; walk forward and replace these numbers
  6686. (beginning-of-line 0)
  6687. (catch 'exit
  6688. (while t
  6689. (catch 'next
  6690. (beginning-of-line 2)
  6691. (if (eobp) (throw 'exit nil))
  6692. (if (looking-at "[ \t]*$") (throw 'next nil))
  6693. (skip-chars-forward " \t") (setq ind1 (current-column))
  6694. (if (> ind1 ind) (throw 'next t))
  6695. (if (< ind1 ind) (throw 'exit t))
  6696. (if (not (org-at-item-p)) (throw 'exit nil))
  6697. (skip-chars-forward " \t")
  6698. (looking-at "\\S-+")
  6699. (replace-match bullet))))
  6700. (goto-line line)
  6701. (move-to-column col)
  6702. (if (string-match "[0-9]" bullet)
  6703. (org-renumber-ordered-list 1))))
  6704. (defun org-beginning-of-item-list ()
  6705. "Go to the beginning of the current item list.
  6706. I.e. to the first item in this list."
  6707. (interactive)
  6708. (org-beginning-of-item)
  6709. (let ((pos (point-at-bol))
  6710. (ind (org-get-indentation))
  6711. ind1)
  6712. ;; find where this list begins
  6713. (catch 'exit
  6714. (while t
  6715. (catch 'next
  6716. (beginning-of-line 0)
  6717. (if (looking-at "[ \t]*$")
  6718. (throw (if (bobp) 'exit 'next) t))
  6719. (skip-chars-forward " \t") (setq ind1 (current-column))
  6720. (if (or (< ind1 ind)
  6721. (and (= ind1 ind)
  6722. (not (org-at-item-p)))
  6723. (bobp))
  6724. (throw 'exit t)
  6725. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6726. (goto-char pos)))
  6727. (defun org-end-of-item-list ()
  6728. "Go to the end of the current item list.
  6729. I.e. to the text after the last item."
  6730. (interactive)
  6731. (org-beginning-of-item)
  6732. (let ((pos (point-at-bol))
  6733. (ind (org-get-indentation))
  6734. ind1)
  6735. ;; find where this list begins
  6736. (catch 'exit
  6737. (while t
  6738. (catch 'next
  6739. (beginning-of-line 2)
  6740. (if (looking-at "[ \t]*$")
  6741. (throw (if (eobp) 'exit 'next) t))
  6742. (skip-chars-forward " \t") (setq ind1 (current-column))
  6743. (if (or (< ind1 ind)
  6744. (and (= ind1 ind)
  6745. (not (org-at-item-p)))
  6746. (eobp))
  6747. (progn
  6748. (setq pos (point-at-bol))
  6749. (throw 'exit t))))))
  6750. (goto-char pos)))
  6751. (defvar org-last-indent-begin-marker (make-marker))
  6752. (defvar org-last-indent-end-marker (make-marker))
  6753. (defun org-outdent-item (arg)
  6754. "Outdent a local list item."
  6755. (interactive "p")
  6756. (org-indent-item (- arg)))
  6757. (defun org-indent-item (arg)
  6758. "Indent a local list item."
  6759. (interactive "p")
  6760. (unless (org-at-item-p)
  6761. (error "Not on an item"))
  6762. (save-excursion
  6763. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6764. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6765. (setq beg org-last-indent-begin-marker
  6766. end org-last-indent-end-marker)
  6767. (org-beginning-of-item)
  6768. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6769. (org-end-of-item)
  6770. (setq end (move-marker org-last-indent-end-marker (point))))
  6771. (goto-char beg)
  6772. (setq tmp (org-item-indent-positions)
  6773. ind (car tmp)
  6774. ind-down (nth 2 tmp)
  6775. ind-up (nth 1 tmp)
  6776. delta (if (> arg 0)
  6777. (if ind-down (- ind-down ind) 2)
  6778. (if ind-up (- ind-up ind) -2)))
  6779. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6780. (while (< (point) end)
  6781. (beginning-of-line 1)
  6782. (skip-chars-forward " \t") (setq ind1 (current-column))
  6783. (delete-region (point-at-bol) (point))
  6784. (or (eolp) (indent-to-column (+ ind1 delta)))
  6785. (beginning-of-line 2))))
  6786. (org-fix-bullet-type)
  6787. (org-maybe-renumber-ordered-list-safe)
  6788. (save-excursion
  6789. (beginning-of-line 0)
  6790. (condition-case nil (org-beginning-of-item) (error nil))
  6791. (org-maybe-renumber-ordered-list-safe)))
  6792. (defun org-item-indent-positions ()
  6793. "Return indentation for plain list items.
  6794. This returns a list with three values: The current indentation, the
  6795. parent indentation and the indentation a child should habe.
  6796. Assumes cursor in item line."
  6797. (let* ((bolpos (point-at-bol))
  6798. (ind (org-get-indentation))
  6799. ind-down ind-up pos)
  6800. (save-excursion
  6801. (org-beginning-of-item-list)
  6802. (skip-chars-backward "\n\r \t")
  6803. (when (org-in-item-p)
  6804. (org-beginning-of-item)
  6805. (setq ind-up (org-get-indentation))))
  6806. (setq pos (point))
  6807. (save-excursion
  6808. (cond
  6809. ((and (condition-case nil (progn (org-previous-item) t)
  6810. (error nil))
  6811. (or (forward-char 1) t)
  6812. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6813. (setq ind-down (org-get-indentation)))
  6814. ((and (goto-char pos)
  6815. (org-at-item-p))
  6816. (goto-char (match-end 0))
  6817. (skip-chars-forward " \t")
  6818. (setq ind-down (current-column)))))
  6819. (list ind ind-up ind-down)))
  6820. ;;; The orgstruct minor mode
  6821. ;; Define a minor mode which can be used in other modes in order to
  6822. ;; integrate the org-mode structure editing commands.
  6823. ;; This is really a hack, because the org-mode structure commands use
  6824. ;; keys which normally belong to the major mode. Here is how it
  6825. ;; works: The minor mode defines all the keys necessary to operate the
  6826. ;; structure commands, but wraps the commands into a function which
  6827. ;; tests if the cursor is currently at a headline or a plain list
  6828. ;; item. If that is the case, the structure command is used,
  6829. ;; temporarily setting many Org-mode variables like regular
  6830. ;; expressions for filling etc. However, when any of those keys is
  6831. ;; used at a different location, function uses `key-binding' to look
  6832. ;; up if the key has an associated command in another currently active
  6833. ;; keymap (minor modes, major mode, global), and executes that
  6834. ;; command. There might be problems if any of the keys is otherwise
  6835. ;; used as a prefix key.
  6836. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6837. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6838. ;; addresses this by checking explicitly for both bindings.
  6839. (defvar orgstruct-mode-map (make-sparse-keymap)
  6840. "Keymap for the minor `orgstruct-mode'.")
  6841. (defvar org-local-vars nil
  6842. "List of local variables, for use by `orgstruct-mode'")
  6843. ;;;###autoload
  6844. (define-minor-mode orgstruct-mode
  6845. "Toggle the minor more `orgstruct-mode'.
  6846. This mode is for using Org-mode structure commands in other modes.
  6847. The following key behave as if Org-mode was active, if the cursor
  6848. is on a headline, or on a plain list item (both in the definition
  6849. of Org-mode).
  6850. M-up Move entry/item up
  6851. M-down Move entry/item down
  6852. M-left Promote
  6853. M-right Demote
  6854. M-S-up Move entry/item up
  6855. M-S-down Move entry/item down
  6856. M-S-left Promote subtree
  6857. M-S-right Demote subtree
  6858. M-q Fill paragraph and items like in Org-mode
  6859. C-c ^ Sort entries
  6860. C-c - Cycle list bullet
  6861. TAB Cycle item visibility
  6862. M-RET Insert new heading/item
  6863. S-M-RET Insert new TODO heading / Chekbox item
  6864. C-c C-c Set tags / toggle checkbox"
  6865. nil " OrgStruct" nil
  6866. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6867. ;;;###autoload
  6868. (defun turn-on-orgstruct ()
  6869. "Unconditionally turn on `orgstruct-mode'."
  6870. (orgstruct-mode 1))
  6871. ;;;###autoload
  6872. (defun turn-on-orgstruct++ ()
  6873. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6874. In addition to setting orgstruct-mode, this also exports all indentation and
  6875. autofilling variables from org-mode into the buffer. Note that turning
  6876. off orgstruct-mode will *not* remove these additional settings."
  6877. (orgstruct-mode 1)
  6878. (let (var val)
  6879. (mapc
  6880. (lambda (x)
  6881. (when (string-match
  6882. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6883. (symbol-name (car x)))
  6884. (setq var (car x) val (nth 1 x))
  6885. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6886. org-local-vars)))
  6887. (defun orgstruct-error ()
  6888. "Error when there is no default binding for a structure key."
  6889. (interactive)
  6890. (error "This key has no function outside structure elements"))
  6891. (defun orgstruct-setup ()
  6892. "Setup orgstruct keymaps."
  6893. (let ((nfunc 0)
  6894. (bindings
  6895. (list
  6896. '([(meta up)] org-metaup)
  6897. '([(meta down)] org-metadown)
  6898. '([(meta left)] org-metaleft)
  6899. '([(meta right)] org-metaright)
  6900. '([(meta shift up)] org-shiftmetaup)
  6901. '([(meta shift down)] org-shiftmetadown)
  6902. '([(meta shift left)] org-shiftmetaleft)
  6903. '([(meta shift right)] org-shiftmetaright)
  6904. '([(shift up)] org-shiftup)
  6905. '([(shift down)] org-shiftdown)
  6906. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6907. '("\M-q" fill-paragraph)
  6908. '("\C-c^" org-sort)
  6909. '("\C-c-" org-cycle-list-bullet)))
  6910. elt key fun cmd)
  6911. (while (setq elt (pop bindings))
  6912. (setq nfunc (1+ nfunc))
  6913. (setq key (org-key (car elt))
  6914. fun (nth 1 elt)
  6915. cmd (orgstruct-make-binding fun nfunc key))
  6916. (org-defkey orgstruct-mode-map key cmd))
  6917. ;; Special treatment needed for TAB and RET
  6918. (org-defkey orgstruct-mode-map [(tab)]
  6919. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6920. (org-defkey orgstruct-mode-map "\C-i"
  6921. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6922. (org-defkey orgstruct-mode-map "\M-\C-m"
  6923. (orgstruct-make-binding 'org-insert-heading 105
  6924. "\M-\C-m" [(meta return)]))
  6925. (org-defkey orgstruct-mode-map [(meta return)]
  6926. (orgstruct-make-binding 'org-insert-heading 106
  6927. [(meta return)] "\M-\C-m"))
  6928. (org-defkey orgstruct-mode-map [(shift meta return)]
  6929. (orgstruct-make-binding 'org-insert-todo-heading 107
  6930. [(meta return)] "\M-\C-m"))
  6931. (unless org-local-vars
  6932. (setq org-local-vars (org-get-local-variables)))
  6933. t))
  6934. (defun orgstruct-make-binding (fun n &rest keys)
  6935. "Create a function for binding in the structure minor mode.
  6936. FUN is the command to call inside a table. N is used to create a unique
  6937. command name. KEYS are keys that should be checked in for a command
  6938. to execute outside of tables."
  6939. (eval
  6940. (list 'defun
  6941. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6942. '(arg)
  6943. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6944. "Outside of structure, run the binding of `"
  6945. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6946. "'.")
  6947. '(interactive "p")
  6948. (list 'if
  6949. '(org-context-p 'headline 'item)
  6950. (list 'org-run-like-in-org-mode (list 'quote fun))
  6951. (list 'let '(orgstruct-mode)
  6952. (list 'call-interactively
  6953. (append '(or)
  6954. (mapcar (lambda (k)
  6955. (list 'key-binding k))
  6956. keys)
  6957. '('orgstruct-error))))))))
  6958. (defun org-context-p (&rest contexts)
  6959. "Check if local context is and of CONTEXTS.
  6960. Possible values in the list of contexts are `table', `headline', and `item'."
  6961. (let ((pos (point)))
  6962. (goto-char (point-at-bol))
  6963. (prog1 (or (and (memq 'table contexts)
  6964. (looking-at "[ \t]*|"))
  6965. (and (memq 'headline contexts)
  6966. (looking-at "\\*+"))
  6967. (and (memq 'item contexts)
  6968. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6969. (goto-char pos))))
  6970. (defun org-get-local-variables ()
  6971. "Return a list of all local variables in an org-mode buffer."
  6972. (let (varlist)
  6973. (with-current-buffer (get-buffer-create "*Org tmp*")
  6974. (erase-buffer)
  6975. (org-mode)
  6976. (setq varlist (buffer-local-variables)))
  6977. (kill-buffer "*Org tmp*")
  6978. (delq nil
  6979. (mapcar
  6980. (lambda (x)
  6981. (setq x
  6982. (if (symbolp x)
  6983. (list x)
  6984. (list (car x) (list 'quote (cdr x)))))
  6985. (if (string-match
  6986. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6987. (symbol-name (car x)))
  6988. x nil))
  6989. varlist))))
  6990. ;;;###autoload
  6991. (defun org-run-like-in-org-mode (cmd)
  6992. (unless org-local-vars
  6993. (setq org-local-vars (org-get-local-variables)))
  6994. (eval (list 'let org-local-vars
  6995. (list 'call-interactively (list 'quote cmd)))))
  6996. ;;;; Archiving
  6997. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6998. (defun org-archive-subtree (&optional find-done)
  6999. "Move the current subtree to the archive.
  7000. The archive can be a certain top-level heading in the current file, or in
  7001. a different file. The tree will be moved to that location, the subtree
  7002. heading be marked DONE, and the current time will be added.
  7003. When called with prefix argument FIND-DONE, find whole trees without any
  7004. open TODO items and archive them (after getting confirmation from the user).
  7005. If the cursor is not at a headline when this comand is called, try all level
  7006. 1 trees. If the cursor is on a headline, only try the direct children of
  7007. this heading."
  7008. (interactive "P")
  7009. (if find-done
  7010. (org-archive-all-done)
  7011. ;; Save all relevant TODO keyword-relatex variables
  7012. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7013. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7014. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7015. (tr-org-done-keywords org-done-keywords)
  7016. (tr-org-todo-regexp org-todo-regexp)
  7017. (tr-org-todo-line-regexp org-todo-line-regexp)
  7018. (tr-org-odd-levels-only org-odd-levels-only)
  7019. (this-buffer (current-buffer))
  7020. (org-archive-location org-archive-location)
  7021. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7022. ;; start of variables that will be used for saving context
  7023. ;; The compiler complains about them - keep them anyway!
  7024. (file (abbreviate-file-name (buffer-file-name)))
  7025. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7026. (time (format-time-string
  7027. (substring (cdr org-time-stamp-formats) 1 -1)
  7028. (current-time)))
  7029. afile heading buffer level newfile-p
  7030. category todo priority
  7031. ;; start of variables that will be used for savind context
  7032. ltags itags prop)
  7033. ;; Try to find a local archive location
  7034. (save-excursion
  7035. (save-restriction
  7036. (widen)
  7037. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7038. (if (and prop (string-match "\\S-" prop))
  7039. (setq org-archive-location prop)
  7040. (if (or (re-search-backward re nil t)
  7041. (re-search-forward re nil t))
  7042. (setq org-archive-location (match-string 1))))))
  7043. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7044. (progn
  7045. (setq afile (format (match-string 1 org-archive-location)
  7046. (file-name-nondirectory buffer-file-name))
  7047. heading (match-string 2 org-archive-location)))
  7048. (error "Invalid `org-archive-location'"))
  7049. (if (> (length afile) 0)
  7050. (setq newfile-p (not (file-exists-p afile))
  7051. buffer (find-file-noselect afile))
  7052. (setq buffer (current-buffer)))
  7053. (unless buffer
  7054. (error "Cannot access file \"%s\"" afile))
  7055. (if (and (> (length heading) 0)
  7056. (string-match "^\\*+" heading))
  7057. (setq level (match-end 0))
  7058. (setq heading nil level 0))
  7059. (save-excursion
  7060. (org-back-to-heading t)
  7061. ;; Get context information that will be lost by moving the tree
  7062. (org-refresh-category-properties)
  7063. (setq category (org-get-category)
  7064. todo (and (looking-at org-todo-line-regexp)
  7065. (match-string 2))
  7066. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7067. ltags (org-get-tags)
  7068. itags (org-delete-all ltags (org-get-tags-at)))
  7069. (setq ltags (mapconcat 'identity ltags " ")
  7070. itags (mapconcat 'identity itags " "))
  7071. ;; We first only copy, in case something goes wrong
  7072. ;; we need to protect this-command, to avoid kill-region sets it,
  7073. ;; which would lead to duplication of subtrees
  7074. (let (this-command) (org-copy-subtree))
  7075. (set-buffer buffer)
  7076. ;; Enforce org-mode for the archive buffer
  7077. (if (not (org-mode-p))
  7078. ;; Force the mode for future visits.
  7079. (let ((org-insert-mode-line-in-empty-file t)
  7080. (org-inhibit-startup t))
  7081. (call-interactively 'org-mode)))
  7082. (when newfile-p
  7083. (goto-char (point-max))
  7084. (insert (format "\nArchived entries from file %s\n\n"
  7085. (buffer-file-name this-buffer))))
  7086. ;; Force the TODO keywords of the original buffer
  7087. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7088. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7089. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7090. (org-done-keywords tr-org-done-keywords)
  7091. (org-todo-regexp tr-org-todo-regexp)
  7092. (org-todo-line-regexp tr-org-todo-line-regexp)
  7093. (org-odd-levels-only
  7094. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7095. org-odd-levels-only
  7096. tr-org-odd-levels-only)))
  7097. (goto-char (point-min))
  7098. (if heading
  7099. (progn
  7100. (if (re-search-forward
  7101. (concat "^" (regexp-quote heading)
  7102. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7103. nil t)
  7104. (goto-char (match-end 0))
  7105. ;; Heading not found, just insert it at the end
  7106. (goto-char (point-max))
  7107. (or (bolp) (insert "\n"))
  7108. (insert "\n" heading "\n")
  7109. (end-of-line 0))
  7110. ;; Make the subtree visible
  7111. (show-subtree)
  7112. (org-end-of-subtree t)
  7113. (skip-chars-backward " \t\r\n")
  7114. (and (looking-at "[ \t\r\n]*")
  7115. (replace-match "\n\n")))
  7116. ;; No specific heading, just go to end of file.
  7117. (goto-char (point-max)) (insert "\n"))
  7118. ;; Paste
  7119. (org-paste-subtree (org-get-legal-level level 1))
  7120. ;; Mark the entry as done
  7121. (when (and org-archive-mark-done
  7122. (looking-at org-todo-line-regexp)
  7123. (or (not (match-end 2))
  7124. (not (member (match-string 2) org-done-keywords))))
  7125. (let (org-log-done)
  7126. (org-todo
  7127. (car (or (member org-archive-mark-done org-done-keywords)
  7128. org-done-keywords)))))
  7129. ;; Add the context info
  7130. (when org-archive-save-context-info
  7131. (let ((l org-archive-save-context-info) e n v)
  7132. (while (setq e (pop l))
  7133. (when (and (setq v (symbol-value e))
  7134. (stringp v) (string-match "\\S-" v))
  7135. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7136. (org-entry-put (point) n v)))))
  7137. ;; Save the buffer, if it is not the same buffer.
  7138. (if (not (eq this-buffer buffer)) (save-buffer))))
  7139. ;; Here we are back in the original buffer. Everything seems to have
  7140. ;; worked. So now cut the tree and finish up.
  7141. (let (this-command) (org-cut-subtree))
  7142. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7143. (message "Subtree archived %s"
  7144. (if (eq this-buffer buffer)
  7145. (concat "under heading: " heading)
  7146. (concat "in file: " (abbreviate-file-name afile)))))))
  7147. (defun org-refresh-category-properties ()
  7148. "Refresh category text properties in teh buffer."
  7149. (let ((def-cat (cond
  7150. ((null org-category)
  7151. (if buffer-file-name
  7152. (file-name-sans-extension
  7153. (file-name-nondirectory buffer-file-name))
  7154. "???"))
  7155. ((symbolp org-category) (symbol-name org-category))
  7156. (t org-category)))
  7157. beg end cat pos optionp)
  7158. (org-unmodified
  7159. (save-excursion
  7160. (save-restriction
  7161. (widen)
  7162. (goto-char (point-min))
  7163. (put-text-property (point) (point-max) 'org-category def-cat)
  7164. (while (re-search-forward
  7165. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7166. (setq pos (match-end 0)
  7167. optionp (equal (char-after (match-beginning 0)) ?#)
  7168. cat (org-trim (match-string 2)))
  7169. (if optionp
  7170. (setq beg (point-at-bol) end (point-max))
  7171. (org-back-to-heading t)
  7172. (setq beg (point) end (org-end-of-subtree t t)))
  7173. (put-text-property beg end 'org-category cat)
  7174. (goto-char pos)))))))
  7175. (defun org-archive-all-done (&optional tag)
  7176. "Archive sublevels of the current tree without open TODO items.
  7177. If the cursor is not on a headline, try all level 1 trees. If
  7178. it is on a headline, try all direct children.
  7179. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7180. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7181. (rea (concat ".*:" org-archive-tag ":"))
  7182. (begm (make-marker))
  7183. (endm (make-marker))
  7184. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7185. "Move subtree to archive (no open TODO items)? "))
  7186. beg end (cntarch 0))
  7187. (if (org-on-heading-p)
  7188. (progn
  7189. (setq re1 (concat "^" (regexp-quote
  7190. (make-string
  7191. (1+ (- (match-end 0) (match-beginning 0) 1))
  7192. ?*))
  7193. " "))
  7194. (move-marker begm (point))
  7195. (move-marker endm (org-end-of-subtree t)))
  7196. (setq re1 "^* ")
  7197. (move-marker begm (point-min))
  7198. (move-marker endm (point-max)))
  7199. (save-excursion
  7200. (goto-char begm)
  7201. (while (re-search-forward re1 endm t)
  7202. (setq beg (match-beginning 0)
  7203. end (save-excursion (org-end-of-subtree t) (point)))
  7204. (goto-char beg)
  7205. (if (re-search-forward re end t)
  7206. (goto-char end)
  7207. (goto-char beg)
  7208. (if (and (or (not tag) (not (looking-at rea)))
  7209. (y-or-n-p question))
  7210. (progn
  7211. (if tag
  7212. (org-toggle-tag org-archive-tag 'on)
  7213. (org-archive-subtree))
  7214. (setq cntarch (1+ cntarch)))
  7215. (goto-char end)))))
  7216. (message "%d trees archived" cntarch)))
  7217. (defun org-cycle-hide-drawers (state)
  7218. "Re-hide all drawers after a visibility state change."
  7219. (when (and (org-mode-p)
  7220. (not (memq state '(overview folded))))
  7221. (save-excursion
  7222. (let* ((globalp (memq state '(contents all)))
  7223. (beg (if globalp (point-min) (point)))
  7224. (end (if globalp (point-max) (org-end-of-subtree t))))
  7225. (goto-char beg)
  7226. (while (re-search-forward org-drawer-regexp end t)
  7227. (org-flag-drawer t))))))
  7228. (defun org-flag-drawer (flag)
  7229. (save-excursion
  7230. (beginning-of-line 1)
  7231. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7232. (let ((b (match-end 0))
  7233. (outline-regexp org-outline-regexp))
  7234. (if (re-search-forward
  7235. "^[ \t]*:END:"
  7236. (save-excursion (outline-next-heading) (point)) t)
  7237. (outline-flag-region b (point-at-eol) flag)
  7238. (error ":END: line missing"))))))
  7239. (defun org-cycle-hide-archived-subtrees (state)
  7240. "Re-hide all archived subtrees after a visibility state change."
  7241. (when (and (not org-cycle-open-archived-trees)
  7242. (not (memq state '(overview folded))))
  7243. (save-excursion
  7244. (let* ((globalp (memq state '(contents all)))
  7245. (beg (if globalp (point-min) (point)))
  7246. (end (if globalp (point-max) (org-end-of-subtree t))))
  7247. (org-hide-archived-subtrees beg end)
  7248. (goto-char beg)
  7249. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7250. (message "%s" (substitute-command-keys
  7251. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7252. (defun org-force-cycle-archived ()
  7253. "Cycle subtree even if it is archived."
  7254. (interactive)
  7255. (setq this-command 'org-cycle)
  7256. (let ((org-cycle-open-archived-trees t))
  7257. (call-interactively 'org-cycle)))
  7258. (defun org-hide-archived-subtrees (beg end)
  7259. "Re-hide all archived subtrees after a visibility state change."
  7260. (save-excursion
  7261. (let* ((re (concat ":" org-archive-tag ":")))
  7262. (goto-char beg)
  7263. (while (re-search-forward re end t)
  7264. (and (org-on-heading-p) (hide-subtree))
  7265. (org-end-of-subtree t)))))
  7266. (defun org-toggle-tag (tag &optional onoff)
  7267. "Toggle the tag TAG for the current line.
  7268. If ONOFF is `on' or `off', don't toggle but set to this state."
  7269. (unless (org-on-heading-p t) (error "Not on headling"))
  7270. (let (res current)
  7271. (save-excursion
  7272. (beginning-of-line)
  7273. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7274. (point-at-eol) t)
  7275. (progn
  7276. (setq current (match-string 1))
  7277. (replace-match ""))
  7278. (setq current ""))
  7279. (setq current (nreverse (org-split-string current ":")))
  7280. (cond
  7281. ((eq onoff 'on)
  7282. (setq res t)
  7283. (or (member tag current) (push tag current)))
  7284. ((eq onoff 'off)
  7285. (or (not (member tag current)) (setq current (delete tag current))))
  7286. (t (if (member tag current)
  7287. (setq current (delete tag current))
  7288. (setq res t)
  7289. (push tag current))))
  7290. (end-of-line 1)
  7291. (if current
  7292. (progn
  7293. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7294. (org-set-tags nil t))
  7295. (delete-horizontal-space))
  7296. (run-hooks 'org-after-tags-change-hook))
  7297. res))
  7298. (defun org-toggle-archive-tag (&optional arg)
  7299. "Toggle the archive tag for the current headline.
  7300. With prefix ARG, check all children of current headline and offer tagging
  7301. the children that do not contain any open TODO items."
  7302. (interactive "P")
  7303. (if arg
  7304. (org-archive-all-done 'tag)
  7305. (let (set)
  7306. (save-excursion
  7307. (org-back-to-heading t)
  7308. (setq set (org-toggle-tag org-archive-tag))
  7309. (when set (hide-subtree)))
  7310. (and set (beginning-of-line 1))
  7311. (message "Subtree %s" (if set "archived" "unarchived")))))
  7312. ;;;; Tables
  7313. ;;; The table editor
  7314. ;; Watch out: Here we are talking about two different kind of tables.
  7315. ;; Most of the code is for the tables created with the Org-mode table editor.
  7316. ;; Sometimes, we talk about tables created and edited with the table.el
  7317. ;; Emacs package. We call the former org-type tables, and the latter
  7318. ;; table.el-type tables.
  7319. (defun org-before-change-function (beg end)
  7320. "Every change indicates that a table might need an update."
  7321. (setq org-table-may-need-update t))
  7322. (defconst org-table-line-regexp "^[ \t]*|"
  7323. "Detects an org-type table line.")
  7324. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7325. "Detects an org-type table line.")
  7326. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7327. "Detects a table line marked for automatic recalculation.")
  7328. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7329. "Detects a table line marked for automatic recalculation.")
  7330. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7331. "Detects a table line marked for automatic recalculation.")
  7332. (defconst org-table-hline-regexp "^[ \t]*|-"
  7333. "Detects an org-type table hline.")
  7334. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7335. "Detects a table-type table hline.")
  7336. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7337. "Detects an org-type or table-type table.")
  7338. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7339. "Searching from within a table (any type) this finds the first line
  7340. outside the table.")
  7341. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7342. "Searching from within a table (any type) this finds the first line
  7343. outside the table.")
  7344. (defvar org-table-last-highlighted-reference nil)
  7345. (defvar org-table-formula-history nil)
  7346. (defvar org-table-column-names nil
  7347. "Alist with column names, derived from the `!' line.")
  7348. (defvar org-table-column-name-regexp nil
  7349. "Regular expression matching the current column names.")
  7350. (defvar org-table-local-parameters nil
  7351. "Alist with parameter names, derived from the `$' line.")
  7352. (defvar org-table-named-field-locations nil
  7353. "Alist with locations of named fields.")
  7354. (defvar org-table-current-line-types nil
  7355. "Table row types, non-nil only for the duration of a comand.")
  7356. (defvar org-table-current-begin-line nil
  7357. "Table begin line, non-nil only for the duration of a comand.")
  7358. (defvar org-table-current-begin-pos nil
  7359. "Table begin position, non-nil only for the duration of a comand.")
  7360. (defvar org-table-dlines nil
  7361. "Vector of data line line numbers in the current table.")
  7362. (defvar org-table-hlines nil
  7363. "Vector of hline line numbers in the current table.")
  7364. (defconst org-table-range-regexp
  7365. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7366. ;; 1 2 3 4 5
  7367. "Regular expression for matching ranges in formulas.")
  7368. (defconst org-table-range-regexp2
  7369. (concat
  7370. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7371. "\\.\\."
  7372. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7373. "Match a range for reference display.")
  7374. (defconst org-table-translate-regexp
  7375. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7376. "Match a reference that needs translation, for reference display.")
  7377. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7378. (defun org-table-create-with-table.el ()
  7379. "Use the table.el package to insert a new table.
  7380. If there is already a table at point, convert between Org-mode tables
  7381. and table.el tables."
  7382. (interactive)
  7383. (require 'table)
  7384. (cond
  7385. ((org-at-table.el-p)
  7386. (if (y-or-n-p "Convert table to Org-mode table? ")
  7387. (org-table-convert)))
  7388. ((org-at-table-p)
  7389. (if (y-or-n-p "Convert table to table.el table? ")
  7390. (org-table-convert)))
  7391. (t (call-interactively 'table-insert))))
  7392. (defun org-table-create-or-convert-from-region (arg)
  7393. "Convert region to table, or create an empty table.
  7394. If there is an active region, convert it to a table, using the function
  7395. `org-table-convert-region'. See the documentation of that function
  7396. to learn how the prefix argument is interpreted to determine the field
  7397. separator.
  7398. If there is no such region, create an empty table with `org-table-create'."
  7399. (interactive "P")
  7400. (if (org-region-active-p)
  7401. (org-table-convert-region (region-beginning) (region-end) arg)
  7402. (org-table-create arg)))
  7403. (defun org-table-create (&optional size)
  7404. "Query for a size and insert a table skeleton.
  7405. SIZE is a string Columns x Rows like for example \"3x2\"."
  7406. (interactive "P")
  7407. (unless size
  7408. (setq size (read-string
  7409. (concat "Table size Columns x Rows [e.g. "
  7410. org-table-default-size "]: ")
  7411. "" nil org-table-default-size)))
  7412. (let* ((pos (point))
  7413. (indent (make-string (current-column) ?\ ))
  7414. (split (org-split-string size " *x *"))
  7415. (rows (string-to-number (nth 1 split)))
  7416. (columns (string-to-number (car split)))
  7417. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7418. "\n")))
  7419. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7420. (point-at-bol) (point)))
  7421. (beginning-of-line 1)
  7422. (newline))
  7423. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7424. (dotimes (i rows) (insert line))
  7425. (goto-char pos)
  7426. (if (> rows 1)
  7427. ;; Insert a hline after the first row.
  7428. (progn
  7429. (end-of-line 1)
  7430. (insert "\n|-")
  7431. (goto-char pos)))
  7432. (org-table-align)))
  7433. (defun org-table-convert-region (beg0 end0 &optional separator)
  7434. "Convert region to a table.
  7435. The region goes from BEG0 to END0, but these borders will be moved
  7436. slightly, to make sure a beginning of line in the first line is included.
  7437. SEPARATOR specifies the field separator in the lines. It can have the
  7438. following values:
  7439. '(4) Use the comma as a field separator
  7440. '(16) Use a TAB as field separator
  7441. integer When a number, use that many spaces as field separator
  7442. nil When nil, the command tries to be smart and figure out the
  7443. separator in the following way:
  7444. - when each line contains a TAB, assume TAB-separated material
  7445. - when each line contains a comme, assume CSV material
  7446. - else, assume one or more SPACE charcters as separator."
  7447. (interactive "rP")
  7448. (let* ((beg (min beg0 end0))
  7449. (end (max beg0 end0))
  7450. re)
  7451. (goto-char beg)
  7452. (beginning-of-line 1)
  7453. (setq beg (move-marker (make-marker) (point)))
  7454. (goto-char end)
  7455. (if (bolp) (backward-char 1) (end-of-line 1))
  7456. (setq end (move-marker (make-marker) (point)))
  7457. ;; Get the right field separator
  7458. (unless separator
  7459. (goto-char beg)
  7460. (setq separator
  7461. (cond
  7462. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7463. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7464. (t 1))))
  7465. (setq re (cond
  7466. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7467. ((equal separator '(16)) "^\\|\t")
  7468. ((integerp separator)
  7469. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7470. (t (error "This should not happen"))))
  7471. (goto-char beg)
  7472. (while (re-search-forward re end t)
  7473. (replace-match "| " t t))
  7474. (goto-char beg)
  7475. (insert " ")
  7476. (org-table-align)))
  7477. (defun org-table-import (file arg)
  7478. "Import FILE as a table.
  7479. The file is assumed to be tab-separated. Such files can be produced by most
  7480. spreadsheet and database applications. If no tabs (at least one per line)
  7481. are found, lines will be split on whitespace into fields."
  7482. (interactive "f\nP")
  7483. (or (bolp) (newline))
  7484. (let ((beg (point))
  7485. (pm (point-max)))
  7486. (insert-file-contents file)
  7487. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7488. (defun org-table-export ()
  7489. "Export table as a tab-separated file.
  7490. Such a file can be imported into a spreadsheet program like Excel."
  7491. (interactive)
  7492. (let* ((beg (org-table-begin))
  7493. (end (org-table-end))
  7494. (table (buffer-substring beg end))
  7495. (file (read-file-name "Export table to: "))
  7496. buf)
  7497. (unless (or (not (file-exists-p file))
  7498. (y-or-n-p (format "Overwrite file %s? " file)))
  7499. (error "Abort"))
  7500. (with-current-buffer (find-file-noselect file)
  7501. (setq buf (current-buffer))
  7502. (erase-buffer)
  7503. (fundamental-mode)
  7504. (insert table)
  7505. (goto-char (point-min))
  7506. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7507. (replace-match "" t t)
  7508. (end-of-line 1))
  7509. (goto-char (point-min))
  7510. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7511. (replace-match "" t t)
  7512. (goto-char (min (1+ (point)) (point-max))))
  7513. (goto-char (point-min))
  7514. (while (re-search-forward "^-[-+]*$" nil t)
  7515. (replace-match "")
  7516. (if (looking-at "\n")
  7517. (delete-char 1)))
  7518. (goto-char (point-min))
  7519. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7520. (replace-match "\t" t t))
  7521. (save-buffer))
  7522. (kill-buffer buf)))
  7523. (defvar org-table-aligned-begin-marker (make-marker)
  7524. "Marker at the beginning of the table last aligned.
  7525. Used to check if cursor still is in that table, to minimize realignment.")
  7526. (defvar org-table-aligned-end-marker (make-marker)
  7527. "Marker at the end of the table last aligned.
  7528. Used to check if cursor still is in that table, to minimize realignment.")
  7529. (defvar org-table-last-alignment nil
  7530. "List of flags for flushright alignment, from the last re-alignment.
  7531. This is being used to correctly align a single field after TAB or RET.")
  7532. (defvar org-table-last-column-widths nil
  7533. "List of max width of fields in each column.
  7534. This is being used to correctly align a single field after TAB or RET.")
  7535. (defvar org-table-overlay-coordinates nil
  7536. "Overlay coordinates after each align of a table.")
  7537. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7538. (defvar org-last-recalc-line nil)
  7539. (defconst org-narrow-column-arrow "=>"
  7540. "Used as display property in narrowed table columns.")
  7541. (defun org-table-align ()
  7542. "Align the table at point by aligning all vertical bars."
  7543. (interactive)
  7544. (let* (
  7545. ;; Limits of table
  7546. (beg (org-table-begin))
  7547. (end (org-table-end))
  7548. ;; Current cursor position
  7549. (linepos (org-current-line))
  7550. (colpos (org-table-current-column))
  7551. (winstart (window-start))
  7552. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7553. lines (new "") lengths l typenums ty fields maxfields i
  7554. column
  7555. (indent "") cnt frac
  7556. rfmt hfmt
  7557. (spaces '(1 . 1))
  7558. (sp1 (car spaces))
  7559. (sp2 (cdr spaces))
  7560. (rfmt1 (concat
  7561. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7562. (hfmt1 (concat
  7563. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7564. emptystrings links dates emph narrow fmax f1 len c e)
  7565. (untabify beg end)
  7566. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7567. ;; Check if we have links or dates
  7568. (goto-char beg)
  7569. (setq links (re-search-forward org-bracket-link-regexp end t))
  7570. (goto-char beg)
  7571. (setq emph (and org-hide-emphasis-markers
  7572. (re-search-forward org-emph-re end t)))
  7573. (goto-char beg)
  7574. (setq dates (and org-display-custom-times
  7575. (re-search-forward org-ts-regexp-both end t)))
  7576. ;; Make sure the link properties are right
  7577. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7578. ;; Make sure the date properties are right
  7579. (when dates (goto-char beg) (while (org-activate-dates end)))
  7580. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7581. ;; Check if we are narrowing any columns
  7582. (goto-char beg)
  7583. (setq narrow (and org-format-transports-properties-p
  7584. (re-search-forward "<[0-9]+>" end t)))
  7585. ;; Get the rows
  7586. (setq lines (org-split-string
  7587. (buffer-substring beg end) "\n"))
  7588. ;; Store the indentation of the first line
  7589. (if (string-match "^ *" (car lines))
  7590. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7591. ;; Mark the hlines by setting the corresponding element to nil
  7592. ;; At the same time, we remove trailing space.
  7593. (setq lines (mapcar (lambda (l)
  7594. (if (string-match "^ *|-" l)
  7595. nil
  7596. (if (string-match "[ \t]+$" l)
  7597. (substring l 0 (match-beginning 0))
  7598. l)))
  7599. lines))
  7600. ;; Get the data fields by splitting the lines.
  7601. (setq fields (mapcar
  7602. (lambda (l)
  7603. (org-split-string l " *| *"))
  7604. (delq nil (copy-sequence lines))))
  7605. ;; How many fields in the longest line?
  7606. (condition-case nil
  7607. (setq maxfields (apply 'max (mapcar 'length fields)))
  7608. (error
  7609. (kill-region beg end)
  7610. (org-table-create org-table-default-size)
  7611. (error "Empty table - created default table")))
  7612. ;; A list of empty strings to fill any short rows on output
  7613. (setq emptystrings (make-list maxfields ""))
  7614. ;; Check for special formatting.
  7615. (setq i -1)
  7616. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7617. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7618. ;; Check if there is an explicit width specified
  7619. (when narrow
  7620. (setq c column fmax nil)
  7621. (while c
  7622. (setq e (pop c))
  7623. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7624. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7625. ;; Find fields that are wider than fmax, and shorten them
  7626. (when fmax
  7627. (loop for xx in column do
  7628. (when (and (stringp xx)
  7629. (> (org-string-width xx) fmax))
  7630. (org-add-props xx nil
  7631. 'help-echo
  7632. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7633. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7634. (unless (> f1 1)
  7635. (error "Cannot narrow field starting with wide link \"%s\""
  7636. (match-string 0 xx)))
  7637. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7638. (add-text-properties (- f1 2) f1
  7639. (list 'display org-narrow-column-arrow)
  7640. xx)))))
  7641. ;; Get the maximum width for each column
  7642. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7643. ;; Get the fraction of numbers, to decide about alignment of the column
  7644. (setq cnt 0 frac 0.0)
  7645. (loop for x in column do
  7646. (if (equal x "")
  7647. nil
  7648. (setq frac ( / (+ (* frac cnt)
  7649. (if (string-match org-table-number-regexp x) 1 0))
  7650. (setq cnt (1+ cnt))))))
  7651. (push (>= frac org-table-number-fraction) typenums))
  7652. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7653. ;; Store the alignment of this table, for later editing of single fields
  7654. (setq org-table-last-alignment typenums
  7655. org-table-last-column-widths lengths)
  7656. ;; With invisible characters, `format' does not get the field width right
  7657. ;; So we need to make these fields wide by hand.
  7658. (when (or links emph)
  7659. (loop for i from 0 upto (1- maxfields) do
  7660. (setq len (nth i lengths))
  7661. (loop for j from 0 upto (1- (length fields)) do
  7662. (setq c (nthcdr i (car (nthcdr j fields))))
  7663. (if (and (stringp (car c))
  7664. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7665. ; (string-match org-bracket-link-regexp (car c))
  7666. (< (org-string-width (car c)) len))
  7667. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7668. ;; Compute the formats needed for output of the table
  7669. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7670. (while (setq l (pop lengths))
  7671. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7672. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7673. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7674. (setq rfmt (concat rfmt "\n")
  7675. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7676. (setq new (mapconcat
  7677. (lambda (l)
  7678. (if l (apply 'format rfmt
  7679. (append (pop fields) emptystrings))
  7680. hfmt))
  7681. lines ""))
  7682. ;; Replace the old one
  7683. (delete-region beg end)
  7684. (move-marker end nil)
  7685. (move-marker org-table-aligned-begin-marker (point))
  7686. (insert new)
  7687. (move-marker org-table-aligned-end-marker (point))
  7688. (when (and orgtbl-mode (not (org-mode-p)))
  7689. (goto-char org-table-aligned-begin-marker)
  7690. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7691. ;; Try to move to the old location
  7692. (goto-line winstartline)
  7693. (setq winstart (point-at-bol))
  7694. (goto-line linepos)
  7695. (set-window-start (selected-window) winstart 'noforce)
  7696. (org-table-goto-column colpos)
  7697. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7698. (setq org-table-may-need-update nil)
  7699. ))
  7700. (defun org-string-width (s)
  7701. "Compute width of string, ignoring invisible characters.
  7702. This ignores character with invisibility property `org-link', and also
  7703. characters with property `org-cwidth', because these will become invisible
  7704. upon the next fontification round."
  7705. (let (b l)
  7706. (when (or (eq t buffer-invisibility-spec)
  7707. (assq 'org-link buffer-invisibility-spec))
  7708. (while (setq b (text-property-any 0 (length s)
  7709. 'invisible 'org-link s))
  7710. (setq s (concat (substring s 0 b)
  7711. (substring s (or (next-single-property-change
  7712. b 'invisible s) (length s)))))))
  7713. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7714. (setq s (concat (substring s 0 b)
  7715. (substring s (or (next-single-property-change
  7716. b 'org-cwidth s) (length s))))))
  7717. (setq l (string-width s) b -1)
  7718. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7719. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7720. l))
  7721. (defun org-table-begin (&optional table-type)
  7722. "Find the beginning of the table and return its position.
  7723. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7724. (save-excursion
  7725. (if (not (re-search-backward
  7726. (if table-type org-table-any-border-regexp
  7727. org-table-border-regexp)
  7728. nil t))
  7729. (progn (goto-char (point-min)) (point))
  7730. (goto-char (match-beginning 0))
  7731. (beginning-of-line 2)
  7732. (point))))
  7733. (defun org-table-end (&optional table-type)
  7734. "Find the end of the table and return its position.
  7735. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7736. (save-excursion
  7737. (if (not (re-search-forward
  7738. (if table-type org-table-any-border-regexp
  7739. org-table-border-regexp)
  7740. nil t))
  7741. (goto-char (point-max))
  7742. (goto-char (match-beginning 0)))
  7743. (point-marker)))
  7744. (defun org-table-justify-field-maybe (&optional new)
  7745. "Justify the current field, text to left, number to right.
  7746. Optional argument NEW may specify text to replace the current field content."
  7747. (cond
  7748. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7749. ((org-at-table-hline-p))
  7750. ((and (not new)
  7751. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7752. (current-buffer)))
  7753. (< (point) org-table-aligned-begin-marker)
  7754. (>= (point) org-table-aligned-end-marker)))
  7755. ;; This is not the same table, force a full re-align
  7756. (setq org-table-may-need-update t))
  7757. (t ;; realign the current field, based on previous full realign
  7758. (let* ((pos (point)) s
  7759. (col (org-table-current-column))
  7760. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7761. l f n o e)
  7762. (when (> col 0)
  7763. (skip-chars-backward "^|\n")
  7764. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7765. (progn
  7766. (setq s (match-string 1)
  7767. o (match-string 0)
  7768. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7769. e (not (= (match-beginning 2) (match-end 2))))
  7770. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7771. l (if e "|" (setq org-table-may-need-update t) ""))
  7772. n (format f s))
  7773. (if new
  7774. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7775. (setq n (format f new))
  7776. (setq n (concat new "|") org-table-may-need-update t)))
  7777. (or (equal n o)
  7778. (let (org-table-may-need-update)
  7779. (replace-match n t t))))
  7780. (setq org-table-may-need-update t))
  7781. (goto-char pos))))))
  7782. (defun org-table-next-field ()
  7783. "Go to the next field in the current table, creating new lines as needed.
  7784. Before doing so, re-align the table if necessary."
  7785. (interactive)
  7786. (org-table-maybe-eval-formula)
  7787. (org-table-maybe-recalculate-line)
  7788. (if (and org-table-automatic-realign
  7789. org-table-may-need-update)
  7790. (org-table-align))
  7791. (let ((end (org-table-end)))
  7792. (if (org-at-table-hline-p)
  7793. (end-of-line 1))
  7794. (condition-case nil
  7795. (progn
  7796. (re-search-forward "|" end)
  7797. (if (looking-at "[ \t]*$")
  7798. (re-search-forward "|" end))
  7799. (if (and (looking-at "-")
  7800. org-table-tab-jumps-over-hlines
  7801. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7802. (goto-char (match-beginning 1)))
  7803. (if (looking-at "-")
  7804. (progn
  7805. (beginning-of-line 0)
  7806. (org-table-insert-row 'below))
  7807. (if (looking-at " ") (forward-char 1))))
  7808. (error
  7809. (org-table-insert-row 'below)))))
  7810. (defun org-table-previous-field ()
  7811. "Go to the previous field in the table.
  7812. Before doing so, re-align the table if necessary."
  7813. (interactive)
  7814. (org-table-justify-field-maybe)
  7815. (org-table-maybe-recalculate-line)
  7816. (if (and org-table-automatic-realign
  7817. org-table-may-need-update)
  7818. (org-table-align))
  7819. (if (org-at-table-hline-p)
  7820. (end-of-line 1))
  7821. (re-search-backward "|" (org-table-begin))
  7822. (re-search-backward "|" (org-table-begin))
  7823. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7824. (re-search-backward "|" (org-table-begin)))
  7825. (if (looking-at "| ?")
  7826. (goto-char (match-end 0))))
  7827. (defun org-table-next-row ()
  7828. "Go to the next row (same column) in the current table.
  7829. Before doing so, re-align the table if necessary."
  7830. (interactive)
  7831. (org-table-maybe-eval-formula)
  7832. (org-table-maybe-recalculate-line)
  7833. (if (or (looking-at "[ \t]*$")
  7834. (save-excursion (skip-chars-backward " \t") (bolp)))
  7835. (newline)
  7836. (if (and org-table-automatic-realign
  7837. org-table-may-need-update)
  7838. (org-table-align))
  7839. (let ((col (org-table-current-column)))
  7840. (beginning-of-line 2)
  7841. (if (or (not (org-at-table-p))
  7842. (org-at-table-hline-p))
  7843. (progn
  7844. (beginning-of-line 0)
  7845. (org-table-insert-row 'below)))
  7846. (org-table-goto-column col)
  7847. (skip-chars-backward "^|\n\r")
  7848. (if (looking-at " ") (forward-char 1)))))
  7849. (defun org-table-copy-down (n)
  7850. "Copy a field down in the current column.
  7851. If the field at the cursor is empty, copy into it the content of the nearest
  7852. non-empty field above. With argument N, use the Nth non-empty field.
  7853. If the current field is not empty, it is copied down to the next row, and
  7854. the cursor is moved with it. Therefore, repeating this command causes the
  7855. column to be filled row-by-row.
  7856. If the variable `org-table-copy-increment' is non-nil and the field is an
  7857. integer or a timestamp, it will be incremented while copying. In the case of
  7858. a timestamp, if the cursor is on the year, change the year. If it is on the
  7859. month or the day, change that. Point will stay on the current date field
  7860. in order to easily repeat the interval."
  7861. (interactive "p")
  7862. (let* ((colpos (org-table-current-column))
  7863. (col (current-column))
  7864. (field (org-table-get-field))
  7865. (non-empty (string-match "[^ \t]" field))
  7866. (beg (org-table-begin))
  7867. txt)
  7868. (org-table-check-inside-data-field)
  7869. (if non-empty
  7870. (progn
  7871. (setq txt (org-trim field))
  7872. (org-table-next-row)
  7873. (org-table-blank-field))
  7874. (save-excursion
  7875. (setq txt
  7876. (catch 'exit
  7877. (while (progn (beginning-of-line 1)
  7878. (re-search-backward org-table-dataline-regexp
  7879. beg t))
  7880. (org-table-goto-column colpos t)
  7881. (if (and (looking-at
  7882. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7883. (= (setq n (1- n)) 0))
  7884. (throw 'exit (match-string 1))))))))
  7885. (if txt
  7886. (progn
  7887. (if (and org-table-copy-increment
  7888. (string-match "^[0-9]+$" txt))
  7889. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7890. (insert txt)
  7891. (move-to-column col)
  7892. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7893. (org-timestamp-up 1)
  7894. (org-table-maybe-recalculate-line))
  7895. (org-table-align)
  7896. (move-to-column col))
  7897. (error "No non-empty field found"))))
  7898. (defun org-table-check-inside-data-field ()
  7899. "Is point inside a table data field?
  7900. I.e. not on a hline or before the first or after the last column?
  7901. This actually throws an error, so it aborts the current command."
  7902. (if (or (not (org-at-table-p))
  7903. (= (org-table-current-column) 0)
  7904. (org-at-table-hline-p)
  7905. (looking-at "[ \t]*$"))
  7906. (error "Not in table data field")))
  7907. (defvar org-table-clip nil
  7908. "Clipboard for table regions.")
  7909. (defun org-table-blank-field ()
  7910. "Blank the current table field or active region."
  7911. (interactive)
  7912. (org-table-check-inside-data-field)
  7913. (if (and (interactive-p) (org-region-active-p))
  7914. (let (org-table-clip)
  7915. (org-table-cut-region (region-beginning) (region-end)))
  7916. (skip-chars-backward "^|")
  7917. (backward-char 1)
  7918. (if (looking-at "|[^|\n]+")
  7919. (let* ((pos (match-beginning 0))
  7920. (match (match-string 0))
  7921. (len (org-string-width match)))
  7922. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7923. (goto-char (+ 2 pos))
  7924. (substring match 1)))))
  7925. (defun org-table-get-field (&optional n replace)
  7926. "Return the value of the field in column N of current row.
  7927. N defaults to current field.
  7928. If REPLACE is a string, replace field with this value. The return value
  7929. is always the old value."
  7930. (and n (org-table-goto-column n))
  7931. (skip-chars-backward "^|\n")
  7932. (backward-char 1)
  7933. (if (looking-at "|[^|\r\n]*")
  7934. (let* ((pos (match-beginning 0))
  7935. (val (buffer-substring (1+ pos) (match-end 0))))
  7936. (if replace
  7937. (replace-match (concat "|" replace) t t))
  7938. (goto-char (min (point-at-eol) (+ 2 pos)))
  7939. val)
  7940. (forward-char 1) ""))
  7941. (defun org-table-field-info (arg)
  7942. "Show info about the current field, and highlight any reference at point."
  7943. (interactive "P")
  7944. (org-table-get-specials)
  7945. (save-excursion
  7946. (let* ((pos (point))
  7947. (col (org-table-current-column))
  7948. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7949. (name (car (rassoc (list (org-current-line) col)
  7950. org-table-named-field-locations)))
  7951. (eql (org-table-get-stored-formulas))
  7952. (dline (org-table-current-dline))
  7953. (ref (format "@%d$%d" dline col))
  7954. (ref1 (org-table-convert-refs-to-an ref))
  7955. (fequation (or (assoc name eql) (assoc ref eql)))
  7956. (cequation (assoc (int-to-string col) eql))
  7957. (eqn (or fequation cequation)))
  7958. (goto-char pos)
  7959. (condition-case nil
  7960. (org-table-show-reference 'local)
  7961. (error nil))
  7962. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7963. dline col
  7964. (if cname (concat " or $" cname) "")
  7965. dline col ref1
  7966. (if name (concat " or $" name) "")
  7967. ;; FIXME: formula info not correct if special table line
  7968. (if eqn
  7969. (concat ", formula: "
  7970. (org-table-formula-to-user
  7971. (concat
  7972. (if (string-match "^[$@]"(car eqn)) "" "$")
  7973. (car eqn) "=" (cdr eqn))))
  7974. "")))))
  7975. (defun org-table-current-column ()
  7976. "Find out which column we are in.
  7977. When called interactively, column is also displayed in echo area."
  7978. (interactive)
  7979. (if (interactive-p) (org-table-check-inside-data-field))
  7980. (save-excursion
  7981. (let ((cnt 0) (pos (point)))
  7982. (beginning-of-line 1)
  7983. (while (search-forward "|" pos t)
  7984. (setq cnt (1+ cnt)))
  7985. (if (interactive-p) (message "This is table column %d" cnt))
  7986. cnt)))
  7987. (defun org-table-current-dline ()
  7988. "Find out what table data line we are in.
  7989. Only datalins count for this."
  7990. (interactive)
  7991. (if (interactive-p) (org-table-check-inside-data-field))
  7992. (save-excursion
  7993. (let ((cnt 0) (pos (point)))
  7994. (goto-char (org-table-begin))
  7995. (while (<= (point) pos)
  7996. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7997. (beginning-of-line 2))
  7998. (if (interactive-p) (message "This is table line %d" cnt))
  7999. cnt)))
  8000. (defun org-table-goto-column (n &optional on-delim force)
  8001. "Move the cursor to the Nth column in the current table line.
  8002. With optional argument ON-DELIM, stop with point before the left delimiter
  8003. of the field.
  8004. If there are less than N fields, just go to after the last delimiter.
  8005. However, when FORCE is non-nil, create new columns if necessary."
  8006. (interactive "p")
  8007. (let ((pos (point-at-eol)))
  8008. (beginning-of-line 1)
  8009. (when (> n 0)
  8010. (while (and (> (setq n (1- n)) -1)
  8011. (or (search-forward "|" pos t)
  8012. (and force
  8013. (progn (end-of-line 1)
  8014. (skip-chars-backward "^|")
  8015. (insert " | "))))))
  8016. ; (backward-char 2) t)))))
  8017. (when (and force (not (looking-at ".*|")))
  8018. (save-excursion (end-of-line 1) (insert " | ")))
  8019. (if on-delim
  8020. (backward-char 1)
  8021. (if (looking-at " ") (forward-char 1))))))
  8022. (defun org-at-table-p (&optional table-type)
  8023. "Return t if the cursor is inside an org-type table.
  8024. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8025. (if org-enable-table-editor
  8026. (save-excursion
  8027. (beginning-of-line 1)
  8028. (looking-at (if table-type org-table-any-line-regexp
  8029. org-table-line-regexp)))
  8030. nil))
  8031. (defun org-at-table.el-p ()
  8032. "Return t if and only if we are at a table.el table."
  8033. (and (org-at-table-p 'any)
  8034. (save-excursion
  8035. (goto-char (org-table-begin 'any))
  8036. (looking-at org-table1-hline-regexp))))
  8037. (defun org-table-recognize-table.el ()
  8038. "If there is a table.el table nearby, recognize it and move into it."
  8039. (if org-table-tab-recognizes-table.el
  8040. (if (org-at-table.el-p)
  8041. (progn
  8042. (beginning-of-line 1)
  8043. (if (looking-at org-table-dataline-regexp)
  8044. nil
  8045. (if (looking-at org-table1-hline-regexp)
  8046. (progn
  8047. (beginning-of-line 2)
  8048. (if (looking-at org-table-any-border-regexp)
  8049. (beginning-of-line -1)))))
  8050. (if (re-search-forward "|" (org-table-end t) t)
  8051. (progn
  8052. (require 'table)
  8053. (if (table--at-cell-p (point))
  8054. t
  8055. (message "recognizing table.el table...")
  8056. (table-recognize-table)
  8057. (message "recognizing table.el table...done")))
  8058. (error "This should not happen..."))
  8059. t)
  8060. nil)
  8061. nil))
  8062. (defun org-at-table-hline-p ()
  8063. "Return t if the cursor is inside a hline in a table."
  8064. (if org-enable-table-editor
  8065. (save-excursion
  8066. (beginning-of-line 1)
  8067. (looking-at org-table-hline-regexp))
  8068. nil))
  8069. (defun org-table-insert-column ()
  8070. "Insert a new column into the table."
  8071. (interactive)
  8072. (if (not (org-at-table-p))
  8073. (error "Not at a table"))
  8074. (org-table-find-dataline)
  8075. (let* ((col (max 1 (org-table-current-column)))
  8076. (beg (org-table-begin))
  8077. (end (org-table-end))
  8078. ;; Current cursor position
  8079. (linepos (org-current-line))
  8080. (colpos col))
  8081. (goto-char beg)
  8082. (while (< (point) end)
  8083. (if (org-at-table-hline-p)
  8084. nil
  8085. (org-table-goto-column col t)
  8086. (insert "| "))
  8087. (beginning-of-line 2))
  8088. (move-marker end nil)
  8089. (goto-line linepos)
  8090. (org-table-goto-column colpos)
  8091. (org-table-align)
  8092. (org-table-fix-formulas "$" nil (1- col) 1)))
  8093. (defun org-table-find-dataline ()
  8094. "Find a dataline in the current table, which is needed for column commands."
  8095. (if (and (org-at-table-p)
  8096. (not (org-at-table-hline-p)))
  8097. t
  8098. (let ((col (current-column))
  8099. (end (org-table-end)))
  8100. (move-to-column col)
  8101. (while (and (< (point) end)
  8102. (or (not (= (current-column) col))
  8103. (org-at-table-hline-p)))
  8104. (beginning-of-line 2)
  8105. (move-to-column col))
  8106. (if (and (org-at-table-p)
  8107. (not (org-at-table-hline-p)))
  8108. t
  8109. (error
  8110. "Please position cursor in a data line for column operations")))))
  8111. (defun org-table-delete-column ()
  8112. "Delete a column from the table."
  8113. (interactive)
  8114. (if (not (org-at-table-p))
  8115. (error "Not at a table"))
  8116. (org-table-find-dataline)
  8117. (org-table-check-inside-data-field)
  8118. (let* ((col (org-table-current-column))
  8119. (beg (org-table-begin))
  8120. (end (org-table-end))
  8121. ;; Current cursor position
  8122. (linepos (org-current-line))
  8123. (colpos col))
  8124. (goto-char beg)
  8125. (while (< (point) end)
  8126. (if (org-at-table-hline-p)
  8127. nil
  8128. (org-table-goto-column col t)
  8129. (and (looking-at "|[^|\n]+|")
  8130. (replace-match "|")))
  8131. (beginning-of-line 2))
  8132. (move-marker end nil)
  8133. (goto-line linepos)
  8134. (org-table-goto-column colpos)
  8135. (org-table-align)
  8136. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8137. col -1 col)))
  8138. (defun org-table-move-column-right ()
  8139. "Move column to the right."
  8140. (interactive)
  8141. (org-table-move-column nil))
  8142. (defun org-table-move-column-left ()
  8143. "Move column to the left."
  8144. (interactive)
  8145. (org-table-move-column 'left))
  8146. (defun org-table-move-column (&optional left)
  8147. "Move the current column to the right. With arg LEFT, move to the left."
  8148. (interactive "P")
  8149. (if (not (org-at-table-p))
  8150. (error "Not at a table"))
  8151. (org-table-find-dataline)
  8152. (org-table-check-inside-data-field)
  8153. (let* ((col (org-table-current-column))
  8154. (col1 (if left (1- col) col))
  8155. (beg (org-table-begin))
  8156. (end (org-table-end))
  8157. ;; Current cursor position
  8158. (linepos (org-current-line))
  8159. (colpos (if left (1- col) (1+ col))))
  8160. (if (and left (= col 1))
  8161. (error "Cannot move column further left"))
  8162. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8163. (error "Cannot move column further right"))
  8164. (goto-char beg)
  8165. (while (< (point) end)
  8166. (if (org-at-table-hline-p)
  8167. nil
  8168. (org-table-goto-column col1 t)
  8169. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8170. (replace-match "|\\2|\\1|")))
  8171. (beginning-of-line 2))
  8172. (move-marker end nil)
  8173. (goto-line linepos)
  8174. (org-table-goto-column colpos)
  8175. (org-table-align)
  8176. (org-table-fix-formulas
  8177. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8178. (cons (number-to-string colpos) (number-to-string col))))))
  8179. (defun org-table-move-row-down ()
  8180. "Move table row down."
  8181. (interactive)
  8182. (org-table-move-row nil))
  8183. (defun org-table-move-row-up ()
  8184. "Move table row up."
  8185. (interactive)
  8186. (org-table-move-row 'up))
  8187. (defun org-table-move-row (&optional up)
  8188. "Move the current table line down. With arg UP, move it up."
  8189. (interactive "P")
  8190. (let* ((col (current-column))
  8191. (pos (point))
  8192. (hline1p (save-excursion (beginning-of-line 1)
  8193. (looking-at org-table-hline-regexp)))
  8194. (dline1 (org-table-current-dline))
  8195. (dline2 (+ dline1 (if up -1 1)))
  8196. (tonew (if up 0 2))
  8197. txt hline2p)
  8198. (beginning-of-line tonew)
  8199. (unless (org-at-table-p)
  8200. (goto-char pos)
  8201. (error "Cannot move row further"))
  8202. (setq hline2p (looking-at org-table-hline-regexp))
  8203. (goto-char pos)
  8204. (beginning-of-line 1)
  8205. (setq pos (point))
  8206. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8207. (delete-region (point) (1+ (point-at-eol)))
  8208. (beginning-of-line tonew)
  8209. (insert txt)
  8210. (beginning-of-line 0)
  8211. (move-to-column col)
  8212. (unless (or hline1p hline2p)
  8213. (org-table-fix-formulas
  8214. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8215. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8216. (defun org-table-insert-row (&optional arg)
  8217. "Insert a new row above the current line into the table.
  8218. With prefix ARG, insert below the current line."
  8219. (interactive "P")
  8220. (if (not (org-at-table-p))
  8221. (error "Not at a table"))
  8222. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8223. (new (org-table-clean-line line)))
  8224. ;; Fix the first field if necessary
  8225. (if (string-match "^[ \t]*| *[#$] *|" line)
  8226. (setq new (replace-match (match-string 0 line) t t new)))
  8227. (beginning-of-line (if arg 2 1))
  8228. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8229. (beginning-of-line 0)
  8230. (re-search-forward "| ?" (point-at-eol) t)
  8231. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8232. (org-table-align))
  8233. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8234. (defun org-table-insert-hline (&optional above)
  8235. "Insert a horizontal-line below the current line into the table.
  8236. With prefix ABOVE, insert above the current line."
  8237. (interactive "P")
  8238. (if (not (org-at-table-p))
  8239. (error "Not at a table"))
  8240. (let ((line (org-table-clean-line
  8241. (buffer-substring (point-at-bol) (point-at-eol))))
  8242. (col (current-column)))
  8243. (while (string-match "|\\( +\\)|" line)
  8244. (setq line (replace-match
  8245. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8246. ?-) "|") t t line)))
  8247. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8248. (beginning-of-line (if above 1 2))
  8249. (insert line "\n")
  8250. (beginning-of-line (if above 1 -1))
  8251. (move-to-column col)
  8252. (and org-table-overlay-coordinates (org-table-align))))
  8253. (defun org-table-hline-and-move (&optional same-column)
  8254. "Insert a hline and move to the row below that line."
  8255. (interactive "P")
  8256. (let ((col (org-table-current-column)))
  8257. (org-table-maybe-eval-formula)
  8258. (org-table-maybe-recalculate-line)
  8259. (org-table-insert-hline)
  8260. (end-of-line 2)
  8261. (if (looking-at "\n[ \t]*|-")
  8262. (progn (insert "\n|") (org-table-align))
  8263. (org-table-next-field))
  8264. (if same-column (org-table-goto-column col))))
  8265. (defun org-table-clean-line (s)
  8266. "Convert a table line S into a string with only \"|\" and space.
  8267. In particular, this does handle wide and invisible characters."
  8268. (if (string-match "^[ \t]*|-" s)
  8269. ;; It's a hline, just map the characters
  8270. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8271. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8272. (setq s (replace-match
  8273. (concat "|" (make-string (org-string-width (match-string 1 s))
  8274. ?\ ) "|")
  8275. t t s)))
  8276. s))
  8277. (defun org-table-kill-row ()
  8278. "Delete the current row or horizontal line from the table."
  8279. (interactive)
  8280. (if (not (org-at-table-p))
  8281. (error "Not at a table"))
  8282. (let ((col (current-column))
  8283. (dline (org-table-current-dline)))
  8284. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8285. (if (not (org-at-table-p)) (beginning-of-line 0))
  8286. (move-to-column col)
  8287. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8288. dline -1 dline)))
  8289. (defun org-table-sort-lines (with-case &optional sorting-type)
  8290. "Sort table lines according to the column at point.
  8291. The position of point indicates the column to be used for
  8292. sorting, and the range of lines is the range between the nearest
  8293. horizontal separator lines, or the entire table of no such lines
  8294. exist. If point is before the first column, you will be prompted
  8295. for the sorting column. If there is an active region, the mark
  8296. specifies the first line and the sorting column, while point
  8297. should be in the last line to be included into the sorting.
  8298. The command then prompts for the sorting type which can be
  8299. alphabetically, numerically, or by time (as given in a time stamp
  8300. in the field). Sorting in reverse order is also possible.
  8301. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8302. If SORTING-TYPE is specified when this function is called from a Lisp
  8303. program, no prompting will take place. SORTING-TYPE must be a character,
  8304. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8305. should be done in reverse order."
  8306. (interactive "P")
  8307. (let* ((thisline (org-current-line))
  8308. (thiscol (org-table-current-column))
  8309. beg end bcol ecol tend tbeg column lns pos)
  8310. (when (equal thiscol 0)
  8311. (if (interactive-p)
  8312. (setq thiscol
  8313. (string-to-number
  8314. (read-string "Use column N for sorting: ")))
  8315. (setq thiscol 1))
  8316. (org-table-goto-column thiscol))
  8317. (org-table-check-inside-data-field)
  8318. (if (org-region-active-p)
  8319. (progn
  8320. (setq beg (region-beginning) end (region-end))
  8321. (goto-char beg)
  8322. (setq column (org-table-current-column)
  8323. beg (point-at-bol))
  8324. (goto-char end)
  8325. (setq end (point-at-bol 2)))
  8326. (setq column (org-table-current-column)
  8327. pos (point)
  8328. tbeg (org-table-begin)
  8329. tend (org-table-end))
  8330. (if (re-search-backward org-table-hline-regexp tbeg t)
  8331. (setq beg (point-at-bol 2))
  8332. (goto-char tbeg)
  8333. (setq beg (point-at-bol 1)))
  8334. (goto-char pos)
  8335. (if (re-search-forward org-table-hline-regexp tend t)
  8336. (setq end (point-at-bol 1))
  8337. (goto-char tend)
  8338. (setq end (point-at-bol))))
  8339. (setq beg (move-marker (make-marker) beg)
  8340. end (move-marker (make-marker) end))
  8341. (untabify beg end)
  8342. (goto-char beg)
  8343. (org-table-goto-column column)
  8344. (skip-chars-backward "^|")
  8345. (setq bcol (current-column))
  8346. (org-table-goto-column (1+ column))
  8347. (skip-chars-backward "^|")
  8348. (setq ecol (1- (current-column)))
  8349. (org-table-goto-column column)
  8350. (setq lns (mapcar (lambda(x) (cons
  8351. (org-sort-remove-invisible
  8352. (nth (1- column)
  8353. (org-split-string x "[ \t]*|[ \t]*")))
  8354. x))
  8355. (org-split-string (buffer-substring beg end) "\n")))
  8356. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8357. (delete-region beg end)
  8358. (move-marker beg nil)
  8359. (move-marker end nil)
  8360. (insert (mapconcat 'cdr lns "\n") "\n")
  8361. (goto-line thisline)
  8362. (org-table-goto-column thiscol)
  8363. (message "%d lines sorted, based on column %d" (length lns) column)))
  8364. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8365. (defun org-sort-remove-invisible (s)
  8366. (remove-text-properties 0 (length s) org-rm-props s)
  8367. (while (string-match org-bracket-link-regexp s)
  8368. (setq s (replace-match (if (match-end 2)
  8369. (match-string 3 s)
  8370. (match-string 1 s)) t t s)))
  8371. s)
  8372. (defun org-table-cut-region (beg end)
  8373. "Copy region in table to the clipboard and blank all relevant fields."
  8374. (interactive "r")
  8375. (org-table-copy-region beg end 'cut))
  8376. (defun org-table-copy-region (beg end &optional cut)
  8377. "Copy rectangular region in table to clipboard.
  8378. A special clipboard is used which can only be accessed
  8379. with `org-table-paste-rectangle'."
  8380. (interactive "rP")
  8381. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8382. region cols
  8383. (rpl (if cut " " nil)))
  8384. (goto-char beg)
  8385. (org-table-check-inside-data-field)
  8386. (setq l01 (org-current-line)
  8387. c01 (org-table-current-column))
  8388. (goto-char end)
  8389. (org-table-check-inside-data-field)
  8390. (setq l02 (org-current-line)
  8391. c02 (org-table-current-column))
  8392. (setq l1 (min l01 l02) l2 (max l01 l02)
  8393. c1 (min c01 c02) c2 (max c01 c02))
  8394. (catch 'exit
  8395. (while t
  8396. (catch 'nextline
  8397. (if (> l1 l2) (throw 'exit t))
  8398. (goto-line l1)
  8399. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8400. (setq cols nil ic1 c1 ic2 c2)
  8401. (while (< ic1 (1+ ic2))
  8402. (push (org-table-get-field ic1 rpl) cols)
  8403. (setq ic1 (1+ ic1)))
  8404. (push (nreverse cols) region)
  8405. (setq l1 (1+ l1)))))
  8406. (setq org-table-clip (nreverse region))
  8407. (if cut (org-table-align))
  8408. org-table-clip))
  8409. (defun org-table-paste-rectangle ()
  8410. "Paste a rectangular region into a table.
  8411. The upper right corner ends up in the current field. All involved fields
  8412. will be overwritten. If the rectangle does not fit into the present table,
  8413. the table is enlarged as needed. The process ignores horizontal separator
  8414. lines."
  8415. (interactive)
  8416. (unless (and org-table-clip (listp org-table-clip))
  8417. (error "First cut/copy a region to paste!"))
  8418. (org-table-check-inside-data-field)
  8419. (let* ((clip org-table-clip)
  8420. (line (org-current-line))
  8421. (col (org-table-current-column))
  8422. (org-enable-table-editor t)
  8423. (org-table-automatic-realign nil)
  8424. c cols field)
  8425. (while (setq cols (pop clip))
  8426. (while (org-at-table-hline-p) (beginning-of-line 2))
  8427. (if (not (org-at-table-p))
  8428. (progn (end-of-line 0) (org-table-next-field)))
  8429. (setq c col)
  8430. (while (setq field (pop cols))
  8431. (org-table-goto-column c nil 'force)
  8432. (org-table-get-field nil field)
  8433. (setq c (1+ c)))
  8434. (beginning-of-line 2))
  8435. (goto-line line)
  8436. (org-table-goto-column col)
  8437. (org-table-align)))
  8438. (defun org-table-convert ()
  8439. "Convert from `org-mode' table to table.el and back.
  8440. Obviously, this only works within limits. When an Org-mode table is
  8441. converted to table.el, all horizontal separator lines get lost, because
  8442. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8443. A table.el table can be converted to an Org-mode table only if it does not
  8444. do row or column spanning. Multiline cells will become multiple cells.
  8445. Beware, Org-mode does not test if the table can be successfully converted - it
  8446. blindly applies a recipe that works for simple tables."
  8447. (interactive)
  8448. (require 'table)
  8449. (if (org-at-table.el-p)
  8450. ;; convert to Org-mode table
  8451. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8452. (end (move-marker (make-marker) (org-table-end t))))
  8453. (table-unrecognize-region beg end)
  8454. (goto-char beg)
  8455. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8456. (replace-match ""))
  8457. (goto-char beg))
  8458. (if (org-at-table-p)
  8459. ;; convert to table.el table
  8460. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8461. (end (move-marker (make-marker) (org-table-end))))
  8462. ;; first, get rid of all horizontal lines
  8463. (goto-char beg)
  8464. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8465. (replace-match ""))
  8466. ;; insert a hline before first
  8467. (goto-char beg)
  8468. (org-table-insert-hline 'above)
  8469. (beginning-of-line -1)
  8470. ;; insert a hline after each line
  8471. (while (progn (beginning-of-line 3) (< (point) end))
  8472. (org-table-insert-hline))
  8473. (goto-char beg)
  8474. (setq end (move-marker end (org-table-end)))
  8475. ;; replace "+" at beginning and ending of hlines
  8476. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8477. (replace-match "\\1+-"))
  8478. (goto-char beg)
  8479. (while (re-search-forward "-|[ \t]*$" end t)
  8480. (replace-match "-+"))
  8481. (goto-char beg)))))
  8482. (defun org-table-wrap-region (arg)
  8483. "Wrap several fields in a column like a paragraph.
  8484. This is useful if you'd like to spread the contents of a field over several
  8485. lines, in order to keep the table compact.
  8486. If there is an active region, and both point and mark are in the same column,
  8487. the text in the column is wrapped to minimum width for the given number of
  8488. lines. Generally, this makes the table more compact. A prefix ARG may be
  8489. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8490. formats the selected text to two lines. If the region was longer than two
  8491. lines, the remaining lines remain empty. A negative prefix argument reduces
  8492. the current number of lines by that amount. The wrapped text is pasted back
  8493. into the table. If you formatted it to more lines than it was before, fields
  8494. further down in the table get overwritten - so you might need to make space in
  8495. the table first.
  8496. If there is no region, the current field is split at the cursor position and
  8497. the text fragment to the right of the cursor is prepended to the field one
  8498. line down.
  8499. If there is no region, but you specify a prefix ARG, the current field gets
  8500. blank, and the content is appended to the field above."
  8501. (interactive "P")
  8502. (org-table-check-inside-data-field)
  8503. (if (org-region-active-p)
  8504. ;; There is a region: fill as a paragraph
  8505. (let* ((beg (region-beginning))
  8506. (cline (save-excursion (goto-char beg) (org-current-line)))
  8507. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8508. nlines)
  8509. (org-table-cut-region (region-beginning) (region-end))
  8510. (if (> (length (car org-table-clip)) 1)
  8511. (error "Region must be limited to single column"))
  8512. (setq nlines (if arg
  8513. (if (< arg 1)
  8514. (+ (length org-table-clip) arg)
  8515. arg)
  8516. (length org-table-clip)))
  8517. (setq org-table-clip
  8518. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8519. nil nlines)))
  8520. (goto-line cline)
  8521. (org-table-goto-column ccol)
  8522. (org-table-paste-rectangle))
  8523. ;; No region, split the current field at point
  8524. (if arg
  8525. ;; combine with field above
  8526. (let ((s (org-table-blank-field))
  8527. (col (org-table-current-column)))
  8528. (beginning-of-line 0)
  8529. (while (org-at-table-hline-p) (beginning-of-line 0))
  8530. (org-table-goto-column col)
  8531. (skip-chars-forward "^|")
  8532. (skip-chars-backward " ")
  8533. (insert " " (org-trim s))
  8534. (org-table-align))
  8535. ;; split field
  8536. (when (looking-at "\\([^|]+\\)+|")
  8537. (let ((s (match-string 1)))
  8538. (replace-match " |")
  8539. (goto-char (match-beginning 0))
  8540. (org-table-next-row)
  8541. (insert (org-trim s) " ")
  8542. (org-table-align))))))
  8543. (defvar org-field-marker nil)
  8544. (defun org-table-edit-field (arg)
  8545. "Edit table field in a different window.
  8546. This is mainly useful for fields that contain hidden parts.
  8547. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8548. it can be edited in place."
  8549. (interactive "P")
  8550. (if arg
  8551. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8552. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8553. (remove-text-properties b e '(org-cwidth t invisible t
  8554. display t intangible t))
  8555. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8556. (font-lock-fontify-block)))
  8557. (let ((pos (move-marker (make-marker) (point)))
  8558. (field (org-table-get-field))
  8559. (cw (current-window-configuration))
  8560. p)
  8561. (org-switch-to-buffer-other-window "*Org tmp*")
  8562. (erase-buffer)
  8563. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8564. (let ((org-inhibit-startup t)) (org-mode))
  8565. (goto-char (setq p (point-max)))
  8566. (insert (org-trim field))
  8567. (remove-text-properties p (point-max)
  8568. '(invisible t org-cwidth t display t
  8569. intangible t))
  8570. (goto-char p)
  8571. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8572. (org-set-local 'org-window-configuration cw)
  8573. (org-set-local 'org-field-marker pos)
  8574. (message "Edit and finish with C-c C-c"))))
  8575. (defun org-table-finish-edit-field ()
  8576. "Finish editing a table data field.
  8577. Remove all newline characters, insert the result into the table, realign
  8578. the table and kill the editing buffer."
  8579. (let ((pos org-field-marker)
  8580. (cw org-window-configuration)
  8581. (cb (current-buffer))
  8582. text)
  8583. (goto-char (point-min))
  8584. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8585. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8586. (replace-match " "))
  8587. (setq text (org-trim (buffer-string)))
  8588. (set-window-configuration cw)
  8589. (kill-buffer cb)
  8590. (select-window (get-buffer-window (marker-buffer pos)))
  8591. (goto-char pos)
  8592. (move-marker pos nil)
  8593. (org-table-check-inside-data-field)
  8594. (org-table-get-field nil text)
  8595. (org-table-align)
  8596. (message "New field value inserted")))
  8597. (defun org-trim (s)
  8598. "Remove whitespace at beginning and end of string."
  8599. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8600. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8601. s)
  8602. (defun org-wrap (string &optional width lines)
  8603. "Wrap string to either a number of lines, or a width in characters.
  8604. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8605. that costs. If there is a word longer than WIDTH, the text is actually
  8606. wrapped to the length of that word.
  8607. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8608. many lines, whatever width that takes.
  8609. The return value is a list of lines, without newlines at the end."
  8610. (let* ((words (org-split-string string "[ \t\n]+"))
  8611. (maxword (apply 'max (mapcar 'org-string-width words)))
  8612. w ll)
  8613. (cond (width
  8614. (org-do-wrap words (max maxword width)))
  8615. (lines
  8616. (setq w maxword)
  8617. (setq ll (org-do-wrap words maxword))
  8618. (if (<= (length ll) lines)
  8619. ll
  8620. (setq ll words)
  8621. (while (> (length ll) lines)
  8622. (setq w (1+ w))
  8623. (setq ll (org-do-wrap words w)))
  8624. ll))
  8625. (t (error "Cannot wrap this")))))
  8626. (defun org-do-wrap (words width)
  8627. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8628. (let (lines line)
  8629. (while words
  8630. (setq line (pop words))
  8631. (while (and words (< (+ (length line) (length (car words))) width))
  8632. (setq line (concat line " " (pop words))))
  8633. (setq lines (push line lines)))
  8634. (nreverse lines)))
  8635. (defun org-split-string (string &optional separators)
  8636. "Splits STRING into substrings at SEPARATORS.
  8637. No empty strings are returned if there are matches at the beginning
  8638. and end of string."
  8639. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8640. (start 0)
  8641. notfirst
  8642. (list nil))
  8643. (while (and (string-match rexp string
  8644. (if (and notfirst
  8645. (= start (match-beginning 0))
  8646. (< start (length string)))
  8647. (1+ start) start))
  8648. (< (match-beginning 0) (length string)))
  8649. (setq notfirst t)
  8650. (or (eq (match-beginning 0) 0)
  8651. (and (eq (match-beginning 0) (match-end 0))
  8652. (eq (match-beginning 0) start))
  8653. (setq list
  8654. (cons (substring string start (match-beginning 0))
  8655. list)))
  8656. (setq start (match-end 0)))
  8657. (or (eq start (length string))
  8658. (setq list
  8659. (cons (substring string start)
  8660. list)))
  8661. (nreverse list)))
  8662. (defun org-table-map-tables (function)
  8663. "Apply FUNCTION to the start of all tables in the buffer."
  8664. (save-excursion
  8665. (save-restriction
  8666. (widen)
  8667. (goto-char (point-min))
  8668. (while (re-search-forward org-table-any-line-regexp nil t)
  8669. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8670. (beginning-of-line 1)
  8671. (if (looking-at org-table-line-regexp)
  8672. (save-excursion (funcall function)))
  8673. (re-search-forward org-table-any-border-regexp nil 1))))
  8674. (message "Mapping tables: done"))
  8675. (defvar org-timecnt) ; dynamically scoped parameter
  8676. (defun org-table-sum (&optional beg end nlast)
  8677. "Sum numbers in region of current table column.
  8678. The result will be displayed in the echo area, and will be available
  8679. as kill to be inserted with \\[yank].
  8680. If there is an active region, it is interpreted as a rectangle and all
  8681. numbers in that rectangle will be summed. If there is no active
  8682. region and point is located in a table column, sum all numbers in that
  8683. column.
  8684. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8685. numbers are assumed to be times as well (in decimal hours) and the
  8686. numbers are added as such.
  8687. If NLAST is a number, only the NLAST fields will actually be summed."
  8688. (interactive)
  8689. (save-excursion
  8690. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8691. (cond
  8692. ((and beg end)) ; beg and end given explicitly
  8693. ((org-region-active-p)
  8694. (setq beg (region-beginning) end (region-end)))
  8695. (t
  8696. (setq col (org-table-current-column))
  8697. (goto-char (org-table-begin))
  8698. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8699. (error "No table data"))
  8700. (org-table-goto-column col)
  8701. (setq beg (point))
  8702. (goto-char (org-table-end))
  8703. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8704. (error "No table data"))
  8705. (org-table-goto-column col)
  8706. (setq end (point))))
  8707. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8708. (items1 (cond ((not nlast) items)
  8709. ((>= nlast (length items)) items)
  8710. (t (setq items (reverse items))
  8711. (setcdr (nthcdr (1- nlast) items) nil)
  8712. (nreverse items))))
  8713. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8714. items1)))
  8715. (res (apply '+ numbers))
  8716. (sres (if (= org-timecnt 0)
  8717. (format "%g" res)
  8718. (setq diff (* 3600 res)
  8719. h (floor (/ diff 3600)) diff (mod diff 3600)
  8720. m (floor (/ diff 60)) diff (mod diff 60)
  8721. s diff)
  8722. (format "%d:%02d:%02d" h m s))))
  8723. (kill-new sres)
  8724. (if (interactive-p)
  8725. (message "%s"
  8726. (substitute-command-keys
  8727. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8728. (length numbers) sres))))
  8729. sres))))
  8730. (defun org-table-get-number-for-summing (s)
  8731. (let (n)
  8732. (if (string-match "^ *|? *" s)
  8733. (setq s (replace-match "" nil nil s)))
  8734. (if (string-match " *|? *$" s)
  8735. (setq s (replace-match "" nil nil s)))
  8736. (setq n (string-to-number s))
  8737. (cond
  8738. ((and (string-match "0" s)
  8739. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8740. ((string-match "\\`[ \t]+\\'" s) nil)
  8741. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8742. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8743. (m (string-to-number (or (match-string 2 s) "0")))
  8744. (s (string-to-number (or (match-string 4 s) "0"))))
  8745. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8746. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8747. ((equal n 0) nil)
  8748. (t n))))
  8749. (defun org-table-current-field-formula (&optional key noerror)
  8750. "Return the formula active for the current field.
  8751. Assumes that specials are in place.
  8752. If KEY is given, return the key to this formula.
  8753. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8754. (let* ((name (car (rassoc (list (org-current-line)
  8755. (org-table-current-column))
  8756. org-table-named-field-locations)))
  8757. (col (org-table-current-column))
  8758. (scol (int-to-string col))
  8759. (ref (format "@%d$%d" (org-table-current-dline) col))
  8760. (stored-list (org-table-get-stored-formulas noerror))
  8761. (ass (or (assoc name stored-list)
  8762. (assoc ref stored-list)
  8763. (assoc scol stored-list))))
  8764. (if key
  8765. (car ass)
  8766. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8767. (cdr ass))))))
  8768. (defun org-table-get-formula (&optional equation named)
  8769. "Read a formula from the minibuffer, offer stored formula as default.
  8770. When NAMED is non-nil, look for a named equation."
  8771. (let* ((stored-list (org-table-get-stored-formulas))
  8772. (name (car (rassoc (list (org-current-line)
  8773. (org-table-current-column))
  8774. org-table-named-field-locations)))
  8775. (ref (format "@%d$%d" (org-table-current-dline)
  8776. (org-table-current-column)))
  8777. (refass (assoc ref stored-list))
  8778. (scol (if named
  8779. (if name name ref)
  8780. (int-to-string (org-table-current-column))))
  8781. (dummy (and (or name refass) (not named)
  8782. (not (y-or-n-p "Replace field formula with column formula? " ))
  8783. (error "Abort")))
  8784. (name (or name ref))
  8785. (org-table-may-need-update nil)
  8786. (stored (cdr (assoc scol stored-list)))
  8787. (eq (cond
  8788. ((and stored equation (string-match "^ *=? *$" equation))
  8789. stored)
  8790. ((stringp equation)
  8791. equation)
  8792. (t (org-table-formula-from-user
  8793. (read-string
  8794. (org-table-formula-to-user
  8795. (format "%s formula %s%s="
  8796. (if named "Field" "Column")
  8797. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8798. scol))
  8799. (if stored (org-table-formula-to-user stored) "")
  8800. 'org-table-formula-history
  8801. )))))
  8802. mustsave)
  8803. (when (not (string-match "\\S-" eq))
  8804. ;; remove formula
  8805. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8806. (org-table-store-formulas stored-list)
  8807. (error "Formula removed"))
  8808. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8809. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8810. (if (and name (not named))
  8811. ;; We set the column equation, delete the named one.
  8812. (setq stored-list (delq (assoc name stored-list) stored-list)
  8813. mustsave t))
  8814. (if stored
  8815. (setcdr (assoc scol stored-list) eq)
  8816. (setq stored-list (cons (cons scol eq) stored-list)))
  8817. (if (or mustsave (not (equal stored eq)))
  8818. (org-table-store-formulas stored-list))
  8819. eq))
  8820. (defun org-table-store-formulas (alist)
  8821. "Store the list of formulas below the current table."
  8822. (setq alist (sort alist 'org-table-formula-less-p))
  8823. (save-excursion
  8824. (goto-char (org-table-end))
  8825. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8826. (progn
  8827. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8828. (goto-char (match-beginning 2))
  8829. (delete-region (match-beginning 2) (match-end 0)))
  8830. (insert "#+TBLFM:"))
  8831. (insert " "
  8832. (mapconcat (lambda (x)
  8833. (concat
  8834. (if (equal (string-to-char (car x)) ?@) "" "$")
  8835. (car x) "=" (cdr x)))
  8836. alist "::")
  8837. "\n")))
  8838. (defsubst org-table-formula-make-cmp-string (a)
  8839. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8840. (concat
  8841. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8842. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8843. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8844. (defun org-table-formula-less-p (a b)
  8845. "Compare two formulas for sorting."
  8846. (let ((as (org-table-formula-make-cmp-string (car a)))
  8847. (bs (org-table-formula-make-cmp-string (car b))))
  8848. (and as bs (string< as bs))))
  8849. (defun org-table-get-stored-formulas (&optional noerror)
  8850. "Return an alist with the stored formulas directly after current table."
  8851. (interactive)
  8852. (let (scol eq eq-alist strings string seen)
  8853. (save-excursion
  8854. (goto-char (org-table-end))
  8855. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8856. (setq strings (org-split-string (match-string 2) " *:: *"))
  8857. (while (setq string (pop strings))
  8858. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8859. (setq scol (if (match-end 2)
  8860. (match-string 2 string)
  8861. (match-string 1 string))
  8862. eq (match-string 3 string)
  8863. eq-alist (cons (cons scol eq) eq-alist))
  8864. (if (member scol seen)
  8865. (if noerror
  8866. (progn
  8867. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8868. (ding)
  8869. (sit-for 2))
  8870. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8871. (push scol seen))))))
  8872. (nreverse eq-alist)))
  8873. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8874. "Modify the equations after the table structure has been edited.
  8875. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8876. For all numbers larger than LIMIT, shift them by DELTA."
  8877. (save-excursion
  8878. (goto-char (org-table-end))
  8879. (when (looking-at "#\\+TBLFM:")
  8880. (let ((re (concat key "\\([0-9]+\\)"))
  8881. (re2
  8882. (when remove
  8883. (if (equal key "$")
  8884. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8885. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8886. s n a)
  8887. (when remove
  8888. (while (re-search-forward re2 (point-at-eol) t)
  8889. (replace-match "")))
  8890. (while (re-search-forward re (point-at-eol) t)
  8891. (setq s (match-string 1) n (string-to-number s))
  8892. (cond
  8893. ((setq a (assoc s replace))
  8894. (replace-match (concat key (cdr a)) t t))
  8895. ((and limit (> n limit))
  8896. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8897. (defun org-table-get-specials ()
  8898. "Get the column names and local parameters for this table."
  8899. (save-excursion
  8900. (let ((beg (org-table-begin)) (end (org-table-end))
  8901. names name fields fields1 field cnt
  8902. c v l line col types dlines hlines)
  8903. (setq org-table-column-names nil
  8904. org-table-local-parameters nil
  8905. org-table-named-field-locations nil
  8906. org-table-current-begin-line nil
  8907. org-table-current-begin-pos nil
  8908. org-table-current-line-types nil)
  8909. (goto-char beg)
  8910. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8911. (setq names (org-split-string (match-string 1) " *| *")
  8912. cnt 1)
  8913. (while (setq name (pop names))
  8914. (setq cnt (1+ cnt))
  8915. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8916. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8917. (setq org-table-column-names (nreverse org-table-column-names))
  8918. (setq org-table-column-name-regexp
  8919. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8920. (goto-char beg)
  8921. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8922. (setq fields (org-split-string (match-string 1) " *| *"))
  8923. (while (setq field (pop fields))
  8924. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8925. (push (cons (match-string 1 field) (match-string 2 field))
  8926. org-table-local-parameters))))
  8927. (goto-char beg)
  8928. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8929. (setq c (match-string 1)
  8930. fields (org-split-string (match-string 2) " *| *"))
  8931. (save-excursion
  8932. (beginning-of-line (if (equal c "_") 2 0))
  8933. (setq line (org-current-line) col 1)
  8934. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8935. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8936. (while (and fields1 (setq field (pop fields)))
  8937. (setq v (pop fields1) col (1+ col))
  8938. (when (and (stringp field) (stringp v)
  8939. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8940. (push (cons field v) org-table-local-parameters)
  8941. (push (list field line col) org-table-named-field-locations))))
  8942. ;; Analyse the line types
  8943. (goto-char beg)
  8944. (setq org-table-current-begin-line (org-current-line)
  8945. org-table-current-begin-pos (point)
  8946. l org-table-current-begin-line)
  8947. (while (looking-at "[ \t]*|\\(-\\)?")
  8948. (push (if (match-end 1) 'hline 'dline) types)
  8949. (if (match-end 1) (push l hlines) (push l dlines))
  8950. (beginning-of-line 2)
  8951. (setq l (1+ l)))
  8952. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8953. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8954. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8955. (defun org-table-maybe-eval-formula ()
  8956. "Check if the current field starts with \"=\" or \":=\".
  8957. If yes, store the formula and apply it."
  8958. ;; We already know we are in a table. Get field will only return a formula
  8959. ;; when appropriate. It might return a separator line, but no problem.
  8960. (when org-table-formula-evaluate-inline
  8961. (let* ((field (org-trim (or (org-table-get-field) "")))
  8962. named eq)
  8963. (when (string-match "^:?=\\(.*\\)" field)
  8964. (setq named (equal (string-to-char field) ?:)
  8965. eq (match-string 1 field))
  8966. (if (or (fboundp 'calc-eval)
  8967. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8968. (org-table-eval-formula (if named '(4) nil)
  8969. (org-table-formula-from-user eq))
  8970. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8971. (defvar org-recalc-commands nil
  8972. "List of commands triggering the recalculation of a line.
  8973. Will be filled automatically during use.")
  8974. (defvar org-recalc-marks
  8975. '((" " . "Unmarked: no special line, no automatic recalculation")
  8976. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8977. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8978. ("!" . "Column name definition line. Reference in formula as $name.")
  8979. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8980. ("_" . "Names for values in row below this one.")
  8981. ("^" . "Names for values in row above this one.")))
  8982. (defun org-table-rotate-recalc-marks (&optional newchar)
  8983. "Rotate the recalculation mark in the first column.
  8984. If in any row, the first field is not consistent with a mark,
  8985. insert a new column for the markers.
  8986. When there is an active region, change all the lines in the region,
  8987. after prompting for the marking character.
  8988. After each change, a message will be displayed indicating the meaning
  8989. of the new mark."
  8990. (interactive)
  8991. (unless (org-at-table-p) (error "Not at a table"))
  8992. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8993. (beg (org-table-begin))
  8994. (end (org-table-end))
  8995. (l (org-current-line))
  8996. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8997. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8998. (have-col
  8999. (save-excursion
  9000. (goto-char beg)
  9001. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9002. (col (org-table-current-column))
  9003. (forcenew (car (assoc newchar org-recalc-marks)))
  9004. epos new)
  9005. (when l1
  9006. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9007. (setq newchar (char-to-string (read-char-exclusive))
  9008. forcenew (car (assoc newchar org-recalc-marks))))
  9009. (if (and newchar (not forcenew))
  9010. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9011. newchar))
  9012. (if l1 (goto-line l1))
  9013. (save-excursion
  9014. (beginning-of-line 1)
  9015. (unless (looking-at org-table-dataline-regexp)
  9016. (error "Not at a table data line")))
  9017. (unless have-col
  9018. (org-table-goto-column 1)
  9019. (org-table-insert-column)
  9020. (org-table-goto-column (1+ col)))
  9021. (setq epos (point-at-eol))
  9022. (save-excursion
  9023. (beginning-of-line 1)
  9024. (org-table-get-field
  9025. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9026. (concat " "
  9027. (setq new (or forcenew
  9028. (cadr (member (match-string 1) marks))))
  9029. " ")
  9030. " # ")))
  9031. (if (and l1 l2)
  9032. (progn
  9033. (goto-line l1)
  9034. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9035. (and (looking-at org-table-dataline-regexp)
  9036. (org-table-get-field 1 (concat " " new " "))))
  9037. (goto-line l1)))
  9038. (if (not (= epos (point-at-eol))) (org-table-align))
  9039. (goto-line l)
  9040. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9041. (defun org-table-maybe-recalculate-line ()
  9042. "Recompute the current line if marked for it, and if we haven't just done it."
  9043. (interactive)
  9044. (and org-table-allow-automatic-line-recalculation
  9045. (not (and (memq last-command org-recalc-commands)
  9046. (equal org-last-recalc-line (org-current-line))))
  9047. (save-excursion (beginning-of-line 1)
  9048. (looking-at org-table-auto-recalculate-regexp))
  9049. (org-table-recalculate) t))
  9050. (defvar org-table-formula-debug nil
  9051. "Non-nil means, debug table formulas.
  9052. When nil, simply write \"#ERROR\" in corrupted fields.")
  9053. (make-variable-buffer-local 'org-table-formula-debug)
  9054. (defvar modes)
  9055. (defsubst org-set-calc-mode (var &optional value)
  9056. (if (stringp var)
  9057. (setq var (assoc var '(("D" calc-angle-mode deg)
  9058. ("R" calc-angle-mode rad)
  9059. ("F" calc-prefer-frac t)
  9060. ("S" calc-symbolic-mode t)))
  9061. value (nth 2 var) var (nth 1 var)))
  9062. (if (memq var modes)
  9063. (setcar (cdr (memq var modes)) value)
  9064. (cons var (cons value modes)))
  9065. modes)
  9066. (defun org-table-eval-formula (&optional arg equation
  9067. suppress-align suppress-const
  9068. suppress-store suppress-analysis)
  9069. "Replace the table field value at the cursor by the result of a calculation.
  9070. This function makes use of Dave Gillespie's Calc package, in my view the
  9071. most exciting program ever written for GNU Emacs. So you need to have Calc
  9072. installed in order to use this function.
  9073. In a table, this command replaces the value in the current field with the
  9074. result of a formula. It also installs the formula as the \"current\" column
  9075. formula, by storing it in a special line below the table. When called
  9076. with a `C-u' prefix, the current field must ba a named field, and the
  9077. formula is installed as valid in only this specific field.
  9078. When called with two `C-u' prefixes, insert the active equation
  9079. for the field back into the current field, so that it can be
  9080. edited there. This is useful in order to use \\[org-table-show-reference]
  9081. to check the referenced fields.
  9082. When called, the command first prompts for a formula, which is read in
  9083. the minibuffer. Previously entered formulas are available through the
  9084. history list, and the last used formula is offered as a default.
  9085. These stored formulas are adapted correctly when moving, inserting, or
  9086. deleting columns with the corresponding commands.
  9087. The formula can be any algebraic expression understood by the Calc package.
  9088. For details, see the Org-mode manual.
  9089. This function can also be called from Lisp programs and offers
  9090. additional arguments: EQUATION can be the formula to apply. If this
  9091. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9092. used to speed-up recursive calls by by-passing unnecessary aligns.
  9093. SUPPRESS-CONST suppresses the interpretation of constants in the
  9094. formula, assuming that this has been done already outside the function.
  9095. SUPPRESS-STORE means the formula should not be stored, either because
  9096. it is already stored, or because it is a modified equation that should
  9097. not overwrite the stored one."
  9098. (interactive "P")
  9099. (org-table-check-inside-data-field)
  9100. (or suppress-analysis (org-table-get-specials))
  9101. (if (equal arg '(16))
  9102. (let ((eq (org-table-current-field-formula)))
  9103. (or eq (error "No equation active for current field"))
  9104. (org-table-get-field nil eq)
  9105. (org-table-align)
  9106. (setq org-table-may-need-update t))
  9107. (let* (fields
  9108. (ndown (if (integerp arg) arg 1))
  9109. (org-table-automatic-realign nil)
  9110. (case-fold-search nil)
  9111. (down (> ndown 1))
  9112. (formula (if (and equation suppress-store)
  9113. equation
  9114. (org-table-get-formula equation (equal arg '(4)))))
  9115. (n0 (org-table-current-column))
  9116. (modes (copy-sequence org-calc-default-modes))
  9117. (numbers nil) ; was a variable, now fixed default
  9118. (keep-empty nil)
  9119. n form form0 bw fmt x ev orig c lispp literal)
  9120. ;; Parse the format string. Since we have a lot of modes, this is
  9121. ;; a lot of work. However, I think calc still uses most of the time.
  9122. (if (string-match ";" formula)
  9123. (let ((tmp (org-split-string formula ";")))
  9124. (setq formula (car tmp)
  9125. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9126. (nth 1 tmp)))
  9127. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9128. (setq c (string-to-char (match-string 1 fmt))
  9129. n (string-to-number (match-string 2 fmt)))
  9130. (if (= c ?p)
  9131. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9132. (setq modes (org-set-calc-mode
  9133. 'calc-float-format
  9134. (list (cdr (assoc c '((?n . float) (?f . fix)
  9135. (?s . sci) (?e . eng))))
  9136. n))))
  9137. (setq fmt (replace-match "" t t fmt)))
  9138. (if (string-match "[NT]" fmt)
  9139. (setq numbers (equal (match-string 0 fmt) "N")
  9140. fmt (replace-match "" t t fmt)))
  9141. (if (string-match "L" fmt)
  9142. (setq literal t
  9143. fmt (replace-match "" t t fmt)))
  9144. (if (string-match "E" fmt)
  9145. (setq keep-empty t
  9146. fmt (replace-match "" t t fmt)))
  9147. (while (string-match "[DRFS]" fmt)
  9148. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9149. (setq fmt (replace-match "" t t fmt)))
  9150. (unless (string-match "\\S-" fmt)
  9151. (setq fmt nil))))
  9152. (if (and (not suppress-const) org-table-formula-use-constants)
  9153. (setq formula (org-table-formula-substitute-names formula)))
  9154. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9155. (while (> ndown 0)
  9156. (setq fields (org-split-string
  9157. (org-no-properties
  9158. (buffer-substring (point-at-bol) (point-at-eol)))
  9159. " *| *"))
  9160. (if (eq numbers t)
  9161. (setq fields (mapcar
  9162. (lambda (x) (number-to-string (string-to-number x)))
  9163. fields)))
  9164. (setq ndown (1- ndown))
  9165. (setq form (copy-sequence formula)
  9166. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9167. (if (and lispp literal) (setq lispp 'literal))
  9168. ;; Check for old vertical references
  9169. (setq form (org-rewrite-old-row-references form))
  9170. ;; Insert complex ranges
  9171. (while (string-match org-table-range-regexp form)
  9172. (setq form
  9173. (replace-match
  9174. (save-match-data
  9175. (org-table-make-reference
  9176. (org-table-get-range (match-string 0 form) nil n0)
  9177. keep-empty numbers lispp))
  9178. t t form)))
  9179. ;; Insert simple ranges
  9180. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9181. (setq form
  9182. (replace-match
  9183. (save-match-data
  9184. (org-table-make-reference
  9185. (org-sublist
  9186. fields (string-to-number (match-string 1 form))
  9187. (string-to-number (match-string 2 form)))
  9188. keep-empty numbers lispp))
  9189. t t form)))
  9190. (setq form0 form)
  9191. ;; Insert the references to fields in same row
  9192. (while (string-match "\\$\\([0-9]+\\)" form)
  9193. (setq n (string-to-number (match-string 1 form))
  9194. x (nth (1- (if (= n 0) n0 n)) fields))
  9195. (unless x (error "Invalid field specifier \"%s\""
  9196. (match-string 0 form)))
  9197. (setq form (replace-match
  9198. (save-match-data
  9199. (org-table-make-reference x nil numbers lispp))
  9200. t t form)))
  9201. (if lispp
  9202. (setq ev (condition-case nil
  9203. (eval (eval (read form)))
  9204. (error "#ERROR"))
  9205. ev (if (numberp ev) (number-to-string ev) ev))
  9206. (or (fboundp 'calc-eval)
  9207. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9208. (setq ev (calc-eval (cons form modes)
  9209. (if numbers 'num))))
  9210. (when org-table-formula-debug
  9211. (with-output-to-temp-buffer "*Substitution History*"
  9212. (princ (format "Substitution history of formula
  9213. Orig: %s
  9214. $xyz-> %s
  9215. @r$c-> %s
  9216. $1-> %s\n" orig formula form0 form))
  9217. (if (listp ev)
  9218. (princ (format " %s^\nError: %s"
  9219. (make-string (car ev) ?\-) (nth 1 ev)))
  9220. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9221. ev (or fmt "NONE")
  9222. (if fmt (format fmt (string-to-number ev)) ev)))))
  9223. (setq bw (get-buffer-window "*Substitution History*"))
  9224. (shrink-window-if-larger-than-buffer bw)
  9225. (unless (and (interactive-p) (not ndown))
  9226. (unless (let (inhibit-redisplay)
  9227. (y-or-n-p "Debugging Formula. Continue to next? "))
  9228. (org-table-align)
  9229. (error "Abort"))
  9230. (delete-window bw)
  9231. (message "")))
  9232. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9233. (org-table-justify-field-maybe
  9234. (if fmt (format fmt (string-to-number ev)) ev))
  9235. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9236. (call-interactively 'org-return)
  9237. (setq ndown 0)))
  9238. (and down (org-table-maybe-recalculate-line))
  9239. (or suppress-align (and org-table-may-need-update
  9240. (org-table-align))))))
  9241. (defun org-table-put-field-property (prop value)
  9242. (save-excursion
  9243. (put-text-property (progn (skip-chars-backward "^|") (point))
  9244. (progn (skip-chars-forward "^|") (point))
  9245. prop value)))
  9246. (defun org-table-get-range (desc &optional tbeg col highlight)
  9247. "Get a calc vector from a column, accorting to descriptor DESC.
  9248. Optional arguments TBEG and COL can give the beginning of the table and
  9249. the current column, to avoid unnecessary parsing.
  9250. HIGHLIGHT means, just highlight the range."
  9251. (if (not (equal (string-to-char desc) ?@))
  9252. (setq desc (concat "@" desc)))
  9253. (save-excursion
  9254. (or tbeg (setq tbeg (org-table-begin)))
  9255. (or col (setq col (org-table-current-column)))
  9256. (let ((thisline (org-current-line))
  9257. beg end c1 c2 r1 r2 rangep tmp)
  9258. (unless (string-match org-table-range-regexp desc)
  9259. (error "Invalid table range specifier `%s'" desc))
  9260. (setq rangep (match-end 3)
  9261. r1 (and (match-end 1) (match-string 1 desc))
  9262. r2 (and (match-end 4) (match-string 4 desc))
  9263. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9264. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9265. (and c1 (setq c1 (+ (string-to-number c1)
  9266. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9267. (and c2 (setq c2 (+ (string-to-number c2)
  9268. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9269. (if (equal r1 "") (setq r1 nil))
  9270. (if (equal r2 "") (setq r2 nil))
  9271. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9272. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9273. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9274. (if (not r1) (setq r1 thisline))
  9275. (if (not r2) (setq r2 thisline))
  9276. (if (not c1) (setq c1 col))
  9277. (if (not c2) (setq c2 col))
  9278. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9279. ;; just one field
  9280. (progn
  9281. (goto-line r1)
  9282. (while (not (looking-at org-table-dataline-regexp))
  9283. (beginning-of-line 2))
  9284. (prog1 (org-trim (org-table-get-field c1))
  9285. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9286. ;; A range, return a vector
  9287. ;; First sort the numbers to get a regular ractangle
  9288. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9289. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9290. (goto-line r1)
  9291. (while (not (looking-at org-table-dataline-regexp))
  9292. (beginning-of-line 2))
  9293. (org-table-goto-column c1)
  9294. (setq beg (point))
  9295. (goto-line r2)
  9296. (while (not (looking-at org-table-dataline-regexp))
  9297. (beginning-of-line 0))
  9298. (org-table-goto-column c2)
  9299. (setq end (point))
  9300. (if highlight
  9301. (org-table-highlight-rectangle
  9302. beg (progn (skip-chars-forward "^|\n") (point))))
  9303. ;; return string representation of calc vector
  9304. (mapcar 'org-trim
  9305. (apply 'append (org-table-copy-region beg end)))))))
  9306. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9307. "Analyze descriptor DESC and retrieve the corresponding line number.
  9308. The cursor is currently in line CLINE, the table begins in line BLINE,
  9309. and TABLE is a vector with line types."
  9310. (if (string-match "^[0-9]+$" desc)
  9311. (aref org-table-dlines (string-to-number desc))
  9312. (setq cline (or cline (org-current-line))
  9313. bline (or bline org-table-current-begin-line)
  9314. table (or table org-table-current-line-types))
  9315. (if (or
  9316. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9317. ;; 1 2 3 4 5 6
  9318. (and (not (match-end 3)) (not (match-end 6)))
  9319. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9320. (error "invalid row descriptor `%s'" desc))
  9321. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9322. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9323. (odir (and (match-end 5) (match-string 5 desc)))
  9324. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9325. (i (- cline bline))
  9326. (rel (and (match-end 6)
  9327. (or (and (match-end 1) (not (match-end 3)))
  9328. (match-end 5)))))
  9329. (if (and hn (not hdir))
  9330. (progn
  9331. (setq i 0 hdir "+")
  9332. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9333. (if (and (not hn) on (not odir))
  9334. (error "should never happen");;(aref org-table-dlines on)
  9335. (if (and hn (> hn 0))
  9336. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9337. (if on
  9338. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9339. (+ bline i)))))
  9340. (defun org-find-row-type (table i type backwards relative n)
  9341. (let ((l (length table)))
  9342. (while (> n 0)
  9343. (while (and (setq i (+ i (if backwards -1 1)))
  9344. (>= i 0) (< i l)
  9345. (not (eq (aref table i) type))
  9346. (if (and relative (eq (aref table i) 'hline))
  9347. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9348. t)))
  9349. (setq n (1- n)))
  9350. (if (or (< i 0) (>= i l))
  9351. (error "Row descriptior leads outside table")
  9352. i)))
  9353. (defun org-rewrite-old-row-references (s)
  9354. (if (string-match "&[-+0-9I]" s)
  9355. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9356. s))
  9357. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9358. "Convert list ELEMENTS to something appropriate to insert into formula.
  9359. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9360. NUMBERS indicates that everything should be converted to numbers.
  9361. LISPP means to return something appropriate for a Lisp list."
  9362. (if (stringp elements) ; just a single val
  9363. (if lispp
  9364. (if (eq lispp 'literal)
  9365. elements
  9366. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9367. (if (equal elements "") (setq elements "0"))
  9368. (if numbers (number-to-string (string-to-number elements)) elements))
  9369. (unless keep-empty
  9370. (setq elements
  9371. (delq nil
  9372. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9373. elements))))
  9374. (setq elements (or elements '("0")))
  9375. (if lispp
  9376. (mapconcat
  9377. (lambda (x)
  9378. (if (eq lispp 'literal)
  9379. x
  9380. (prin1-to-string (if numbers (string-to-number x) x))))
  9381. elements " ")
  9382. (concat "[" (mapconcat
  9383. (lambda (x)
  9384. (if numbers (number-to-string (string-to-number x)) x))
  9385. elements
  9386. ",") "]"))))
  9387. (defun org-table-recalculate (&optional all noalign)
  9388. "Recalculate the current table line by applying all stored formulas.
  9389. With prefix arg ALL, do this for all lines in the table."
  9390. (interactive "P")
  9391. (or (memq this-command org-recalc-commands)
  9392. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9393. (unless (org-at-table-p) (error "Not at a table"))
  9394. (if (equal all '(16))
  9395. (org-table-iterate)
  9396. (org-table-get-specials)
  9397. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9398. (lambda (a b) (string< (car a) (car b)))))
  9399. (inhibit-redisplay (not debug-on-error))
  9400. (line-re org-table-dataline-regexp)
  9401. (thisline (org-current-line))
  9402. (thiscol (org-table-current-column))
  9403. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9404. ;; Insert constants in all formulas
  9405. (setq eqlist
  9406. (mapcar (lambda (x)
  9407. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9408. x)
  9409. eqlist))
  9410. ;; Split the equation list
  9411. (while (setq eq (pop eqlist))
  9412. (if (<= (string-to-char (car eq)) ?9)
  9413. (push eq eqlnum)
  9414. (push eq eqlname)))
  9415. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9416. (if all
  9417. (progn
  9418. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9419. (goto-char (setq beg (org-table-begin)))
  9420. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9421. ;; This is a table with marked lines, compute selected lines
  9422. (setq line-re org-table-recalculate-regexp)
  9423. ;; Move forward to the first non-header line
  9424. (if (and (re-search-forward org-table-dataline-regexp end t)
  9425. (re-search-forward org-table-hline-regexp end t)
  9426. (re-search-forward org-table-dataline-regexp end t))
  9427. (setq beg (match-beginning 0))
  9428. nil))) ;; just leave beg where it is
  9429. (setq beg (point-at-bol)
  9430. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9431. (goto-char beg)
  9432. (and all (message "Re-applying formulas to full table..."))
  9433. ;; First find the named fields, and mark them untouchanble
  9434. (remove-text-properties beg end '(org-untouchable t))
  9435. (while (setq eq (pop eqlname))
  9436. (setq name (car eq)
  9437. a (assoc name org-table-named-field-locations))
  9438. (and (not a)
  9439. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9440. (setq a (list name
  9441. (aref org-table-dlines
  9442. (string-to-number (match-string 1 name)))
  9443. (string-to-number (match-string 2 name)))))
  9444. (when (and a (or all (equal (nth 1 a) thisline)))
  9445. (message "Re-applying formula to field: %s" name)
  9446. (goto-line (nth 1 a))
  9447. (org-table-goto-column (nth 2 a))
  9448. (push (append a (list (cdr eq))) eqlname1)
  9449. (org-table-put-field-property :org-untouchable t)))
  9450. ;; Now evauluate the column formulas, but skip fields covered by
  9451. ;; field formulas
  9452. (goto-char beg)
  9453. (while (re-search-forward line-re end t)
  9454. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9455. ;; Unprotected line, recalculate
  9456. (and all (message "Re-applying formulas to full table...(line %d)"
  9457. (setq cnt (1+ cnt))))
  9458. (setq org-last-recalc-line (org-current-line))
  9459. (setq eql eqlnum)
  9460. (while (setq entry (pop eql))
  9461. (goto-line org-last-recalc-line)
  9462. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9463. (unless (get-text-property (point) :org-untouchable)
  9464. (org-table-eval-formula nil (cdr entry)
  9465. 'noalign 'nocst 'nostore 'noanalysis)))))
  9466. ;; Now evaluate the field formulas
  9467. (while (setq eq (pop eqlname1))
  9468. (message "Re-applying formula to field: %s" (car eq))
  9469. (goto-line (nth 1 eq))
  9470. (org-table-goto-column (nth 2 eq))
  9471. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9472. 'nostore 'noanalysis))
  9473. (goto-line thisline)
  9474. (org-table-goto-column thiscol)
  9475. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9476. (or noalign (and org-table-may-need-update (org-table-align))
  9477. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9478. ;; back to initial position
  9479. (message "Re-applying formulas...done")
  9480. (goto-line thisline)
  9481. (org-table-goto-column thiscol)
  9482. (or noalign (and org-table-may-need-update (org-table-align))
  9483. (and all (message "Re-applying formulas...done"))))))
  9484. (defun org-table-iterate (&optional arg)
  9485. "Recalculate the table until it does not change anymore."
  9486. (interactive "P")
  9487. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9488. (i 0)
  9489. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9490. thistbl)
  9491. (catch 'exit
  9492. (while (< i imax)
  9493. (setq i (1+ i))
  9494. (org-table-recalculate 'all)
  9495. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9496. (if (not (string= lasttbl thistbl))
  9497. (setq lasttbl thistbl)
  9498. (if (> i 1)
  9499. (message "Convergence after %d iterations" i)
  9500. (message "Table was already stable"))
  9501. (throw 'exit t)))
  9502. (error "No convergence after %d iterations" i))))
  9503. (defun org-table-formula-substitute-names (f)
  9504. "Replace $const with values in string F."
  9505. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9506. ;; First, check for column names
  9507. (while (setq start (string-match org-table-column-name-regexp f start))
  9508. (setq start (1+ start))
  9509. (setq a (assoc (match-string 1 f) org-table-column-names))
  9510. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9511. ;; Parameters and constants
  9512. (setq start 0)
  9513. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9514. (setq start (1+ start))
  9515. (if (setq a (save-match-data
  9516. (org-table-get-constant (match-string 1 f))))
  9517. (setq f (replace-match
  9518. (concat (if pp "(") a (if pp ")")) t t f))))
  9519. (if org-table-formula-debug
  9520. (put-text-property 0 (length f) :orig-formula f1 f))
  9521. f))
  9522. (defun org-table-get-constant (const)
  9523. "Find the value for a parameter or constant in a formula.
  9524. Parameters get priority."
  9525. (or (cdr (assoc const org-table-local-parameters))
  9526. (cdr (assoc const org-table-formula-constants-local))
  9527. (cdr (assoc const org-table-formula-constants))
  9528. (and (fboundp 'constants-get) (constants-get const))
  9529. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9530. (org-entry-get nil (substring const 5) 'inherit))
  9531. "#UNDEFINED_NAME"))
  9532. (defvar org-table-fedit-map
  9533. (let ((map (make-sparse-keymap)))
  9534. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9535. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9536. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9537. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9538. (org-defkey map "\C-c?" 'org-table-show-reference)
  9539. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9540. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9541. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9542. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9543. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9544. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9545. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9546. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9547. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9548. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9549. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9550. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9551. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9552. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9553. map))
  9554. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9555. '("Edit-Formulas"
  9556. ["Finish and Install" org-table-fedit-finish t]
  9557. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9558. ["Abort" org-table-fedit-abort t]
  9559. "--"
  9560. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9561. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9562. "--"
  9563. "Shift Reference at Point"
  9564. ["Up" org-table-fedit-ref-up t]
  9565. ["Down" org-table-fedit-ref-down t]
  9566. ["Left" org-table-fedit-ref-left t]
  9567. ["Right" org-table-fedit-ref-right t]
  9568. "-"
  9569. "Change Test Row for Column Formulas"
  9570. ["Up" org-table-fedit-line-up t]
  9571. ["Down" org-table-fedit-line-down t]
  9572. "--"
  9573. ["Scroll Table Window" org-table-fedit-scroll t]
  9574. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9575. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9576. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9577. org-table-overlay-coordinates)]
  9578. "--"
  9579. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9580. :style toggle :selected org-table-buffer-is-an]))
  9581. (defvar org-pos)
  9582. (defun org-table-edit-formulas ()
  9583. "Edit the formulas of the current table in a separate buffer."
  9584. (interactive)
  9585. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9586. (beginning-of-line 0))
  9587. (unless (org-at-table-p) (error "Not at a table"))
  9588. (org-table-get-specials)
  9589. (let ((key (org-table-current-field-formula 'key 'noerror))
  9590. (eql (sort (org-table-get-stored-formulas 'noerror)
  9591. 'org-table-formula-less-p))
  9592. (pos (move-marker (make-marker) (point)))
  9593. (startline 1)
  9594. (wc (current-window-configuration))
  9595. (titles '((column . "# Column Formulas\n")
  9596. (field . "# Field Formulas\n")
  9597. (named . "# Named Field Formulas\n")))
  9598. entry s type title)
  9599. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9600. (erase-buffer)
  9601. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9602. (let ((font-lock-global-modes '(not fundamental-mode)))
  9603. (fundamental-mode))
  9604. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9605. (org-set-local 'org-pos pos)
  9606. (org-set-local 'org-window-configuration wc)
  9607. (use-local-map org-table-fedit-map)
  9608. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9609. (easy-menu-add org-table-fedit-menu)
  9610. (setq startline (org-current-line))
  9611. (while (setq entry (pop eql))
  9612. (setq type (cond
  9613. ((equal (string-to-char (car entry)) ?@) 'field)
  9614. ((string-match "^[0-9]" (car entry)) 'column)
  9615. (t 'named)))
  9616. (when (setq title (assq type titles))
  9617. (or (bobp) (insert "\n"))
  9618. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9619. (setq titles (delq title titles)))
  9620. (if (equal key (car entry)) (setq startline (org-current-line)))
  9621. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9622. (car entry) " = " (cdr entry) "\n"))
  9623. (remove-text-properties 0 (length s) '(face nil) s)
  9624. (insert s))
  9625. (if (eq org-table-use-standard-references t)
  9626. (org-table-fedit-toggle-ref-type))
  9627. (goto-line startline)
  9628. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9629. (defun org-table-fedit-post-command ()
  9630. (when (not (memq this-command '(lisp-complete-symbol)))
  9631. (let ((win (selected-window)))
  9632. (save-excursion
  9633. (condition-case nil
  9634. (org-table-show-reference)
  9635. (error nil))
  9636. (select-window win)))))
  9637. (defun org-table-formula-to-user (s)
  9638. "Convert a formula from internal to user representation."
  9639. (if (eq org-table-use-standard-references t)
  9640. (org-table-convert-refs-to-an s)
  9641. s))
  9642. (defun org-table-formula-from-user (s)
  9643. "Convert a formula from user to internal representation."
  9644. (if org-table-use-standard-references
  9645. (org-table-convert-refs-to-rc s)
  9646. s))
  9647. (defun org-table-convert-refs-to-rc (s)
  9648. "Convert spreadsheet references from AB7 to @7$28.
  9649. Works for single references, but also for entire formulas and even the
  9650. full TBLFM line."
  9651. (let ((start 0))
  9652. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9653. (cond
  9654. ((match-end 3)
  9655. ;; format match, just advance
  9656. (setq start (match-end 0)))
  9657. ((and (> (match-beginning 0) 0)
  9658. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9659. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9660. ;; 3.e5 or something like this.
  9661. (setq start (match-end 0)))
  9662. (t
  9663. (setq start (match-beginning 0)
  9664. s (replace-match
  9665. (if (equal (match-string 2 s) "&")
  9666. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9667. (format "@%d$%d"
  9668. (string-to-number (match-string 2 s))
  9669. (org-letters-to-number (match-string 1 s))))
  9670. t t s)))))
  9671. s))
  9672. (defun org-table-convert-refs-to-an (s)
  9673. "Convert spreadsheet references from to @7$28 to AB7.
  9674. Works for single references, but also for entire formulas and even the
  9675. full TBLFM line."
  9676. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9677. (setq s (replace-match
  9678. (format "%s%d"
  9679. (org-number-to-letters
  9680. (string-to-number (match-string 2 s)))
  9681. (string-to-number (match-string 1 s)))
  9682. t t s)))
  9683. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9684. (setq s (replace-match (concat "\\1"
  9685. (org-number-to-letters
  9686. (string-to-number (match-string 2 s))) "&")
  9687. t nil s)))
  9688. s)
  9689. (defun org-letters-to-number (s)
  9690. "Convert a base 26 number represented by letters into an integer.
  9691. For example: AB -> 28."
  9692. (let ((n 0))
  9693. (setq s (upcase s))
  9694. (while (> (length s) 0)
  9695. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9696. s (substring s 1)))
  9697. n))
  9698. (defun org-number-to-letters (n)
  9699. "Convert an integer into a base 26 number represented by letters.
  9700. For example: 28 -> AB."
  9701. (let ((s ""))
  9702. (while (> n 0)
  9703. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9704. n (/ (1- n) 26)))
  9705. s))
  9706. (defun org-table-fedit-convert-buffer (function)
  9707. "Convert all references in this buffer, using FUNTION."
  9708. (let ((line (org-current-line)))
  9709. (goto-char (point-min))
  9710. (while (not (eobp))
  9711. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9712. (delete-region (point) (point-at-eol))
  9713. (or (eobp) (forward-char 1)))
  9714. (goto-line line)))
  9715. (defun org-table-fedit-toggle-ref-type ()
  9716. "Convert all references in the buffer from B3 to @3$2 and back."
  9717. (interactive)
  9718. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9719. (org-table-fedit-convert-buffer
  9720. (if org-table-buffer-is-an
  9721. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9722. (message "Reference type switched to %s"
  9723. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9724. (defun org-table-fedit-ref-up ()
  9725. "Shift the reference at point one row/hline up."
  9726. (interactive)
  9727. (org-table-fedit-shift-reference 'up))
  9728. (defun org-table-fedit-ref-down ()
  9729. "Shift the reference at point one row/hline down."
  9730. (interactive)
  9731. (org-table-fedit-shift-reference 'down))
  9732. (defun org-table-fedit-ref-left ()
  9733. "Shift the reference at point one field to the left."
  9734. (interactive)
  9735. (org-table-fedit-shift-reference 'left))
  9736. (defun org-table-fedit-ref-right ()
  9737. "Shift the reference at point one field to the right."
  9738. (interactive)
  9739. (org-table-fedit-shift-reference 'right))
  9740. (defun org-table-fedit-shift-reference (dir)
  9741. (cond
  9742. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9743. (if (memq dir '(left right))
  9744. (org-rematch-and-replace 1 (eq dir 'left))
  9745. (error "Cannot shift reference in this direction")))
  9746. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9747. ;; A B3-like reference
  9748. (if (memq dir '(up down))
  9749. (org-rematch-and-replace 2 (eq dir 'up))
  9750. (org-rematch-and-replace 1 (eq dir 'left))))
  9751. ((org-at-regexp-p
  9752. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9753. ;; An internal reference
  9754. (if (memq dir '(up down))
  9755. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9756. (org-rematch-and-replace 5 (eq dir 'left))))))
  9757. (defun org-rematch-and-replace (n &optional decr hline)
  9758. "Re-match the group N, and replace it with the shifted refrence."
  9759. (or (match-end n) (error "Cannot shift reference in this direction"))
  9760. (goto-char (match-beginning n))
  9761. (and (looking-at (regexp-quote (match-string n)))
  9762. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9763. t t)))
  9764. (defun org-shift-refpart (ref &optional decr hline)
  9765. "Shift a refrence part REF.
  9766. If DECR is set, decrease the references row/column, else increase.
  9767. If HLINE is set, this may be a hline reference, it certainly is not
  9768. a translation reference."
  9769. (save-match-data
  9770. (let* ((sign (string-match "^[-+]" ref)) n)
  9771. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9772. (cond
  9773. ((and hline (string-match "^I+" ref))
  9774. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9775. (setq n (+ n (if decr -1 1)))
  9776. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9777. (if sign
  9778. (setq sign (if (< n 0) "-" "+") n (abs n))
  9779. (setq n (max 1 n)))
  9780. (concat sign (make-string n ?I)))
  9781. ((string-match "^[0-9]+" ref)
  9782. (setq n (string-to-number (concat sign ref)))
  9783. (setq n (+ n (if decr -1 1)))
  9784. (if sign
  9785. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9786. (number-to-string (max 1 n))))
  9787. ((string-match "^[a-zA-Z]+" ref)
  9788. (org-number-to-letters
  9789. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9790. (t (error "Cannot shift reference"))))))
  9791. (defun org-table-fedit-toggle-coordinates ()
  9792. "Toggle the display of coordinates in the refrenced table."
  9793. (interactive)
  9794. (let ((pos (marker-position org-pos)))
  9795. (with-current-buffer (marker-buffer org-pos)
  9796. (save-excursion
  9797. (goto-char pos)
  9798. (org-table-toggle-coordinate-overlays)))))
  9799. (defun org-table-fedit-finish (&optional arg)
  9800. "Parse the buffer for formula definitions and install them.
  9801. With prefix ARG, apply the new formulas to the table."
  9802. (interactive "P")
  9803. (org-table-remove-rectangle-highlight)
  9804. (if org-table-use-standard-references
  9805. (progn
  9806. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9807. (setq org-table-buffer-is-an nil)))
  9808. (let ((pos org-pos) eql var form)
  9809. (goto-char (point-min))
  9810. (while (re-search-forward
  9811. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9812. nil t)
  9813. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9814. form (match-string 3))
  9815. (setq form (org-trim form))
  9816. (when (not (equal form ""))
  9817. (while (string-match "[ \t]*\n[ \t]*" form)
  9818. (setq form (replace-match " " t t form)))
  9819. (when (assoc var eql)
  9820. (error "Double formulas for %s" var))
  9821. (push (cons var form) eql)))
  9822. (setq org-pos nil)
  9823. (set-window-configuration org-window-configuration)
  9824. (select-window (get-buffer-window (marker-buffer pos)))
  9825. (goto-char pos)
  9826. (unless (org-at-table-p)
  9827. (error "Lost table position - cannot install formulae"))
  9828. (org-table-store-formulas eql)
  9829. (move-marker pos nil)
  9830. (kill-buffer "*Edit Formulas*")
  9831. (if arg
  9832. (org-table-recalculate 'all)
  9833. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9834. (defun org-table-fedit-abort ()
  9835. "Abort editing formulas, without installing the changes."
  9836. (interactive)
  9837. (org-table-remove-rectangle-highlight)
  9838. (let ((pos org-pos))
  9839. (set-window-configuration org-window-configuration)
  9840. (select-window (get-buffer-window (marker-buffer pos)))
  9841. (goto-char pos)
  9842. (move-marker pos nil)
  9843. (message "Formula editing aborted without installing changes")))
  9844. (defun org-table-fedit-lisp-indent ()
  9845. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9846. (interactive)
  9847. (let ((pos (point)) beg end ind)
  9848. (beginning-of-line 1)
  9849. (cond
  9850. ((looking-at "[ \t]")
  9851. (goto-char pos)
  9852. (call-interactively 'lisp-indent-line))
  9853. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9854. ((not (fboundp 'pp-buffer))
  9855. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9856. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9857. (goto-char (- (match-end 0) 2))
  9858. (setq beg (point))
  9859. (setq ind (make-string (current-column) ?\ ))
  9860. (condition-case nil (forward-sexp 1)
  9861. (error
  9862. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9863. (setq end (point))
  9864. (save-restriction
  9865. (narrow-to-region beg end)
  9866. (if (eq last-command this-command)
  9867. (progn
  9868. (goto-char (point-min))
  9869. (setq this-command nil)
  9870. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9871. (replace-match " ")))
  9872. (pp-buffer)
  9873. (untabify (point-min) (point-max))
  9874. (goto-char (1+ (point-min)))
  9875. (while (re-search-forward "^." nil t)
  9876. (beginning-of-line 1)
  9877. (insert ind))
  9878. (goto-char (point-max))
  9879. (backward-delete-char 1)))
  9880. (goto-char beg))
  9881. (t nil))))
  9882. (defvar org-show-positions nil)
  9883. (defun org-table-show-reference (&optional local)
  9884. "Show the location/value of the $ expression at point."
  9885. (interactive)
  9886. (org-table-remove-rectangle-highlight)
  9887. (catch 'exit
  9888. (let ((pos (if local (point) org-pos))
  9889. (face2 'highlight)
  9890. (org-inhibit-highlight-removal t)
  9891. (win (selected-window))
  9892. (org-show-positions nil)
  9893. var name e what match dest)
  9894. (if local (org-table-get-specials))
  9895. (setq what (cond
  9896. ((or (org-at-regexp-p org-table-range-regexp2)
  9897. (org-at-regexp-p org-table-translate-regexp)
  9898. (org-at-regexp-p org-table-range-regexp))
  9899. (setq match
  9900. (save-match-data
  9901. (org-table-convert-refs-to-rc (match-string 0))))
  9902. 'range)
  9903. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9904. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9905. ((not local) nil)
  9906. (t (error "No reference at point")))
  9907. match (and what (or match (match-string 0))))
  9908. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9909. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9910. 'secondary-selection))
  9911. (org-add-hook 'before-change-functions
  9912. 'org-table-remove-rectangle-highlight)
  9913. (if (eq what 'name) (setq var (substring match 1)))
  9914. (when (eq what 'range)
  9915. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9916. (setq match (org-table-formula-substitute-names match)))
  9917. (unless local
  9918. (save-excursion
  9919. (end-of-line 1)
  9920. (re-search-backward "^\\S-" nil t)
  9921. (beginning-of-line 1)
  9922. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9923. (setq dest
  9924. (save-match-data
  9925. (org-table-convert-refs-to-rc (match-string 1))))
  9926. (org-table-add-rectangle-overlay
  9927. (match-beginning 1) (match-end 1) face2))))
  9928. (if (and (markerp pos) (marker-buffer pos))
  9929. (if (get-buffer-window (marker-buffer pos))
  9930. (select-window (get-buffer-window (marker-buffer pos)))
  9931. (org-switch-to-buffer-other-window (get-buffer-window
  9932. (marker-buffer pos)))))
  9933. (goto-char pos)
  9934. (org-table-force-dataline)
  9935. (when dest
  9936. (setq name (substring dest 1))
  9937. (cond
  9938. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9939. (setq e (assoc name org-table-named-field-locations))
  9940. (goto-line (nth 1 e))
  9941. (org-table-goto-column (nth 2 e)))
  9942. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9943. (let ((l (string-to-number (match-string 1 dest)))
  9944. (c (string-to-number (match-string 2 dest))))
  9945. (goto-line (aref org-table-dlines l))
  9946. (org-table-goto-column c)))
  9947. (t (org-table-goto-column (string-to-number name))))
  9948. (move-marker pos (point))
  9949. (org-table-highlight-rectangle nil nil face2))
  9950. (cond
  9951. ((equal dest match))
  9952. ((not match))
  9953. ((eq what 'range)
  9954. (condition-case nil
  9955. (save-excursion
  9956. (org-table-get-range match nil nil 'highlight))
  9957. (error nil)))
  9958. ((setq e (assoc var org-table-named-field-locations))
  9959. (goto-line (nth 1 e))
  9960. (org-table-goto-column (nth 2 e))
  9961. (org-table-highlight-rectangle (point) (point))
  9962. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9963. ((setq e (assoc var org-table-column-names))
  9964. (org-table-goto-column (string-to-number (cdr e)))
  9965. (org-table-highlight-rectangle (point) (point))
  9966. (goto-char (org-table-begin))
  9967. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9968. (org-table-end) t)
  9969. (progn
  9970. (goto-char (match-beginning 1))
  9971. (org-table-highlight-rectangle)
  9972. (message "Named column (column %s)" (cdr e)))
  9973. (error "Column name not found")))
  9974. ((eq what 'column)
  9975. ;; column number
  9976. (org-table-goto-column (string-to-number (substring match 1)))
  9977. (org-table-highlight-rectangle (point) (point))
  9978. (message "Column %s" (substring match 1)))
  9979. ((setq e (assoc var org-table-local-parameters))
  9980. (goto-char (org-table-begin))
  9981. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9982. (progn
  9983. (goto-char (match-beginning 1))
  9984. (org-table-highlight-rectangle)
  9985. (message "Local parameter."))
  9986. (error "Parameter not found")))
  9987. (t
  9988. (cond
  9989. ((not var) (error "No reference at point"))
  9990. ((setq e (assoc var org-table-formula-constants-local))
  9991. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9992. var (cdr e)))
  9993. ((setq e (assoc var org-table-formula-constants))
  9994. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9995. var (cdr e)))
  9996. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9997. (message "Constant: $%s=%s, from `constants.el'%s."
  9998. var e (format " (%s units)" constants-unit-system)))
  9999. (t (error "Undefined name $%s" var)))))
  10000. (goto-char pos)
  10001. (when (and org-show-positions
  10002. (not (memq this-command '(org-table-fedit-scroll
  10003. org-table-fedit-scroll-down))))
  10004. (push pos org-show-positions)
  10005. (push org-table-current-begin-pos org-show-positions)
  10006. (let ((min (apply 'min org-show-positions))
  10007. (max (apply 'max org-show-positions)))
  10008. (goto-char min) (recenter 0)
  10009. (goto-char max)
  10010. (or (pos-visible-in-window-p max) (recenter -1))))
  10011. (select-window win))))
  10012. (defun org-table-force-dataline ()
  10013. "Make sure the cursor is in a dataline in a table."
  10014. (unless (save-excursion
  10015. (beginning-of-line 1)
  10016. (looking-at org-table-dataline-regexp))
  10017. (let* ((re org-table-dataline-regexp)
  10018. (p1 (save-excursion (re-search-forward re nil 'move)))
  10019. (p2 (save-excursion (re-search-backward re nil 'move))))
  10020. (cond ((and p1 p2)
  10021. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10022. p1 p2)))
  10023. ((or p1 p2) (goto-char (or p1 p2)))
  10024. (t (error "No table dataline around here"))))))
  10025. (defun org-table-fedit-line-up ()
  10026. "Move cursor one line up in the window showing the table."
  10027. (interactive)
  10028. (org-table-fedit-move 'previous-line))
  10029. (defun org-table-fedit-line-down ()
  10030. "Move cursor one line down in the window showing the table."
  10031. (interactive)
  10032. (org-table-fedit-move 'next-line))
  10033. (defun org-table-fedit-move (command)
  10034. "Move the cursor in the window shoinw the table.
  10035. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10036. (let ((org-table-allow-automatic-line-recalculation nil)
  10037. (pos org-pos) (win (selected-window)) p)
  10038. (select-window (get-buffer-window (marker-buffer org-pos)))
  10039. (setq p (point))
  10040. (call-interactively command)
  10041. (while (and (org-at-table-p)
  10042. (org-at-table-hline-p))
  10043. (call-interactively command))
  10044. (or (org-at-table-p) (goto-char p))
  10045. (move-marker pos (point))
  10046. (select-window win)))
  10047. (defun org-table-fedit-scroll (N)
  10048. (interactive "p")
  10049. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10050. (scroll-other-window N)))
  10051. (defun org-table-fedit-scroll-down (N)
  10052. (interactive "p")
  10053. (org-table-fedit-scroll (- N)))
  10054. (defvar org-table-rectangle-overlays nil)
  10055. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10056. "Add a new overlay."
  10057. (let ((ov (org-make-overlay beg end)))
  10058. (org-overlay-put ov 'face (or face 'secondary-selection))
  10059. (push ov org-table-rectangle-overlays)))
  10060. (defun org-table-highlight-rectangle (&optional beg end face)
  10061. "Highlight rectangular region in a table."
  10062. (setq beg (or beg (point)) end (or end (point)))
  10063. (let ((b (min beg end))
  10064. (e (max beg end))
  10065. l1 c1 l2 c2 tmp)
  10066. (and (boundp 'org-show-positions)
  10067. (setq org-show-positions (cons b (cons e org-show-positions))))
  10068. (goto-char (min beg end))
  10069. (setq l1 (org-current-line)
  10070. c1 (org-table-current-column))
  10071. (goto-char (max beg end))
  10072. (setq l2 (org-current-line)
  10073. c2 (org-table-current-column))
  10074. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10075. (goto-line l1)
  10076. (beginning-of-line 1)
  10077. (loop for line from l1 to l2 do
  10078. (when (looking-at org-table-dataline-regexp)
  10079. (org-table-goto-column c1)
  10080. (skip-chars-backward "^|\n") (setq beg (point))
  10081. (org-table-goto-column c2)
  10082. (skip-chars-forward "^|\n") (setq end (point))
  10083. (org-table-add-rectangle-overlay beg end face))
  10084. (beginning-of-line 2))
  10085. (goto-char b))
  10086. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10087. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10088. "Remove the rectangle overlays."
  10089. (unless org-inhibit-highlight-removal
  10090. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10091. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10092. (setq org-table-rectangle-overlays nil)))
  10093. (defvar org-table-coordinate-overlays nil
  10094. "Collects the cooordinate grid overlays, so that they can be removed.")
  10095. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10096. (defun org-table-overlay-coordinates ()
  10097. "Add overlays to the table at point, to show row/column coordinates."
  10098. (interactive)
  10099. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10100. (setq org-table-coordinate-overlays nil)
  10101. (save-excursion
  10102. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10103. (goto-char (org-table-begin))
  10104. (while (org-at-table-p)
  10105. (setq eol (point-at-eol))
  10106. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10107. (push ov org-table-coordinate-overlays)
  10108. (setq hline (looking-at org-table-hline-regexp))
  10109. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10110. (format "%4d" (setq id (1+ id)))))
  10111. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10112. (when hline
  10113. (setq ic 0)
  10114. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10115. (setq beg (1+ (match-beginning 0))
  10116. ic (1+ ic)
  10117. s1 (concat "$" (int-to-string ic))
  10118. s2 (org-number-to-letters ic)
  10119. str (if (eq org-table-use-standard-references t) s2 s1))
  10120. (setq ov (org-make-overlay beg (+ beg (length str))))
  10121. (push ov org-table-coordinate-overlays)
  10122. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10123. (beginning-of-line 2)))))
  10124. (defun org-table-toggle-coordinate-overlays ()
  10125. "Toggle the display of Row/Column numbers in tables."
  10126. (interactive)
  10127. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10128. (message "Row/Column number display turned %s"
  10129. (if org-table-overlay-coordinates "on" "off"))
  10130. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10131. (org-table-align))
  10132. (unless org-table-overlay-coordinates
  10133. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10134. (setq org-table-coordinate-overlays nil)))
  10135. (defun org-table-toggle-formula-debugger ()
  10136. "Toggle the formula debugger in tables."
  10137. (interactive)
  10138. (setq org-table-formula-debug (not org-table-formula-debug))
  10139. (message "Formula debugging has been turned %s"
  10140. (if org-table-formula-debug "on" "off")))
  10141. ;;; The orgtbl minor mode
  10142. ;; Define a minor mode which can be used in other modes in order to
  10143. ;; integrate the org-mode table editor.
  10144. ;; This is really a hack, because the org-mode table editor uses several
  10145. ;; keys which normally belong to the major mode, for example the TAB and
  10146. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10147. ;; necessary to operate the table editor, but wraps the commands into a
  10148. ;; function which tests if the cursor is currently inside a table. If that
  10149. ;; is the case, the table editor command is executed. However, when any of
  10150. ;; those keys is used outside a table, the function uses `key-binding' to
  10151. ;; look up if the key has an associated command in another currently active
  10152. ;; keymap (minor modes, major mode, global), and executes that command.
  10153. ;; There might be problems if any of the keys used by the table editor is
  10154. ;; otherwise used as a prefix key.
  10155. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10156. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10157. ;; addresses this by checking explicitly for both bindings.
  10158. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10159. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10160. ;; Some modes bind other commands to simple characters, for example
  10161. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10162. ;; active, this binding is ignored inside tables and replaced with a
  10163. ;; modified self-insert.
  10164. (defvar orgtbl-mode nil
  10165. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10166. table editor in arbitrary modes.")
  10167. (make-variable-buffer-local 'orgtbl-mode)
  10168. (defvar orgtbl-mode-map (make-keymap)
  10169. "Keymap for `orgtbl-mode'.")
  10170. ;;;###autoload
  10171. (defun turn-on-orgtbl ()
  10172. "Unconditionally turn on `orgtbl-mode'."
  10173. (orgtbl-mode 1))
  10174. (defvar org-old-auto-fill-inhibit-regexp nil
  10175. "Local variable used by `orgtbl-mode'")
  10176. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10177. "Matches a line belonging to an orgtbl.")
  10178. (defconst orgtbl-extra-font-lock-keywords
  10179. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10180. 0 (quote 'org-table) 'prepend))
  10181. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10182. ;;;###autoload
  10183. (defun orgtbl-mode (&optional arg)
  10184. "The `org-mode' table editor as a minor mode for use in other modes."
  10185. (interactive)
  10186. (if (org-mode-p)
  10187. ;; Exit without error, in case some hook functions calls this
  10188. ;; by accident in org-mode.
  10189. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10190. (setq orgtbl-mode
  10191. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10192. (if orgtbl-mode
  10193. (progn
  10194. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10195. ;; Make sure we are first in minor-mode-map-alist
  10196. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10197. (and c (setq minor-mode-map-alist
  10198. (cons c (delq c minor-mode-map-alist)))))
  10199. (org-set-local (quote org-table-may-need-update) t)
  10200. (org-add-hook 'before-change-functions 'org-before-change-function
  10201. nil 'local)
  10202. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10203. auto-fill-inhibit-regexp)
  10204. (org-set-local 'auto-fill-inhibit-regexp
  10205. (if auto-fill-inhibit-regexp
  10206. (concat orgtbl-line-start-regexp "\\|"
  10207. auto-fill-inhibit-regexp)
  10208. orgtbl-line-start-regexp))
  10209. (org-add-to-invisibility-spec '(org-cwidth))
  10210. (when (fboundp 'font-lock-add-keywords)
  10211. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10212. (org-restart-font-lock))
  10213. (easy-menu-add orgtbl-mode-menu)
  10214. (run-hooks 'orgtbl-mode-hook))
  10215. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10216. (org-cleanup-narrow-column-properties)
  10217. (org-remove-from-invisibility-spec '(org-cwidth))
  10218. (remove-hook 'before-change-functions 'org-before-change-function t)
  10219. (when (fboundp 'font-lock-remove-keywords)
  10220. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10221. (org-restart-font-lock))
  10222. (easy-menu-remove orgtbl-mode-menu)
  10223. (force-mode-line-update 'all))))
  10224. (defun org-cleanup-narrow-column-properties ()
  10225. "Remove all properties related to narrow-column invisibility."
  10226. (let ((s 1))
  10227. (while (setq s (text-property-any s (point-max)
  10228. 'display org-narrow-column-arrow))
  10229. (remove-text-properties s (1+ s) '(display t)))
  10230. (setq s 1)
  10231. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10232. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10233. (setq s 1)
  10234. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10235. (remove-text-properties s (1+ s) '(invisible t)))))
  10236. ;; Install it as a minor mode.
  10237. (put 'orgtbl-mode :included t)
  10238. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10239. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10240. (defun orgtbl-make-binding (fun n &rest keys)
  10241. "Create a function for binding in the table minor mode.
  10242. FUN is the command to call inside a table. N is used to create a unique
  10243. command name. KEYS are keys that should be checked in for a command
  10244. to execute outside of tables."
  10245. (eval
  10246. (list 'defun
  10247. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10248. '(arg)
  10249. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10250. "Outside of tables, run the binding of `"
  10251. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10252. "'.")
  10253. '(interactive "p")
  10254. (list 'if
  10255. '(org-at-table-p)
  10256. (list 'call-interactively (list 'quote fun))
  10257. (list 'let '(orgtbl-mode)
  10258. (list 'call-interactively
  10259. (append '(or)
  10260. (mapcar (lambda (k)
  10261. (list 'key-binding k))
  10262. keys)
  10263. '('orgtbl-error))))))))
  10264. (defun orgtbl-error ()
  10265. "Error when there is no default binding for a table key."
  10266. (interactive)
  10267. (error "This key has no function outside tables"))
  10268. (defun orgtbl-setup ()
  10269. "Setup orgtbl keymaps."
  10270. (let ((nfunc 0)
  10271. (bindings
  10272. (list
  10273. '([(meta shift left)] org-table-delete-column)
  10274. '([(meta left)] org-table-move-column-left)
  10275. '([(meta right)] org-table-move-column-right)
  10276. '([(meta shift right)] org-table-insert-column)
  10277. '([(meta shift up)] org-table-kill-row)
  10278. '([(meta shift down)] org-table-insert-row)
  10279. '([(meta up)] org-table-move-row-up)
  10280. '([(meta down)] org-table-move-row-down)
  10281. '("\C-c\C-w" org-table-cut-region)
  10282. '("\C-c\M-w" org-table-copy-region)
  10283. '("\C-c\C-y" org-table-paste-rectangle)
  10284. '("\C-c-" org-table-insert-hline)
  10285. '("\C-c}" org-table-toggle-coordinate-overlays)
  10286. '("\C-c{" org-table-toggle-formula-debugger)
  10287. '("\C-m" org-table-next-row)
  10288. '([(shift return)] org-table-copy-down)
  10289. '("\C-c\C-q" org-table-wrap-region)
  10290. '("\C-c?" org-table-field-info)
  10291. '("\C-c " org-table-blank-field)
  10292. '("\C-c+" org-table-sum)
  10293. '("\C-c=" org-table-eval-formula)
  10294. '("\C-c'" org-table-edit-formulas)
  10295. '("\C-c`" org-table-edit-field)
  10296. '("\C-c*" org-table-recalculate)
  10297. '("\C-c|" org-table-create-or-convert-from-region)
  10298. '("\C-c^" org-table-sort-lines)
  10299. '([(control ?#)] org-table-rotate-recalc-marks)))
  10300. elt key fun cmd)
  10301. (while (setq elt (pop bindings))
  10302. (setq nfunc (1+ nfunc))
  10303. (setq key (org-key (car elt))
  10304. fun (nth 1 elt)
  10305. cmd (orgtbl-make-binding fun nfunc key))
  10306. (org-defkey orgtbl-mode-map key cmd))
  10307. ;; Special treatment needed for TAB and RET
  10308. (org-defkey orgtbl-mode-map [(return)]
  10309. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10310. (org-defkey orgtbl-mode-map "\C-m"
  10311. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10312. (org-defkey orgtbl-mode-map [(tab)]
  10313. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10314. (org-defkey orgtbl-mode-map "\C-i"
  10315. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10316. (org-defkey orgtbl-mode-map [(shift tab)]
  10317. (orgtbl-make-binding 'org-table-previous-field 104
  10318. [(shift tab)] [(tab)] "\C-i"))
  10319. (org-defkey orgtbl-mode-map "\M-\C-m"
  10320. (orgtbl-make-binding 'org-table-wrap-region 105
  10321. "\M-\C-m" [(meta return)]))
  10322. (org-defkey orgtbl-mode-map [(meta return)]
  10323. (orgtbl-make-binding 'org-table-wrap-region 106
  10324. [(meta return)] "\M-\C-m"))
  10325. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10326. (when orgtbl-optimized
  10327. ;; If the user wants maximum table support, we need to hijack
  10328. ;; some standard editing functions
  10329. (org-remap orgtbl-mode-map
  10330. 'self-insert-command 'orgtbl-self-insert-command
  10331. 'delete-char 'org-delete-char
  10332. 'delete-backward-char 'org-delete-backward-char)
  10333. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10334. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10335. '("OrgTbl"
  10336. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10337. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10338. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10339. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10340. "--"
  10341. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10342. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10343. ["Copy Field from Above"
  10344. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10345. "--"
  10346. ("Column"
  10347. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10348. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10349. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10350. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10351. ("Row"
  10352. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10353. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10354. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10355. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10356. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10357. "--"
  10358. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10359. ("Rectangle"
  10360. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10361. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10362. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10363. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10364. "--"
  10365. ("Radio tables"
  10366. ["Insert table template" orgtbl-insert-radio-table
  10367. (assq major-mode orgtbl-radio-table-templates)]
  10368. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10369. "--"
  10370. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10371. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10372. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10373. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10374. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10375. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10376. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10377. ["Sum Column/Rectangle" org-table-sum
  10378. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10379. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10380. ["Debug Formulas"
  10381. org-table-toggle-formula-debugger :active (org-at-table-p)
  10382. :keys "C-c {"
  10383. :style toggle :selected org-table-formula-debug]
  10384. ["Show Col/Row Numbers"
  10385. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10386. :keys "C-c }"
  10387. :style toggle :selected org-table-overlay-coordinates]
  10388. ))
  10389. t))
  10390. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10391. "If the cursor is inside a table, realign the table.
  10392. It it is a table to be sent away to a receiver, do it.
  10393. With prefix arg, also recompute table."
  10394. (interactive "P")
  10395. (let ((pos (point)) action)
  10396. (save-excursion
  10397. (beginning-of-line 1)
  10398. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10399. ((looking-at "[ \t]*|") pos)
  10400. ((looking-at "#\\+TBLFM:") 'recalc))))
  10401. (cond
  10402. ((integerp action)
  10403. (goto-char action)
  10404. (org-table-maybe-eval-formula)
  10405. (if arg
  10406. (call-interactively 'org-table-recalculate)
  10407. (org-table-maybe-recalculate-line))
  10408. (call-interactively 'org-table-align)
  10409. (orgtbl-send-table 'maybe))
  10410. ((eq action 'recalc)
  10411. (save-excursion
  10412. (beginning-of-line 1)
  10413. (skip-chars-backward " \r\n\t")
  10414. (if (org-at-table-p)
  10415. (org-call-with-arg 'org-table-recalculate t))))
  10416. (t (let (orgtbl-mode)
  10417. (call-interactively (key-binding "\C-c\C-c")))))))
  10418. (defun orgtbl-tab (arg)
  10419. "Justification and field motion for `orgtbl-mode'."
  10420. (interactive "P")
  10421. (if arg (org-table-edit-field t)
  10422. (org-table-justify-field-maybe)
  10423. (org-table-next-field)))
  10424. (defun orgtbl-ret ()
  10425. "Justification and field motion for `orgtbl-mode'."
  10426. (interactive)
  10427. (org-table-justify-field-maybe)
  10428. (org-table-next-row))
  10429. (defun orgtbl-self-insert-command (N)
  10430. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10431. If the cursor is in a table looking at whitespace, the whitespace is
  10432. overwritten, and the table is not marked as requiring realignment."
  10433. (interactive "p")
  10434. (if (and (org-at-table-p)
  10435. (or
  10436. (and org-table-auto-blank-field
  10437. (member last-command
  10438. '(orgtbl-hijacker-command-100
  10439. orgtbl-hijacker-command-101
  10440. orgtbl-hijacker-command-102
  10441. orgtbl-hijacker-command-103
  10442. orgtbl-hijacker-command-104
  10443. orgtbl-hijacker-command-105))
  10444. (org-table-blank-field))
  10445. t)
  10446. (eq N 1)
  10447. (looking-at "[^|\n]* +|"))
  10448. (let (org-table-may-need-update)
  10449. (goto-char (1- (match-end 0)))
  10450. (delete-backward-char 1)
  10451. (goto-char (match-beginning 0))
  10452. (self-insert-command N))
  10453. (setq org-table-may-need-update t)
  10454. (let (orgtbl-mode)
  10455. (call-interactively (key-binding (vector last-input-event))))))
  10456. (defun org-force-self-insert (N)
  10457. "Needed to enforce self-insert under remapping."
  10458. (interactive "p")
  10459. (self-insert-command N))
  10460. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10461. "Regula expression matching exponentials as produced by calc.")
  10462. (defvar org-table-clean-did-remove-column nil)
  10463. (defun orgtbl-export (table target)
  10464. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10465. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10466. org-table-last-alignment org-table-last-column-widths
  10467. maxcol column)
  10468. (if (not (fboundp func))
  10469. (error "Cannot export orgtbl table to %s" target))
  10470. (setq lines (org-table-clean-before-export lines))
  10471. (setq table
  10472. (mapcar
  10473. (lambda (x)
  10474. (if (string-match org-table-hline-regexp x)
  10475. 'hline
  10476. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10477. lines))
  10478. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10479. table)))
  10480. (loop for i from (1- maxcol) downto 0 do
  10481. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10482. (setq column (delq nil column))
  10483. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10484. (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))
  10485. (funcall func table nil)))
  10486. (defun orgtbl-send-table (&optional maybe)
  10487. "Send a tranformed version of this table to the receiver position.
  10488. With argument MAYBE, fail quietly if no transformation is defined for
  10489. this table."
  10490. (interactive)
  10491. (catch 'exit
  10492. (unless (org-at-table-p) (error "Not at a table"))
  10493. ;; when non-interactive, we assume align has just happened.
  10494. (when (interactive-p) (org-table-align))
  10495. (save-excursion
  10496. (goto-char (org-table-begin))
  10497. (beginning-of-line 0)
  10498. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10499. (if maybe
  10500. (throw 'exit nil)
  10501. (error "Don't know how to transform this table."))))
  10502. (let* ((name (match-string 1))
  10503. beg
  10504. (transform (intern (match-string 2)))
  10505. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10506. (skip (plist-get params :skip))
  10507. (skipcols (plist-get params :skipcols))
  10508. (txt (buffer-substring-no-properties
  10509. (org-table-begin) (org-table-end)))
  10510. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10511. (lines (org-table-clean-before-export lines))
  10512. (i0 (if org-table-clean-did-remove-column 2 1))
  10513. (table (mapcar
  10514. (lambda (x)
  10515. (if (string-match org-table-hline-regexp x)
  10516. 'hline
  10517. (org-remove-by-index
  10518. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10519. skipcols i0)))
  10520. lines))
  10521. (fun (if (= i0 2) 'cdr 'identity))
  10522. (org-table-last-alignment
  10523. (org-remove-by-index (funcall fun org-table-last-alignment)
  10524. skipcols i0))
  10525. (org-table-last-column-widths
  10526. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10527. skipcols i0)))
  10528. (unless (fboundp transform)
  10529. (error "No such transformation function %s" transform))
  10530. (setq txt (funcall transform table params))
  10531. ;; Find the insertion place
  10532. (save-excursion
  10533. (goto-char (point-min))
  10534. (unless (re-search-forward
  10535. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10536. (error "Don't know where to insert translated table"))
  10537. (goto-char (match-beginning 0))
  10538. (beginning-of-line 2)
  10539. (setq beg (point))
  10540. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10541. (error "Cannot find end of insertion region"))
  10542. (beginning-of-line 1)
  10543. (delete-region beg (point))
  10544. (goto-char beg)
  10545. (insert txt "\n"))
  10546. (message "Table converted and installed at receiver location"))))
  10547. (defun org-remove-by-index (list indices &optional i0)
  10548. "Remove the elements in LIST with indices in INDICES.
  10549. First element has index 0, or I0 if given."
  10550. (if (not indices)
  10551. list
  10552. (if (integerp indices) (setq indices (list indices)))
  10553. (setq i0 (1- (or i0 0)))
  10554. (delq :rm (mapcar (lambda (x)
  10555. (setq i0 (1+ i0))
  10556. (if (memq i0 indices) :rm x))
  10557. list))))
  10558. (defun orgtbl-toggle-comment ()
  10559. "Comment or uncomment the orgtbl at point."
  10560. (interactive)
  10561. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10562. (re2 (concat "^" orgtbl-line-start-regexp))
  10563. (commented (save-excursion (beginning-of-line 1)
  10564. (cond ((looking-at re1) t)
  10565. ((looking-at re2) nil)
  10566. (t (error "Not at an org table")))))
  10567. (re (if commented re1 re2))
  10568. beg end)
  10569. (save-excursion
  10570. (beginning-of-line 1)
  10571. (while (looking-at re) (beginning-of-line 0))
  10572. (beginning-of-line 2)
  10573. (setq beg (point))
  10574. (while (looking-at re) (beginning-of-line 2))
  10575. (setq end (point)))
  10576. (comment-region beg end (if commented '(4) nil))))
  10577. (defun orgtbl-insert-radio-table ()
  10578. "Insert a radio table template appropriate for this major mode."
  10579. (interactive)
  10580. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10581. (txt (nth 1 e))
  10582. name pos)
  10583. (unless e (error "No radio table setup defined for %s" major-mode))
  10584. (setq name (read-string "Table name: "))
  10585. (while (string-match "%n" txt)
  10586. (setq txt (replace-match name t t txt)))
  10587. (or (bolp) (insert "\n"))
  10588. (setq pos (point))
  10589. (insert txt)
  10590. (goto-char pos)))
  10591. (defun org-get-param (params header i sym &optional hsym)
  10592. "Get parameter value for symbol SYM.
  10593. If this is a header line, actually get the value for the symbol with an
  10594. additional \"h\" inserted after the colon.
  10595. If the value is a protperty list, get the element for the current column.
  10596. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10597. (let ((val (plist-get params sym)))
  10598. (and hsym header (setq val (or (plist-get params hsym) val)))
  10599. (if (consp val) (plist-get val i) val)))
  10600. (defun orgtbl-to-generic (table params)
  10601. "Convert the orgtbl-mode TABLE to some other format.
  10602. This generic routine can be used for many standard cases.
  10603. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10604. separator line, or a list of fields for that line.
  10605. PARAMS is a property list of parameters that can influence the conversion.
  10606. For the generic converter, some parameters are obligatory: You need to
  10607. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10608. :splice, you must have :tstart and :tend.
  10609. Valid parameters are
  10610. :tstart String to start the table. Ignored when :splice is t.
  10611. :tend String to end the table. Ignored when :splice is t.
  10612. :splice When set to t, return only table body lines, don't wrap
  10613. them into :tstart and :tend. Default is nil.
  10614. :hline String to be inserted on horizontal separation lines.
  10615. May be nil to ignore hlines.
  10616. :lstart String to start a new table line.
  10617. :lend String to end a table line
  10618. :sep Separator between two fields
  10619. :lfmt Format for entire line, with enough %s to capture all fields.
  10620. If this is present, :lstart, :lend, and :sep are ignored.
  10621. :fmt A format to be used to wrap the field, should contain
  10622. %s for the original field value. For example, to wrap
  10623. everything in dollars, you could use :fmt \"$%s$\".
  10624. This may also be a property list with column numbers and
  10625. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10626. :hlstart :hlend :hlsep :hlfmt :hfmt
  10627. Same as above, specific for the header lines in the table.
  10628. All lines before the first hline are treated as header.
  10629. If any of these is not present, the data line value is used.
  10630. :efmt Use this format to print numbers with exponentials.
  10631. The format should have %s twice for inserting mantissa
  10632. and exponent, for example \"%s\\\\times10^{%s}\". This
  10633. may also be a property list with column numbers and
  10634. formats. :fmt will still be applied after :efmt.
  10635. In addition to this, the parameters :skip and :skipcols are always handled
  10636. directly by `orgtbl-send-table'. See manual."
  10637. (interactive)
  10638. (let* ((p params)
  10639. (splicep (plist-get p :splice))
  10640. (hline (plist-get p :hline))
  10641. rtn line i fm efm lfmt h)
  10642. ;; Do we have a header?
  10643. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10644. (setq h t))
  10645. ;; Put header
  10646. (unless splicep
  10647. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10648. ;; Now loop over all lines
  10649. (while (setq line (pop table))
  10650. (if (eq line 'hline)
  10651. ;; A horizontal separator line
  10652. (progn (if hline (push hline rtn))
  10653. (setq h nil)) ; no longer in header
  10654. ;; A normal line. Convert the fields, push line onto the result list
  10655. (setq i 0)
  10656. (setq line
  10657. (mapcar
  10658. (lambda (f)
  10659. (setq i (1+ i)
  10660. fm (org-get-param p h i :fmt :hfmt)
  10661. efm (org-get-param p h i :efmt))
  10662. (if (and efm (string-match orgtbl-exp-regexp f))
  10663. (setq f (format
  10664. efm (match-string 1 f) (match-string 2 f))))
  10665. (if fm (setq f (format fm f)))
  10666. f)
  10667. line))
  10668. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10669. (push (apply 'format lfmt line) rtn)
  10670. (push (concat
  10671. (org-get-param p h i :lstart :hlstart)
  10672. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10673. (org-get-param p h i :lend :hlend))
  10674. rtn))))
  10675. (unless splicep
  10676. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10677. (mapconcat 'identity (nreverse rtn) "\n")))
  10678. (defun orgtbl-to-latex (table params)
  10679. "Convert the orgtbl-mode TABLE to LaTeX.
  10680. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10681. separator line, or a list of fields for that line.
  10682. PARAMS is a property list of parameters that can influence the conversion.
  10683. Supports all parameters from `orgtbl-to-generic'. Most important for
  10684. LaTeX are:
  10685. :splice When set to t, return only table body lines, don't wrap
  10686. them into a tabular environment. Default is nil.
  10687. :fmt A format to be used to wrap the field, should contain %s for the
  10688. original field value. For example, to wrap everything in dollars,
  10689. use :fmt \"$%s$\". This may also be a property list with column
  10690. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10691. :efmt Format for transforming numbers with exponentials. The format
  10692. should have %s twice for inserting mantissa and exponent, for
  10693. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10694. This may also be a property list with column numbers and formats.
  10695. The general parameters :skip and :skipcols have already been applied when
  10696. this function is called."
  10697. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10698. org-table-last-alignment ""))
  10699. (params2
  10700. (list
  10701. :tstart (concat "\\begin{tabular}{" alignment "}")
  10702. :tend "\\end{tabular}"
  10703. :lstart "" :lend " \\\\" :sep " & "
  10704. :efmt "%s\\,(%s)" :hline "\\hline")))
  10705. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10706. (defun orgtbl-to-html (table params)
  10707. "Convert the orgtbl-mode TABLE to LaTeX.
  10708. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10709. separator line, or a list of fields for that line.
  10710. PARAMS is a property list of parameters that can influence the conversion.
  10711. Currently this function recognizes the following parameters:
  10712. :splice When set to t, return only table body lines, don't wrap
  10713. them into a <table> environment. Default is nil.
  10714. The general parameters :skip and :skipcols have already been applied when
  10715. this function is called. The function does *not* use `orgtbl-to-generic',
  10716. so you cannot specify parameters for it."
  10717. (let* ((splicep (plist-get params :splice))
  10718. html)
  10719. ;; Just call the formatter we already have
  10720. ;; We need to make text lines for it, so put the fields back together.
  10721. (setq html (org-format-org-table-html
  10722. (mapcar
  10723. (lambda (x)
  10724. (if (eq x 'hline)
  10725. "|----+----|"
  10726. (concat "| " (mapconcat 'identity x " | ") " |")))
  10727. table)
  10728. splicep))
  10729. (if (string-match "\n+\\'" html)
  10730. (setq html (replace-match "" t t html)))
  10731. html))
  10732. (defun orgtbl-to-texinfo (table params)
  10733. "Convert the orgtbl-mode TABLE to TeXInfo.
  10734. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10735. separator line, or a list of fields for that line.
  10736. PARAMS is a property list of parameters that can influence the conversion.
  10737. Supports all parameters from `orgtbl-to-generic'. Most important for
  10738. TeXInfo are:
  10739. :splice nil/t When set to t, return only table body lines, don't wrap
  10740. them into a multitable environment. Default is nil.
  10741. :fmt fmt A format to be used to wrap the field, should contain
  10742. %s for the original field value. For example, to wrap
  10743. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10744. This may also be a property list with column numbers and
  10745. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10746. :cf \"f1 f2..\" The column fractions for the table. By default these
  10747. are computed automatically from the width of the columns
  10748. under org-mode.
  10749. The general parameters :skip and :skipcols have already been applied when
  10750. this function is called."
  10751. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10752. (colfrac (or (plist-get params :cf)
  10753. (mapconcat
  10754. (lambda (x) (format "%.3f" (/ (float x) total)))
  10755. org-table-last-column-widths " ")))
  10756. (params2
  10757. (list
  10758. :tstart (concat "@multitable @columnfractions " colfrac)
  10759. :tend "@end multitable"
  10760. :lstart "@item " :lend "" :sep " @tab "
  10761. :hlstart "@headitem ")))
  10762. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10763. ;;;; Link Stuff
  10764. ;;; Link abbreviations
  10765. (defun org-link-expand-abbrev (link)
  10766. "Apply replacements as defined in `org-link-abbrev-alist."
  10767. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10768. (let* ((key (match-string 1 link))
  10769. (as (or (assoc key org-link-abbrev-alist-local)
  10770. (assoc key org-link-abbrev-alist)))
  10771. (tag (and (match-end 2) (match-string 3 link)))
  10772. rpl)
  10773. (if (not as)
  10774. link
  10775. (setq rpl (cdr as))
  10776. (cond
  10777. ((symbolp rpl) (funcall rpl tag))
  10778. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10779. (t (concat rpl tag)))))
  10780. link))
  10781. ;;; Storing and inserting links
  10782. (defvar org-insert-link-history nil
  10783. "Minibuffer history for links inserted with `org-insert-link'.")
  10784. (defvar org-stored-links nil
  10785. "Contains the links stored with `org-store-link'.")
  10786. (defvar org-store-link-plist nil
  10787. "Plist with info about the most recently link created with `org-store-link'.")
  10788. (defvar org-link-protocols nil
  10789. "Link protocols added to Org-mode using `org-add-link-type'.")
  10790. (defvar org-store-link-functions nil
  10791. "List of functions that are called to create and store a link.
  10792. Each function will be called in turn until one returns a non-nil
  10793. value. Each function should check if it is responsible for creating
  10794. this link (for example by looking at the major mode).
  10795. If not, it must exit and return nil.
  10796. If yes, it should return a non-nil value after a calling
  10797. `org-store-link-props' with a list of properties and values.
  10798. Special properties are:
  10799. :type The link prefix. like \"http\". This must be given.
  10800. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10801. This is obligatory as well.
  10802. :description Optional default description for the second pair
  10803. of brackets in an Org-mode link. The user can still change
  10804. this when inserting this link into an Org-mode buffer.
  10805. In addition to these, any additional properties can be specified
  10806. and then used in remember templates.")
  10807. (defun org-add-link-type (type &optional follow publish)
  10808. "Add TYPE to the list of `org-link-types'.
  10809. Re-compute all regular expressions depending on `org-link-types'
  10810. FOLLOW and PUBLISH are two functions. Both take the link path as
  10811. an argument.
  10812. FOLLOW should do whatever is necessary to follow the link, for example
  10813. to find a file or display a mail message.
  10814. PUBLISH takes the path and retuns the string that should be used when
  10815. this document is published. FIMXE: This is actually not yet implemented."
  10816. (add-to-list 'org-link-types type t)
  10817. (org-make-link-regexps)
  10818. (add-to-list 'org-link-protocols
  10819. (list type follow publish)))
  10820. (defun org-add-agenda-custom-command (entry)
  10821. "Replace or add a command in `org-agenda-custom-commands'.
  10822. This is mostly for hacking and trying a new command - once the command
  10823. works you probably want to add it to `org-agenda-custom-commands' for good."
  10824. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10825. (if ass
  10826. (setcdr ass (cdr entry))
  10827. (push entry org-agenda-custom-commands))))
  10828. ;;;###autoload
  10829. (defun org-store-link (arg)
  10830. "\\<org-mode-map>Store an org-link to the current location.
  10831. This link is added to `org-stored-links' and can later be inserted
  10832. into an org-buffer with \\[org-insert-link].
  10833. For some link types, a prefix arg is interpreted:
  10834. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10835. For file links, arg negates `org-context-in-file-links'."
  10836. (interactive "P")
  10837. (setq org-store-link-plist nil) ; reset
  10838. (let (link cpltxt desc description search txt)
  10839. (cond
  10840. ((run-hook-with-args-until-success 'org-store-link-functions)
  10841. (setq link (plist-get org-store-link-plist :link)
  10842. desc (or (plist-get org-store-link-plist :description) link)))
  10843. ((eq major-mode 'bbdb-mode)
  10844. (let ((name (bbdb-record-name (bbdb-current-record)))
  10845. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10846. (setq cpltxt (concat "bbdb:" (or name company))
  10847. link (org-make-link cpltxt))
  10848. (org-store-link-props :type "bbdb" :name name :company company)))
  10849. ((eq major-mode 'Info-mode)
  10850. (setq link (org-make-link "info:"
  10851. (file-name-nondirectory Info-current-file)
  10852. ":" Info-current-node))
  10853. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10854. ":" Info-current-node))
  10855. (org-store-link-props :type "info" :file Info-current-file
  10856. :node Info-current-node))
  10857. ((eq major-mode 'calendar-mode)
  10858. (let ((cd (calendar-cursor-to-date)))
  10859. (setq link
  10860. (format-time-string
  10861. (car org-time-stamp-formats)
  10862. (apply 'encode-time
  10863. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10864. nil nil nil))))
  10865. (org-store-link-props :type "calendar" :date cd)))
  10866. ((or (eq major-mode 'vm-summary-mode)
  10867. (eq major-mode 'vm-presentation-mode))
  10868. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10869. (vm-follow-summary-cursor)
  10870. (save-excursion
  10871. (vm-select-folder-buffer)
  10872. (let* ((message (car vm-message-pointer))
  10873. (folder buffer-file-name)
  10874. (subject (vm-su-subject message))
  10875. (to (vm-get-header-contents message "To"))
  10876. (from (vm-get-header-contents message "From"))
  10877. (message-id (vm-su-message-id message)))
  10878. (org-store-link-props :type "vm" :from from :to to :subject subject
  10879. :message-id message-id)
  10880. (setq message-id (org-remove-angle-brackets message-id))
  10881. (setq folder (abbreviate-file-name folder))
  10882. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10883. folder)
  10884. (setq folder (replace-match "" t t folder)))
  10885. (setq cpltxt (org-email-link-description))
  10886. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10887. ((eq major-mode 'wl-summary-mode)
  10888. (let* ((msgnum (wl-summary-message-number))
  10889. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10890. msgnum 'message-id))
  10891. (wl-message-entity
  10892. (if (fboundp 'elmo-message-entity)
  10893. (elmo-message-entity
  10894. wl-summary-buffer-elmo-folder msgnum)
  10895. (elmo-msgdb-overview-get-entity
  10896. msgnum (wl-summary-buffer-msgdb))))
  10897. (from (wl-summary-line-from))
  10898. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  10899. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10900. (wl-summary-line-subject))))
  10901. (org-store-link-props :type "wl" :from from :to to
  10902. :subject subject :message-id message-id)
  10903. (setq message-id (org-remove-angle-brackets message-id))
  10904. (setq cpltxt (org-email-link-description))
  10905. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10906. "#" message-id))))
  10907. ((or (equal major-mode 'mh-folder-mode)
  10908. (equal major-mode 'mh-show-mode))
  10909. (let ((from (org-mhe-get-header "From:"))
  10910. (to (org-mhe-get-header "To:"))
  10911. (message-id (org-mhe-get-header "Message-Id:"))
  10912. (subject (org-mhe-get-header "Subject:")))
  10913. (org-store-link-props :type "mh" :from from :to to
  10914. :subject subject :message-id message-id)
  10915. (setq cpltxt (org-email-link-description))
  10916. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10917. (org-remove-angle-brackets message-id)))))
  10918. ((or (eq major-mode 'rmail-mode)
  10919. (eq major-mode 'rmail-summary-mode))
  10920. (save-window-excursion
  10921. (save-restriction
  10922. (when (eq major-mode 'rmail-summary-mode)
  10923. (rmail-show-message rmail-current-message))
  10924. (rmail-narrow-to-non-pruned-header)
  10925. (let ((folder buffer-file-name)
  10926. (message-id (mail-fetch-field "message-id"))
  10927. (from (mail-fetch-field "from"))
  10928. (to (mail-fetch-field "to"))
  10929. (subject (mail-fetch-field "subject")))
  10930. (org-store-link-props
  10931. :type "rmail" :from from :to to
  10932. :subject subject :message-id message-id)
  10933. (setq message-id (org-remove-angle-brackets message-id))
  10934. (setq cpltxt (org-email-link-description))
  10935. (setq link (org-make-link "rmail:" folder "#" message-id)))
  10936. (rmail-show-message rmail-current-message))))
  10937. ((eq major-mode 'gnus-group-mode)
  10938. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10939. (gnus-group-group-name)) ; version
  10940. ((fboundp 'gnus-group-name)
  10941. (gnus-group-name))
  10942. (t "???"))))
  10943. (unless group (error "Not on a group"))
  10944. (org-store-link-props :type "gnus" :group group)
  10945. (setq cpltxt (concat
  10946. (if (org-xor arg org-usenet-links-prefer-google)
  10947. "http://groups.google.com/groups?group="
  10948. "gnus:")
  10949. group)
  10950. link (org-make-link cpltxt))))
  10951. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10952. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10953. (let* ((group gnus-newsgroup-name)
  10954. (article (gnus-summary-article-number))
  10955. (header (gnus-summary-article-header article))
  10956. (from (mail-header-from header))
  10957. (message-id (mail-header-id header))
  10958. (date (mail-header-date header))
  10959. (subject (gnus-summary-subject-string)))
  10960. (org-store-link-props :type "gnus" :from from :subject subject
  10961. :message-id message-id :group group)
  10962. (setq cpltxt (org-email-link-description))
  10963. (if (org-xor arg org-usenet-links-prefer-google)
  10964. (setq link
  10965. (concat
  10966. cpltxt "\n "
  10967. (format "http://groups.google.com/groups?as_umsgid=%s"
  10968. (org-fixup-message-id-for-http message-id))))
  10969. (setq link (org-make-link "gnus:" group
  10970. "#" (number-to-string article))))))
  10971. ((eq major-mode 'w3-mode)
  10972. (setq cpltxt (url-view-url t)
  10973. link (org-make-link cpltxt))
  10974. (org-store-link-props :type "w3" :url (url-view-url t)))
  10975. ((eq major-mode 'w3m-mode)
  10976. (setq cpltxt (or w3m-current-title w3m-current-url)
  10977. link (org-make-link w3m-current-url))
  10978. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10979. ((setq search (run-hook-with-args-until-success
  10980. 'org-create-file-search-functions))
  10981. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10982. "::" search))
  10983. (setq cpltxt (or description link)))
  10984. ((eq major-mode 'image-mode)
  10985. (setq cpltxt (concat "file:"
  10986. (abbreviate-file-name buffer-file-name))
  10987. link (org-make-link cpltxt))
  10988. (org-store-link-props :type "image" :file buffer-file-name))
  10989. ((eq major-mode 'dired-mode)
  10990. ;; link to the file in the current line
  10991. (setq cpltxt (concat "file:"
  10992. (abbreviate-file-name
  10993. (expand-file-name
  10994. (dired-get-filename nil t))))
  10995. link (org-make-link cpltxt)))
  10996. ((and buffer-file-name (org-mode-p))
  10997. ;; Just link to current headline
  10998. (setq cpltxt (concat "file:"
  10999. (abbreviate-file-name buffer-file-name)))
  11000. ;; Add a context search string
  11001. (when (org-xor org-context-in-file-links arg)
  11002. ;; Check if we are on a target
  11003. (if (org-in-regexp "<<\\(.*?\\)>>")
  11004. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11005. (setq txt (cond
  11006. ((org-on-heading-p) nil)
  11007. ((org-region-active-p)
  11008. (buffer-substring (region-beginning) (region-end)))
  11009. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11010. (when (or (null txt) (string-match "\\S-" txt))
  11011. (setq cpltxt
  11012. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11013. desc "NONE"))))
  11014. (if (string-match "::\\'" cpltxt)
  11015. (setq cpltxt (substring cpltxt 0 -2)))
  11016. (setq link (org-make-link cpltxt)))
  11017. ((buffer-file-name (buffer-base-buffer))
  11018. ;; Just link to this file here.
  11019. (setq cpltxt (concat "file:"
  11020. (abbreviate-file-name
  11021. (buffer-file-name (buffer-base-buffer)))))
  11022. ;; Add a context string
  11023. (when (org-xor org-context-in-file-links arg)
  11024. (setq txt (if (org-region-active-p)
  11025. (buffer-substring (region-beginning) (region-end))
  11026. (buffer-substring (point-at-bol) (point-at-eol))))
  11027. ;; Only use search option if there is some text.
  11028. (when (string-match "\\S-" txt)
  11029. (setq cpltxt
  11030. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11031. desc "NONE")))
  11032. (setq link (org-make-link cpltxt)))
  11033. ((interactive-p)
  11034. (error "Cannot link to a buffer which is not visiting a file"))
  11035. (t (setq link nil)))
  11036. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11037. (setq link (or link cpltxt)
  11038. desc (or desc cpltxt))
  11039. (if (equal desc "NONE") (setq desc nil))
  11040. (if (and (interactive-p) link)
  11041. (progn
  11042. (setq org-stored-links
  11043. (cons (list link desc) org-stored-links))
  11044. (message "Stored: %s" (or desc link)))
  11045. (and link (org-make-link-string link desc)))))
  11046. (defun org-store-link-props (&rest plist)
  11047. "Store link properties, extract names and addresses."
  11048. (let (x adr)
  11049. (when (setq x (plist-get plist :from))
  11050. (setq adr (mail-extract-address-components x))
  11051. (plist-put plist :fromname (car adr))
  11052. (plist-put plist :fromaddress (nth 1 adr)))
  11053. (when (setq x (plist-get plist :to))
  11054. (setq adr (mail-extract-address-components x))
  11055. (plist-put plist :toname (car adr))
  11056. (plist-put plist :toaddress (nth 1 adr))))
  11057. (let ((from (plist-get plist :from))
  11058. (to (plist-get plist :to)))
  11059. (when (and from to org-from-is-user-regexp)
  11060. (plist-put plist :fromto
  11061. (if (string-match org-from-is-user-regexp from)
  11062. (concat "to %t")
  11063. (concat "from %f")))))
  11064. (setq org-store-link-plist plist))
  11065. (defun org-email-link-description (&optional fmt)
  11066. "Return the description part of an email link.
  11067. This takes information from `org-store-link-plist' and formats it
  11068. according to FMT (default from `org-email-link-description-format')."
  11069. (setq fmt (or fmt org-email-link-description-format))
  11070. (let* ((p org-store-link-plist)
  11071. (to (plist-get p :toaddress))
  11072. (from (plist-get p :fromaddress))
  11073. (table
  11074. (list
  11075. (cons "%c" (plist-get p :fromto))
  11076. (cons "%F" (plist-get p :from))
  11077. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11078. (cons "%T" (plist-get p :to))
  11079. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11080. (cons "%s" (plist-get p :subject))
  11081. (cons "%m" (plist-get p :message-id)))))
  11082. (when (string-match "%c" fmt)
  11083. ;; Check if the user wrote this message
  11084. (if (and org-from-is-user-regexp from to
  11085. (save-match-data (string-match org-from-is-user-regexp from)))
  11086. (setq fmt (replace-match "to %t" t t fmt))
  11087. (setq fmt (replace-match "from %f" t t fmt))))
  11088. (org-replace-escapes fmt table)))
  11089. (defun org-make-org-heading-search-string (&optional string heading)
  11090. "Make search string for STRING or current headline."
  11091. (interactive)
  11092. (let ((s (or string (org-get-heading))))
  11093. (unless (and string (not heading))
  11094. ;; We are using a headline, clean up garbage in there.
  11095. (if (string-match org-todo-regexp s)
  11096. (setq s (replace-match "" t t s)))
  11097. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11098. (setq s (replace-match "" t t s)))
  11099. (setq s (org-trim s))
  11100. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11101. org-comment-string "\\)") s)
  11102. (setq s (replace-match "" t t s)))
  11103. (while (string-match org-ts-regexp s)
  11104. (setq s (replace-match "" t t s))))
  11105. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11106. (setq s (replace-match " " t t s)))
  11107. (or string (setq s (concat "*" s))) ; Add * for headlines
  11108. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11109. (defun org-make-link (&rest strings)
  11110. "Concatenate STRINGS."
  11111. (apply 'concat strings))
  11112. (defun org-make-link-string (link &optional description)
  11113. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11114. (unless (string-match "\\S-" link)
  11115. (error "Empty link"))
  11116. (when (stringp description)
  11117. ;; Remove brackets from the description, they are fatal.
  11118. (while (string-match "\\[" description)
  11119. (setq description (replace-match "{" t t description)))
  11120. (while (string-match "\\]" description)
  11121. (setq description (replace-match "}" t t description))))
  11122. (when (equal (org-link-escape link) description)
  11123. ;; No description needed, it is identical
  11124. (setq description nil))
  11125. (when (and (not description)
  11126. (not (equal link (org-link-escape link))))
  11127. (setq description link))
  11128. (concat "[[" (org-link-escape link) "]"
  11129. (if description (concat "[" description "]") "")
  11130. "]"))
  11131. (defconst org-link-escape-chars
  11132. '((?\ . "%20")
  11133. (?\[ . "%5B")
  11134. (?\] . "%5D")
  11135. (?\340 . "%E0") ; `a
  11136. (?\342 . "%E2") ; ^a
  11137. (?\347 . "%E7") ; ,c
  11138. (?\350 . "%E8") ; `e
  11139. (?\351 . "%E9") ; 'e
  11140. (?\352 . "%EA") ; ^e
  11141. (?\356 . "%EE") ; ^i
  11142. (?\364 . "%F4") ; ^o
  11143. (?\371 . "%F9") ; `u
  11144. (?\373 . "%FB") ; ^u
  11145. (?\; . "%3B")
  11146. (?? . "%3F")
  11147. (?= . "%3D")
  11148. (?+ . "%2B")
  11149. )
  11150. "Association list of escapes for some characters problematic in links.
  11151. This is the list that is used for internal purposes.")
  11152. (defconst org-link-escape-chars-browser
  11153. '((?\ . "%20")) ; 32 for the SPC char
  11154. "Association list of escapes for some characters problematic in links.
  11155. This is the list that is used before handing over to the browser.")
  11156. (defun org-link-escape (text &optional table)
  11157. "Escape charaters in TEXT that are problematic for links."
  11158. (setq table (or table org-link-escape-chars))
  11159. (when text
  11160. (let ((re (mapconcat (lambda (x) (regexp-quote
  11161. (char-to-string (car x))))
  11162. table "\\|")))
  11163. (while (string-match re text)
  11164. (setq text
  11165. (replace-match
  11166. (cdr (assoc (string-to-char (match-string 0 text))
  11167. table))
  11168. t t text)))
  11169. text)))
  11170. (defun org-link-unescape (text &optional table)
  11171. "Reverse the action of `org-link-escape'."
  11172. (setq table (or table org-link-escape-chars))
  11173. (when text
  11174. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11175. table "\\|")))
  11176. (while (string-match re text)
  11177. (setq text
  11178. (replace-match
  11179. (char-to-string (car (rassoc (match-string 0 text) table)))
  11180. t t text)))
  11181. text)))
  11182. (defun org-xor (a b)
  11183. "Exclusive or."
  11184. (if a (not b) b))
  11185. (defun org-get-header (header)
  11186. "Find a header field in the current buffer."
  11187. (save-excursion
  11188. (goto-char (point-min))
  11189. (let ((case-fold-search t) s)
  11190. (cond
  11191. ((eq header 'from)
  11192. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11193. (setq s (match-string 1)))
  11194. (while (string-match "\"" s)
  11195. (setq s (replace-match "" t t s)))
  11196. (if (string-match "[<(].*" s)
  11197. (setq s (replace-match "" t t s))))
  11198. ((eq header 'message-id)
  11199. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11200. (setq s (match-string 1))))
  11201. ((eq header 'subject)
  11202. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11203. (setq s (match-string 1)))))
  11204. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11205. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11206. s)))
  11207. (defun org-fixup-message-id-for-http (s)
  11208. "Replace special characters in a message id, so it can be used in an http query."
  11209. (while (string-match "<" s)
  11210. (setq s (replace-match "%3C" t t s)))
  11211. (while (string-match ">" s)
  11212. (setq s (replace-match "%3E" t t s)))
  11213. (while (string-match "@" s)
  11214. (setq s (replace-match "%40" t t s)))
  11215. s)
  11216. ;;;###autoload
  11217. (defun org-insert-link-global ()
  11218. "Insert a link like Org-mode does.
  11219. This command can be called in any mode to insert a link in Org-mode syntax."
  11220. (interactive)
  11221. (org-run-like-in-org-mode 'org-insert-link))
  11222. (defun org-insert-link (&optional complete-file)
  11223. "Insert a link. At the prompt, enter the link.
  11224. Completion can be used to select a link previously stored with
  11225. `org-store-link'. When the empty string is entered (i.e. if you just
  11226. press RET at the prompt), the link defaults to the most recently
  11227. stored link. As SPC triggers completion in the minibuffer, you need to
  11228. use M-SPC or C-q SPC to force the insertion of a space character.
  11229. You will also be prompted for a description, and if one is given, it will
  11230. be displayed in the buffer instead of the link.
  11231. If there is already a link at point, this command will allow you to edit link
  11232. and description parts.
  11233. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11234. selected using completion. The path to the file will be relative to
  11235. the current directory if the file is in the current directory or a
  11236. subdirectory. Otherwise, the link will be the absolute path as
  11237. completed in the minibuffer (i.e. normally ~/path/to/file).
  11238. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11239. is in the current directory or below.
  11240. With three \\[universal-argument] prefixes, negate the meaning of
  11241. `org-keep-stored-link-after-insertion'."
  11242. (interactive "P")
  11243. (let* ((wcf (current-window-configuration))
  11244. (region (if (org-region-active-p)
  11245. (buffer-substring (region-beginning) (region-end))))
  11246. (remove (and region (list (region-beginning) (region-end))))
  11247. (desc region)
  11248. tmphist ; byte-compile incorrectly complains about this
  11249. link entry file)
  11250. (cond
  11251. ((org-in-regexp org-bracket-link-regexp 1)
  11252. ;; We do have a link at point, and we are going to edit it.
  11253. (setq remove (list (match-beginning 0) (match-end 0)))
  11254. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11255. (setq link (read-string "Link: "
  11256. (org-link-unescape
  11257. (org-match-string-no-properties 1)))))
  11258. ((or (org-in-regexp org-angle-link-re)
  11259. (org-in-regexp org-plain-link-re))
  11260. ;; Convert to bracket link
  11261. (setq remove (list (match-beginning 0) (match-end 0))
  11262. link (read-string "Link: "
  11263. (org-remove-angle-brackets (match-string 0)))))
  11264. ((equal complete-file '(4))
  11265. ;; Completing read for file names.
  11266. (setq file (read-file-name "File: "))
  11267. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11268. (pwd1 (file-name-as-directory (abbreviate-file-name
  11269. (expand-file-name ".")))))
  11270. (cond
  11271. ((equal complete-file '(16))
  11272. (setq link (org-make-link
  11273. "file:"
  11274. (abbreviate-file-name (expand-file-name file)))))
  11275. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11276. (setq link (org-make-link "file:" (match-string 1 file))))
  11277. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11278. (expand-file-name file))
  11279. (setq link (org-make-link
  11280. "file:" (match-string 1 (expand-file-name file)))))
  11281. (t (setq link (org-make-link "file:" file))))))
  11282. (t
  11283. ;; Read link, with completion for stored links.
  11284. (with-output-to-temp-buffer "*Org Links*"
  11285. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11286. (when org-stored-links
  11287. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11288. (princ (mapconcat
  11289. (lambda (x)
  11290. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11291. (reverse org-stored-links) "\n"))))
  11292. (let ((cw (selected-window)))
  11293. (select-window (get-buffer-window "*Org Links*"))
  11294. (shrink-window-if-larger-than-buffer)
  11295. (setq truncate-lines t)
  11296. (select-window cw))
  11297. ;; Fake a link history, containing the stored links.
  11298. (setq tmphist (append (mapcar 'car org-stored-links)
  11299. org-insert-link-history))
  11300. (unwind-protect
  11301. (setq link (org-completing-read
  11302. "Link: "
  11303. (append
  11304. (mapcar (lambda (x) (list (concat (car x) ":")))
  11305. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11306. (mapcar (lambda (x) (list (concat x ":")))
  11307. org-link-types))
  11308. nil nil nil
  11309. 'tmphist
  11310. (or (car (car org-stored-links)))))
  11311. (set-window-configuration wcf)
  11312. (kill-buffer "*Org Links*"))
  11313. (setq entry (assoc link org-stored-links))
  11314. (or entry (push link org-insert-link-history))
  11315. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11316. (not org-keep-stored-link-after-insertion))
  11317. (setq org-stored-links (delq (assoc link org-stored-links)
  11318. org-stored-links)))
  11319. (setq desc (or desc (nth 1 entry)))))
  11320. (if (string-match org-plain-link-re link)
  11321. ;; URL-like link, normalize the use of angular brackets.
  11322. (setq link (org-make-link (org-remove-angle-brackets link))))
  11323. ;; Check if we are linking to the current file with a search option
  11324. ;; If yes, simplify the link by using only the search option.
  11325. (when (and buffer-file-name
  11326. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11327. (let* ((path (match-string 1 link))
  11328. (case-fold-search nil)
  11329. (search (match-string 2 link)))
  11330. (save-match-data
  11331. (if (equal (file-truename buffer-file-name) (file-truename path))
  11332. ;; We are linking to this same file, with a search option
  11333. (setq link search)))))
  11334. ;; Check if we can/should use a relative path. If yes, simplify the link
  11335. (when (string-match "\\<file:\\(.*\\)" link)
  11336. (let* ((path (match-string 1 link))
  11337. (origpath path)
  11338. (desc-is-link (equal link desc))
  11339. (case-fold-search nil))
  11340. (cond
  11341. ((eq org-link-file-path-type 'absolute)
  11342. (setq path (abbreviate-file-name (expand-file-name path))))
  11343. ((eq org-link-file-path-type 'noabbrev)
  11344. (setq path (expand-file-name path)))
  11345. ((eq org-link-file-path-type 'relative)
  11346. (setq path (file-relative-name path)))
  11347. (t
  11348. (save-match-data
  11349. (if (string-match (concat "^" (regexp-quote
  11350. (file-name-as-directory
  11351. (expand-file-name "."))))
  11352. (expand-file-name path))
  11353. ;; We are linking a file with relative path name.
  11354. (setq path (substring (expand-file-name path)
  11355. (match-end 0)))))))
  11356. (setq link (concat "file:" path))
  11357. (if (equal desc origpath)
  11358. (setq desc path))))
  11359. (setq desc (read-string "Description: " desc))
  11360. (unless (string-match "\\S-" desc) (setq desc nil))
  11361. (if remove (apply 'delete-region remove))
  11362. (insert (org-make-link-string link desc))))
  11363. (defun org-completing-read (&rest args)
  11364. (let ((minibuffer-local-completion-map
  11365. (copy-keymap minibuffer-local-completion-map)))
  11366. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11367. (apply 'completing-read args)))
  11368. ;;; Opening/following a link
  11369. (defvar org-link-search-failed nil)
  11370. (defun org-next-link ()
  11371. "Move forward to the next link.
  11372. If the link is in hidden text, expose it."
  11373. (interactive)
  11374. (when (and org-link-search-failed (eq this-command last-command))
  11375. (goto-char (point-min))
  11376. (message "Link search wrapped back to beginning of buffer"))
  11377. (setq org-link-search-failed nil)
  11378. (let* ((pos (point))
  11379. (ct (org-context))
  11380. (a (assoc :link ct)))
  11381. (if a (goto-char (nth 2 a)))
  11382. (if (re-search-forward org-any-link-re nil t)
  11383. (progn
  11384. (goto-char (match-beginning 0))
  11385. (if (org-invisible-p) (org-show-context)))
  11386. (goto-char pos)
  11387. (setq org-link-search-failed t)
  11388. (error "No further link found"))))
  11389. (defun org-previous-link ()
  11390. "Move backward to the previous link.
  11391. If the link is in hidden text, expose it."
  11392. (interactive)
  11393. (when (and org-link-search-failed (eq this-command last-command))
  11394. (goto-char (point-max))
  11395. (message "Link search wrapped back to end of buffer"))
  11396. (setq org-link-search-failed nil)
  11397. (let* ((pos (point))
  11398. (ct (org-context))
  11399. (a (assoc :link ct)))
  11400. (if a (goto-char (nth 1 a)))
  11401. (if (re-search-backward org-any-link-re nil t)
  11402. (progn
  11403. (goto-char (match-beginning 0))
  11404. (if (org-invisible-p) (org-show-context)))
  11405. (goto-char pos)
  11406. (setq org-link-search-failed t)
  11407. (error "No further link found"))))
  11408. (defun org-find-file-at-mouse (ev)
  11409. "Open file link or URL at mouse."
  11410. (interactive "e")
  11411. (mouse-set-point ev)
  11412. (org-open-at-point 'in-emacs))
  11413. (defun org-open-at-mouse (ev)
  11414. "Open file link or URL at mouse."
  11415. (interactive "e")
  11416. (mouse-set-point ev)
  11417. (org-open-at-point))
  11418. (defvar org-window-config-before-follow-link nil
  11419. "The window configuration before following a link.
  11420. This is saved in case the need arises to restore it.")
  11421. (defvar org-open-link-marker (make-marker)
  11422. "Marker pointing to the location where `org-open-at-point; was called.")
  11423. ;;;###autoload
  11424. (defun org-open-at-point-global ()
  11425. "Follow a link like Org-mode does.
  11426. This command can be called in any mode to follow a link that has
  11427. Org-mode syntax."
  11428. (interactive)
  11429. (org-run-like-in-org-mode 'org-open-at-point))
  11430. (defun org-open-at-point (&optional in-emacs)
  11431. "Open link at or after point.
  11432. If there is no link at point, this function will search forward up to
  11433. the end of the current subtree.
  11434. Normally, files will be opened by an appropriate application. If the
  11435. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11436. (interactive "P")
  11437. (catch 'abort
  11438. (move-marker org-open-link-marker (point))
  11439. (setq org-window-config-before-follow-link (current-window-configuration))
  11440. (org-remove-occur-highlights nil nil t)
  11441. (if (org-at-timestamp-p t)
  11442. (org-follow-timestamp-link)
  11443. (let (type path link line search (pos (point)))
  11444. (catch 'match
  11445. (save-excursion
  11446. (skip-chars-forward "^]\n\r")
  11447. (when (org-in-regexp org-bracket-link-regexp)
  11448. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11449. (while (string-match " *\n *" link)
  11450. (setq link (replace-match " " t t link)))
  11451. (setq link (org-link-expand-abbrev link))
  11452. (if (string-match org-link-re-with-space2 link)
  11453. (setq type (match-string 1 link) path (match-string 2 link))
  11454. (setq type "thisfile" path link))
  11455. (throw 'match t)))
  11456. (when (get-text-property (point) 'org-linked-text)
  11457. (setq type "thisfile"
  11458. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11459. (1+ (point)) (point))
  11460. path (buffer-substring
  11461. (previous-single-property-change pos 'org-linked-text)
  11462. (next-single-property-change pos 'org-linked-text)))
  11463. (throw 'match t))
  11464. (save-excursion
  11465. (when (or (org-in-regexp org-angle-link-re)
  11466. (org-in-regexp org-plain-link-re))
  11467. (setq type (match-string 1) path (match-string 2))
  11468. (throw 'match t)))
  11469. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11470. (setq type "tree-match"
  11471. path (match-string 1))
  11472. (throw 'match t))
  11473. (save-excursion
  11474. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11475. (setq type "tags"
  11476. path (match-string 1))
  11477. (while (string-match ":" path)
  11478. (setq path (replace-match "+" t t path)))
  11479. (throw 'match t))))
  11480. (unless path
  11481. (error "No link found"))
  11482. ;; Remove any trailing spaces in path
  11483. (if (string-match " +\\'" path)
  11484. (setq path (replace-match "" t t path)))
  11485. (cond
  11486. ((assoc type org-link-protocols)
  11487. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11488. ((equal type "mailto")
  11489. (let ((cmd (car org-link-mailto-program))
  11490. (args (cdr org-link-mailto-program)) args1
  11491. (address path) (subject "") a)
  11492. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11493. (setq address (match-string 1 path)
  11494. subject (org-link-escape (match-string 2 path))))
  11495. (while args
  11496. (cond
  11497. ((not (stringp (car args))) (push (pop args) args1))
  11498. (t (setq a (pop args))
  11499. (if (string-match "%a" a)
  11500. (setq a (replace-match address t t a)))
  11501. (if (string-match "%s" a)
  11502. (setq a (replace-match subject t t a)))
  11503. (push a args1))))
  11504. (apply cmd (nreverse args1))))
  11505. ((member type '("http" "https" "ftp" "news"))
  11506. (browse-url (concat type ":" (org-link-escape
  11507. path org-link-escape-chars-browser))))
  11508. ((member type '("message"))
  11509. (browse-url (concat type ":" path)))
  11510. ((string= type "tags")
  11511. (org-tags-view in-emacs path))
  11512. ((string= type "thisfile")
  11513. (if in-emacs
  11514. (switch-to-buffer-other-window
  11515. (org-get-buffer-for-internal-link (current-buffer)))
  11516. (org-mark-ring-push))
  11517. (let ((cmd `(org-link-search
  11518. ,path
  11519. ,(cond ((equal in-emacs '(4)) 'occur)
  11520. ((equal in-emacs '(16)) 'org-occur)
  11521. (t nil))
  11522. ,pos)))
  11523. (condition-case nil (eval cmd)
  11524. (error (progn (widen) (eval cmd))))))
  11525. ((string= type "tree-match")
  11526. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11527. ((string= type "file")
  11528. (if (string-match "::\\([0-9]+\\)\\'" path)
  11529. (setq line (string-to-number (match-string 1 path))
  11530. path (substring path 0 (match-beginning 0)))
  11531. (if (string-match "::\\(.+\\)\\'" path)
  11532. (setq search (match-string 1 path)
  11533. path (substring path 0 (match-beginning 0)))))
  11534. (if (string-match "[*?{]" (file-name-nondirectory path))
  11535. (dired path)
  11536. (org-open-file path in-emacs line search)))
  11537. ((string= type "news")
  11538. (org-follow-gnus-link path))
  11539. ((string= type "bbdb")
  11540. (org-follow-bbdb-link path))
  11541. ((string= type "info")
  11542. (org-follow-info-link path))
  11543. ((string= type "gnus")
  11544. (let (group article)
  11545. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11546. (error "Error in Gnus link"))
  11547. (setq group (match-string 1 path)
  11548. article (match-string 3 path))
  11549. (org-follow-gnus-link group article)))
  11550. ((string= type "vm")
  11551. (let (folder article)
  11552. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11553. (error "Error in VM link"))
  11554. (setq folder (match-string 1 path)
  11555. article (match-string 3 path))
  11556. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11557. (org-follow-vm-link folder article in-emacs)))
  11558. ((string= type "wl")
  11559. (let (folder article)
  11560. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11561. (error "Error in Wanderlust link"))
  11562. (setq folder (match-string 1 path)
  11563. article (match-string 3 path))
  11564. (org-follow-wl-link folder article)))
  11565. ((string= type "mhe")
  11566. (let (folder article)
  11567. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11568. (error "Error in MHE link"))
  11569. (setq folder (match-string 1 path)
  11570. article (match-string 3 path))
  11571. (org-follow-mhe-link folder article)))
  11572. ((string= type "rmail")
  11573. (let (folder article)
  11574. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11575. (error "Error in RMAIL link"))
  11576. (setq folder (match-string 1 path)
  11577. article (match-string 3 path))
  11578. (org-follow-rmail-link folder article)))
  11579. ((string= type "shell")
  11580. (let ((cmd path))
  11581. (if (or (not org-confirm-shell-link-function)
  11582. (funcall org-confirm-shell-link-function
  11583. (format "Execute \"%s\" in shell? "
  11584. (org-add-props cmd nil
  11585. 'face 'org-warning))))
  11586. (progn
  11587. (message "Executing %s" cmd)
  11588. (shell-command cmd))
  11589. (error "Abort"))))
  11590. ((string= type "elisp")
  11591. (let ((cmd path))
  11592. (if (or (not org-confirm-elisp-link-function)
  11593. (funcall org-confirm-elisp-link-function
  11594. (format "Execute \"%s\" as elisp? "
  11595. (org-add-props cmd nil
  11596. 'face 'org-warning))))
  11597. (message "%s => %s" cmd (eval (read cmd)))
  11598. (error "Abort"))))
  11599. (t
  11600. (browse-url-at-point)))))
  11601. (move-marker org-open-link-marker nil)))
  11602. ;;; File search
  11603. (defvar org-create-file-search-functions nil
  11604. "List of functions to construct the right search string for a file link.
  11605. These functions are called in turn with point at the location to
  11606. which the link should point.
  11607. A function in the hook should first test if it would like to
  11608. handle this file type, for example by checking the major-mode or
  11609. the file extension. If it decides not to handle this file, it
  11610. should just return nil to give other functions a chance. If it
  11611. does handle the file, it must return the search string to be used
  11612. when following the link. The search string will be part of the
  11613. file link, given after a double colon, and `org-open-at-point'
  11614. will automatically search for it. If special measures must be
  11615. taken to make the search successful, another function should be
  11616. added to the companion hook `org-execute-file-search-functions',
  11617. which see.
  11618. A function in this hook may also use `setq' to set the variable
  11619. `description' to provide a suggestion for the descriptive text to
  11620. be used for this link when it gets inserted into an Org-mode
  11621. buffer with \\[org-insert-link].")
  11622. (defvar org-execute-file-search-functions nil
  11623. "List of functions to execute a file search triggered by a link.
  11624. Functions added to this hook must accept a single argument, the
  11625. search string that was part of the file link, the part after the
  11626. double colon. The function must first check if it would like to
  11627. handle this search, for example by checking the major-mode or the
  11628. file extension. If it decides not to handle this search, it
  11629. should just return nil to give other functions a chance. If it
  11630. does handle the search, it must return a non-nil value to keep
  11631. other functions from trying.
  11632. Each function can access the current prefix argument through the
  11633. variable `current-prefix-argument'. Note that a single prefix is
  11634. used to force opening a link in Emacs, so it may be good to only
  11635. use a numeric or double prefix to guide the search function.
  11636. In case this is needed, a function in this hook can also restore
  11637. the window configuration before `org-open-at-point' was called using:
  11638. (set-window-configuration org-window-config-before-follow-link)")
  11639. (defun org-link-search (s &optional type avoid-pos)
  11640. "Search for a link search option.
  11641. If S is surrounded by forward slashes, it is interpreted as a
  11642. regular expression. In org-mode files, this will create an `org-occur'
  11643. sparse tree. In ordinary files, `occur' will be used to list matches.
  11644. If the current buffer is in `dired-mode', grep will be used to search
  11645. in all files. If AVOID-POS is given, ignore matches near that position."
  11646. (let ((case-fold-search t)
  11647. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11648. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11649. (append '(("") (" ") ("\t") ("\n"))
  11650. org-emphasis-alist)
  11651. "\\|") "\\)"))
  11652. (pos (point))
  11653. (pre "") (post "")
  11654. words re0 re1 re2 re3 re4 re5 re2a reall)
  11655. (cond
  11656. ;; First check if there are any special
  11657. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11658. ;; Now try the builtin stuff
  11659. ((save-excursion
  11660. (goto-char (point-min))
  11661. (and
  11662. (re-search-forward
  11663. (concat "<<" (regexp-quote s0) ">>") nil t)
  11664. (setq pos (match-beginning 0))))
  11665. ;; There is an exact target for this
  11666. (goto-char pos))
  11667. ((string-match "^/\\(.*\\)/$" s)
  11668. ;; A regular expression
  11669. (cond
  11670. ((org-mode-p)
  11671. (org-occur (match-string 1 s)))
  11672. ;;((eq major-mode 'dired-mode)
  11673. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11674. (t (org-do-occur (match-string 1 s)))))
  11675. (t
  11676. ;; A normal search strings
  11677. (when (equal (string-to-char s) ?*)
  11678. ;; Anchor on headlines, post may include tags.
  11679. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11680. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11681. s (substring s 1)))
  11682. (remove-text-properties
  11683. 0 (length s)
  11684. '(face nil mouse-face nil keymap nil fontified nil) s)
  11685. ;; Make a series of regular expressions to find a match
  11686. (setq words (org-split-string s "[ \n\r\t]+")
  11687. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11688. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11689. "\\)" markers)
  11690. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11691. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11692. re1 (concat pre re2 post)
  11693. re3 (concat pre re4 post)
  11694. re5 (concat pre ".*" re4)
  11695. re2 (concat pre re2)
  11696. re2a (concat pre re2a)
  11697. re4 (concat pre re4)
  11698. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11699. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11700. re5 "\\)"
  11701. ))
  11702. (cond
  11703. ((eq type 'org-occur) (org-occur reall))
  11704. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11705. (t (goto-char (point-min))
  11706. (if (or (org-search-not-self 1 re0 nil t)
  11707. (org-search-not-self 1 re1 nil t)
  11708. (org-search-not-self 1 re2 nil t)
  11709. (org-search-not-self 1 re2a nil t)
  11710. (org-search-not-self 1 re3 nil t)
  11711. (org-search-not-self 1 re4 nil t)
  11712. (org-search-not-self 1 re5 nil t)
  11713. )
  11714. (goto-char (match-beginning 1))
  11715. (goto-char pos)
  11716. (error "No match")))))
  11717. (t
  11718. ;; Normal string-search
  11719. (goto-char (point-min))
  11720. (if (search-forward s nil t)
  11721. (goto-char (match-beginning 0))
  11722. (error "No match"))))
  11723. (and (org-mode-p) (org-show-context 'link-search))))
  11724. (defun org-search-not-self (group &rest args)
  11725. "Execute `re-search-forward', but only accept matches that do not
  11726. enclose the position of `org-open-link-marker'."
  11727. (let ((m org-open-link-marker))
  11728. (catch 'exit
  11729. (while (apply 're-search-forward args)
  11730. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11731. (goto-char (match-end group))
  11732. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11733. (> (match-beginning 0) (marker-position m))
  11734. (< (match-end 0) (marker-position m)))
  11735. (save-match-data
  11736. (or (not (org-in-regexp
  11737. org-bracket-link-analytic-regexp 1))
  11738. (not (match-end 4)) ; no description
  11739. (and (<= (match-beginning 4) (point))
  11740. (>= (match-end 4) (point))))))
  11741. (throw 'exit (point))))))))
  11742. (defun org-get-buffer-for-internal-link (buffer)
  11743. "Return a buffer to be used for displaying the link target of internal links."
  11744. (cond
  11745. ((not org-display-internal-link-with-indirect-buffer)
  11746. buffer)
  11747. ((string-match "(Clone)$" (buffer-name buffer))
  11748. (message "Buffer is already a clone, not making another one")
  11749. ;; we also do not modify visibility in this case
  11750. buffer)
  11751. (t ; make a new indirect buffer for displaying the link
  11752. (let* ((bn (buffer-name buffer))
  11753. (ibn (concat bn "(Clone)"))
  11754. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11755. (with-current-buffer ib (org-overview))
  11756. ib))))
  11757. (defun org-do-occur (regexp &optional cleanup)
  11758. "Call the Emacs command `occur'.
  11759. If CLEANUP is non-nil, remove the printout of the regular expression
  11760. in the *Occur* buffer. This is useful if the regex is long and not useful
  11761. to read."
  11762. (occur regexp)
  11763. (when cleanup
  11764. (let ((cwin (selected-window)) win beg end)
  11765. (when (setq win (get-buffer-window "*Occur*"))
  11766. (select-window win))
  11767. (goto-char (point-min))
  11768. (when (re-search-forward "match[a-z]+" nil t)
  11769. (setq beg (match-end 0))
  11770. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11771. (setq end (1- (match-beginning 0)))))
  11772. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11773. (goto-char (point-min))
  11774. (select-window cwin))))
  11775. ;;; The mark ring for links jumps
  11776. (defvar org-mark-ring nil
  11777. "Mark ring for positions before jumps in Org-mode.")
  11778. (defvar org-mark-ring-last-goto nil
  11779. "Last position in the mark ring used to go back.")
  11780. ;; Fill and close the ring
  11781. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11782. (loop for i from 1 to org-mark-ring-length do
  11783. (push (make-marker) org-mark-ring))
  11784. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11785. org-mark-ring)
  11786. (defun org-mark-ring-push (&optional pos buffer)
  11787. "Put the current position or POS into the mark ring and rotate it."
  11788. (interactive)
  11789. (setq pos (or pos (point)))
  11790. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11791. (move-marker (car org-mark-ring)
  11792. (or pos (point))
  11793. (or buffer (current-buffer)))
  11794. (message "%s"
  11795. (substitute-command-keys
  11796. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11797. (defun org-mark-ring-goto (&optional n)
  11798. "Jump to the previous position in the mark ring.
  11799. With prefix arg N, jump back that many stored positions. When
  11800. called several times in succession, walk through the entire ring.
  11801. Org-mode commands jumping to a different position in the current file,
  11802. or to another Org-mode file, automatically push the old position
  11803. onto the ring."
  11804. (interactive "p")
  11805. (let (p m)
  11806. (if (eq last-command this-command)
  11807. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11808. (setq p org-mark-ring))
  11809. (setq org-mark-ring-last-goto p)
  11810. (setq m (car p))
  11811. (switch-to-buffer (marker-buffer m))
  11812. (goto-char m)
  11813. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11814. (defun org-remove-angle-brackets (s)
  11815. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11816. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11817. s)
  11818. (defun org-add-angle-brackets (s)
  11819. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11820. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11821. s)
  11822. ;;; Following specific links
  11823. (defun org-follow-timestamp-link ()
  11824. (cond
  11825. ((org-at-date-range-p t)
  11826. (let ((org-agenda-start-on-weekday)
  11827. (t1 (match-string 1))
  11828. (t2 (match-string 2)))
  11829. (setq t1 (time-to-days (org-time-string-to-time t1))
  11830. t2 (time-to-days (org-time-string-to-time t2)))
  11831. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11832. ((org-at-timestamp-p t)
  11833. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11834. (substring (match-string 1) 0 10)))
  11835. 1))
  11836. (t (error "This should not happen"))))
  11837. (defun org-follow-bbdb-link (name)
  11838. "Follow a BBDB link to NAME."
  11839. (require 'bbdb)
  11840. (let ((inhibit-redisplay (not debug-on-error))
  11841. (bbdb-electric-p nil))
  11842. (catch 'exit
  11843. ;; Exact match on name
  11844. (bbdb-name (concat "\\`" name "\\'") nil)
  11845. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11846. ;; Exact match on name
  11847. (bbdb-company (concat "\\`" name "\\'") nil)
  11848. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11849. ;; Partial match on name
  11850. (bbdb-name name nil)
  11851. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11852. ;; Partial match on company
  11853. (bbdb-company name nil)
  11854. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11855. ;; General match including network address and notes
  11856. (bbdb name nil)
  11857. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11858. (delete-window (get-buffer-window "*BBDB*"))
  11859. (error "No matching BBDB record")))))
  11860. (defun org-follow-info-link (name)
  11861. "Follow an info file & node link to NAME."
  11862. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11863. (string-match "\\(.*\\)" name))
  11864. (progn
  11865. (require 'info)
  11866. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11867. (Info-find-node (match-string 1 name) (match-string 2 name))
  11868. (Info-find-node (match-string 1 name) "Top")))
  11869. (message "Could not open: %s" name)))
  11870. (defun org-follow-gnus-link (&optional group article)
  11871. "Follow a Gnus link to GROUP and ARTICLE."
  11872. (require 'gnus)
  11873. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11874. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11875. (cond ((and group article)
  11876. (gnus-group-read-group 1 nil group)
  11877. (gnus-summary-goto-article (string-to-number article) nil t))
  11878. (group (gnus-group-jump-to-group group))))
  11879. (defun org-follow-vm-link (&optional folder article readonly)
  11880. "Follow a VM link to FOLDER and ARTICLE."
  11881. (require 'vm)
  11882. (setq article (org-add-angle-brackets article))
  11883. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11884. ;; ange-ftp or efs or tramp access
  11885. (let ((user (or (match-string 1 folder) (user-login-name)))
  11886. (host (match-string 2 folder))
  11887. (file (match-string 3 folder)))
  11888. (cond
  11889. ((featurep 'tramp)
  11890. ;; use tramp to access the file
  11891. (if (featurep 'xemacs)
  11892. (setq folder (format "[%s@%s]%s" user host file))
  11893. (setq folder (format "/%s@%s:%s" user host file))))
  11894. (t
  11895. ;; use ange-ftp or efs
  11896. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11897. (setq folder (format "/%s@%s:%s" user host file))))))
  11898. (when folder
  11899. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11900. (sit-for 0.1)
  11901. (when article
  11902. (vm-select-folder-buffer)
  11903. (widen)
  11904. (let ((case-fold-search t))
  11905. (goto-char (point-min))
  11906. (if (not (re-search-forward
  11907. (concat "^" "message-id: *" (regexp-quote article))))
  11908. (error "Could not find the specified message in this folder"))
  11909. (vm-isearch-update)
  11910. (vm-isearch-narrow)
  11911. (vm-beginning-of-message)
  11912. (vm-summarize)))))
  11913. (defun org-follow-wl-link (folder article)
  11914. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11915. (if (and (string= folder "%")
  11916. article
  11917. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11918. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11919. ;; Thus, we recompose folder and article ids.
  11920. (setq folder (format "%s#%s" folder (match-string 1 article))
  11921. article (match-string 3 article)))
  11922. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11923. (error "No such folder: %s" folder))
  11924. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11925. (and article
  11926. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11927. (wl-summary-redisplay)))
  11928. (defun org-follow-rmail-link (folder article)
  11929. "Follow an RMAIL link to FOLDER and ARTICLE."
  11930. (setq article (org-add-angle-brackets article))
  11931. (let (message-number)
  11932. (save-excursion
  11933. (save-window-excursion
  11934. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11935. (setq message-number
  11936. (save-restriction
  11937. (widen)
  11938. (goto-char (point-max))
  11939. (if (re-search-backward
  11940. (concat "^Message-ID:\\s-+" (regexp-quote
  11941. (or article "")))
  11942. nil t)
  11943. (rmail-what-message))))))
  11944. (if message-number
  11945. (progn
  11946. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11947. (rmail-show-message message-number)
  11948. message-number)
  11949. (error "Message not found"))))
  11950. ;;; mh-e integration based on planner-mode
  11951. (defun org-mhe-get-message-real-folder ()
  11952. "Return the name of the current message real folder, so if you use
  11953. sequences, it will now work."
  11954. (save-excursion
  11955. (let* ((folder
  11956. (if (equal major-mode 'mh-folder-mode)
  11957. mh-current-folder
  11958. ;; Refer to the show buffer
  11959. mh-show-folder-buffer))
  11960. (end-index
  11961. (if (boundp 'mh-index-folder)
  11962. (min (length mh-index-folder) (length folder))))
  11963. )
  11964. ;; a simple test on mh-index-data does not work, because
  11965. ;; mh-index-data is always nil in a show buffer.
  11966. (if (and (boundp 'mh-index-folder)
  11967. (string= mh-index-folder (substring folder 0 end-index)))
  11968. (if (equal major-mode 'mh-show-mode)
  11969. (save-window-excursion
  11970. (let (pop-up-frames)
  11971. (when (buffer-live-p (get-buffer folder))
  11972. (progn
  11973. (pop-to-buffer folder)
  11974. (org-mhe-get-message-folder-from-index)
  11975. )
  11976. )))
  11977. (org-mhe-get-message-folder-from-index)
  11978. )
  11979. folder
  11980. )
  11981. )))
  11982. (defun org-mhe-get-message-folder-from-index ()
  11983. "Returns the name of the message folder in a index folder buffer."
  11984. (save-excursion
  11985. (mh-index-previous-folder)
  11986. (re-search-forward "^\\(+.*\\)$" nil t)
  11987. (message "%s" (match-string 1))))
  11988. (defun org-mhe-get-message-folder ()
  11989. "Return the name of the current message folder. Be careful if you
  11990. use sequences."
  11991. (save-excursion
  11992. (if (equal major-mode 'mh-folder-mode)
  11993. mh-current-folder
  11994. ;; Refer to the show buffer
  11995. mh-show-folder-buffer)))
  11996. (defun org-mhe-get-message-num ()
  11997. "Return the number of the current message. Be careful if you
  11998. use sequences."
  11999. (save-excursion
  12000. (if (equal major-mode 'mh-folder-mode)
  12001. (mh-get-msg-num nil)
  12002. ;; Refer to the show buffer
  12003. (mh-show-buffer-message-number))))
  12004. (defun org-mhe-get-header (header)
  12005. "Return a header of the message in folder mode. This will create a
  12006. show buffer for the corresponding message. If you have a more clever
  12007. idea..."
  12008. (let* ((folder (org-mhe-get-message-folder))
  12009. (num (org-mhe-get-message-num))
  12010. (buffer (get-buffer-create (concat "show-" folder)))
  12011. (header-field))
  12012. (with-current-buffer buffer
  12013. (mh-display-msg num folder)
  12014. (if (equal major-mode 'mh-folder-mode)
  12015. (mh-header-display)
  12016. (mh-show-header-display))
  12017. (set-buffer buffer)
  12018. (setq header-field (mh-get-header-field header))
  12019. (if (equal major-mode 'mh-folder-mode)
  12020. (mh-show)
  12021. (mh-show-show))
  12022. header-field)))
  12023. (defun org-follow-mhe-link (folder article)
  12024. "Follow an MHE link to FOLDER and ARTICLE.
  12025. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12026. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12027. ARTICLE is searched in all folders. Indexed searches (swish++,
  12028. namazu, and others supported by MH-E) will always search in all
  12029. folders."
  12030. (require 'mh-e)
  12031. (require 'mh-search)
  12032. (require 'mh-utils)
  12033. (mh-find-path)
  12034. (if (not article)
  12035. (mh-visit-folder (mh-normalize-folder-name folder))
  12036. (setq article (org-add-angle-brackets article))
  12037. (mh-search-choose)
  12038. (if (equal mh-searcher 'pick)
  12039. (progn
  12040. (mh-search folder (list "--message-id" article))
  12041. (when (and org-mhe-search-all-folders
  12042. (not (org-mhe-get-message-real-folder)))
  12043. (kill-this-buffer)
  12044. (mh-search "+" (list "--message-id" article))))
  12045. (mh-search "+" article))
  12046. (if (org-mhe-get-message-real-folder)
  12047. (mh-show-msg 1)
  12048. (kill-this-buffer)
  12049. (error "Message not found"))))
  12050. ;;; BibTeX links
  12051. ;; Use the custom search meachnism to construct and use search strings for
  12052. ;; file links to BibTeX database entries.
  12053. (defun org-create-file-search-in-bibtex ()
  12054. "Create the search string and description for a BibTeX database entry."
  12055. (when (eq major-mode 'bibtex-mode)
  12056. ;; yes, we want to construct this search string.
  12057. ;; Make a good description for this entry, using names, year and the title
  12058. ;; Put it into the `description' variable which is dynamically scoped.
  12059. (let ((bibtex-autokey-names 1)
  12060. (bibtex-autokey-names-stretch 1)
  12061. (bibtex-autokey-name-case-convert-function 'identity)
  12062. (bibtex-autokey-name-separator " & ")
  12063. (bibtex-autokey-additional-names " et al.")
  12064. (bibtex-autokey-year-length 4)
  12065. (bibtex-autokey-name-year-separator " ")
  12066. (bibtex-autokey-titlewords 3)
  12067. (bibtex-autokey-titleword-separator " ")
  12068. (bibtex-autokey-titleword-case-convert-function 'identity)
  12069. (bibtex-autokey-titleword-length 'infty)
  12070. (bibtex-autokey-year-title-separator ": "))
  12071. (setq description (bibtex-generate-autokey)))
  12072. ;; Now parse the entry, get the key and return it.
  12073. (save-excursion
  12074. (bibtex-beginning-of-entry)
  12075. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12076. (defun org-execute-file-search-in-bibtex (s)
  12077. "Find the link search string S as a key for a database entry."
  12078. (when (eq major-mode 'bibtex-mode)
  12079. ;; Yes, we want to do the search in this file.
  12080. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12081. (goto-char (point-min))
  12082. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12083. (regexp-quote s) "[ \t\n]*,") nil t)
  12084. (goto-char (match-beginning 0)))
  12085. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12086. ;; Use double prefix to indicate that any web link should be browsed
  12087. (let ((b (current-buffer)) (p (point)))
  12088. ;; Restore the window configuration because we just use the web link
  12089. (set-window-configuration org-window-config-before-follow-link)
  12090. (save-excursion (set-buffer b) (goto-char p)
  12091. (bibtex-url)))
  12092. (recenter 0)) ; Move entry start to beginning of window
  12093. ;; return t to indicate that the search is done.
  12094. t))
  12095. ;; Finally add the functions to the right hooks.
  12096. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12097. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12098. ;; end of Bibtex link setup
  12099. ;;; Following file links
  12100. (defun org-open-file (path &optional in-emacs line search)
  12101. "Open the file at PATH.
  12102. First, this expands any special file name abbreviations. Then the
  12103. configuration variable `org-file-apps' is checked if it contains an
  12104. entry for this file type, and if yes, the corresponding command is launched.
  12105. If no application is found, Emacs simply visits the file.
  12106. With optional argument IN-EMACS, Emacs will visit the file.
  12107. Optional LINE specifies a line to go to, optional SEARCH a string to
  12108. search for. If LINE or SEARCH is given, the file will always be
  12109. opened in Emacs.
  12110. If the file does not exist, an error is thrown."
  12111. (setq in-emacs (or in-emacs line search))
  12112. (let* ((file (if (equal path "")
  12113. buffer-file-name
  12114. (substitute-in-file-name (expand-file-name path))))
  12115. (apps (append org-file-apps (org-default-apps)))
  12116. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12117. (dirp (if remp nil (file-directory-p file)))
  12118. (dfile (downcase file))
  12119. (old-buffer (current-buffer))
  12120. (old-pos (point))
  12121. (old-mode major-mode)
  12122. ext cmd)
  12123. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12124. (setq ext (match-string 1 dfile))
  12125. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12126. (setq ext (match-string 1 dfile))))
  12127. (if in-emacs
  12128. (setq cmd 'emacs)
  12129. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12130. (and dirp (cdr (assoc 'directory apps)))
  12131. (cdr (assoc ext apps))
  12132. (cdr (assoc t apps)))))
  12133. (when (eq cmd 'mailcap)
  12134. (require 'mailcap)
  12135. (mailcap-parse-mailcaps)
  12136. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12137. (command (mailcap-mime-info mime-type)))
  12138. (if (stringp command)
  12139. (setq cmd command)
  12140. (setq cmd 'emacs))))
  12141. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12142. (not (file-exists-p file))
  12143. (not org-open-non-existing-files))
  12144. (error "No such file: %s" file))
  12145. (cond
  12146. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12147. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12148. (while (string-match "['\"]%s['\"]" cmd)
  12149. (setq cmd (replace-match "%s" t t cmd)))
  12150. (while (string-match "%s" cmd)
  12151. (setq cmd (replace-match
  12152. (save-match-data (shell-quote-argument file))
  12153. t t cmd)))
  12154. (save-window-excursion
  12155. (start-process-shell-command cmd nil cmd)))
  12156. ((or (stringp cmd)
  12157. (eq cmd 'emacs))
  12158. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12159. (widen)
  12160. (if line (goto-line line)
  12161. (if search (org-link-search search))))
  12162. ((consp cmd)
  12163. (eval cmd))
  12164. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12165. (and (org-mode-p) (eq old-mode 'org-mode)
  12166. (or (not (equal old-buffer (current-buffer)))
  12167. (not (equal old-pos (point))))
  12168. (org-mark-ring-push old-pos old-buffer))))
  12169. (defun org-default-apps ()
  12170. "Return the default applications for this operating system."
  12171. (cond
  12172. ((eq system-type 'darwin)
  12173. org-file-apps-defaults-macosx)
  12174. ((eq system-type 'windows-nt)
  12175. org-file-apps-defaults-windowsnt)
  12176. (t org-file-apps-defaults-gnu)))
  12177. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12178. (defun org-file-remote-p (file)
  12179. "Test whether FILE specifies a location on a remote system.
  12180. Return non-nil if the location is indeed remote.
  12181. For example, the filename \"/user@host:/foo\" specifies a location
  12182. on the system \"/user@host:\"."
  12183. (cond ((fboundp 'file-remote-p)
  12184. (file-remote-p file))
  12185. ((fboundp 'tramp-handle-file-remote-p)
  12186. (tramp-handle-file-remote-p file))
  12187. ((and (boundp 'ange-ftp-name-format)
  12188. (string-match (car ange-ftp-name-format) file))
  12189. t)
  12190. (t nil)))
  12191. ;;;; Hooks for remember.el, and refiling
  12192. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12193. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12194. ;;;###autoload
  12195. (defun org-remember-insinuate ()
  12196. "Setup remember.el for use wiht Org-mode."
  12197. (require 'remember)
  12198. (setq remember-annotation-functions '(org-remember-annotation))
  12199. (setq remember-handler-functions '(org-remember-handler))
  12200. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12201. ;;;###autoload
  12202. (defun org-remember-annotation ()
  12203. "Return a link to the current location as an annotation for remember.el.
  12204. If you are using Org-mode files as target for data storage with
  12205. remember.el, then the annotations should include a link compatible with the
  12206. conventions in Org-mode. This function returns such a link."
  12207. (org-store-link nil))
  12208. (defconst org-remember-help
  12209. "Select a destination location for the note.
  12210. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12211. RET on headline -> Store as sublevel entry to current headline
  12212. RET at beg-of-buf -> Append to file as level 2 headline
  12213. <left>/<right> -> before/after current headline, same headings level")
  12214. (defvar org-remember-previous-location nil)
  12215. (defvar org-force-remember-template-char) ;; dynamically scoped
  12216. (defun org-select-remember-template (&optional use-char)
  12217. (when org-remember-templates
  12218. (let* ((templates (mapcar (lambda (x)
  12219. (if (stringp (car x))
  12220. (append (list (nth 1 x) (car x)) (cddr x))
  12221. (append (list (car x) "") (cdr x))))
  12222. org-remember-templates))
  12223. (char (or use-char
  12224. (cond
  12225. ((= (length templates) 1)
  12226. (caar templates))
  12227. ((and (boundp 'org-force-remember-template-char)
  12228. org-force-remember-template-char)
  12229. (if (stringp org-force-remember-template-char)
  12230. (string-to-char org-force-remember-template-char)
  12231. org-force-remember-template-char))
  12232. (t
  12233. (message "Select template: %s"
  12234. (mapconcat
  12235. (lambda (x)
  12236. (cond
  12237. ((not (string-match "\\S-" (nth 1 x)))
  12238. (format "[%c]" (car x)))
  12239. ((equal (downcase (car x))
  12240. (downcase (aref (nth 1 x) 0)))
  12241. (format "[%c]%s" (car x)
  12242. (substring (nth 1 x) 1)))
  12243. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12244. templates " "))
  12245. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12246. (when (equal char0 ?\C-g)
  12247. (jump-to-register remember-register)
  12248. (kill-buffer remember-buffer))
  12249. char0))))))
  12250. (cddr (assoc char templates)))))
  12251. (defvar x-last-selected-text)
  12252. (defvar x-last-selected-text-primary)
  12253. ;;;###autoload
  12254. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12255. "Initialize *remember* buffer with template, invoke `org-mode'.
  12256. This function should be placed into `remember-mode-hook' and in fact requires
  12257. to be run from that hook to function properly."
  12258. (if org-remember-templates
  12259. (let* ((entry (org-select-remember-template use-char))
  12260. (tpl (car entry))
  12261. (plist-p (if org-store-link-plist t nil))
  12262. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12263. (string-match "\\S-" (nth 1 entry)))
  12264. (nth 1 entry)
  12265. org-default-notes-file))
  12266. (headline (nth 2 entry))
  12267. (v-c (or (and (eq window-system 'x)
  12268. (fboundp 'x-cut-buffer-or-selection-value)
  12269. (x-cut-buffer-or-selection-value))
  12270. (org-bound-and-true-p x-last-selected-text)
  12271. (org-bound-and-true-p x-last-selected-text-primary)
  12272. (and (> (length kill-ring) 0) (current-kill 0))))
  12273. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12274. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12275. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12276. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12277. ;; `initial' and `annotation' are bound in `remember'
  12278. (v-i (if (boundp 'initial) initial))
  12279. (v-a (if (and (boundp 'annotation) annotation)
  12280. (if (equal annotation "[[]]") "" annotation)
  12281. ""))
  12282. (v-A (if (and v-a
  12283. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12284. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12285. v-a))
  12286. (v-n user-full-name)
  12287. (org-startup-folded nil)
  12288. org-time-was-given org-end-time-was-given x
  12289. prompt completions char time pos default histvar)
  12290. (setq org-store-link-plist
  12291. (append (list :annotation v-a :initial v-i)
  12292. org-store-link-plist))
  12293. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12294. (erase-buffer)
  12295. (insert (substitute-command-keys
  12296. (format
  12297. "## Filing location: Select interactively, default, or last used:
  12298. ## %s to select file and header location interactively.
  12299. ## %s \"%s\" -> \"* %s\"
  12300. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12301. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12302. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12303. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12304. (abbreviate-file-name (or file org-default-notes-file))
  12305. (or headline "")
  12306. (or (car org-remember-previous-location) "???")
  12307. (or (cdr org-remember-previous-location) "???"))))
  12308. (insert tpl) (goto-char (point-min))
  12309. ;; Simple %-escapes
  12310. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12311. (when (and initial (equal (match-string 0) "%i"))
  12312. (save-match-data
  12313. (let* ((lead (buffer-substring
  12314. (point-at-bol) (match-beginning 0))))
  12315. (setq v-i (mapconcat 'identity
  12316. (org-split-string initial "\n")
  12317. (concat "\n" lead))))))
  12318. (replace-match
  12319. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12320. t t))
  12321. ;; %[] Insert contents of a file.
  12322. (goto-char (point-min))
  12323. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12324. (let ((start (match-beginning 0))
  12325. (end (match-end 0))
  12326. (filename (expand-file-name (match-string 1))))
  12327. (goto-char start)
  12328. (delete-region start end)
  12329. (condition-case error
  12330. (insert-file-contents filename)
  12331. (error (insert (format "%%![Couldn't insert %s: %s]"
  12332. filename error))))))
  12333. ;; %() embedded elisp
  12334. (goto-char (point-min))
  12335. (while (re-search-forward "%\\((.+)\\)" nil t)
  12336. (goto-char (match-beginning 0))
  12337. (let ((template-start (point)))
  12338. (forward-char 1)
  12339. (let ((result
  12340. (condition-case error
  12341. (eval (read (current-buffer)))
  12342. (error (format "%%![Error: %s]" error)))))
  12343. (delete-region template-start (point))
  12344. (insert result))))
  12345. ;; From the property list
  12346. (when plist-p
  12347. (goto-char (point-min))
  12348. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12349. (and (setq x (or (plist-get org-store-link-plist
  12350. (intern (match-string 1))) ""))
  12351. (replace-match x t t))))
  12352. ;; Turn on org-mode in the remember buffer, set local variables
  12353. (org-mode)
  12354. (org-set-local 'org-finish-function 'org-remember-finalize)
  12355. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12356. (org-set-local 'org-default-notes-file file))
  12357. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12358. (org-set-local 'org-remember-default-headline headline))
  12359. ;; Interactive template entries
  12360. (goto-char (point-min))
  12361. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12362. (setq char (if (match-end 3) (match-string 3))
  12363. prompt (if (match-end 2) (match-string 2)))
  12364. (goto-char (match-beginning 0))
  12365. (replace-match "")
  12366. (setq completions nil default nil)
  12367. (when prompt
  12368. (setq completions (org-split-string prompt "|")
  12369. prompt (pop completions)
  12370. default (car completions)
  12371. histvar (intern (concat
  12372. "org-remember-template-prompt-history::"
  12373. (or prompt "")))
  12374. completions (mapcar 'list completions)))
  12375. (cond
  12376. ((member char '("G" "g"))
  12377. (let* ((org-last-tags-completion-table
  12378. (org-global-tags-completion-table
  12379. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12380. (org-add-colon-after-tag-completion t)
  12381. (ins (completing-read
  12382. (if prompt (concat prompt ": ") "Tags: ")
  12383. 'org-tags-completion-function nil nil nil
  12384. 'org-tags-history)))
  12385. (setq ins (mapconcat 'identity
  12386. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12387. ":"))
  12388. (when (string-match "\\S-" ins)
  12389. (or (equal (char-before) ?:) (insert ":"))
  12390. (insert ins)
  12391. (or (equal (char-after) ?:) (insert ":")))))
  12392. (char
  12393. (setq org-time-was-given (equal (upcase char) char))
  12394. (setq time (org-read-date (equal (upcase char) "U") t nil
  12395. prompt))
  12396. (org-insert-time-stamp time org-time-was-given
  12397. (member char '("u" "U"))
  12398. nil nil (list org-end-time-was-given)))
  12399. (t
  12400. (insert (org-completing-read
  12401. (concat (if prompt prompt "Enter string")
  12402. (if default (concat " [" default "]"))
  12403. ": ")
  12404. completions nil nil nil histvar default)))))
  12405. (goto-char (point-min))
  12406. (if (re-search-forward "%\\?" nil t)
  12407. (replace-match "")
  12408. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12409. (org-mode)
  12410. (org-set-local 'org-finish-function 'org-remember-finalize))
  12411. (when (save-excursion
  12412. (goto-char (point-min))
  12413. (re-search-forward "%!" nil t))
  12414. (replace-match "")
  12415. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12416. (defun org-remember-finish-immediately ()
  12417. "File remember note immediately.
  12418. This should be run in `post-command-hook' and will remove itself
  12419. from that hook."
  12420. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12421. (when org-finish-function
  12422. (funcall org-finish-function)))
  12423. (defvar org-clock-marker) ; Defined below
  12424. (defun org-remember-finalize ()
  12425. "Finalize the remember process."
  12426. (unless (fboundp 'remember-finalize)
  12427. (defalias 'remember-finalize 'remember-buffer))
  12428. (when (and org-clock-marker
  12429. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12430. ;; FIXME: test this, this is w/o notetaking!
  12431. (let (org-log-done) (org-clock-out)))
  12432. (when buffer-file-name
  12433. (save-buffer)
  12434. (setq buffer-file-name nil))
  12435. (remember-finalize))
  12436. ;;;###autoload
  12437. (defun org-remember (&optional goto org-force-remember-template-char)
  12438. "Call `remember'. If this is already a remember buffer, re-apply template.
  12439. If there is an active region, make sure remember uses it as initial content
  12440. of the remember buffer.
  12441. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12442. anything, just go to the file/headline where the selected template usually
  12443. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12444. note stored by remember.
  12445. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12446. associated with a template in `org-remember-templates'."
  12447. (interactive "P")
  12448. (cond
  12449. ((equal goto '(4)) (org-go-to-remember-target))
  12450. ((equal goto '(16)) (org-remember-goto-last-stored))
  12451. (t
  12452. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12453. (progn
  12454. (when (< (length org-remember-templates) 2)
  12455. (error "No other template available"))
  12456. (erase-buffer)
  12457. (let ((annotation (plist-get org-store-link-plist :annotation))
  12458. (initial (plist-get org-store-link-plist :initial)))
  12459. (org-remember-apply-template))
  12460. (message "Press C-c C-c to remember data"))
  12461. (if (org-region-active-p)
  12462. (remember (buffer-substring (point) (mark)))
  12463. (call-interactively 'remember))))))
  12464. (defun org-remember-goto-last-stored ()
  12465. "Go to the location where the last remember note was stored."
  12466. (interactive)
  12467. (bookmark-jump "org-remember-last-stored")
  12468. (message "This is the last note stored by remember"))
  12469. (defun org-go-to-remember-target (&optional template-key)
  12470. "Go to the target location of a remember template.
  12471. The user is queried for the template."
  12472. (interactive)
  12473. (let* ((entry (org-select-remember-template template-key))
  12474. (file (nth 1 entry))
  12475. (heading (nth 2 entry))
  12476. visiting)
  12477. (unless (and file (stringp file) (string-match "\\S-" file))
  12478. (setq file org-default-notes-file))
  12479. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12480. (setq heading org-remember-default-headline))
  12481. (setq visiting (org-find-base-buffer-visiting file))
  12482. (if (not visiting) (find-file-noselect file))
  12483. (switch-to-buffer (or visiting (get-file-buffer file)))
  12484. (widen)
  12485. (goto-char (point-min))
  12486. (if (re-search-forward
  12487. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12488. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12489. nil t)
  12490. (goto-char (match-beginning 0))
  12491. (error "Target headline not found: %s" heading))))
  12492. (defvar org-note-abort nil) ; dynamically scoped
  12493. ;;;###autoload
  12494. (defun org-remember-handler ()
  12495. "Store stuff from remember.el into an org file.
  12496. First prompts for an org file. If the user just presses return, the value
  12497. of `org-default-notes-file' is used.
  12498. Then the command offers the headings tree of the selected file in order to
  12499. file the text at a specific location.
  12500. You can either immediately press RET to get the note appended to the
  12501. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12502. find a better place. Then press RET or <left> or <right> in insert the note.
  12503. Key Cursor position Note gets inserted
  12504. -----------------------------------------------------------------------------
  12505. RET buffer-start as level 1 heading at end of file
  12506. RET on headline as sublevel of the heading at cursor
  12507. RET no heading at cursor position, level taken from context.
  12508. Or use prefix arg to specify level manually.
  12509. <left> on headline as same level, before current heading
  12510. <right> on headline as same level, after current heading
  12511. So the fastest way to store the note is to press RET RET to append it to
  12512. the default file. This way your current train of thought is not
  12513. interrupted, in accordance with the principles of remember.el.
  12514. You can also get the fast execution without prompting by using
  12515. C-u C-c C-c to exit the remember buffer. See also the variable
  12516. `org-remember-store-without-prompt'.
  12517. Before being stored away, the function ensures that the text has a
  12518. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12519. is constructed from the current date and some additional data.
  12520. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12521. also indented so that it starts in the same column as the headline
  12522. \(i.e. after the stars).
  12523. See also the variable `org-reverse-note-order'."
  12524. (goto-char (point-min))
  12525. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12526. (replace-match ""))
  12527. (goto-char (point-max))
  12528. (beginning-of-line 1)
  12529. (while (looking-at "[ \t]*$\\|##.*")
  12530. (delete-region (1- (point)) (point-max))
  12531. (beginning-of-line 1))
  12532. (catch 'quit
  12533. (if org-note-abort (throw 'quit nil))
  12534. (let* ((txt (buffer-substring (point-min) (point-max)))
  12535. (fastp (org-xor (equal current-prefix-arg '(4))
  12536. org-remember-store-without-prompt))
  12537. (file (cond
  12538. (fastp org-default-notes-file)
  12539. ((and (eq org-remember-interactive-interface 'refile)
  12540. org-refile-targets)
  12541. org-default-notes-file)
  12542. ((not (and (equal current-prefix-arg '(16))
  12543. org-remember-previous-location))
  12544. (org-get-org-file))))
  12545. (heading org-remember-default-headline)
  12546. (visiting (and file (org-find-base-buffer-visiting file)))
  12547. (org-startup-folded nil)
  12548. (org-startup-align-all-tables nil)
  12549. (org-goto-start-pos 1)
  12550. spos exitcmd level indent reversed)
  12551. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12552. (setq file (car org-remember-previous-location)
  12553. heading (cdr org-remember-previous-location)
  12554. fastp t))
  12555. (setq current-prefix-arg nil)
  12556. (if (string-match "[ \t\n]+\\'" txt)
  12557. (setq txt (replace-match "" t t txt)))
  12558. ;; Modify text so that it becomes a nice subtree which can be inserted
  12559. ;; into an org tree.
  12560. (let* ((lines (split-string txt "\n"))
  12561. first)
  12562. (setq first (car lines) lines (cdr lines))
  12563. (if (string-match "^\\*+ " first)
  12564. ;; Is already a headline
  12565. (setq indent nil)
  12566. ;; We need to add a headline: Use time and first buffer line
  12567. (setq lines (cons first lines)
  12568. first (concat "* " (current-time-string)
  12569. " (" (remember-buffer-desc) ")")
  12570. indent " "))
  12571. (if (and org-adapt-indentation indent)
  12572. (setq lines (mapcar
  12573. (lambda (x)
  12574. (if (string-match "\\S-" x)
  12575. (concat indent x) x))
  12576. lines)))
  12577. (setq txt (concat first "\n"
  12578. (mapconcat 'identity lines "\n"))))
  12579. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12580. (setq txt (replace-match "\n\n" t t txt))
  12581. (if (string-match "[ \t\n]*\\'" txt)
  12582. (setq txt (replace-match "\n" t t txt))))
  12583. ;; Put the modified text back into the remember buffer, for refile.
  12584. (erase-buffer)
  12585. (insert txt)
  12586. (goto-char (point-min))
  12587. (when (and (eq org-remember-interactive-interface 'refile)
  12588. (not fastp))
  12589. (org-refile nil (or visiting (find-file-noselect file)))
  12590. (throw 'quit t))
  12591. ;; Find the file
  12592. (if (not visiting) (find-file-noselect file))
  12593. (with-current-buffer (or visiting (get-file-buffer file))
  12594. (unless (org-mode-p)
  12595. (error "Target files for remember notes must be in Org-mode"))
  12596. (save-excursion
  12597. (save-restriction
  12598. (widen)
  12599. (and (goto-char (point-min))
  12600. (not (re-search-forward "^\\* " nil t))
  12601. (insert "\n* " (or heading "Notes") "\n"))
  12602. (setq reversed (org-notes-order-reversed-p))
  12603. ;; Find the default location
  12604. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12605. (goto-char (point-min))
  12606. (if (re-search-forward
  12607. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12608. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12609. nil t)
  12610. (setq org-goto-start-pos (match-beginning 0))
  12611. (when fastp
  12612. (goto-char (point-max))
  12613. (unless (bolp) (newline))
  12614. (insert "* " heading "\n")
  12615. (setq org-goto-start-pos (point-at-bol 0)))))
  12616. ;; Ask the User for a location, using the appropriate interface
  12617. (cond
  12618. (fastp (setq spos org-goto-start-pos
  12619. exitcmd 'return))
  12620. ((eq org-remember-interactive-interface 'outline)
  12621. (setq spos (org-get-location (current-buffer)
  12622. org-remember-help)
  12623. exitcmd (cdr spos)
  12624. spos (car spos)))
  12625. ((eq org-remember-interactive-interface 'outline-path-completion)
  12626. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12627. (org-refile-use-outline-path t))
  12628. (setq spos (org-refile-get-location "Heading: ")
  12629. exitcmd 'return
  12630. spos (nth 3 spos))))
  12631. (t (error "this should not hapen")))
  12632. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12633. ; not handle this note
  12634. (goto-char spos)
  12635. (cond ((org-on-heading-p t)
  12636. (org-back-to-heading t)
  12637. (setq level (funcall outline-level))
  12638. (cond
  12639. ((eq exitcmd 'return)
  12640. ;; sublevel of current
  12641. (setq org-remember-previous-location
  12642. (cons (abbreviate-file-name file)
  12643. (org-get-heading 'notags)))
  12644. (if reversed
  12645. (outline-next-heading)
  12646. (org-end-of-subtree t)
  12647. (if (not (bolp))
  12648. (if (looking-at "[ \t]*\n")
  12649. (beginning-of-line 2)
  12650. (end-of-line 1)
  12651. (insert "\n"))))
  12652. (bookmark-set "org-remember-last-stored")
  12653. (org-paste-subtree (org-get-legal-level level 1) txt))
  12654. ((eq exitcmd 'left)
  12655. ;; before current
  12656. (bookmark-set "org-remember-last-stored")
  12657. (org-paste-subtree level txt))
  12658. ((eq exitcmd 'right)
  12659. ;; after current
  12660. (org-end-of-subtree t)
  12661. (bookmark-set "org-remember-last-stored")
  12662. (org-paste-subtree level txt))
  12663. (t (error "This should not happen"))))
  12664. ((and (bobp) (not reversed))
  12665. ;; Put it at the end, one level below level 1
  12666. (save-restriction
  12667. (widen)
  12668. (goto-char (point-max))
  12669. (if (not (bolp)) (newline))
  12670. (bookmark-set "org-remember-last-stored")
  12671. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12672. ((and (bobp) reversed)
  12673. ;; Put it at the start, as level 1
  12674. (save-restriction
  12675. (widen)
  12676. (goto-char (point-min))
  12677. (re-search-forward "^\\*+ " nil t)
  12678. (beginning-of-line 1)
  12679. (bookmark-set "org-remember-last-stored")
  12680. (org-paste-subtree 1 txt)))
  12681. (t
  12682. ;; Put it right there, with automatic level determined by
  12683. ;; org-paste-subtree or from prefix arg
  12684. (bookmark-set "org-remember-last-stored")
  12685. (org-paste-subtree
  12686. (if (numberp current-prefix-arg) current-prefix-arg)
  12687. txt)))
  12688. (when remember-save-after-remembering
  12689. (save-buffer)
  12690. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12691. t) ;; return t to indicate that we took care of this note.
  12692. (defun org-get-org-file ()
  12693. "Read a filename, with default directory `org-directory'."
  12694. (let ((default (or org-default-notes-file remember-data-file)))
  12695. (read-file-name (format "File name [%s]: " default)
  12696. (file-name-as-directory org-directory)
  12697. default)))
  12698. (defun org-notes-order-reversed-p ()
  12699. "Check if the current file should receive notes in reversed order."
  12700. (cond
  12701. ((not org-reverse-note-order) nil)
  12702. ((eq t org-reverse-note-order) t)
  12703. ((not (listp org-reverse-note-order)) nil)
  12704. (t (catch 'exit
  12705. (let ((all org-reverse-note-order)
  12706. entry)
  12707. (while (setq entry (pop all))
  12708. (if (string-match (car entry) buffer-file-name)
  12709. (throw 'exit (cdr entry))))
  12710. nil)))))
  12711. ;;; Refiling
  12712. (defvar org-refile-target-table nil
  12713. "The list of refile targets, created by `org-refile'.")
  12714. (defvar org-agenda-new-buffers nil
  12715. "Buffers created to visit agenda files.")
  12716. (defun org-get-refile-targets (&optional default-buffer)
  12717. "Produce a table with refile targets."
  12718. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12719. targets txt re files f desc descre)
  12720. (with-current-buffer (or default-buffer (current-buffer))
  12721. (while (setq entry (pop entries))
  12722. (setq files (car entry) desc (cdr entry))
  12723. (cond
  12724. ((null files) (setq files (list (current-buffer))))
  12725. ((eq files 'org-agenda-files)
  12726. (setq files (org-agenda-files 'unrestricted)))
  12727. ((and (symbolp files) (fboundp files))
  12728. (setq files (funcall files)))
  12729. ((and (symbolp files) (boundp files))
  12730. (setq files (symbol-value files))))
  12731. (if (stringp files) (setq files (list files)))
  12732. (cond
  12733. ((eq (car desc) :tag)
  12734. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12735. ((eq (car desc) :todo)
  12736. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12737. ((eq (car desc) :regexp)
  12738. (setq descre (cdr desc)))
  12739. ((eq (car desc) :level)
  12740. (setq descre (concat "^\\*\\{" (number-to-string
  12741. (if org-odd-levels-only
  12742. (1- (* 2 (cdr desc)))
  12743. (cdr desc)))
  12744. "\\}[ \t]")))
  12745. ((eq (car desc) :maxlevel)
  12746. (setq descre (concat "^\\*\\{1," (number-to-string
  12747. (if org-odd-levels-only
  12748. (1- (* 2 (cdr desc)))
  12749. (cdr desc)))
  12750. "\\}[ \t]")))
  12751. (t (error "Bad refiling target description %s" desc)))
  12752. (while (setq f (pop files))
  12753. (save-excursion
  12754. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12755. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12756. (save-excursion
  12757. (save-restriction
  12758. (widen)
  12759. (goto-char (point-min))
  12760. (while (re-search-forward descre nil t)
  12761. (goto-char (point-at-bol))
  12762. (when (looking-at org-complex-heading-regexp)
  12763. (setq txt (match-string 4)
  12764. re (concat "^" (regexp-quote
  12765. (buffer-substring (match-beginning 1)
  12766. (match-end 4)))))
  12767. (if (match-end 5) (setq re (concat re "[ \t]+"
  12768. (regexp-quote
  12769. (match-string 5)))))
  12770. (setq re (concat re "[ \t]*$"))
  12771. (when org-refile-use-outline-path
  12772. (setq txt (mapconcat 'identity
  12773. (append
  12774. (if (eq org-refile-use-outline-path 'file)
  12775. (list (file-name-nondirectory
  12776. (buffer-file-name (buffer-base-buffer))))
  12777. (if (eq org-refile-use-outline-path 'full-file-path)
  12778. (list (buffer-file-name (buffer-base-buffer)))))
  12779. (org-get-outline-path)
  12780. (list txt))
  12781. "/")))
  12782. (push (list txt f re (point)) targets))
  12783. (goto-char (point-at-eol))))))))
  12784. (nreverse targets))))
  12785. (defun org-get-outline-path ()
  12786. "Return the outline path to the current entry, as a list."
  12787. (let (rtn)
  12788. (save-excursion
  12789. (while (org-up-heading-safe)
  12790. (when (looking-at org-complex-heading-regexp)
  12791. (push (org-match-string-no-properties 4) rtn)))
  12792. rtn)))
  12793. (defvar org-refile-history nil
  12794. "History for refiling operations.")
  12795. (defun org-refile (&optional goto default-buffer)
  12796. "Move the entry at point to another heading.
  12797. The list of target headings is compiled using the information in
  12798. `org-refile-targets', which see. This list is created upon first use, and
  12799. you can update it by calling this command with a double prefix (`C-u C-u').
  12800. FIXME: Can we find a better way of updating?
  12801. At the target location, the entry is filed as a subitem of the target heading.
  12802. Depending on `org-reverse-note-order', the new subitem will either be the
  12803. first of the last subitem.
  12804. With prefix arg GOTO, the command will only visit the target location,
  12805. not actually move anything.
  12806. With a double prefix `C-c C-c', go to the location where the last refiling
  12807. operation has put the subtree.
  12808. With a double prefix argument, the command can be used to jump to any
  12809. heading in the current buffer."
  12810. (interactive "P")
  12811. (let* ((cbuf (current-buffer))
  12812. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12813. (fname (and filename (file-truename filename)))
  12814. pos it nbuf file re level reversed)
  12815. (if (equal goto '(16))
  12816. (org-refile-goto-last-stored)
  12817. (when (setq it (org-refile-get-location
  12818. (if goto "Goto: " "Refile to: ") default-buffer))
  12819. (setq file (nth 1 it)
  12820. re (nth 2 it)
  12821. pos (nth 3 it))
  12822. (setq nbuf (or (find-buffer-visiting file)
  12823. (find-file-noselect file)))
  12824. (if goto
  12825. (progn
  12826. (switch-to-buffer nbuf)
  12827. (goto-char pos)
  12828. (org-show-context 'org-goto))
  12829. (org-copy-special)
  12830. (save-excursion
  12831. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12832. (find-file-noselect file))))
  12833. (setq reversed (org-notes-order-reversed-p))
  12834. (save-excursion
  12835. (save-restriction
  12836. (widen)
  12837. (goto-char pos)
  12838. (looking-at outline-regexp)
  12839. (setq level (org-get-legal-level (funcall outline-level) 1))
  12840. (goto-char
  12841. (if reversed
  12842. (outline-next-heading)
  12843. (or (save-excursion (outline-get-next-sibling))
  12844. (org-end-of-subtree t t)
  12845. (point-max))))
  12846. (bookmark-set "org-refile-last-stored")
  12847. (org-paste-subtree level))))
  12848. (org-cut-special)
  12849. (message "Entry refiled to \"%s\"" (car it)))))))
  12850. (defun org-refile-goto-last-stored ()
  12851. "Go to the location where the last refile was stored."
  12852. (interactive)
  12853. (bookmark-jump "org-refile-last-stored")
  12854. (message "This is the location of the last refile"))
  12855. (defun org-refile-get-location (&optional prompt default-buffer)
  12856. "Prompt the user for a refile location, using PROMPT."
  12857. (let ((org-refile-targets org-refile-targets)
  12858. (org-refile-use-outline-path org-refile-use-outline-path))
  12859. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12860. (unless org-refile-target-table
  12861. (error "No refile targets"))
  12862. (let* ((cbuf (current-buffer))
  12863. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12864. (fname (and filename (file-truename filename)))
  12865. (tbl (mapcar
  12866. (lambda (x)
  12867. (if (not (equal fname (file-truename (nth 1 x))))
  12868. (cons (concat (car x) " (" (file-name-nondirectory
  12869. (nth 1 x)) ")")
  12870. (cdr x))
  12871. x))
  12872. org-refile-target-table))
  12873. (completion-ignore-case t)
  12874. pos it nbuf file re level reversed)
  12875. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12876. tbl)))
  12877. ;;;; Dynamic blocks
  12878. (defun org-find-dblock (name)
  12879. "Find the first dynamic block with name NAME in the buffer.
  12880. If not found, stay at current position and return nil."
  12881. (let (pos)
  12882. (save-excursion
  12883. (goto-char (point-min))
  12884. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12885. nil t)
  12886. (match-beginning 0))))
  12887. (if pos (goto-char pos))
  12888. pos))
  12889. (defconst org-dblock-start-re
  12890. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12891. "Matches the startline of a dynamic block, with parameters.")
  12892. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12893. "Matches the end of a dyhamic block.")
  12894. (defun org-create-dblock (plist)
  12895. "Create a dynamic block section, with parameters taken from PLIST.
  12896. PLIST must containe a :name entry which is used as name of the block."
  12897. (unless (bolp) (newline))
  12898. (let ((name (plist-get plist :name)))
  12899. (insert "#+BEGIN: " name)
  12900. (while plist
  12901. (if (eq (car plist) :name)
  12902. (setq plist (cddr plist))
  12903. (insert " " (prin1-to-string (pop plist)))))
  12904. (insert "\n\n#+END:\n")
  12905. (beginning-of-line -2)))
  12906. (defun org-prepare-dblock ()
  12907. "Prepare dynamic block for refresh.
  12908. This empties the block, puts the cursor at the insert position and returns
  12909. the property list including an extra property :name with the block name."
  12910. (unless (looking-at org-dblock-start-re)
  12911. (error "Not at a dynamic block"))
  12912. (let* ((begdel (1+ (match-end 0)))
  12913. (name (org-no-properties (match-string 1)))
  12914. (params (append (list :name name)
  12915. (read (concat "(" (match-string 3) ")")))))
  12916. (unless (re-search-forward org-dblock-end-re nil t)
  12917. (error "Dynamic block not terminated"))
  12918. (delete-region begdel (match-beginning 0))
  12919. (goto-char begdel)
  12920. (open-line 1)
  12921. params))
  12922. (defun org-map-dblocks (&optional command)
  12923. "Apply COMMAND to all dynamic blocks in the current buffer.
  12924. If COMMAND is not given, use `org-update-dblock'."
  12925. (let ((cmd (or command 'org-update-dblock))
  12926. pos)
  12927. (save-excursion
  12928. (goto-char (point-min))
  12929. (while (re-search-forward org-dblock-start-re nil t)
  12930. (goto-char (setq pos (match-beginning 0)))
  12931. (condition-case nil
  12932. (funcall cmd)
  12933. (error (message "Error during update of dynamic block")))
  12934. (goto-char pos)
  12935. (unless (re-search-forward org-dblock-end-re nil t)
  12936. (error "Dynamic block not terminated"))))))
  12937. (defun org-dblock-update (&optional arg)
  12938. "User command for updating dynamic blocks.
  12939. Update the dynamic block at point. With prefix ARG, update all dynamic
  12940. blocks in the buffer."
  12941. (interactive "P")
  12942. (if arg
  12943. (org-update-all-dblocks)
  12944. (or (looking-at org-dblock-start-re)
  12945. (org-beginning-of-dblock))
  12946. (org-update-dblock)))
  12947. (defun org-update-dblock ()
  12948. "Update the dynamic block at point
  12949. This means to empty the block, parse for parameters and then call
  12950. the correct writing function."
  12951. (save-window-excursion
  12952. (let* ((pos (point))
  12953. (line (org-current-line))
  12954. (params (org-prepare-dblock))
  12955. (name (plist-get params :name))
  12956. (cmd (intern (concat "org-dblock-write:" name))))
  12957. (message "Updating dynamic block `%s' at line %d..." name line)
  12958. (funcall cmd params)
  12959. (message "Updating dynamic block `%s' at line %d...done" name line)
  12960. (goto-char pos))))
  12961. (defun org-beginning-of-dblock ()
  12962. "Find the beginning of the dynamic block at point.
  12963. Error if there is no scuh block at point."
  12964. (let ((pos (point))
  12965. beg)
  12966. (end-of-line 1)
  12967. (if (and (re-search-backward org-dblock-start-re nil t)
  12968. (setq beg (match-beginning 0))
  12969. (re-search-forward org-dblock-end-re nil t)
  12970. (> (match-end 0) pos))
  12971. (goto-char beg)
  12972. (goto-char pos)
  12973. (error "Not in a dynamic block"))))
  12974. (defun org-update-all-dblocks ()
  12975. "Update all dynamic blocks in the buffer.
  12976. This function can be used in a hook."
  12977. (when (org-mode-p)
  12978. (org-map-dblocks 'org-update-dblock)))
  12979. ;;;; Completion
  12980. (defconst org-additional-option-like-keywords
  12981. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12982. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  12983. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  12984. (defun org-complete (&optional arg)
  12985. "Perform completion on word at point.
  12986. At the beginning of a headline, this completes TODO keywords as given in
  12987. `org-todo-keywords'.
  12988. If the current word is preceded by a backslash, completes the TeX symbols
  12989. that are supported for HTML support.
  12990. If the current word is preceded by \"#+\", completes special words for
  12991. setting file options.
  12992. In the line after \"#+STARTUP:, complete valid keywords.\"
  12993. At all other locations, this simply calls the value of
  12994. `org-completion-fallback-command'."
  12995. (interactive "P")
  12996. (org-without-partial-completion
  12997. (catch 'exit
  12998. (let* ((end (point))
  12999. (beg1 (save-excursion
  13000. (skip-chars-backward (org-re "[:alnum:]_@"))
  13001. (point)))
  13002. (beg (save-excursion
  13003. (skip-chars-backward "a-zA-Z0-9_:$")
  13004. (point)))
  13005. (confirm (lambda (x) (stringp (car x))))
  13006. (searchhead (equal (char-before beg) ?*))
  13007. (tag (and (equal (char-before beg1) ?:)
  13008. (equal (char-after (point-at-bol)) ?*)))
  13009. (prop (and (equal (char-before beg1) ?:)
  13010. (not (equal (char-after (point-at-bol)) ?*))))
  13011. (texp (equal (char-before beg) ?\\))
  13012. (link (equal (char-before beg) ?\[))
  13013. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13014. beg)
  13015. "#+"))
  13016. (startup (string-match "^#\\+STARTUP:.*"
  13017. (buffer-substring (point-at-bol) (point))))
  13018. (completion-ignore-case opt)
  13019. (type nil)
  13020. (tbl nil)
  13021. (table (cond
  13022. (opt
  13023. (setq type :opt)
  13024. (append
  13025. (mapcar
  13026. (lambda (x)
  13027. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13028. (cons (match-string 2 x) (match-string 1 x)))
  13029. (org-split-string (org-get-current-options) "\n"))
  13030. (mapcar 'list org-additional-option-like-keywords)))
  13031. (startup
  13032. (setq type :startup)
  13033. org-startup-options)
  13034. (link (append org-link-abbrev-alist-local
  13035. org-link-abbrev-alist))
  13036. (texp
  13037. (setq type :tex)
  13038. org-html-entities)
  13039. ((string-match "\\`\\*+[ \t]+\\'"
  13040. (buffer-substring (point-at-bol) beg))
  13041. (setq type :todo)
  13042. (mapcar 'list org-todo-keywords-1))
  13043. (searchhead
  13044. (setq type :searchhead)
  13045. (save-excursion
  13046. (goto-char (point-min))
  13047. (while (re-search-forward org-todo-line-regexp nil t)
  13048. (push (list
  13049. (org-make-org-heading-search-string
  13050. (match-string 3) t))
  13051. tbl)))
  13052. tbl)
  13053. (tag (setq type :tag beg beg1)
  13054. (or org-tag-alist (org-get-buffer-tags)))
  13055. (prop (setq type :prop beg beg1)
  13056. (mapcar 'list (org-buffer-property-keys nil t t)))
  13057. (t (progn
  13058. (call-interactively org-completion-fallback-command)
  13059. (throw 'exit nil)))))
  13060. (pattern (buffer-substring-no-properties beg end))
  13061. (completion (try-completion pattern table confirm)))
  13062. (cond ((eq completion t)
  13063. (if (not (assoc (upcase pattern) table))
  13064. (message "Already complete")
  13065. (if (equal type :opt)
  13066. (insert (substring (cdr (assoc (upcase pattern) table))
  13067. (length pattern)))
  13068. (if (memq type '(:tag :prop)) (insert ":")))))
  13069. ((null completion)
  13070. (message "Can't find completion for \"%s\"" pattern)
  13071. (ding))
  13072. ((not (string= pattern completion))
  13073. (delete-region beg end)
  13074. (if (string-match " +$" completion)
  13075. (setq completion (replace-match "" t t completion)))
  13076. (insert completion)
  13077. (if (get-buffer-window "*Completions*")
  13078. (delete-window (get-buffer-window "*Completions*")))
  13079. (if (assoc completion table)
  13080. (if (eq type :todo) (insert " ")
  13081. (if (memq type '(:tag :prop)) (insert ":"))))
  13082. (if (and (equal type :opt) (assoc completion table))
  13083. (message "%s" (substitute-command-keys
  13084. "Press \\[org-complete] again to insert example settings"))))
  13085. (t
  13086. (message "Making completion list...")
  13087. (let ((list (sort (all-completions pattern table confirm)
  13088. 'string<)))
  13089. (with-output-to-temp-buffer "*Completions*"
  13090. (condition-case nil
  13091. ;; Protection needed for XEmacs and emacs 21
  13092. (display-completion-list list pattern)
  13093. (error (display-completion-list list)))))
  13094. (message "Making completion list...%s" "done")))))))
  13095. ;;;; TODO, DEADLINE, Comments
  13096. (defun org-toggle-comment ()
  13097. "Change the COMMENT state of an entry."
  13098. (interactive)
  13099. (save-excursion
  13100. (org-back-to-heading)
  13101. (let (case-fold-search)
  13102. (if (looking-at (concat outline-regexp
  13103. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13104. (replace-match "" t t nil 1)
  13105. (if (looking-at outline-regexp)
  13106. (progn
  13107. (goto-char (match-end 0))
  13108. (insert org-comment-string " ")))))))
  13109. (defvar org-last-todo-state-is-todo nil
  13110. "This is non-nil when the last TODO state change led to a TODO state.
  13111. If the last change removed the TODO tag or switched to DONE, then
  13112. this is nil.")
  13113. (defvar org-setting-tags nil) ; dynamically skiped
  13114. ;; FIXME: better place
  13115. (defun org-property-or-variable-value (var &optional inherit)
  13116. "Check if there is a property fixing the value of VAR.
  13117. If yes, return this value. If not, return the current value of the variable."
  13118. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13119. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13120. (read prop)
  13121. (symbol-value var))))
  13122. (defun org-parse-local-options (string var)
  13123. "Parse STRING for startup setting relevant for variable VAR."
  13124. (let ((rtn (symbol-value var))
  13125. e opts)
  13126. (save-match-data
  13127. (if (or (not string) (not (string-match "\\S-" string)))
  13128. rtn
  13129. (setq opts (delq nil (mapcar (lambda (x)
  13130. (setq e (assoc x org-startup-options))
  13131. (if (eq (nth 1 e) var) e nil))
  13132. (org-split-string string "[ \t]+"))))
  13133. (if (not opts)
  13134. rtn
  13135. (setq rtn nil)
  13136. (while (setq e (pop opts))
  13137. (if (not (nth 3 e))
  13138. (setq rtn (nth 2 e))
  13139. (if (not (listp rtn)) (setq rtn nil))
  13140. (push (nth 2 e) rtn)))
  13141. rtn)))))
  13142. (defvar org-blocker-hook nil
  13143. "Hook for functions that are allowed to block a state change.
  13144. Each function gets as its single argument a property list, see
  13145. `org-trigger-hook' for more information about this list.
  13146. If any of the functions in this hook returns nil, the state change
  13147. is blocked.")
  13148. (defvar org-trigger-hook nil
  13149. "Hook for functions that are triggered by a state change.
  13150. Each function gets as its single argument a property list with at least
  13151. the following elements:
  13152. (:type type-of-change :position pos-at-entry-start
  13153. :from old-state :to new-state)
  13154. Depending on the type, more properties may be present.
  13155. This mechanism is currently implemented for:
  13156. TODO state changes
  13157. ------------------
  13158. :type todo-state-change
  13159. :from previous state (keyword as a string), or nil
  13160. :to new state (keyword as a string), or nil")
  13161. (defun org-todo (&optional arg)
  13162. "Change the TODO state of an item.
  13163. The state of an item is given by a keyword at the start of the heading,
  13164. like
  13165. *** TODO Write paper
  13166. *** DONE Call mom
  13167. The different keywords are specified in the variable `org-todo-keywords'.
  13168. By default the available states are \"TODO\" and \"DONE\".
  13169. So for this example: when the item starts with TODO, it is changed to DONE.
  13170. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13171. DONE are present, add TODO at the beginning of the heading.
  13172. With C-u prefix arg, use completion to determine the new state.
  13173. With numeric prefix arg, switch to that state.
  13174. For calling through lisp, arg is also interpreted in the following way:
  13175. 'none -> empty state
  13176. \"\"(empty string) -> switch to empty state
  13177. 'done -> switch to DONE
  13178. 'nextset -> switch to the next set of keywords
  13179. 'previousset -> switch to the previous set of keywords
  13180. \"WAITING\" -> switch to the specified keyword, but only if it
  13181. really is a member of `org-todo-keywords'."
  13182. (interactive "P")
  13183. (save-excursion
  13184. (catch 'exit
  13185. (org-back-to-heading)
  13186. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13187. (or (looking-at (concat " +" org-todo-regexp " *"))
  13188. (looking-at " *"))
  13189. (let* ((match-data (match-data))
  13190. (startpos (point-at-bol))
  13191. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13192. (org-log-done (org-parse-local-options logging 'org-log-done))
  13193. (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
  13194. (this (match-string 1))
  13195. (hl-pos (match-beginning 0))
  13196. (head (org-get-todo-sequence-head this))
  13197. (ass (assoc head org-todo-kwd-alist))
  13198. (interpret (nth 1 ass))
  13199. (done-word (nth 3 ass))
  13200. (final-done-word (nth 4 ass))
  13201. (last-state (or this ""))
  13202. (completion-ignore-case t)
  13203. (member (member this org-todo-keywords-1))
  13204. (tail (cdr member))
  13205. (state (cond
  13206. ((and org-todo-key-trigger
  13207. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13208. (and (not arg) org-use-fast-todo-selection
  13209. (not (eq org-use-fast-todo-selection 'prefix)))))
  13210. ;; Use fast selection
  13211. (org-fast-todo-selection))
  13212. ((and (equal arg '(4))
  13213. (or (not org-use-fast-todo-selection)
  13214. (not org-todo-key-trigger)))
  13215. ;; Read a state with completion
  13216. (completing-read "State: " (mapcar (lambda(x) (list x))
  13217. org-todo-keywords-1)
  13218. nil t))
  13219. ((eq arg 'right)
  13220. (if this
  13221. (if tail (car tail) nil)
  13222. (car org-todo-keywords-1)))
  13223. ((eq arg 'left)
  13224. (if (equal member org-todo-keywords-1)
  13225. nil
  13226. (if this
  13227. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13228. org-todo-keywords-1)
  13229. (org-last org-todo-keywords-1))))
  13230. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13231. (setq arg nil))) ; hack to fall back to cycling
  13232. (arg
  13233. ;; user or caller requests a specific state
  13234. (cond
  13235. ((equal arg "") nil)
  13236. ((eq arg 'none) nil)
  13237. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13238. ((eq arg 'nextset)
  13239. (or (car (cdr (member head org-todo-heads)))
  13240. (car org-todo-heads)))
  13241. ((eq arg 'previousset)
  13242. (let ((org-todo-heads (reverse org-todo-heads)))
  13243. (or (car (cdr (member head org-todo-heads)))
  13244. (car org-todo-heads))))
  13245. ((car (member arg org-todo-keywords-1)))
  13246. ((nth (1- (prefix-numeric-value arg))
  13247. org-todo-keywords-1))))
  13248. ((null member) (or head (car org-todo-keywords-1)))
  13249. ((equal this final-done-word) nil) ;; -> make empty
  13250. ((null tail) nil) ;; -> first entry
  13251. ((eq interpret 'sequence)
  13252. (car tail))
  13253. ((memq interpret '(type priority))
  13254. (if (eq this-command last-command)
  13255. (car tail)
  13256. (if (> (length tail) 0)
  13257. (or done-word (car org-done-keywords))
  13258. nil)))
  13259. (t nil)))
  13260. (next (if state (concat " " state " ") " "))
  13261. (change-plist (list :type 'todo-state-change :from this :to state
  13262. :position startpos))
  13263. dostates)
  13264. (when org-blocker-hook
  13265. (unless (save-excursion
  13266. (save-match-data
  13267. (run-hook-with-args-until-failure
  13268. 'org-blocker-hook change-plist)))
  13269. (if (interactive-p)
  13270. (error "TODO state change from %s to %s blocked" this state)
  13271. ;; fail silently
  13272. (message "TODO state change from %s to %s blocked" this state)
  13273. (throw 'exit nil))))
  13274. (store-match-data match-data)
  13275. (replace-match next t t)
  13276. (unless (pos-visible-in-window-p hl-pos)
  13277. (message "TODO state changed to %s" (org-trim next)))
  13278. (unless head
  13279. (setq head (org-get-todo-sequence-head state)
  13280. ass (assoc head org-todo-kwd-alist)
  13281. interpret (nth 1 ass)
  13282. done-word (nth 3 ass)
  13283. final-done-word (nth 4 ass)))
  13284. (when (memq arg '(nextset previousset))
  13285. (message "Keyword-Set %d/%d: %s"
  13286. (- (length org-todo-sets) -1
  13287. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13288. (length org-todo-sets)
  13289. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13290. (setq org-last-todo-state-is-todo
  13291. (not (member state org-done-keywords)))
  13292. (when (and org-log-done (not (memq arg '(nextset previousset))))
  13293. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  13294. (or (not org-todo-log-states)
  13295. (member state org-todo-log-states))))
  13296. (cond
  13297. ((and state (member state org-not-done-keywords)
  13298. (not (member this org-not-done-keywords)))
  13299. ;; This is now a todo state and was not one before
  13300. ;; Remove any CLOSED timestamp, and possibly log the state change
  13301. (org-add-planning-info nil nil 'closed)
  13302. (and dostates (org-add-log-maybe 'state state 'findpos)))
  13303. ((and state dostates)
  13304. ;; This is a non-nil state, and we need to log it
  13305. (org-add-log-maybe 'state state 'findpos))
  13306. ((and (member state org-done-keywords)
  13307. (not (member this org-done-keywords)))
  13308. ;; It is now done, and it was not done before
  13309. (org-add-planning-info 'closed (org-current-time))
  13310. (org-add-log-maybe 'done state 'findpos))))
  13311. ;; Fixup tag positioning
  13312. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13313. (run-hooks 'org-after-todo-state-change-hook)
  13314. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  13315. (if (and arg (not (member state org-done-keywords)))
  13316. (setq head (org-get-todo-sequence-head state)))
  13317. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13318. ;; Fixup cursor location if close to the keyword
  13319. (if (and (outline-on-heading-p)
  13320. (not (bolp))
  13321. (save-excursion (beginning-of-line 1)
  13322. (looking-at org-todo-line-regexp))
  13323. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13324. (progn
  13325. (goto-char (or (match-end 2) (match-end 1)))
  13326. (just-one-space)))
  13327. (when org-trigger-hook
  13328. (save-excursion
  13329. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13330. (defun org-get-todo-sequence-head (kwd)
  13331. "Return the head of the TODO sequence to which KWD belongs.
  13332. If KWD is not set, check if there is a text property remembering the
  13333. right sequence."
  13334. (let (p)
  13335. (cond
  13336. ((not kwd)
  13337. (or (get-text-property (point-at-bol) 'org-todo-head)
  13338. (progn
  13339. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13340. nil (point-at-eol)))
  13341. (get-text-property p 'org-todo-head))))
  13342. ((not (member kwd org-todo-keywords-1))
  13343. (car org-todo-keywords-1))
  13344. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13345. (defun org-fast-todo-selection ()
  13346. "Fast TODO keyword selection with single keys.
  13347. Returns the new TODO keyword, or nil if no state change should occur."
  13348. (let* ((fulltable org-todo-key-alist)
  13349. (done-keywords org-done-keywords) ;; needed for the faces.
  13350. (maxlen (apply 'max (mapcar
  13351. (lambda (x)
  13352. (if (stringp (car x)) (string-width (car x)) 0))
  13353. fulltable)))
  13354. (expert nil)
  13355. (fwidth (+ maxlen 3 1 3))
  13356. (ncol (/ (- (window-width) 4) fwidth))
  13357. tg cnt e c tbl
  13358. groups ingroup)
  13359. (save-window-excursion
  13360. (if expert
  13361. (set-buffer (get-buffer-create " *Org todo*"))
  13362. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13363. (erase-buffer)
  13364. (org-set-local 'org-done-keywords done-keywords)
  13365. (setq tbl fulltable cnt 0)
  13366. (while (setq e (pop tbl))
  13367. (cond
  13368. ((equal e '(:startgroup))
  13369. (push '() groups) (setq ingroup t)
  13370. (when (not (= cnt 0))
  13371. (setq cnt 0)
  13372. (insert "\n"))
  13373. (insert "{ "))
  13374. ((equal e '(:endgroup))
  13375. (setq ingroup nil cnt 0)
  13376. (insert "}\n"))
  13377. (t
  13378. (setq tg (car e) c (cdr e))
  13379. (if ingroup (push tg (car groups)))
  13380. (setq tg (org-add-props tg nil 'face
  13381. (org-get-todo-face tg)))
  13382. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13383. (insert "[" c "] " tg (make-string
  13384. (- fwidth 4 (length tg)) ?\ ))
  13385. (when (= (setq cnt (1+ cnt)) ncol)
  13386. (insert "\n")
  13387. (if ingroup (insert " "))
  13388. (setq cnt 0)))))
  13389. (insert "\n")
  13390. (goto-char (point-min))
  13391. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13392. (fit-window-to-buffer))
  13393. (message "[a-z..]:Set [SPC]:clear")
  13394. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13395. (cond
  13396. ((or (= c ?\C-g)
  13397. (and (= c ?q) (not (rassoc c fulltable))))
  13398. (setq quit-flag t))
  13399. ((= c ?\ ) nil)
  13400. ((setq e (rassoc c fulltable) tg (car e))
  13401. tg)
  13402. (t (setq quit-flag t))))))
  13403. (defun org-get-repeat ()
  13404. "Check if tere is a deadline/schedule with repeater in this entry."
  13405. (save-match-data
  13406. (save-excursion
  13407. (org-back-to-heading t)
  13408. (if (re-search-forward
  13409. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13410. (match-string 1)))))
  13411. (defvar org-last-changed-timestamp)
  13412. (defvar org-log-post-message)
  13413. (defun org-auto-repeat-maybe ()
  13414. "Check if the current headline contains a repeated deadline/schedule.
  13415. If yes, set TODO state back to what it was and change the base date
  13416. of repeating deadline/scheduled time stamps to new date.
  13417. This function should be run in the `org-after-todo-state-change-hook'."
  13418. ;; last-state is dynamically scoped into this function
  13419. (let* ((repeat (org-get-repeat))
  13420. (aa (assoc last-state org-todo-kwd-alist))
  13421. (interpret (nth 1 aa))
  13422. (head (nth 2 aa))
  13423. (done-word (nth 3 aa))
  13424. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13425. (msg "Entry repeats: ")
  13426. (org-log-done)
  13427. re type n what ts)
  13428. (when repeat
  13429. (org-todo (if (eq interpret 'type) last-state head))
  13430. (when (and org-log-repeat
  13431. (not (memq 'org-add-log-note
  13432. (default-value 'post-command-hook))))
  13433. ;; Make sure a note is taken
  13434. (let ((org-log-done '(done)))
  13435. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  13436. 'findpos)))
  13437. (org-back-to-heading t)
  13438. (org-add-planning-info nil nil 'closed)
  13439. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13440. org-deadline-time-regexp "\\)\\|\\("
  13441. org-ts-regexp "\\)"))
  13442. (while (re-search-forward
  13443. re (save-excursion (outline-next-heading) (point)) t)
  13444. (setq type (if (match-end 1) org-scheduled-string
  13445. (if (match-end 3) org-deadline-string "Plain:"))
  13446. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13447. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13448. (setq n (string-to-number (match-string 1 ts))
  13449. what (match-string 2 ts))
  13450. (if (equal what "w") (setq n (* n 7) what "d"))
  13451. (org-timestamp-change n (cdr (assoc what whata)))
  13452. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13453. (setq org-log-post-message msg)
  13454. (message "%s" msg))))
  13455. (defun org-show-todo-tree (arg)
  13456. "Make a compact tree which shows all headlines marked with TODO.
  13457. The tree will show the lines where the regexp matches, and all higher
  13458. headlines above the match.
  13459. With \\[universal-argument] prefix, also show the DONE entries.
  13460. With a numeric prefix N, construct a sparse tree for the Nth element
  13461. of `org-todo-keywords-1'."
  13462. (interactive "P")
  13463. (let ((case-fold-search nil)
  13464. (kwd-re
  13465. (cond ((null arg) org-not-done-regexp)
  13466. ((equal arg '(4))
  13467. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13468. (mapcar 'list org-todo-keywords-1))))
  13469. (concat "\\("
  13470. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13471. "\\)\\>")))
  13472. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13473. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13474. org-todo-keywords-1)))
  13475. (t (error "Invalid prefix argument: %s" arg)))))
  13476. (message "%d TODO entries found"
  13477. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13478. (defun org-deadline (&optional remove)
  13479. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13480. With argument REMOVE, remove any deadline from the item."
  13481. (interactive "P")
  13482. (if remove
  13483. (progn
  13484. (org-remove-timestamp-with-keyword org-deadline-string)
  13485. (message "Item no longer has a deadline."))
  13486. (org-add-planning-info 'deadline nil 'closed)))
  13487. (defun org-schedule (&optional remove)
  13488. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13489. With argument REMOVE, remove any scheduling date from the item."
  13490. (interactive "P")
  13491. (if remove
  13492. (progn
  13493. (org-remove-timestamp-with-keyword org-scheduled-string)
  13494. (message "Item is no longer scheduled."))
  13495. (org-add-planning-info 'scheduled nil 'closed)))
  13496. (defun org-remove-timestamp-with-keyword (keyword)
  13497. "Remove all time stamps with KEYWORD in the current entry."
  13498. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13499. beg)
  13500. (save-excursion
  13501. (org-back-to-heading t)
  13502. (setq beg (point))
  13503. (org-end-of-subtree t t)
  13504. (while (re-search-backward re beg t)
  13505. (replace-match "")
  13506. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13507. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13508. (defun org-add-planning-info (what &optional time &rest remove)
  13509. "Insert new timestamp with keyword in the line directly after the headline.
  13510. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13511. If non is given, the user is prompted for a date.
  13512. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13513. be removed."
  13514. (interactive)
  13515. (let (org-time-was-given org-end-time-was-given)
  13516. (when what (setq time (or time (org-read-date nil 'to-time))))
  13517. (when (and org-insert-labeled-timestamps-at-point
  13518. (member what '(scheduled deadline)))
  13519. (insert
  13520. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13521. (org-insert-time-stamp time org-time-was-given
  13522. nil nil nil (list org-end-time-was-given))
  13523. (setq what nil))
  13524. (save-excursion
  13525. (save-restriction
  13526. (let (col list elt ts buffer-invisibility-spec)
  13527. (org-back-to-heading t)
  13528. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13529. (goto-char (match-end 1))
  13530. (setq col (current-column))
  13531. (goto-char (match-end 0))
  13532. (if (eobp) (insert "\n") (forward-char 1))
  13533. (if (and (not (looking-at outline-regexp))
  13534. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13535. "[^\r\n]*"))
  13536. (not (equal (match-string 1) org-clock-string)))
  13537. (narrow-to-region (match-beginning 0) (match-end 0))
  13538. (insert-before-markers "\n")
  13539. (backward-char 1)
  13540. (narrow-to-region (point) (point))
  13541. (indent-to-column col))
  13542. ;; Check if we have to remove something.
  13543. (setq list (cons what remove))
  13544. (while list
  13545. (setq elt (pop list))
  13546. (goto-char (point-min))
  13547. (when (or (and (eq elt 'scheduled)
  13548. (re-search-forward org-scheduled-time-regexp nil t))
  13549. (and (eq elt 'deadline)
  13550. (re-search-forward org-deadline-time-regexp nil t))
  13551. (and (eq elt 'closed)
  13552. (re-search-forward org-closed-time-regexp nil t)))
  13553. (replace-match "")
  13554. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13555. (if (looking-at " +") (replace-match ""))))
  13556. (goto-char (point-max))
  13557. (when what
  13558. (insert
  13559. (if (not (equal (char-before) ?\ )) " " "")
  13560. (cond ((eq what 'scheduled) org-scheduled-string)
  13561. ((eq what 'deadline) org-deadline-string)
  13562. ((eq what 'closed) org-closed-string))
  13563. " ")
  13564. (setq ts (org-insert-time-stamp
  13565. time
  13566. (or org-time-was-given
  13567. (and (eq what 'closed) org-log-done-with-time))
  13568. (eq what 'closed)
  13569. nil nil (list org-end-time-was-given)))
  13570. (end-of-line 1))
  13571. (goto-char (point-min))
  13572. (widen)
  13573. (if (looking-at "[ \t]+\r?\n")
  13574. (replace-match ""))
  13575. ts)))))
  13576. (defvar org-log-note-marker (make-marker))
  13577. (defvar org-log-note-purpose nil)
  13578. (defvar org-log-note-state nil)
  13579. (defvar org-log-note-window-configuration nil)
  13580. (defvar org-log-note-return-to (make-marker))
  13581. (defvar org-log-post-message nil
  13582. "Message to be displayed after a log note has been stored.
  13583. The auto-repeater uses this.")
  13584. (defun org-add-log-maybe (&optional purpose state findpos)
  13585. "Set up the post command hook to take a note."
  13586. (save-excursion
  13587. (when (and (listp org-log-done)
  13588. (memq purpose org-log-done))
  13589. (when findpos
  13590. (org-back-to-heading t)
  13591. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13592. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13593. "[^\r\n]*\\)?"))
  13594. (goto-char (match-end 0))
  13595. (unless org-log-states-order-reversed
  13596. (and (= (char-after) ?\n) (forward-char 1))
  13597. (org-skip-over-state-notes)
  13598. (skip-chars-backward " \t\n\r")))
  13599. (move-marker org-log-note-marker (point))
  13600. (setq org-log-note-purpose purpose)
  13601. (setq org-log-note-state state)
  13602. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  13603. (defun org-skip-over-state-notes ()
  13604. "Skip past the list of State notes in an entry."
  13605. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13606. (while (looking-at "[ \t]*- State")
  13607. (condition-case nil
  13608. (org-next-item)
  13609. (error (org-end-of-item)))))
  13610. (defun org-add-log-note (&optional purpose)
  13611. "Pop up a window for taking a note, and add this note later at point."
  13612. (remove-hook 'post-command-hook 'org-add-log-note)
  13613. (setq org-log-note-window-configuration (current-window-configuration))
  13614. (delete-other-windows)
  13615. (move-marker org-log-note-return-to (point))
  13616. (switch-to-buffer (marker-buffer org-log-note-marker))
  13617. (goto-char org-log-note-marker)
  13618. (org-switch-to-buffer-other-window "*Org Note*")
  13619. (erase-buffer)
  13620. (let ((org-inhibit-startup t)) (org-mode))
  13621. (insert (format "# Insert note for %s.
  13622. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13623. (cond
  13624. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13625. ((eq org-log-note-purpose 'done) "closed todo item")
  13626. ((eq org-log-note-purpose 'state)
  13627. (format "state change to \"%s\"" org-log-note-state))
  13628. (t (error "This should not happen")))))
  13629. (org-set-local 'org-finish-function 'org-store-log-note))
  13630. (defun org-store-log-note ()
  13631. "Finish taking a log note, and insert it to where it belongs."
  13632. (let ((txt (buffer-string))
  13633. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13634. lines ind)
  13635. (kill-buffer (current-buffer))
  13636. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13637. (setq txt (replace-match "" t t txt)))
  13638. (if (string-match "\\s-+\\'" txt)
  13639. (setq txt (replace-match "" t t txt)))
  13640. (setq lines (org-split-string txt "\n"))
  13641. (when (and note (string-match "\\S-" note))
  13642. (setq note
  13643. (org-replace-escapes
  13644. note
  13645. (list (cons "%u" (user-login-name))
  13646. (cons "%U" user-full-name)
  13647. (cons "%t" (format-time-string
  13648. (org-time-stamp-format 'long 'inactive)
  13649. (current-time)))
  13650. (cons "%s" (if org-log-note-state
  13651. (concat "\"" org-log-note-state "\"")
  13652. "")))))
  13653. (if lines (setq note (concat note " \\\\")))
  13654. (push note lines))
  13655. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13656. (when lines
  13657. (save-excursion
  13658. (set-buffer (marker-buffer org-log-note-marker))
  13659. (save-excursion
  13660. (goto-char org-log-note-marker)
  13661. (move-marker org-log-note-marker nil)
  13662. (end-of-line 1)
  13663. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13664. (indent-relative nil)
  13665. (insert "- " (pop lines))
  13666. (org-indent-line-function)
  13667. (beginning-of-line 1)
  13668. (looking-at "[ \t]*")
  13669. (setq ind (concat (match-string 0) " "))
  13670. (end-of-line 1)
  13671. (while lines (insert "\n" ind (pop lines)))))))
  13672. (set-window-configuration org-log-note-window-configuration)
  13673. (with-current-buffer (marker-buffer org-log-note-return-to)
  13674. (goto-char org-log-note-return-to))
  13675. (move-marker org-log-note-return-to nil)
  13676. (and org-log-post-message (message "%s" org-log-post-message)))
  13677. ;; FIXME: what else would be useful?
  13678. ;; - priority
  13679. ;; - date
  13680. (defun org-sparse-tree (&optional arg)
  13681. "Create a sparse tree, prompt for the details.
  13682. This command can create sparse trees. You first need to select the type
  13683. of match used to create the tree:
  13684. t Show entries with a specific TODO keyword.
  13685. T Show entries selected by a tags match.
  13686. p Enter a property name and its value (both with completion on existing
  13687. names/values) and show entries with that property.
  13688. r Show entries matching a regular expression
  13689. d Show deadlines due within `org-deadline-warning-days'."
  13690. (interactive "P")
  13691. (let (ans kwd value)
  13692. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13693. (setq ans (read-char-exclusive))
  13694. (cond
  13695. ((equal ans ?d)
  13696. (call-interactively 'org-check-deadlines))
  13697. ((equal ans ?b)
  13698. (call-interactively 'org-check-before-date))
  13699. ((equal ans ?t)
  13700. (org-show-todo-tree '(4)))
  13701. ((equal ans ?T)
  13702. (call-interactively 'org-tags-sparse-tree))
  13703. ((member ans '(?p ?P))
  13704. (setq kwd (completing-read "Property: "
  13705. (mapcar 'list (org-buffer-property-keys))))
  13706. (setq value (completing-read "Value: "
  13707. (mapcar 'list (org-property-values kwd))))
  13708. (unless (string-match "\\`{.*}\\'" value)
  13709. (setq value (concat "\"" value "\"")))
  13710. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13711. ((member ans '(?r ?R ?/))
  13712. (call-interactively 'org-occur))
  13713. (t (error "No such sparse tree command \"%c\"" ans)))))
  13714. (defvar org-occur-highlights nil)
  13715. (make-variable-buffer-local 'org-occur-highlights)
  13716. (defun org-occur (regexp &optional keep-previous callback)
  13717. "Make a compact tree which shows all matches of REGEXP.
  13718. The tree will show the lines where the regexp matches, and all higher
  13719. headlines above the match. It will also show the heading after the match,
  13720. to make sure editing the matching entry is easy.
  13721. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13722. call to `org-occur' will be kept, to allow stacking of calls to this
  13723. command.
  13724. If CALLBACK is non-nil, it is a function which is called to confirm
  13725. that the match should indeed be shown."
  13726. (interactive "sRegexp: \nP")
  13727. (or keep-previous (org-remove-occur-highlights nil nil t))
  13728. (let ((cnt 0))
  13729. (save-excursion
  13730. (goto-char (point-min))
  13731. (if (or (not keep-previous) ; do not want to keep
  13732. (not org-occur-highlights)) ; no previous matches
  13733. ;; hide everything
  13734. (org-overview))
  13735. (while (re-search-forward regexp nil t)
  13736. (when (or (not callback)
  13737. (save-match-data (funcall callback)))
  13738. (setq cnt (1+ cnt))
  13739. (when org-highlight-sparse-tree-matches
  13740. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13741. (org-show-context 'occur-tree))))
  13742. (when org-remove-highlights-with-change
  13743. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13744. nil 'local))
  13745. (unless org-sparse-tree-open-archived-trees
  13746. (org-hide-archived-subtrees (point-min) (point-max)))
  13747. (run-hooks 'org-occur-hook)
  13748. (if (interactive-p)
  13749. (message "%d match(es) for regexp %s" cnt regexp))
  13750. cnt))
  13751. (defun org-show-context (&optional key)
  13752. "Make sure point and context and visible.
  13753. How much context is shown depends upon the variables
  13754. `org-show-hierarchy-above', `org-show-following-heading'. and
  13755. `org-show-siblings'."
  13756. (let ((heading-p (org-on-heading-p t))
  13757. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13758. (following-p (org-get-alist-option org-show-following-heading key))
  13759. (entry-p (org-get-alist-option org-show-entry-below key))
  13760. (siblings-p (org-get-alist-option org-show-siblings key)))
  13761. (catch 'exit
  13762. ;; Show heading or entry text
  13763. (if (and heading-p (not entry-p))
  13764. (org-flag-heading nil) ; only show the heading
  13765. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13766. (org-show-hidden-entry))) ; show entire entry
  13767. (when following-p
  13768. ;; Show next sibling, or heading below text
  13769. (save-excursion
  13770. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13771. (org-flag-heading nil))))
  13772. (when siblings-p (org-show-siblings))
  13773. (when hierarchy-p
  13774. ;; show all higher headings, possibly with siblings
  13775. (save-excursion
  13776. (while (and (condition-case nil
  13777. (progn (org-up-heading-all 1) t)
  13778. (error nil))
  13779. (not (bobp)))
  13780. (org-flag-heading nil)
  13781. (when siblings-p (org-show-siblings))))))))
  13782. (defun org-reveal (&optional siblings)
  13783. "Show current entry, hierarchy above it, and the following headline.
  13784. This can be used to show a consistent set of context around locations
  13785. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13786. not t for the search context.
  13787. With optional argument SIBLINGS, on each level of the hierarchy all
  13788. siblings are shown. This repairs the tree structure to what it would
  13789. look like when opened with hierarchical calls to `org-cycle'."
  13790. (interactive "P")
  13791. (let ((org-show-hierarchy-above t)
  13792. (org-show-following-heading t)
  13793. (org-show-siblings (if siblings t org-show-siblings)))
  13794. (org-show-context nil)))
  13795. (defun org-highlight-new-match (beg end)
  13796. "Highlight from BEG to END and mark the highlight is an occur headline."
  13797. (let ((ov (org-make-overlay beg end)))
  13798. (org-overlay-put ov 'face 'secondary-selection)
  13799. (push ov org-occur-highlights)))
  13800. (defun org-remove-occur-highlights (&optional beg end noremove)
  13801. "Remove the occur highlights from the buffer.
  13802. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13803. from the `before-change-functions' in the current buffer."
  13804. (interactive)
  13805. (unless org-inhibit-highlight-removal
  13806. (mapc 'org-delete-overlay org-occur-highlights)
  13807. (setq org-occur-highlights nil)
  13808. (unless noremove
  13809. (remove-hook 'before-change-functions
  13810. 'org-remove-occur-highlights 'local))))
  13811. ;;;; Priorities
  13812. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13813. "Regular expression matching the priority indicator.")
  13814. (defvar org-remove-priority-next-time nil)
  13815. (defun org-priority-up ()
  13816. "Increase the priority of the current item."
  13817. (interactive)
  13818. (org-priority 'up))
  13819. (defun org-priority-down ()
  13820. "Decrease the priority of the current item."
  13821. (interactive)
  13822. (org-priority 'down))
  13823. (defun org-priority (&optional action)
  13824. "Change the priority of an item by ARG.
  13825. ACTION can be `set', `up', `down', or a character."
  13826. (interactive)
  13827. (setq action (or action 'set))
  13828. (let (current new news have remove)
  13829. (save-excursion
  13830. (org-back-to-heading)
  13831. (if (looking-at org-priority-regexp)
  13832. (setq current (string-to-char (match-string 2))
  13833. have t)
  13834. (setq current org-default-priority))
  13835. (cond
  13836. ((or (eq action 'set) (integerp action))
  13837. (if (integerp action)
  13838. (setq new action)
  13839. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13840. (setq new (read-char-exclusive)))
  13841. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13842. (= (upcase org-lowest-priority) org-lowest-priority))
  13843. (setq new (upcase new)))
  13844. (cond ((equal new ?\ ) (setq remove t))
  13845. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13846. (error "Priority must be between `%c' and `%c'"
  13847. org-highest-priority org-lowest-priority))))
  13848. ((eq action 'up)
  13849. (if (and (not have) (eq last-command this-command))
  13850. (setq new org-lowest-priority)
  13851. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13852. org-default-priority (1- current)))))
  13853. ((eq action 'down)
  13854. (if (and (not have) (eq last-command this-command))
  13855. (setq new org-highest-priority)
  13856. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13857. org-default-priority (1+ current)))))
  13858. (t (error "Invalid action")))
  13859. (if (or (< (upcase new) org-highest-priority)
  13860. (> (upcase new) org-lowest-priority))
  13861. (setq remove t))
  13862. (setq news (format "%c" new))
  13863. (if have
  13864. (if remove
  13865. (replace-match "" t t nil 1)
  13866. (replace-match news t t nil 2))
  13867. (if remove
  13868. (error "No priority cookie found in line")
  13869. (looking-at org-todo-line-regexp)
  13870. (if (match-end 2)
  13871. (progn
  13872. (goto-char (match-end 2))
  13873. (insert " [#" news "]"))
  13874. (goto-char (match-beginning 3))
  13875. (insert "[#" news "] ")))))
  13876. (org-preserve-lc (org-set-tags nil 'align))
  13877. (if remove
  13878. (message "Priority removed")
  13879. (message "Priority of current item set to %s" news))))
  13880. (defun org-get-priority (s)
  13881. "Find priority cookie and return priority."
  13882. (save-match-data
  13883. (if (not (string-match org-priority-regexp s))
  13884. (* 1000 (- org-lowest-priority org-default-priority))
  13885. (* 1000 (- org-lowest-priority
  13886. (string-to-char (match-string 2 s)))))))
  13887. ;;;; Tags
  13888. (defun org-scan-tags (action matcher &optional todo-only)
  13889. "Scan headline tags with inheritance and produce output ACTION.
  13890. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13891. evaluated, testing if a given set of tags qualifies a headline for
  13892. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13893. are included in the output."
  13894. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13895. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13896. (org-re
  13897. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13898. (props (list 'face nil
  13899. 'done-face 'org-done
  13900. 'undone-face nil
  13901. 'mouse-face 'highlight
  13902. 'org-not-done-regexp org-not-done-regexp
  13903. 'org-todo-regexp org-todo-regexp
  13904. 'keymap org-agenda-keymap
  13905. 'help-echo
  13906. (format "mouse-2 or RET jump to org file %s"
  13907. (abbreviate-file-name
  13908. (or (buffer-file-name (buffer-base-buffer))
  13909. (buffer-name (buffer-base-buffer)))))))
  13910. (case-fold-search nil)
  13911. lspos
  13912. tags tags-list tags-alist (llast 0) rtn level category i txt
  13913. todo marker entry priority)
  13914. (save-excursion
  13915. (goto-char (point-min))
  13916. (when (eq action 'sparse-tree)
  13917. (org-overview)
  13918. (org-remove-occur-highlights))
  13919. (while (re-search-forward re nil t)
  13920. (catch :skip
  13921. (setq todo (if (match-end 1) (match-string 2))
  13922. tags (if (match-end 4) (match-string 4)))
  13923. (goto-char (setq lspos (1+ (match-beginning 0))))
  13924. (setq level (org-reduced-level (funcall outline-level))
  13925. category (org-get-category))
  13926. (setq i llast llast level)
  13927. ;; remove tag lists from same and sublevels
  13928. (while (>= i level)
  13929. (when (setq entry (assoc i tags-alist))
  13930. (setq tags-alist (delete entry tags-alist)))
  13931. (setq i (1- i)))
  13932. ;; add the nex tags
  13933. (when tags
  13934. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13935. tags-alist
  13936. (cons (cons level tags) tags-alist)))
  13937. ;; compile tags for current headline
  13938. (setq tags-list
  13939. (if org-use-tag-inheritance
  13940. (apply 'append (mapcar 'cdr tags-alist))
  13941. tags))
  13942. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13943. (eval matcher)
  13944. (or (not org-agenda-skip-archived-trees)
  13945. (not (member org-archive-tag tags-list))))
  13946. (and (eq action 'agenda) (org-agenda-skip))
  13947. ;; list this headline
  13948. (if (eq action 'sparse-tree)
  13949. (progn
  13950. (and org-highlight-sparse-tree-matches
  13951. (org-get-heading) (match-end 0)
  13952. (org-highlight-new-match
  13953. (match-beginning 0) (match-beginning 1)))
  13954. (org-show-context 'tags-tree))
  13955. (setq txt (org-format-agenda-item
  13956. ""
  13957. (concat
  13958. (if org-tags-match-list-sublevels
  13959. (make-string (1- level) ?.) "")
  13960. (org-get-heading))
  13961. category tags-list)
  13962. priority (org-get-priority txt))
  13963. (goto-char lspos)
  13964. (setq marker (org-agenda-new-marker))
  13965. (org-add-props txt props
  13966. 'org-marker marker 'org-hd-marker marker 'org-category category
  13967. 'priority priority 'type "tagsmatch")
  13968. (push txt rtn))
  13969. ;; if we are to skip sublevels, jump to end of subtree
  13970. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  13971. (when (and (eq action 'sparse-tree)
  13972. (not org-sparse-tree-open-archived-trees))
  13973. (org-hide-archived-subtrees (point-min) (point-max)))
  13974. (nreverse rtn)))
  13975. (defvar todo-only) ;; dynamically scoped
  13976. (defun org-tags-sparse-tree (&optional todo-only match)
  13977. "Create a sparse tree according to tags string MATCH.
  13978. MATCH can contain positive and negative selection of tags, like
  13979. \"+WORK+URGENT-WITHBOSS\".
  13980. If optional argument TODO_ONLY is non-nil, only select lines that are
  13981. also TODO lines."
  13982. (interactive "P")
  13983. (org-prepare-agenda-buffers (list (current-buffer)))
  13984. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13985. (defvar org-cached-props nil)
  13986. (defun org-cached-entry-get (pom property)
  13987. (if (or (eq t org-use-property-inheritance)
  13988. (member property org-use-property-inheritance))
  13989. ;; Caching is not possible, check it directly
  13990. (org-entry-get pom property 'inherit)
  13991. ;; Get all properties, so that we can do complicated checks easily
  13992. (cdr (assoc property (or org-cached-props
  13993. (setq org-cached-props
  13994. (org-entry-properties pom)))))))
  13995. (defun org-global-tags-completion-table (&optional files)
  13996. "Return the list of all tags in all agenda buffer/files."
  13997. (save-excursion
  13998. (org-uniquify
  13999. (delq nil
  14000. (apply 'append
  14001. (mapcar
  14002. (lambda (file)
  14003. (set-buffer (find-file-noselect file))
  14004. (append (org-get-buffer-tags)
  14005. (mapcar (lambda (x) (if (stringp (car-safe x))
  14006. (list (car-safe x)) nil))
  14007. org-tag-alist)))
  14008. (if (and files (car files))
  14009. files
  14010. (org-agenda-files))))))))
  14011. (defun org-make-tags-matcher (match)
  14012. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14013. ;; todo-only is scoped dynamically into this function, and the function
  14014. ;; may change it it the matcher asksk for it.
  14015. (unless match
  14016. ;; Get a new match request, with completion
  14017. (let ((org-last-tags-completion-table
  14018. (org-global-tags-completion-table)))
  14019. (setq match (completing-read
  14020. "Match: " 'org-tags-completion-function nil nil nil
  14021. 'org-tags-history))))
  14022. ;; Parse the string and create a lisp form
  14023. (let ((match0 match)
  14024. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14025. minus tag mm
  14026. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14027. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14028. (if (string-match "/+" match)
  14029. ;; match contains also a todo-matching request
  14030. (progn
  14031. (setq tagsmatch (substring match 0 (match-beginning 0))
  14032. todomatch (substring match (match-end 0)))
  14033. (if (string-match "^!" todomatch)
  14034. (setq todo-only t todomatch (substring todomatch 1)))
  14035. (if (string-match "^\\s-*$" todomatch)
  14036. (setq todomatch nil)))
  14037. ;; only matching tags
  14038. (setq tagsmatch match todomatch nil))
  14039. ;; Make the tags matcher
  14040. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14041. (setq tagsmatcher t)
  14042. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14043. (while (setq term (pop orterms))
  14044. (while (and (equal (substring term -1) "\\") orterms)
  14045. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14046. (while (string-match re term)
  14047. (setq minus (and (match-end 1)
  14048. (equal (match-string 1 term) "-"))
  14049. tag (match-string 2 term)
  14050. re-p (equal (string-to-char tag) ?{)
  14051. level-p (match-end 3)
  14052. prop-p (match-end 4)
  14053. mm (cond
  14054. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14055. (level-p `(= level ,(string-to-number
  14056. (match-string 3 term))))
  14057. (prop-p
  14058. (setq pn (match-string 4 term)
  14059. pv (match-string 5 term)
  14060. cat-p (equal pn "CATEGORY")
  14061. re-p (equal (string-to-char pv) ?{)
  14062. pv (substring pv 1 -1))
  14063. (if (equal pn "CATEGORY")
  14064. (setq gv '(get-text-property (point) 'org-category))
  14065. (setq gv `(org-cached-entry-get nil ,pn)))
  14066. (if re-p
  14067. `(string-match ,pv (or ,gv ""))
  14068. `(equal ,pv (or ,gv ""))))
  14069. (t `(member ,(downcase tag) tags-list)))
  14070. mm (if minus (list 'not mm) mm)
  14071. term (substring term (match-end 0)))
  14072. (push mm tagsmatcher))
  14073. (push (if (> (length tagsmatcher) 1)
  14074. (cons 'and tagsmatcher)
  14075. (car tagsmatcher))
  14076. orlist)
  14077. (setq tagsmatcher nil))
  14078. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14079. (setq tagsmatcher
  14080. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14081. ;; Make the todo matcher
  14082. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14083. (setq todomatcher t)
  14084. (setq orterms (org-split-string todomatch "|") orlist nil)
  14085. (while (setq term (pop orterms))
  14086. (while (string-match re term)
  14087. (setq minus (and (match-end 1)
  14088. (equal (match-string 1 term) "-"))
  14089. kwd (match-string 2 term)
  14090. re-p (equal (string-to-char kwd) ?{)
  14091. term (substring term (match-end 0))
  14092. mm (if re-p
  14093. `(string-match ,(substring kwd 1 -1) todo)
  14094. (list 'equal 'todo kwd))
  14095. mm (if minus (list 'not mm) mm))
  14096. (push mm todomatcher))
  14097. (push (if (> (length todomatcher) 1)
  14098. (cons 'and todomatcher)
  14099. (car todomatcher))
  14100. orlist)
  14101. (setq todomatcher nil))
  14102. (setq todomatcher (if (> (length orlist) 1)
  14103. (cons 'or orlist) (car orlist))))
  14104. ;; Return the string and lisp forms of the matcher
  14105. (setq matcher (if todomatcher
  14106. (list 'and tagsmatcher todomatcher)
  14107. tagsmatcher))
  14108. (cons match0 matcher)))
  14109. (defun org-match-any-p (re list)
  14110. "Does re match any element of list?"
  14111. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14112. (delq nil list))
  14113. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14114. (defvar org-tags-overlay (org-make-overlay 1 1))
  14115. (org-detach-overlay org-tags-overlay)
  14116. (defun org-align-tags-here (to-col)
  14117. ;; Assumes that this is a headline
  14118. (let ((pos (point)) (col (current-column)) tags)
  14119. (beginning-of-line 1)
  14120. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14121. (< pos (match-beginning 2)))
  14122. (progn
  14123. (setq tags (match-string 2))
  14124. (goto-char (match-beginning 1))
  14125. (insert " ")
  14126. (delete-region (point) (1+ (match-end 0)))
  14127. (backward-char 1)
  14128. (move-to-column
  14129. (max (1+ (current-column))
  14130. (1+ col)
  14131. (if (> to-col 0)
  14132. to-col
  14133. (- (abs to-col) (length tags))))
  14134. t)
  14135. (insert tags)
  14136. (move-to-column (min (current-column) col) t))
  14137. (goto-char pos))))
  14138. (defun org-set-tags (&optional arg just-align)
  14139. "Set the tags for the current headline.
  14140. With prefix ARG, realign all tags in headings in the current buffer."
  14141. (interactive "P")
  14142. (let* ((re (concat "^" outline-regexp))
  14143. (current (org-get-tags-string))
  14144. (col (current-column))
  14145. (org-setting-tags t)
  14146. table current-tags inherited-tags ; computed below when needed
  14147. tags p0 c0 c1 rpl)
  14148. (if arg
  14149. (save-excursion
  14150. (goto-char (point-min))
  14151. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14152. (while (re-search-forward re nil t)
  14153. (org-set-tags nil t)
  14154. (end-of-line 1)))
  14155. (message "All tags realigned to column %d" org-tags-column))
  14156. (if just-align
  14157. (setq tags current)
  14158. ;; Get a new set of tags from the user
  14159. (save-excursion
  14160. (setq table (or org-tag-alist (org-get-buffer-tags))
  14161. org-last-tags-completion-table table
  14162. current-tags (org-split-string current ":")
  14163. inherited-tags (nreverse
  14164. (nthcdr (length current-tags)
  14165. (nreverse (org-get-tags-at))))
  14166. tags
  14167. (if (or (eq t org-use-fast-tag-selection)
  14168. (and org-use-fast-tag-selection
  14169. (delq nil (mapcar 'cdr table))))
  14170. (org-fast-tag-selection
  14171. current-tags inherited-tags table
  14172. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14173. (let ((org-add-colon-after-tag-completion t))
  14174. (org-trim
  14175. (org-without-partial-completion
  14176. (completing-read "Tags: " 'org-tags-completion-function
  14177. nil nil current 'org-tags-history)))))))
  14178. (while (string-match "[-+&]+" tags)
  14179. ;; No boolean logic, just a list
  14180. (setq tags (replace-match ":" t t tags))))
  14181. (if (string-match "\\`[\t ]*\\'" tags)
  14182. (setq tags "")
  14183. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14184. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14185. ;; Insert new tags at the correct column
  14186. (beginning-of-line 1)
  14187. (cond
  14188. ((and (equal current "") (equal tags "")))
  14189. ((re-search-forward
  14190. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14191. (point-at-eol) t)
  14192. (if (equal tags "")
  14193. (setq rpl "")
  14194. (goto-char (match-beginning 0))
  14195. (setq c0 (current-column) p0 (point)
  14196. c1 (max (1+ c0) (if (> org-tags-column 0)
  14197. org-tags-column
  14198. (- (- org-tags-column) (length tags))))
  14199. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14200. (replace-match rpl t t)
  14201. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14202. tags)
  14203. (t (error "Tags alignment failed")))
  14204. (move-to-column col)
  14205. (unless just-align
  14206. (run-hooks 'org-after-tags-change-hook)))))
  14207. (defun org-change-tag-in-region (beg end tag off)
  14208. "Add or remove TAG for each entry in the region.
  14209. This works in the agenda, and also in an org-mode buffer."
  14210. (interactive
  14211. (list (region-beginning) (region-end)
  14212. (let ((org-last-tags-completion-table
  14213. (if (org-mode-p)
  14214. (org-get-buffer-tags)
  14215. (org-global-tags-completion-table))))
  14216. (completing-read
  14217. "Tag: " 'org-tags-completion-function nil nil nil
  14218. 'org-tags-history))
  14219. (progn
  14220. (message "[s]et or [r]emove? ")
  14221. (equal (read-char-exclusive) ?r))))
  14222. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14223. (let ((agendap (equal major-mode 'org-agenda-mode))
  14224. l1 l2 m buf pos newhead (cnt 0))
  14225. (goto-char end)
  14226. (setq l2 (1- (org-current-line)))
  14227. (goto-char beg)
  14228. (setq l1 (org-current-line))
  14229. (loop for l from l1 to l2 do
  14230. (goto-line l)
  14231. (setq m (get-text-property (point) 'org-hd-marker))
  14232. (when (or (and (org-mode-p) (org-on-heading-p))
  14233. (and agendap m))
  14234. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14235. pos (if agendap m (point)))
  14236. (with-current-buffer buf
  14237. (save-excursion
  14238. (save-restriction
  14239. (goto-char pos)
  14240. (setq cnt (1+ cnt))
  14241. (org-toggle-tag tag (if off 'off 'on))
  14242. (setq newhead (org-get-heading)))))
  14243. (and agendap (org-agenda-change-all-lines newhead m))))
  14244. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14245. (defun org-tags-completion-function (string predicate &optional flag)
  14246. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14247. (confirm (lambda (x) (stringp (car x)))))
  14248. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14249. (setq s1 (match-string 1 string)
  14250. s2 (match-string 2 string))
  14251. (setq s1 "" s2 string))
  14252. (cond
  14253. ((eq flag nil)
  14254. ;; try completion
  14255. (setq rtn (try-completion s2 ctable confirm))
  14256. (if (stringp rtn)
  14257. (setq rtn
  14258. (concat s1 s2 (substring rtn (length s2))
  14259. (if (and org-add-colon-after-tag-completion
  14260. (assoc rtn ctable))
  14261. ":" ""))))
  14262. rtn)
  14263. ((eq flag t)
  14264. ;; all-completions
  14265. (all-completions s2 ctable confirm)
  14266. )
  14267. ((eq flag 'lambda)
  14268. ;; exact match?
  14269. (assoc s2 ctable)))
  14270. ))
  14271. (defun org-fast-tag-insert (kwd tags face &optional end)
  14272. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14273. (insert (format "%-12s" (concat kwd ":"))
  14274. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14275. (or end "")))
  14276. (defun org-fast-tag-show-exit (flag)
  14277. (save-excursion
  14278. (goto-line 3)
  14279. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14280. (replace-match ""))
  14281. (when flag
  14282. (end-of-line 1)
  14283. (move-to-column (- (window-width) 19) t)
  14284. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14285. (defun org-set-current-tags-overlay (current prefix)
  14286. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14287. (if (featurep 'xemacs)
  14288. (org-overlay-display org-tags-overlay (concat prefix s)
  14289. 'secondary-selection)
  14290. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14291. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14292. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14293. "Fast tag selection with single keys.
  14294. CURRENT is the current list of tags in the headline, INHERITED is the
  14295. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14296. possibly with grouping information. TODO-TABLE is a similar table with
  14297. TODO keywords, should these have keys assigned to them.
  14298. If the keys are nil, a-z are automatically assigned.
  14299. Returns the new tags string, or nil to not change the current settings."
  14300. (let* ((fulltable (append table todo-table))
  14301. (maxlen (apply 'max (mapcar
  14302. (lambda (x)
  14303. (if (stringp (car x)) (string-width (car x)) 0))
  14304. fulltable)))
  14305. (buf (current-buffer))
  14306. (expert (eq org-fast-tag-selection-single-key 'expert))
  14307. (buffer-tags nil)
  14308. (fwidth (+ maxlen 3 1 3))
  14309. (ncol (/ (- (window-width) 4) fwidth))
  14310. (i-face 'org-done)
  14311. (c-face 'org-todo)
  14312. tg cnt e c char c1 c2 ntable tbl rtn
  14313. ov-start ov-end ov-prefix
  14314. (exit-after-next org-fast-tag-selection-single-key)
  14315. (done-keywords org-done-keywords)
  14316. groups ingroup)
  14317. (save-excursion
  14318. (beginning-of-line 1)
  14319. (if (looking-at
  14320. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14321. (setq ov-start (match-beginning 1)
  14322. ov-end (match-end 1)
  14323. ov-prefix "")
  14324. (setq ov-start (1- (point-at-eol))
  14325. ov-end (1+ ov-start))
  14326. (skip-chars-forward "^\n\r")
  14327. (setq ov-prefix
  14328. (concat
  14329. (buffer-substring (1- (point)) (point))
  14330. (if (> (current-column) org-tags-column)
  14331. " "
  14332. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14333. (org-move-overlay org-tags-overlay ov-start ov-end)
  14334. (save-window-excursion
  14335. (if expert
  14336. (set-buffer (get-buffer-create " *Org tags*"))
  14337. (delete-other-windows)
  14338. (split-window-vertically)
  14339. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14340. (erase-buffer)
  14341. (org-set-local 'org-done-keywords done-keywords)
  14342. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14343. (org-fast-tag-insert "Current" current c-face "\n\n")
  14344. (org-fast-tag-show-exit exit-after-next)
  14345. (org-set-current-tags-overlay current ov-prefix)
  14346. (setq tbl fulltable char ?a cnt 0)
  14347. (while (setq e (pop tbl))
  14348. (cond
  14349. ((equal e '(:startgroup))
  14350. (push '() groups) (setq ingroup t)
  14351. (when (not (= cnt 0))
  14352. (setq cnt 0)
  14353. (insert "\n"))
  14354. (insert "{ "))
  14355. ((equal e '(:endgroup))
  14356. (setq ingroup nil cnt 0)
  14357. (insert "}\n"))
  14358. (t
  14359. (setq tg (car e) c2 nil)
  14360. (if (cdr e)
  14361. (setq c (cdr e))
  14362. ;; automatically assign a character.
  14363. (setq c1 (string-to-char
  14364. (downcase (substring
  14365. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14366. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14367. (while (or (rassoc char ntable) (rassoc char table))
  14368. (setq char (1+ char)))
  14369. (setq c2 c1))
  14370. (setq c (or c2 char)))
  14371. (if ingroup (push tg (car groups)))
  14372. (setq tg (org-add-props tg nil 'face
  14373. (cond
  14374. ((not (assoc tg table))
  14375. (org-get-todo-face tg))
  14376. ((member tg current) c-face)
  14377. ((member tg inherited) i-face)
  14378. (t nil))))
  14379. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14380. (insert "[" c "] " tg (make-string
  14381. (- fwidth 4 (length tg)) ?\ ))
  14382. (push (cons tg c) ntable)
  14383. (when (= (setq cnt (1+ cnt)) ncol)
  14384. (insert "\n")
  14385. (if ingroup (insert " "))
  14386. (setq cnt 0)))))
  14387. (setq ntable (nreverse ntable))
  14388. (insert "\n")
  14389. (goto-char (point-min))
  14390. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14391. (fit-window-to-buffer))
  14392. (setq rtn
  14393. (catch 'exit
  14394. (while t
  14395. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14396. (if groups " [!] no groups" " [!]groups")
  14397. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14398. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14399. (cond
  14400. ((= c ?\r) (throw 'exit t))
  14401. ((= c ?!)
  14402. (setq groups (not groups))
  14403. (goto-char (point-min))
  14404. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14405. ((= c ?\C-c)
  14406. (if (not expert)
  14407. (org-fast-tag-show-exit
  14408. (setq exit-after-next (not exit-after-next)))
  14409. (setq expert nil)
  14410. (delete-other-windows)
  14411. (split-window-vertically)
  14412. (org-switch-to-buffer-other-window " *Org tags*")
  14413. (and (fboundp 'fit-window-to-buffer)
  14414. (fit-window-to-buffer))))
  14415. ((or (= c ?\C-g)
  14416. (and (= c ?q) (not (rassoc c ntable))))
  14417. (org-detach-overlay org-tags-overlay)
  14418. (setq quit-flag t))
  14419. ((= c ?\ )
  14420. (setq current nil)
  14421. (if exit-after-next (setq exit-after-next 'now)))
  14422. ((= c ?\t)
  14423. (condition-case nil
  14424. (setq tg (completing-read
  14425. "Tag: "
  14426. (or buffer-tags
  14427. (with-current-buffer buf
  14428. (org-get-buffer-tags)))))
  14429. (quit (setq tg "")))
  14430. (when (string-match "\\S-" tg)
  14431. (add-to-list 'buffer-tags (list tg))
  14432. (if (member tg current)
  14433. (setq current (delete tg current))
  14434. (push tg current)))
  14435. (if exit-after-next (setq exit-after-next 'now)))
  14436. ((setq e (rassoc c todo-table) tg (car e))
  14437. (with-current-buffer buf
  14438. (save-excursion (org-todo tg)))
  14439. (if exit-after-next (setq exit-after-next 'now)))
  14440. ((setq e (rassoc c ntable) tg (car e))
  14441. (if (member tg current)
  14442. (setq current (delete tg current))
  14443. (loop for g in groups do
  14444. (if (member tg g)
  14445. (mapc (lambda (x)
  14446. (setq current (delete x current)))
  14447. g)))
  14448. (push tg current))
  14449. (if exit-after-next (setq exit-after-next 'now))))
  14450. ;; Create a sorted list
  14451. (setq current
  14452. (sort current
  14453. (lambda (a b)
  14454. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14455. (if (eq exit-after-next 'now) (throw 'exit t))
  14456. (goto-char (point-min))
  14457. (beginning-of-line 2)
  14458. (delete-region (point) (point-at-eol))
  14459. (org-fast-tag-insert "Current" current c-face)
  14460. (org-set-current-tags-overlay current ov-prefix)
  14461. (while (re-search-forward
  14462. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14463. (setq tg (match-string 1))
  14464. (add-text-properties
  14465. (match-beginning 1) (match-end 1)
  14466. (list 'face
  14467. (cond
  14468. ((member tg current) c-face)
  14469. ((member tg inherited) i-face)
  14470. (t (get-text-property (match-beginning 1) 'face))))))
  14471. (goto-char (point-min)))))
  14472. (org-detach-overlay org-tags-overlay)
  14473. (if rtn
  14474. (mapconcat 'identity current ":")
  14475. nil))))
  14476. (defun org-get-tags-string ()
  14477. "Get the TAGS string in the current headline."
  14478. (unless (org-on-heading-p t)
  14479. (error "Not on a heading"))
  14480. (save-excursion
  14481. (beginning-of-line 1)
  14482. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14483. (org-match-string-no-properties 1)
  14484. "")))
  14485. (defun org-get-tags ()
  14486. "Get the list of tags specified in the current headline."
  14487. (org-split-string (org-get-tags-string) ":"))
  14488. (defun org-get-buffer-tags ()
  14489. "Get a table of all tags used in the buffer, for completion."
  14490. (let (tags)
  14491. (save-excursion
  14492. (goto-char (point-min))
  14493. (while (re-search-forward
  14494. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14495. (when (equal (char-after (point-at-bol 0)) ?*)
  14496. (mapc (lambda (x) (add-to-list 'tags x))
  14497. (org-split-string (org-match-string-no-properties 1) ":")))))
  14498. (mapcar 'list tags)))
  14499. ;;;; Properties
  14500. ;;; Setting and retrieving properties
  14501. (defconst org-special-properties
  14502. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14503. "TIMESTAMP" "TIMESTAMP_IA")
  14504. "The special properties valid in Org-mode.
  14505. These are properties that are not defined in the property drawer,
  14506. but in some other way.")
  14507. (defconst org-default-properties
  14508. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14509. "LOCATION" "LOGGING" "COLUMNS")
  14510. "Some properties that are used by Org-mode for various purposes.
  14511. Being in this list makes sure that they are offered for completion.")
  14512. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14513. "Regular expression matching the first line of a property drawer.")
  14514. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14515. "Regular expression matching the first line of a property drawer.")
  14516. (defun org-property-action ()
  14517. "Do an action on properties."
  14518. (interactive)
  14519. (let (c)
  14520. (org-at-property-p)
  14521. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14522. (setq c (read-char-exclusive))
  14523. (cond
  14524. ((equal c ?s)
  14525. (call-interactively 'org-set-property))
  14526. ((equal c ?d)
  14527. (call-interactively 'org-delete-property))
  14528. ((equal c ?D)
  14529. (call-interactively 'org-delete-property-globally))
  14530. ((equal c ?c)
  14531. (call-interactively 'org-compute-property-at-point))
  14532. (t (error "No such property action %c" c)))))
  14533. (defun org-at-property-p ()
  14534. "Is the cursor in a property line?"
  14535. ;; FIXME: Does not check if we are actually in the drawer.
  14536. ;; FIXME: also returns true on any drawers.....
  14537. ;; This is used by C-c C-c for property action.
  14538. (save-excursion
  14539. (beginning-of-line 1)
  14540. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14541. (defmacro org-with-point-at (pom &rest body)
  14542. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14543. (declare (indent 1) (debug t))
  14544. `(save-excursion
  14545. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14546. (save-excursion
  14547. (goto-char (or pom (point)))
  14548. ,@body)))
  14549. (defun org-get-property-block (&optional beg end force)
  14550. "Return the (beg . end) range of the body of the property drawer.
  14551. BEG and END can be beginning and end of subtree, if not given
  14552. they will be found.
  14553. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14554. (catch 'exit
  14555. (save-excursion
  14556. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14557. (end (or end (progn (outline-next-heading) (point)))))
  14558. (goto-char beg)
  14559. (if (re-search-forward org-property-start-re end t)
  14560. (setq beg (1+ (match-end 0)))
  14561. (if force
  14562. (save-excursion
  14563. (org-insert-property-drawer)
  14564. (setq end (progn (outline-next-heading) (point))))
  14565. (throw 'exit nil))
  14566. (goto-char beg)
  14567. (if (re-search-forward org-property-start-re end t)
  14568. (setq beg (1+ (match-end 0)))))
  14569. (if (re-search-forward org-property-end-re end t)
  14570. (setq end (match-beginning 0))
  14571. (or force (throw 'exit nil))
  14572. (goto-char beg)
  14573. (setq end beg)
  14574. (org-indent-line-function)
  14575. (insert ":END:\n"))
  14576. (cons beg end)))))
  14577. (defun org-entry-properties (&optional pom which)
  14578. "Get all properties of the entry at point-or-marker POM.
  14579. This includes the TODO keyword, the tags, time strings for deadline,
  14580. scheduled, and clocking, and any additional properties defined in the
  14581. entry. The return value is an alist, keys may occur multiple times
  14582. if the property key was used several times.
  14583. POM may also be nil, in which case the current entry is used.
  14584. If WHICH is nil or `all', get all properties. If WHICH is
  14585. `special' or `standard', only get that subclass."
  14586. (setq which (or which 'all))
  14587. (org-with-point-at pom
  14588. (let ((clockstr (substring org-clock-string 0 -1))
  14589. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14590. beg end range props sum-props key value string clocksum)
  14591. (save-excursion
  14592. (when (condition-case nil (org-back-to-heading t) (error nil))
  14593. (setq beg (point))
  14594. (setq sum-props (get-text-property (point) 'org-summaries))
  14595. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14596. (outline-next-heading)
  14597. (setq end (point))
  14598. (when (memq which '(all special))
  14599. ;; Get the special properties, like TODO and tags
  14600. (goto-char beg)
  14601. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14602. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14603. (when (looking-at org-priority-regexp)
  14604. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14605. (when (and (setq value (org-get-tags-string))
  14606. (string-match "\\S-" value))
  14607. (push (cons "TAGS" value) props))
  14608. (when (setq value (org-get-tags-at))
  14609. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14610. props))
  14611. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14612. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14613. string (if (equal key clockstr)
  14614. (org-no-properties
  14615. (org-trim
  14616. (buffer-substring
  14617. (match-beginning 3) (goto-char (point-at-eol)))))
  14618. (substring (org-match-string-no-properties 3) 1 -1)))
  14619. (unless key
  14620. (if (= (char-after (match-beginning 3)) ?\[)
  14621. (setq key "TIMESTAMP_IA")
  14622. (setq key "TIMESTAMP")))
  14623. (when (or (equal key clockstr) (not (assoc key props)))
  14624. (push (cons key string) props)))
  14625. )
  14626. (when (memq which '(all standard))
  14627. ;; Get the standard properties, like :PORP: ...
  14628. (setq range (org-get-property-block beg end))
  14629. (when range
  14630. (goto-char (car range))
  14631. (while (re-search-forward
  14632. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14633. (cdr range) t)
  14634. (setq key (org-match-string-no-properties 1)
  14635. value (org-trim (or (org-match-string-no-properties 2) "")))
  14636. (unless (member key excluded)
  14637. (push (cons key (or value "")) props)))))
  14638. (if clocksum
  14639. (push (cons "CLOCKSUM"
  14640. (org-column-number-to-string (/ (float clocksum) 60.)
  14641. 'add_times))
  14642. props))
  14643. (append sum-props (nreverse props)))))))
  14644. (defun org-entry-get (pom property &optional inherit)
  14645. "Get value of PROPERTY for entry at point-or-marker POM.
  14646. If INHERIT is non-nil and the entry does not have the property,
  14647. then also check higher levels of the hierarchy.
  14648. If the property is present but empty, the return value is the empty string.
  14649. If the property is not present at all, nil is returned."
  14650. (org-with-point-at pom
  14651. (if inherit
  14652. (org-entry-get-with-inheritance property)
  14653. (if (member property org-special-properties)
  14654. ;; We need a special property. Use brute force, get all properties.
  14655. (cdr (assoc property (org-entry-properties nil 'special)))
  14656. (let ((range (org-get-property-block)))
  14657. (if (and range
  14658. (goto-char (car range))
  14659. (re-search-forward
  14660. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14661. (cdr range) t))
  14662. ;; Found the property, return it.
  14663. (if (match-end 1)
  14664. (org-match-string-no-properties 1)
  14665. "")))))))
  14666. (defun org-entry-delete (pom property)
  14667. "Delete the property PROPERTY from entry at point-or-marker POM."
  14668. (org-with-point-at pom
  14669. (if (member property org-special-properties)
  14670. nil ; cannot delete these properties.
  14671. (let ((range (org-get-property-block)))
  14672. (if (and range
  14673. (goto-char (car range))
  14674. (re-search-forward
  14675. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14676. (cdr range) t))
  14677. (progn
  14678. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14679. t)
  14680. nil)))))
  14681. ;; Multi-values properties are properties that contain multiple values
  14682. ;; These values are assumed to be single words, separated by whitespace.
  14683. (defun org-entry-add-to-multivalued-property (pom property value)
  14684. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14685. (let* ((old (org-entry-get pom property))
  14686. (values (and old (org-split-string old "[ \t]"))))
  14687. (unless (member value values)
  14688. (setq values (cons value values))
  14689. (org-entry-put pom property
  14690. (mapconcat 'identity values " ")))))
  14691. (defun org-entry-remove-from-multivalued-property (pom property value)
  14692. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14693. (let* ((old (org-entry-get pom property))
  14694. (values (and old (org-split-string old "[ \t]"))))
  14695. (when (member value values)
  14696. (setq values (delete value values))
  14697. (org-entry-put pom property
  14698. (mapconcat 'identity values " ")))))
  14699. (defun org-entry-member-in-multivalued-property (pom property value)
  14700. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14701. (let* ((old (org-entry-get pom property))
  14702. (values (and old (org-split-string old "[ \t]"))))
  14703. (member value values)))
  14704. (defvar org-entry-property-inherited-from (make-marker))
  14705. (defun org-entry-get-with-inheritance (property)
  14706. "Get entry property, and search higher levels if not present."
  14707. (let (tmp)
  14708. (save-excursion
  14709. (save-restriction
  14710. (widen)
  14711. (catch 'ex
  14712. (while t
  14713. (when (setq tmp (org-entry-get nil property))
  14714. (org-back-to-heading t)
  14715. (move-marker org-entry-property-inherited-from (point))
  14716. (throw 'ex tmp))
  14717. (or (org-up-heading-safe) (throw 'ex nil)))))
  14718. (or tmp (cdr (assoc property org-local-properties))
  14719. (cdr (assoc property org-global-properties))))))
  14720. (defun org-entry-put (pom property value)
  14721. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14722. (org-with-point-at pom
  14723. (org-back-to-heading t)
  14724. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14725. range)
  14726. (cond
  14727. ((equal property "TODO")
  14728. (when (and (stringp value) (string-match "\\S-" value)
  14729. (not (member value org-todo-keywords-1)))
  14730. (error "\"%s\" is not a valid TODO state" value))
  14731. (if (or (not value)
  14732. (not (string-match "\\S-" value)))
  14733. (setq value 'none))
  14734. (org-todo value)
  14735. (org-set-tags nil 'align))
  14736. ((equal property "PRIORITY")
  14737. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14738. (string-to-char value) ?\ ))
  14739. (org-set-tags nil 'align))
  14740. ((equal property "SCHEDULED")
  14741. (if (re-search-forward org-scheduled-time-regexp end t)
  14742. (cond
  14743. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14744. ((eq value 'later) (org-timestamp-change 1 'day))
  14745. (t (call-interactively 'org-schedule)))
  14746. (call-interactively 'org-schedule)))
  14747. ((equal property "DEADLINE")
  14748. (if (re-search-forward org-deadline-time-regexp end t)
  14749. (cond
  14750. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14751. ((eq value 'later) (org-timestamp-change 1 'day))
  14752. (t (call-interactively 'org-deadline)))
  14753. (call-interactively 'org-deadline)))
  14754. ((member property org-special-properties)
  14755. (error "The %s property can not yet be set with `org-entry-put'"
  14756. property))
  14757. (t ; a non-special property
  14758. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14759. (setq range (org-get-property-block beg end 'force))
  14760. (goto-char (car range))
  14761. (if (re-search-forward
  14762. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14763. (progn
  14764. (delete-region (match-beginning 1) (match-end 1))
  14765. (goto-char (match-beginning 1)))
  14766. (goto-char (cdr range))
  14767. (insert "\n")
  14768. (backward-char 1)
  14769. (org-indent-line-function)
  14770. (insert ":" property ":"))
  14771. (and value (insert " " value))
  14772. (org-indent-line-function)))))))
  14773. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14774. "Get all property keys in the current buffer.
  14775. With INCLUDE-SPECIALS, also list the special properties that relect things
  14776. like tags and TODO state.
  14777. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14778. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14779. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14780. formats in the current buffer."
  14781. (let (rtn range cfmt cols)
  14782. (save-excursion
  14783. (save-restriction
  14784. (widen)
  14785. (goto-char (point-min))
  14786. (while (re-search-forward org-property-start-re nil t)
  14787. (setq range (org-get-property-block))
  14788. (goto-char (car range))
  14789. (while (re-search-forward
  14790. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14791. (cdr range) t)
  14792. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14793. (outline-next-heading))))
  14794. (when include-specials
  14795. (setq rtn (append org-special-properties rtn)))
  14796. (when include-defaults
  14797. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14798. (when include-columns
  14799. (save-excursion
  14800. (save-restriction
  14801. (widen)
  14802. (goto-char (point-min))
  14803. (while (re-search-forward
  14804. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14805. nil t)
  14806. (setq cfmt (match-string 2) s 0)
  14807. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14808. cfmt s)
  14809. (setq s (match-end 0)
  14810. p (match-string 1 cfmt))
  14811. (unless (or (equal p "ITEM")
  14812. (member p org-special-properties))
  14813. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14814. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14815. (defun org-property-values (key)
  14816. "Return a list of all values of property KEY."
  14817. (save-excursion
  14818. (save-restriction
  14819. (widen)
  14820. (goto-char (point-min))
  14821. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14822. values)
  14823. (while (re-search-forward re nil t)
  14824. (add-to-list 'values (org-trim (match-string 1))))
  14825. (delete "" values)))))
  14826. (defun org-insert-property-drawer ()
  14827. "Insert a property drawer into the current entry."
  14828. (interactive)
  14829. (org-back-to-heading t)
  14830. (looking-at outline-regexp)
  14831. (let ((indent (- (match-end 0)(match-beginning 0)))
  14832. (beg (point))
  14833. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14834. end hiddenp)
  14835. (outline-next-heading)
  14836. (setq end (point))
  14837. (goto-char beg)
  14838. (while (re-search-forward re end t))
  14839. (setq hiddenp (org-invisible-p))
  14840. (end-of-line 1)
  14841. (and (equal (char-after) ?\n) (forward-char 1))
  14842. (org-skip-over-state-notes)
  14843. (skip-chars-backward " \t\n\r")
  14844. (if (eq (char-before) ?*) (forward-char 1))
  14845. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14846. (beginning-of-line 0)
  14847. (indent-to-column indent)
  14848. (beginning-of-line 2)
  14849. (indent-to-column indent)
  14850. (beginning-of-line 0)
  14851. (if hiddenp
  14852. (save-excursion
  14853. (org-back-to-heading t)
  14854. (hide-entry))
  14855. (org-flag-drawer t))))
  14856. (defun org-set-property (property value)
  14857. "In the current entry, set PROPERTY to VALUE.
  14858. When called interactively, this will prompt for a property name, offering
  14859. completion on existing and default properties. And then it will prompt
  14860. for a value, offering competion either on allowed values (via an inherited
  14861. xxx_ALL property) or on existing values in other instances of this property
  14862. in the current file."
  14863. (interactive
  14864. (let* ((prop (completing-read
  14865. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  14866. (cur (org-entry-get nil prop))
  14867. (allowed (org-property-get-allowed-values nil prop 'table))
  14868. (existing (mapcar 'list (org-property-values prop)))
  14869. (val (if allowed
  14870. (completing-read "Value: " allowed nil 'req-match)
  14871. (completing-read
  14872. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14873. (concat "[" cur "]") "")
  14874. ": ")
  14875. existing nil nil "" nil cur))))
  14876. (list prop (if (equal val "") cur val))))
  14877. (unless (equal (org-entry-get nil property) value)
  14878. (org-entry-put nil property value)))
  14879. (defun org-delete-property (property)
  14880. "In the current entry, delete PROPERTY."
  14881. (interactive
  14882. (let* ((prop (completing-read
  14883. "Property: " (org-entry-properties nil 'standard))))
  14884. (list prop)))
  14885. (message "Property %s %s" property
  14886. (if (org-entry-delete nil property)
  14887. "deleted"
  14888. "was not present in the entry")))
  14889. (defun org-delete-property-globally (property)
  14890. "Remove PROPERTY globally, from all entries."
  14891. (interactive
  14892. (let* ((prop (completing-read
  14893. "Globally remove property: "
  14894. (mapcar 'list (org-buffer-property-keys)))))
  14895. (list prop)))
  14896. (save-excursion
  14897. (save-restriction
  14898. (widen)
  14899. (goto-char (point-min))
  14900. (let ((cnt 0))
  14901. (while (re-search-forward
  14902. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14903. nil t)
  14904. (setq cnt (1+ cnt))
  14905. (replace-match ""))
  14906. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14907. (defvar org-columns-current-fmt-compiled) ; defined below
  14908. (defun org-compute-property-at-point ()
  14909. "Compute the property at point.
  14910. This looks for an enclosing column format, extracts the operator and
  14911. then applies it to the proerty in the column format's scope."
  14912. (interactive)
  14913. (unless (org-at-property-p)
  14914. (error "Not at a property"))
  14915. (let ((prop (org-match-string-no-properties 2)))
  14916. (org-columns-get-format-and-top-level)
  14917. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14918. (error "No operator defined for property %s" prop))
  14919. (org-columns-compute prop)))
  14920. (defun org-property-get-allowed-values (pom property &optional table)
  14921. "Get allowed values for the property PROPERTY.
  14922. When TABLE is non-nil, return an alist that can directly be used for
  14923. completion."
  14924. (let (vals)
  14925. (cond
  14926. ((equal property "TODO")
  14927. (setq vals (org-with-point-at pom
  14928. (append org-todo-keywords-1 '("")))))
  14929. ((equal property "PRIORITY")
  14930. (let ((n org-lowest-priority))
  14931. (while (>= n org-highest-priority)
  14932. (push (char-to-string n) vals)
  14933. (setq n (1- n)))))
  14934. ((member property org-special-properties))
  14935. (t
  14936. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14937. (when (and vals (string-match "\\S-" vals))
  14938. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14939. (setq vals (mapcar (lambda (x)
  14940. (cond ((stringp x) x)
  14941. ((numberp x) (number-to-string x))
  14942. ((symbolp x) (symbol-name x))
  14943. (t "???")))
  14944. vals)))))
  14945. (if table (mapcar 'list vals) vals)))
  14946. (defun org-property-previous-allowed-value (&optional previous)
  14947. "Switch to the next allowed value for this property."
  14948. (interactive)
  14949. (org-property-next-allowed-value t))
  14950. (defun org-property-next-allowed-value (&optional previous)
  14951. "Switch to the next allowed value for this property."
  14952. (interactive)
  14953. (unless (org-at-property-p)
  14954. (error "Not at a property"))
  14955. (let* ((key (match-string 2))
  14956. (value (match-string 3))
  14957. (allowed (or (org-property-get-allowed-values (point) key)
  14958. (and (member value '("[ ]" "[-]" "[X]"))
  14959. '("[ ]" "[X]"))))
  14960. nval)
  14961. (unless allowed
  14962. (error "Allowed values for this property have not been defined"))
  14963. (if previous (setq allowed (reverse allowed)))
  14964. (if (member value allowed)
  14965. (setq nval (car (cdr (member value allowed)))))
  14966. (setq nval (or nval (car allowed)))
  14967. (if (equal nval value)
  14968. (error "Only one allowed value for this property"))
  14969. (org-at-property-p)
  14970. (replace-match (concat " :" key ": " nval) t t)
  14971. (org-indent-line-function)
  14972. (beginning-of-line 1)
  14973. (skip-chars-forward " \t")))
  14974. (defun org-find-entry-with-id (ident)
  14975. "Locate the entry that contains the ID property with exact value IDENT.
  14976. IDENT can be a string, a symbol or a number, this function will search for
  14977. the string representation of it.
  14978. Return the position where this entry starts, or nil if there is no such entry."
  14979. (let ((id (cond
  14980. ((stringp ident) ident)
  14981. ((symbol-name ident) (symbol-name ident))
  14982. ((numberp ident) (number-to-string ident))
  14983. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14984. (case-fold-search nil))
  14985. (save-excursion
  14986. (save-restriction
  14987. (goto-char (point-min))
  14988. (when (re-search-forward
  14989. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14990. nil t)
  14991. (org-back-to-heading)
  14992. (point))))))
  14993. ;;; Column View
  14994. (defvar org-columns-overlays nil
  14995. "Holds the list of current column overlays.")
  14996. (defvar org-columns-current-fmt nil
  14997. "Local variable, holds the currently active column format.")
  14998. (defvar org-columns-current-fmt-compiled nil
  14999. "Local variable, holds the currently active column format.
  15000. This is the compiled version of the format.")
  15001. (defvar org-columns-current-widths nil
  15002. "Loval variable, holds the currently widths of fields.")
  15003. (defvar org-columns-current-maxwidths nil
  15004. "Loval variable, holds the currently active maximum column widths.")
  15005. (defvar org-columns-begin-marker (make-marker)
  15006. "Points to the position where last a column creation command was called.")
  15007. (defvar org-columns-top-level-marker (make-marker)
  15008. "Points to the position where current columns region starts.")
  15009. (defvar org-columns-map (make-sparse-keymap)
  15010. "The keymap valid in column display.")
  15011. (defun org-columns-content ()
  15012. "Switch to contents view while in columns view."
  15013. (interactive)
  15014. (org-overview)
  15015. (org-content))
  15016. (org-defkey org-columns-map "c" 'org-columns-content)
  15017. (org-defkey org-columns-map "o" 'org-overview)
  15018. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15019. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15020. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15021. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15022. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15023. (org-defkey org-columns-map "q" 'org-columns-quit)
  15024. (org-defkey org-columns-map "r" 'org-columns-redo)
  15025. (org-defkey org-columns-map "g" 'org-columns-redo)
  15026. (org-defkey org-columns-map [left] 'backward-char)
  15027. (org-defkey org-columns-map "\M-b" 'backward-char)
  15028. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15029. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15030. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15031. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15032. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15033. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15034. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15035. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15036. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15037. (org-defkey org-columns-map ">" 'org-columns-widen)
  15038. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15039. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15040. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15041. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15042. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15043. '("Column"
  15044. ["Edit property" org-columns-edit-value t]
  15045. ["Next allowed value" org-columns-next-allowed-value t]
  15046. ["Previous allowed value" org-columns-previous-allowed-value t]
  15047. ["Show full value" org-columns-show-value t]
  15048. ["Edit allowed values" org-columns-edit-allowed t]
  15049. "--"
  15050. ["Edit column attributes" org-columns-edit-attributes t]
  15051. ["Increase column width" org-columns-widen t]
  15052. ["Decrease column width" org-columns-narrow t]
  15053. "--"
  15054. ["Move column right" org-columns-move-right t]
  15055. ["Move column left" org-columns-move-left t]
  15056. ["Add column" org-columns-new t]
  15057. ["Delete column" org-columns-delete t]
  15058. "--"
  15059. ["CONTENTS" org-columns-content t]
  15060. ["OVERVIEW" org-overview t]
  15061. ["Refresh columns display" org-columns-redo t]
  15062. "--"
  15063. ["Open link" org-columns-open-link t]
  15064. "--"
  15065. ["Quit" org-columns-quit t]))
  15066. (defun org-columns-new-overlay (beg end &optional string face)
  15067. "Create a new column overlay and add it to the list."
  15068. (let ((ov (org-make-overlay beg end)))
  15069. (org-overlay-put ov 'face (or face 'secondary-selection))
  15070. (org-overlay-display ov string face)
  15071. (push ov org-columns-overlays)
  15072. ov))
  15073. (defun org-columns-display-here (&optional props)
  15074. "Overlay the current line with column display."
  15075. (interactive)
  15076. (let* ((fmt org-columns-current-fmt-compiled)
  15077. (beg (point-at-bol))
  15078. (level-face (save-excursion
  15079. (beginning-of-line 1)
  15080. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15081. (org-get-level-face 2))))
  15082. (color (list :foreground
  15083. (face-attribute (or level-face 'default) :foreground)))
  15084. props pom property ass width f string ov column val modval)
  15085. ;; Check if the entry is in another buffer.
  15086. (unless props
  15087. (if (eq major-mode 'org-agenda-mode)
  15088. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15089. (get-text-property (point) 'org-marker))
  15090. props (if pom (org-entry-properties pom) nil))
  15091. (setq props (org-entry-properties nil))))
  15092. ;; Walk the format
  15093. (while (setq column (pop fmt))
  15094. (setq property (car column)
  15095. ass (if (equal property "ITEM")
  15096. (cons "ITEM"
  15097. (save-match-data
  15098. (org-no-properties
  15099. (org-remove-tabs
  15100. (buffer-substring-no-properties
  15101. (point-at-bol) (point-at-eol))))))
  15102. (assoc property props))
  15103. width (or (cdr (assoc property org-columns-current-maxwidths))
  15104. (nth 2 column)
  15105. (length property))
  15106. f (format "%%-%d.%ds | " width width)
  15107. val (or (cdr ass) "")
  15108. modval (if (equal property "ITEM")
  15109. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15110. string (format f (or modval val)))
  15111. ;; Create the overlay
  15112. (org-unmodified
  15113. (setq ov (org-columns-new-overlay
  15114. beg (setq beg (1+ beg)) string
  15115. (list color 'org-column)))
  15116. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15117. (org-overlay-put ov 'keymap org-columns-map)
  15118. (org-overlay-put ov 'org-columns-key property)
  15119. (org-overlay-put ov 'org-columns-value (cdr ass))
  15120. (org-overlay-put ov 'org-columns-value-modified modval)
  15121. (org-overlay-put ov 'org-columns-pom pom)
  15122. (org-overlay-put ov 'org-columns-format f))
  15123. (if (or (not (char-after beg))
  15124. (equal (char-after beg) ?\n))
  15125. (let ((inhibit-read-only t))
  15126. (save-excursion
  15127. (goto-char beg)
  15128. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15129. ;; Make the rest of the line disappear.
  15130. (org-unmodified
  15131. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15132. (org-overlay-put ov 'invisible t)
  15133. (org-overlay-put ov 'keymap org-columns-map)
  15134. (org-overlay-put ov 'intangible t)
  15135. (push ov org-columns-overlays)
  15136. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15137. (org-overlay-put ov 'keymap org-columns-map)
  15138. (push ov org-columns-overlays)
  15139. (let ((inhibit-read-only t))
  15140. (put-text-property (max (point-min) (1- (point-at-bol)))
  15141. (min (point-max) (1+ (point-at-eol)))
  15142. 'read-only "Type `e' to edit property")))))
  15143. (defvar org-previous-header-line-format nil
  15144. "The header line format before column view was turned on.")
  15145. (defvar org-columns-inhibit-recalculation nil
  15146. "Inhibit recomputing of columns on column view startup.")
  15147. (defvar header-line-format)
  15148. (defun org-columns-display-here-title ()
  15149. "Overlay the newline before the current line with the table title."
  15150. (interactive)
  15151. (let ((fmt org-columns-current-fmt-compiled)
  15152. string (title "")
  15153. property width f column str widths)
  15154. (while (setq column (pop fmt))
  15155. (setq property (car column)
  15156. str (or (nth 1 column) property)
  15157. width (or (cdr (assoc property org-columns-current-maxwidths))
  15158. (nth 2 column)
  15159. (length str))
  15160. widths (push width widths)
  15161. f (format "%%-%d.%ds | " width width)
  15162. string (format f str)
  15163. title (concat title string)))
  15164. (setq title (concat
  15165. (org-add-props " " nil 'display '(space :align-to 0))
  15166. (org-add-props title nil 'face '(:weight bold :underline t))))
  15167. (org-set-local 'org-previous-header-line-format header-line-format)
  15168. (org-set-local 'org-columns-current-widths (nreverse widths))
  15169. (setq header-line-format title)))
  15170. (defun org-columns-remove-overlays ()
  15171. "Remove all currently active column overlays."
  15172. (interactive)
  15173. (when (marker-buffer org-columns-begin-marker)
  15174. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15175. (when (local-variable-p 'org-previous-header-line-format)
  15176. (setq header-line-format org-previous-header-line-format)
  15177. (kill-local-variable 'org-previous-header-line-format))
  15178. (move-marker org-columns-begin-marker nil)
  15179. (move-marker org-columns-top-level-marker nil)
  15180. (org-unmodified
  15181. (mapc 'org-delete-overlay org-columns-overlays)
  15182. (setq org-columns-overlays nil)
  15183. (let ((inhibit-read-only t))
  15184. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15185. (defun org-columns-cleanup-item (item fmt)
  15186. "Remove from ITEM what is a column in the format FMT."
  15187. (if (not org-complex-heading-regexp)
  15188. item
  15189. (when (string-match org-complex-heading-regexp item)
  15190. (concat
  15191. (org-add-props (concat (match-string 1 item) " ") nil
  15192. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15193. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15194. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15195. " " (match-string 4 item)
  15196. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15197. (defun org-columns-show-value ()
  15198. "Show the full value of the property."
  15199. (interactive)
  15200. (let ((value (get-char-property (point) 'org-columns-value)))
  15201. (message "Value is: %s" (or value ""))))
  15202. (defun org-columns-quit ()
  15203. "Remove the column overlays and in this way exit column editing."
  15204. (interactive)
  15205. (org-unmodified
  15206. (org-columns-remove-overlays)
  15207. (let ((inhibit-read-only t))
  15208. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15209. (when (eq major-mode 'org-agenda-mode)
  15210. (message
  15211. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15212. (defun org-columns-check-computed ()
  15213. "Check if this column value is computed.
  15214. If yes, throw an error indicating that changing it does not make sense."
  15215. (let ((val (get-char-property (point) 'org-columns-value)))
  15216. (when (and (stringp val)
  15217. (get-char-property 0 'org-computed val))
  15218. (error "This value is computed from the entry's children"))))
  15219. (defun org-columns-todo (&optional arg)
  15220. "Change the TODO state during column view."
  15221. (interactive "P")
  15222. (org-columns-edit-value "TODO"))
  15223. (defun org-columns-set-tags-or-toggle (&optional arg)
  15224. "Toggle checkbox at point, or set tags for current headline."
  15225. (interactive "P")
  15226. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15227. (get-char-property (point) 'org-columns-value))
  15228. (org-columns-next-allowed-value)
  15229. (org-columns-edit-value "TAGS")))
  15230. (defun org-columns-edit-value (&optional key)
  15231. "Edit the value of the property at point in column view.
  15232. Where possible, use the standard interface for changing this line."
  15233. (interactive)
  15234. (org-columns-check-computed)
  15235. (let* ((external-key key)
  15236. (col (current-column))
  15237. (key (or key (get-char-property (point) 'org-columns-key)))
  15238. (value (get-char-property (point) 'org-columns-value))
  15239. (bol (point-at-bol)) (eol (point-at-eol))
  15240. (pom (or (get-text-property bol 'org-hd-marker)
  15241. (point))) ; keep despite of compiler waring
  15242. (line-overlays
  15243. (delq nil (mapcar (lambda (x)
  15244. (and (eq (overlay-buffer x) (current-buffer))
  15245. (>= (overlay-start x) bol)
  15246. (<= (overlay-start x) eol)
  15247. x))
  15248. org-columns-overlays)))
  15249. nval eval allowed)
  15250. (cond
  15251. ((equal key "CLOCKSUM")
  15252. (error "This special column cannot be edited"))
  15253. ((equal key "ITEM")
  15254. (setq eval '(org-with-point-at pom
  15255. (org-edit-headline))))
  15256. ((equal key "TODO")
  15257. (setq eval '(org-with-point-at pom
  15258. (let ((current-prefix-arg
  15259. (if external-key current-prefix-arg '(4))))
  15260. (call-interactively 'org-todo)))))
  15261. ((equal key "PRIORITY")
  15262. (setq eval '(org-with-point-at pom
  15263. (call-interactively 'org-priority))))
  15264. ((equal key "TAGS")
  15265. (setq eval '(org-with-point-at pom
  15266. (let ((org-fast-tag-selection-single-key
  15267. (if (eq org-fast-tag-selection-single-key 'expert)
  15268. t org-fast-tag-selection-single-key)))
  15269. (call-interactively 'org-set-tags)))))
  15270. ((equal key "DEADLINE")
  15271. (setq eval '(org-with-point-at pom
  15272. (call-interactively 'org-deadline))))
  15273. ((equal key "SCHEDULED")
  15274. (setq eval '(org-with-point-at pom
  15275. (call-interactively 'org-schedule))))
  15276. (t
  15277. (setq allowed (org-property-get-allowed-values pom key 'table))
  15278. (if allowed
  15279. (setq nval (completing-read "Value: " allowed nil t))
  15280. (setq nval (read-string "Edit: " value)))
  15281. (setq nval (org-trim nval))
  15282. (when (not (equal nval value))
  15283. (setq eval '(org-entry-put pom key nval)))))
  15284. (when eval
  15285. (let ((inhibit-read-only t))
  15286. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15287. (unwind-protect
  15288. (progn
  15289. (setq org-columns-overlays
  15290. (org-delete-all line-overlays org-columns-overlays))
  15291. (mapc 'org-delete-overlay line-overlays)
  15292. (org-columns-eval eval))
  15293. (org-columns-display-here))))
  15294. (move-to-column col)
  15295. (if (and (org-mode-p)
  15296. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15297. (org-columns-update key))))
  15298. (defun org-edit-headline () ; FIXME: this is not columns specific
  15299. "Edit the current headline, the part without TODO keyword, TAGS."
  15300. (org-back-to-heading)
  15301. (when (looking-at org-todo-line-regexp)
  15302. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15303. (txt (match-string 3))
  15304. (post "")
  15305. txt2)
  15306. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15307. (setq post (match-string 0 txt)
  15308. txt (substring txt 0 (match-beginning 0))))
  15309. (setq txt2 (read-string "Edit: " txt))
  15310. (when (not (equal txt txt2))
  15311. (beginning-of-line 1)
  15312. (insert pre txt2 post)
  15313. (delete-region (point) (point-at-eol))
  15314. (org-set-tags nil t)))))
  15315. (defun org-columns-edit-allowed ()
  15316. "Edit the list of allowed values for the current property."
  15317. (interactive)
  15318. (let* ((key (get-char-property (point) 'org-columns-key))
  15319. (key1 (concat key "_ALL"))
  15320. (allowed (org-entry-get (point) key1 t))
  15321. nval)
  15322. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15323. (setq nval (read-string "Allowed: " allowed))
  15324. (org-entry-put
  15325. (cond ((marker-position org-entry-property-inherited-from)
  15326. org-entry-property-inherited-from)
  15327. ((marker-position org-columns-top-level-marker)
  15328. org-columns-top-level-marker))
  15329. key1 nval)))
  15330. (defmacro org-no-warnings (&rest body)
  15331. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15332. (defun org-columns-eval (form)
  15333. (let (hidep)
  15334. (save-excursion
  15335. (beginning-of-line 1)
  15336. ;; `next-line' is needed here, because it skips invisible line.
  15337. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15338. (setq hidep (org-on-heading-p 1)))
  15339. (eval form)
  15340. (and hidep (hide-entry))))
  15341. (defun org-columns-previous-allowed-value ()
  15342. "Switch to the previous allowed value for this column."
  15343. (interactive)
  15344. (org-columns-next-allowed-value t))
  15345. (defun org-columns-next-allowed-value (&optional previous)
  15346. "Switch to the next allowed value for this column."
  15347. (interactive)
  15348. (org-columns-check-computed)
  15349. (let* ((col (current-column))
  15350. (key (get-char-property (point) 'org-columns-key))
  15351. (value (get-char-property (point) 'org-columns-value))
  15352. (bol (point-at-bol)) (eol (point-at-eol))
  15353. (pom (or (get-text-property bol 'org-hd-marker)
  15354. (point))) ; keep despite of compiler waring
  15355. (line-overlays
  15356. (delq nil (mapcar (lambda (x)
  15357. (and (eq (overlay-buffer x) (current-buffer))
  15358. (>= (overlay-start x) bol)
  15359. (<= (overlay-start x) eol)
  15360. x))
  15361. org-columns-overlays)))
  15362. (allowed (or (org-property-get-allowed-values pom key)
  15363. (and (memq
  15364. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15365. '(checkbox checkbox-n-of-m checkbox-percent))
  15366. '("[ ]" "[X]"))))
  15367. nval)
  15368. (when (equal key "ITEM")
  15369. (error "Cannot edit item headline from here"))
  15370. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15371. (error "Allowed values for this property have not been defined"))
  15372. (if (member key '("SCHEDULED" "DEADLINE"))
  15373. (setq nval (if previous 'earlier 'later))
  15374. (if previous (setq allowed (reverse allowed)))
  15375. (if (member value allowed)
  15376. (setq nval (car (cdr (member value allowed)))))
  15377. (setq nval (or nval (car allowed)))
  15378. (if (equal nval value)
  15379. (error "Only one allowed value for this property")))
  15380. (let ((inhibit-read-only t))
  15381. (remove-text-properties (1- bol) eol '(read-only t))
  15382. (unwind-protect
  15383. (progn
  15384. (setq org-columns-overlays
  15385. (org-delete-all line-overlays org-columns-overlays))
  15386. (mapc 'org-delete-overlay line-overlays)
  15387. (org-columns-eval '(org-entry-put pom key nval)))
  15388. (org-columns-display-here)))
  15389. (move-to-column col)
  15390. (if (and (org-mode-p)
  15391. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15392. (org-columns-update key))))
  15393. (defun org-verify-version (task)
  15394. (cond
  15395. ((eq task 'columns)
  15396. (if (or (featurep 'xemacs)
  15397. (< emacs-major-version 22))
  15398. (error "Emacs 22 is required for the columns feature")))))
  15399. (defun org-columns-open-link (&optional arg)
  15400. (interactive "P")
  15401. (let ((key (get-char-property (point) 'org-columns-key))
  15402. (value (get-char-property (point) 'org-columns-value)))
  15403. (org-open-link-from-string arg)))
  15404. (defun org-open-link-from-string (s &optional arg)
  15405. "Open a link in the string S, as if it was in Org-mode."
  15406. (interactive)
  15407. (with-temp-buffer
  15408. (let ((org-inhibit-startup t))
  15409. (org-mode)
  15410. (insert s)
  15411. (goto-char (point-min))
  15412. (org-open-at-point arg))))
  15413. (defun org-columns-get-format-and-top-level ()
  15414. (let (fmt)
  15415. (when (condition-case nil (org-back-to-heading) (error nil))
  15416. (move-marker org-entry-property-inherited-from nil)
  15417. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15418. (setq fmt (or fmt org-columns-default-format))
  15419. (org-set-local 'org-columns-current-fmt fmt)
  15420. (org-columns-compile-format fmt)
  15421. (if (marker-position org-entry-property-inherited-from)
  15422. (move-marker org-columns-top-level-marker
  15423. org-entry-property-inherited-from)
  15424. (move-marker org-columns-top-level-marker (point)))
  15425. fmt))
  15426. (defun org-columns ()
  15427. "Turn on column view on an org-mode file."
  15428. (interactive)
  15429. (org-verify-version 'columns)
  15430. (org-columns-remove-overlays)
  15431. (move-marker org-columns-begin-marker (point))
  15432. (let (beg end fmt cache maxwidths clocksump)
  15433. (setq fmt (org-columns-get-format-and-top-level))
  15434. (save-excursion
  15435. (goto-char org-columns-top-level-marker)
  15436. (setq beg (point))
  15437. (unless org-columns-inhibit-recalculation
  15438. (org-columns-compute-all))
  15439. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15440. (point-max)))
  15441. ;; Get and cache the properties
  15442. (goto-char beg)
  15443. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15444. (setq clocksump t)
  15445. (save-excursion
  15446. (save-restriction
  15447. (narrow-to-region beg end)
  15448. (org-clock-sum))))
  15449. (while (re-search-forward (concat "^" outline-regexp) end t)
  15450. (push (cons (org-current-line) (org-entry-properties)) cache))
  15451. (when cache
  15452. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15453. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15454. (org-columns-display-here-title)
  15455. (mapc (lambda (x)
  15456. (goto-line (car x))
  15457. (org-columns-display-here (cdr x)))
  15458. cache)))))
  15459. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15460. "Insert a new column, to the leeft o the current column."
  15461. (interactive)
  15462. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15463. cell)
  15464. (setq prop (completing-read
  15465. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15466. nil nil prop))
  15467. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15468. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15469. (if (string-match "\\S-" width)
  15470. (setq width (string-to-number width))
  15471. (setq width nil))
  15472. (setq fmt (completing-read "Summary [none]: "
  15473. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15474. nil t))
  15475. (if (string-match "\\S-" fmt)
  15476. (setq fmt (intern fmt))
  15477. (setq fmt nil))
  15478. (if (eq fmt 'none) (setq fmt nil))
  15479. (if editp
  15480. (progn
  15481. (setcar editp prop)
  15482. (setcdr editp (list title width nil fmt)))
  15483. (setq cell (nthcdr (1- (current-column))
  15484. org-columns-current-fmt-compiled))
  15485. (setcdr cell (cons (list prop title width nil fmt)
  15486. (cdr cell))))
  15487. (org-columns-store-format)
  15488. (org-columns-redo)))
  15489. (defun org-columns-delete ()
  15490. "Delete the column at point from columns view."
  15491. (interactive)
  15492. (let* ((n (current-column))
  15493. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15494. (when (y-or-n-p
  15495. (format "Are you sure you want to remove column \"%s\"? " title))
  15496. (setq org-columns-current-fmt-compiled
  15497. (delq (nth n org-columns-current-fmt-compiled)
  15498. org-columns-current-fmt-compiled))
  15499. (org-columns-store-format)
  15500. (org-columns-redo)
  15501. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15502. (backward-char 1)))))
  15503. (defun org-columns-edit-attributes ()
  15504. "Edit the attributes of the current column."
  15505. (interactive)
  15506. (let* ((n (current-column))
  15507. (info (nth n org-columns-current-fmt-compiled)))
  15508. (apply 'org-columns-new info)))
  15509. (defun org-columns-widen (arg)
  15510. "Make the column wider by ARG characters."
  15511. (interactive "p")
  15512. (debug)
  15513. (let* ((n (current-column))
  15514. (entry (nth n org-columns-current-fmt-compiled))
  15515. (width (or (nth 2 entry)
  15516. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15517. (setq width (max 1 (+ width arg)))
  15518. (setcar (nthcdr 2 entry) width)
  15519. (org-columns-store-format)
  15520. (org-columns-redo)))
  15521. (defun org-columns-narrow (arg)
  15522. "Make the column nrrower by ARG characters."
  15523. (interactive "p")
  15524. (org-columns-widen (- arg)))
  15525. (defun org-columns-move-right ()
  15526. "Swap this column with the one to the right."
  15527. (interactive)
  15528. (let* ((n (current-column))
  15529. (cell (nthcdr n org-columns-current-fmt-compiled))
  15530. e)
  15531. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15532. (error "Cannot shift this column further to the right"))
  15533. (setq e (car cell))
  15534. (setcar cell (car (cdr cell)))
  15535. (setcdr cell (cons e (cdr (cdr cell))))
  15536. (org-columns-store-format)
  15537. (org-columns-redo)
  15538. (forward-char 1)))
  15539. (defun org-columns-move-left ()
  15540. "Swap this column with the one to the left."
  15541. (interactive)
  15542. (let* ((n (current-column)))
  15543. (when (= n 0)
  15544. (error "Cannot shift this column further to the left"))
  15545. (backward-char 1)
  15546. (org-columns-move-right)
  15547. (backward-char 1)))
  15548. (defun org-columns-store-format ()
  15549. "Store the text version of the current columns format in appropriate place.
  15550. This is either in the COLUMNS property of the node starting the current column
  15551. display, or in the #+COLUMNS line of the current buffer."
  15552. (let (fmt (cnt 0))
  15553. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15554. (org-set-local 'org-columns-current-fmt fmt)
  15555. (if (marker-position org-columns-top-level-marker)
  15556. (save-excursion
  15557. (goto-char org-columns-top-level-marker)
  15558. (if (and (org-at-heading-p)
  15559. (org-entry-get nil "COLUMNS"))
  15560. (org-entry-put nil "COLUMNS" fmt)
  15561. (goto-char (point-min))
  15562. ;; Overwrite all #+COLUMNS lines....
  15563. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15564. (setq cnt (1+ cnt))
  15565. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15566. (unless (> cnt 0)
  15567. (goto-char (point-min))
  15568. (or (org-on-heading-p t) (outline-next-heading))
  15569. (let ((inhibit-read-only t))
  15570. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15571. (org-set-local 'org-columns-default-format fmt))))))
  15572. (defvar org-overriding-columns-format nil
  15573. "When set, overrides any other definition.")
  15574. (defvar org-agenda-view-columns-initially nil
  15575. "When set, switch to columns view immediately after creating the agenda.")
  15576. (defun org-agenda-columns ()
  15577. "Turn on column view in the agenda."
  15578. (interactive)
  15579. (org-verify-version 'columns)
  15580. (org-columns-remove-overlays)
  15581. (move-marker org-columns-begin-marker (point))
  15582. (let (fmt cache maxwidths m)
  15583. (cond
  15584. ((and (local-variable-p 'org-overriding-columns-format)
  15585. org-overriding-columns-format)
  15586. (setq fmt org-overriding-columns-format))
  15587. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15588. (setq fmt (org-entry-get m "COLUMNS" t)))
  15589. ((and (boundp 'org-columns-current-fmt)
  15590. (local-variable-p 'org-columns-current-fmt)
  15591. org-columns-current-fmt)
  15592. (setq fmt org-columns-current-fmt))
  15593. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15594. (setq m (get-text-property m 'org-hd-marker))
  15595. (setq fmt (org-entry-get m "COLUMNS" t))))
  15596. (setq fmt (or fmt org-columns-default-format))
  15597. (org-set-local 'org-columns-current-fmt fmt)
  15598. (org-columns-compile-format fmt)
  15599. (save-excursion
  15600. ;; Get and cache the properties
  15601. (goto-char (point-min))
  15602. (while (not (eobp))
  15603. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15604. (get-text-property (point) 'org-marker)))
  15605. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15606. (beginning-of-line 2))
  15607. (when cache
  15608. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15609. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15610. (org-columns-display-here-title)
  15611. (mapc (lambda (x)
  15612. (goto-line (car x))
  15613. (org-columns-display-here (cdr x)))
  15614. cache)))))
  15615. (defun org-columns-get-autowidth-alist (s cache)
  15616. "Derive the maximum column widths from the format and the cache."
  15617. (let ((start 0) rtn)
  15618. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15619. (push (cons (match-string 1 s) 1) rtn)
  15620. (setq start (match-end 0)))
  15621. (mapc (lambda (x)
  15622. (setcdr x (apply 'max
  15623. (mapcar
  15624. (lambda (y)
  15625. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15626. cache))))
  15627. rtn)
  15628. rtn))
  15629. (defun org-columns-compute-all ()
  15630. "Compute all columns that have operators defined."
  15631. (org-unmodified
  15632. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15633. (let ((columns org-columns-current-fmt-compiled) col)
  15634. (while (setq col (pop columns))
  15635. (when (nth 3 col)
  15636. (save-excursion
  15637. (org-columns-compute (car col)))))))
  15638. (defun org-columns-update (property)
  15639. "Recompute PROPERTY, and update the columns display for it."
  15640. (org-columns-compute property)
  15641. (let (fmt val pos)
  15642. (save-excursion
  15643. (mapc (lambda (ov)
  15644. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15645. (setq pos (org-overlay-start ov))
  15646. (goto-char pos)
  15647. (when (setq val (cdr (assoc property
  15648. (get-text-property
  15649. (point-at-bol) 'org-summaries))))
  15650. (setq fmt (org-overlay-get ov 'org-columns-format))
  15651. (org-overlay-put ov 'org-columns-value val)
  15652. (org-overlay-put ov 'display (format fmt val)))))
  15653. org-columns-overlays))))
  15654. (defun org-columns-compute (property)
  15655. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15656. (interactive)
  15657. (let* ((re (concat "^" outline-regexp))
  15658. (lmax 30) ; Does anyone use deeper levels???
  15659. (lsum (make-vector lmax 0))
  15660. (lflag (make-vector lmax nil))
  15661. (level 0)
  15662. (ass (assoc property org-columns-current-fmt-compiled))
  15663. (format (nth 4 ass))
  15664. (printf (nth 5 ass))
  15665. (beg org-columns-top-level-marker)
  15666. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15667. (save-excursion
  15668. ;; Find the region to compute
  15669. (goto-char beg)
  15670. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15671. (goto-char end)
  15672. ;; Walk the tree from the back and do the computations
  15673. (while (re-search-backward re beg t)
  15674. (setq sumpos (match-beginning 0)
  15675. last-level level
  15676. level (org-outline-level)
  15677. val (org-entry-get nil property)
  15678. valflag (and val (string-match "\\S-" val)))
  15679. (cond
  15680. ((< level last-level)
  15681. ;; put the sum of lower levels here as a property
  15682. (setq sum (aref lsum last-level) ; current sum
  15683. flag (aref lflag last-level) ; any valid entries from children?
  15684. str (org-column-number-to-string sum format printf)
  15685. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15686. useval (if flag str1 (if valflag val ""))
  15687. sum-alist (get-text-property sumpos 'org-summaries))
  15688. (if (assoc property sum-alist)
  15689. (setcdr (assoc property sum-alist) useval)
  15690. (push (cons property useval) sum-alist)
  15691. (org-unmodified
  15692. (add-text-properties sumpos (1+ sumpos)
  15693. (list 'org-summaries sum-alist))))
  15694. (when val
  15695. (org-entry-put nil property (if flag str val)))
  15696. ;; add current to current level accumulator
  15697. (when (or flag valflag)
  15698. (aset lsum level (+ (aref lsum level)
  15699. (if flag sum (org-column-string-to-number
  15700. (if flag str val) format))))
  15701. (aset lflag level t))
  15702. ;; clear accumulators for deeper levels
  15703. (loop for l from (1+ level) to (1- lmax) do
  15704. (aset lsum l 0)
  15705. (aset lflag l nil)))
  15706. ((>= level last-level)
  15707. ;; add what we have here to the accumulator for this level
  15708. (aset lsum level (+ (aref lsum level)
  15709. (org-column-string-to-number (or val "0") format)))
  15710. (and valflag (aset lflag level t)))
  15711. (t (error "This should not happen")))))))
  15712. (defun org-columns-redo ()
  15713. "Construct the column display again."
  15714. (interactive)
  15715. (message "Recomputing columns...")
  15716. (save-excursion
  15717. (if (marker-position org-columns-begin-marker)
  15718. (goto-char org-columns-begin-marker))
  15719. (org-columns-remove-overlays)
  15720. (if (org-mode-p)
  15721. (call-interactively 'org-columns)
  15722. (call-interactively 'org-agenda-columns)))
  15723. (message "Recomputing columns...done"))
  15724. (defun org-columns-not-in-agenda ()
  15725. (if (eq major-mode 'org-agenda-mode)
  15726. (error "This command is only allowed in Org-mode buffers")))
  15727. (defun org-string-to-number (s)
  15728. "Convert string to number, and interpret hh:mm:ss."
  15729. (if (not (string-match ":" s))
  15730. (string-to-number s)
  15731. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15732. (while l
  15733. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15734. sum)))
  15735. (defun org-column-number-to-string (n fmt &optional printf)
  15736. "Convert a computed column number to a string value, according to FMT."
  15737. (cond
  15738. ((eq fmt 'add_times)
  15739. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15740. (format "%d:%02d" h m)))
  15741. ((eq fmt 'checkbox)
  15742. (cond ((= n (floor n)) "[X]")
  15743. ((> n 1.) "[-]")
  15744. (t "[ ]")))
  15745. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15746. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15747. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15748. (printf (format printf n))
  15749. ((eq fmt 'currency)
  15750. (format "%.2f" n))
  15751. (t (number-to-string n))))
  15752. (defun org-nofm-to-completion (n m &optional percent)
  15753. (if (not percent)
  15754. (format "[%d/%d]" n m)
  15755. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15756. (defun org-column-string-to-number (s fmt)
  15757. "Convert a column value to a number that can be used for column computing."
  15758. (cond
  15759. ((string-match ":" s)
  15760. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15761. (while l
  15762. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15763. sum))
  15764. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15765. (if (equal s "[X]") 1. 0.000001))
  15766. (t (string-to-number s))))
  15767. (defun org-columns-uncompile-format (cfmt)
  15768. "Turn the compiled columns format back into a string representation."
  15769. (let ((rtn "") e s prop title op width fmt printf)
  15770. (while (setq e (pop cfmt))
  15771. (setq prop (car e)
  15772. title (nth 1 e)
  15773. width (nth 2 e)
  15774. op (nth 3 e)
  15775. fmt (nth 4 e)
  15776. printf (nth 5 e))
  15777. (cond
  15778. ((eq fmt 'add_times) (setq op ":"))
  15779. ((eq fmt 'checkbox) (setq op "X"))
  15780. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15781. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15782. ((eq fmt 'add_numbers) (setq op "+"))
  15783. ((eq fmt 'currency) (setq op "$")))
  15784. (if (and op printf) (setq op (concat op ";" printf)))
  15785. (if (equal title prop) (setq title nil))
  15786. (setq s (concat "%" (if width (number-to-string width))
  15787. prop
  15788. (if title (concat "(" title ")"))
  15789. (if op (concat "{" op "}"))))
  15790. (setq rtn (concat rtn " " s)))
  15791. (org-trim rtn)))
  15792. (defun org-columns-compile-format (fmt)
  15793. "Turn a column format string into an alist of specifications.
  15794. The alist has one entry for each column in the format. The elements of
  15795. that list are:
  15796. property the property
  15797. title the title field for the columns
  15798. width the column width in characters, can be nil for automatic
  15799. operator the operator if any
  15800. format the output format for computed results, derived from operator
  15801. printf a printf format for computed values"
  15802. (let ((start 0) width prop title op f printf)
  15803. (setq org-columns-current-fmt-compiled nil)
  15804. (while (string-match
  15805. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15806. fmt start)
  15807. (setq start (match-end 0)
  15808. width (match-string 1 fmt)
  15809. prop (match-string 2 fmt)
  15810. title (or (match-string 3 fmt) prop)
  15811. op (match-string 4 fmt)
  15812. f nil
  15813. printf nil)
  15814. (if width (setq width (string-to-number width)))
  15815. (when (and op (string-match ";" op))
  15816. (setq printf (substring op (match-end 0))
  15817. op (substring op 0 (match-beginning 0))))
  15818. (cond
  15819. ((equal op "+") (setq f 'add_numbers))
  15820. ((equal op "$") (setq f 'currency))
  15821. ((equal op ":") (setq f 'add_times))
  15822. ((equal op "X") (setq f 'checkbox))
  15823. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15824. ((equal op "X%") (setq f 'checkbox-percent))
  15825. )
  15826. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15827. (setq org-columns-current-fmt-compiled
  15828. (nreverse org-columns-current-fmt-compiled))))
  15829. ;;; Dynamic block for Column view
  15830. (defun org-columns-capture-view ()
  15831. "Get the column view of the current buffer and return it as a list.
  15832. The list will contains the title row and all other rows. Each row is
  15833. a list of fields."
  15834. (save-excursion
  15835. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15836. (n (length title)) row tbl)
  15837. (goto-char (point-min))
  15838. (while (re-search-forward "^\\*+ " nil t)
  15839. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15840. (setq row nil)
  15841. (loop for i from 0 to (1- n) do
  15842. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15843. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15844. "")
  15845. row))
  15846. (setq row (nreverse row))
  15847. (push row tbl)))
  15848. (append (list title 'hline) (nreverse tbl)))))
  15849. (defun org-dblock-write:columnview (params)
  15850. "Write the column view table.
  15851. PARAMS is a property list of parameters:
  15852. :width enforce same column widths with <N> specifiers.
  15853. :id the :ID: property of the entry where the columns view
  15854. should be built, as a string. When `local', call locally.
  15855. When `global' call column view with the cursor at the beginning
  15856. of the buffer (usually this means that the whole buffer switches
  15857. to column view).
  15858. :hlines When t, insert a hline before each item. When a number, insert
  15859. a hline before each level <= that number.
  15860. :vlines When t, make each column a colgroup to enforce vertical lines."
  15861. (let ((pos (move-marker (make-marker) (point)))
  15862. (hlines (plist-get params :hlines))
  15863. (vlines (plist-get params :vlines))
  15864. tbl id idpos nfields tmp)
  15865. (save-excursion
  15866. (save-restriction
  15867. (when (setq id (plist-get params :id))
  15868. (cond ((not id) nil)
  15869. ((eq id 'global) (goto-char (point-min)))
  15870. ((eq id 'local) nil)
  15871. ((setq idpos (org-find-entry-with-id id))
  15872. (goto-char idpos))
  15873. (t (error "Cannot find entry with :ID: %s" id))))
  15874. (org-columns)
  15875. (setq tbl (org-columns-capture-view))
  15876. (setq nfields (length (car tbl)))
  15877. (org-columns-quit)))
  15878. (goto-char pos)
  15879. (move-marker pos nil)
  15880. (when tbl
  15881. (when (plist-get params :hlines)
  15882. (setq tmp nil)
  15883. (while tbl
  15884. (if (eq (car tbl) 'hline)
  15885. (push (pop tbl) tmp)
  15886. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15887. (if (and (not (eq (car tmp) 'hline))
  15888. (or (eq hlines t)
  15889. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15890. (push 'hline tmp)))
  15891. (push (pop tbl) tmp)))
  15892. (setq tbl (nreverse tmp)))
  15893. (when vlines
  15894. (setq tbl (mapcar (lambda (x)
  15895. (if (eq 'hline x) x (cons "" x)))
  15896. tbl))
  15897. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15898. (setq pos (point))
  15899. (insert (org-listtable-to-string tbl))
  15900. (when (plist-get params :width)
  15901. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15902. org-columns-current-widths "|")))
  15903. (goto-char pos)
  15904. (org-table-align))))
  15905. (defun org-listtable-to-string (tbl)
  15906. "Convert a listtable TBL to a string that contains the Org-mode table.
  15907. The table still need to be alligned. The resulting string has no leading
  15908. and tailing newline characters."
  15909. (mapconcat
  15910. (lambda (x)
  15911. (cond
  15912. ((listp x)
  15913. (concat "|" (mapconcat 'identity x "|") "|"))
  15914. ((eq x 'hline) "|-|")
  15915. (t (error "Garbage in listtable: %s" x))))
  15916. tbl "\n"))
  15917. (defun org-insert-columns-dblock ()
  15918. "Create a dynamic block capturing a column view table."
  15919. (interactive)
  15920. (let ((defaults '(:name "columnview" :hlines 1))
  15921. (id (completing-read
  15922. "Capture columns (local, global, entry with :ID: property) [local]: "
  15923. (append '(("global") ("local"))
  15924. (mapcar 'list (org-property-values "ID"))))))
  15925. (if (equal id "") (setq id 'local))
  15926. (if (equal id "global") (setq id 'global))
  15927. (setq defaults (append defaults (list :id id)))
  15928. (org-create-dblock defaults)
  15929. (org-update-dblock)))
  15930. ;;;; Timestamps
  15931. (defvar org-last-changed-timestamp nil)
  15932. (defvar org-time-was-given) ; dynamically scoped parameter
  15933. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15934. (defvar org-ts-what) ; dynamically scoped parameter
  15935. (defun org-time-stamp (arg)
  15936. "Prompt for a date/time and insert a time stamp.
  15937. If the user specifies a time like HH:MM, or if this command is called
  15938. with a prefix argument, the time stamp will contain date and time.
  15939. Otherwise, only the date will be included. All parts of a date not
  15940. specified by the user will be filled in from the current date/time.
  15941. So if you press just return without typing anything, the time stamp
  15942. will represent the current date/time. If there is already a timestamp
  15943. at the cursor, it will be modified."
  15944. (interactive "P")
  15945. (let* ((ts nil)
  15946. (default-time
  15947. ;; Default time is either today, or, when entering a range,
  15948. ;; the range start.
  15949. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15950. (save-excursion
  15951. (re-search-backward
  15952. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15953. (- (point) 20) t)))
  15954. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15955. (current-time)))
  15956. (default-input (and ts (org-get-compact-tod ts)))
  15957. org-time-was-given org-end-time-was-given time)
  15958. (cond
  15959. ((and (org-at-timestamp-p)
  15960. (eq last-command 'org-time-stamp)
  15961. (eq this-command 'org-time-stamp))
  15962. (insert "--")
  15963. (setq time (let ((this-command this-command))
  15964. (org-read-date arg 'totime nil nil default-time default-input)))
  15965. (org-insert-time-stamp time (or org-time-was-given arg)))
  15966. ((org-at-timestamp-p)
  15967. (setq time (let ((this-command this-command))
  15968. (org-read-date arg 'totime nil nil default-time default-input)))
  15969. (when (org-at-timestamp-p) ; just to get the match data
  15970. (replace-match "")
  15971. (setq org-last-changed-timestamp
  15972. (org-insert-time-stamp
  15973. time (or org-time-was-given arg)
  15974. nil nil nil (list org-end-time-was-given))))
  15975. (message "Timestamp updated"))
  15976. (t
  15977. (setq time (let ((this-command this-command))
  15978. (org-read-date arg 'totime nil nil default-time default-input)))
  15979. (org-insert-time-stamp time (or org-time-was-given arg)
  15980. nil nil nil (list org-end-time-was-given))))))
  15981. ;; FIXME: can we use this for something else????
  15982. ;; like computing time differences?????
  15983. (defun org-get-compact-tod (s)
  15984. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15985. (let* ((t1 (match-string 1 s))
  15986. (h1 (string-to-number (match-string 2 s)))
  15987. (m1 (string-to-number (match-string 3 s)))
  15988. (t2 (and (match-end 4) (match-string 5 s)))
  15989. (h2 (and t2 (string-to-number (match-string 6 s))))
  15990. (m2 (and t2 (string-to-number (match-string 7 s))))
  15991. dh dm)
  15992. (if (not t2)
  15993. t1
  15994. (setq dh (- h2 h1) dm (- m2 m1))
  15995. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15996. (concat t1 "+" (number-to-string dh)
  15997. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  15998. (defun org-time-stamp-inactive (&optional arg)
  15999. "Insert an inactive time stamp.
  16000. An inactive time stamp is enclosed in square brackets instead of angle
  16001. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16002. does not link to the calendar and cannot be changed with the S-cursor keys.
  16003. So these are more for recording a certain time/date."
  16004. (interactive "P")
  16005. (let (org-time-was-given org-end-time-was-given time)
  16006. (setq time (org-read-date arg 'totime))
  16007. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16008. nil nil (list org-end-time-was-given))))
  16009. (defvar org-date-ovl (org-make-overlay 1 1))
  16010. (org-overlay-put org-date-ovl 'face 'org-warning)
  16011. (org-detach-overlay org-date-ovl)
  16012. (defvar org-ans1) ; dynamically scoped parameter
  16013. (defvar org-ans2) ; dynamically scoped parameter
  16014. (defvar org-plain-time-of-day-regexp) ; defined below
  16015. (defvar org-read-date-overlay nil)
  16016. (defvar org-dcst nil) ; dynamically scoped
  16017. (defun org-read-date (&optional with-time to-time from-string prompt
  16018. default-time default-input)
  16019. "Read a date, possibly a time, and make things smooth for the user.
  16020. The prompt will suggest to enter an ISO date, but you can also enter anything
  16021. which will at least partially be understood by `parse-time-string'.
  16022. Unrecognized parts of the date will default to the current day, month, year,
  16023. hour and minute. If this command is called to replace a timestamp at point,
  16024. of to enter the second timestamp of a range, the default time is taken from the
  16025. existing stamp. For example,
  16026. 3-2-5 --> 2003-02-05
  16027. feb 15 --> currentyear-02-15
  16028. sep 12 9 --> 2009-09-12
  16029. 12:45 --> today 12:45
  16030. 22 sept 0:34 --> currentyear-09-22 0:34
  16031. 12 --> currentyear-currentmonth-12
  16032. Fri --> nearest Friday (today or later)
  16033. etc.
  16034. Furthermore you can specify a relative date by giving, as the *first* thing
  16035. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16036. change in days weeks, months, years.
  16037. With a single plus or minus, the date is relative to today. With a double
  16038. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16039. +4d --> four days from today
  16040. +4 --> same as above
  16041. +2w --> two weeks from today
  16042. ++5 --> five days from default date
  16043. The function understands only English month and weekday abbreviations,
  16044. but this can be configured with the variables `parse-time-months' and
  16045. `parse-time-weekdays'.
  16046. While prompting, a calendar is popped up - you can also select the
  16047. date with the mouse (button 1). The calendar shows a period of three
  16048. months. To scroll it to other months, use the keys `>' and `<'.
  16049. If you don't like the calendar, turn it off with
  16050. \(setq org-read-date-popup-calendar nil)
  16051. With optional argument TO-TIME, the date will immediately be converted
  16052. to an internal time.
  16053. With an optional argument WITH-TIME, the prompt will suggest to also
  16054. insert a time. Note that when WITH-TIME is not set, you can still
  16055. enter a time, and this function will inform the calling routine about
  16056. this change. The calling routine may then choose to change the format
  16057. used to insert the time stamp into the buffer to include the time.
  16058. With optional argument FROM-STRING, read from this string instead from
  16059. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16060. the time/date that is used for everything that is not specified by the
  16061. user."
  16062. (require 'parse-time)
  16063. (let* ((org-time-stamp-rounding-minutes
  16064. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16065. (org-dcst org-display-custom-times)
  16066. (ct (org-current-time))
  16067. (def (or default-time ct))
  16068. (defdecode (decode-time def))
  16069. (dummy (progn
  16070. (when (< (nth 2 defdecode) org-extend-today-until)
  16071. (setcar (nthcdr 2 defdecode) -1)
  16072. (setcar (nthcdr 1 defdecode) 59)
  16073. (setq def (apply 'encode-time defdecode)
  16074. defdecode (decode-time def)))))
  16075. (calendar-move-hook nil)
  16076. (view-diary-entries-initially nil)
  16077. (view-calendar-holidays-initially nil)
  16078. (timestr (format-time-string
  16079. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16080. (prompt (concat (if prompt (concat prompt " ") "")
  16081. (format "Date+time [%s]: " timestr)))
  16082. ans (org-ans0 "") org-ans1 org-ans2 final)
  16083. (cond
  16084. (from-string (setq ans from-string))
  16085. (org-read-date-popup-calendar
  16086. (save-excursion
  16087. (save-window-excursion
  16088. (calendar)
  16089. (calendar-forward-day (- (time-to-days def)
  16090. (calendar-absolute-from-gregorian
  16091. (calendar-current-date))))
  16092. (org-eval-in-calendar nil t)
  16093. (let* ((old-map (current-local-map))
  16094. (map (copy-keymap calendar-mode-map))
  16095. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16096. (org-defkey map (kbd "RET") 'org-calendar-select)
  16097. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16098. 'org-calendar-select-mouse)
  16099. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16100. 'org-calendar-select-mouse)
  16101. (org-defkey minibuffer-local-map [(meta shift left)]
  16102. (lambda () (interactive)
  16103. (org-eval-in-calendar '(calendar-backward-month 1))))
  16104. (org-defkey minibuffer-local-map [(meta shift right)]
  16105. (lambda () (interactive)
  16106. (org-eval-in-calendar '(calendar-forward-month 1))))
  16107. (org-defkey minibuffer-local-map [(meta shift up)]
  16108. (lambda () (interactive)
  16109. (org-eval-in-calendar '(calendar-backward-year 1))))
  16110. (org-defkey minibuffer-local-map [(meta shift down)]
  16111. (lambda () (interactive)
  16112. (org-eval-in-calendar '(calendar-forward-year 1))))
  16113. (org-defkey minibuffer-local-map [(shift up)]
  16114. (lambda () (interactive)
  16115. (org-eval-in-calendar '(calendar-backward-week 1))))
  16116. (org-defkey minibuffer-local-map [(shift down)]
  16117. (lambda () (interactive)
  16118. (org-eval-in-calendar '(calendar-forward-week 1))))
  16119. (org-defkey minibuffer-local-map [(shift left)]
  16120. (lambda () (interactive)
  16121. (org-eval-in-calendar '(calendar-backward-day 1))))
  16122. (org-defkey minibuffer-local-map [(shift right)]
  16123. (lambda () (interactive)
  16124. (org-eval-in-calendar '(calendar-forward-day 1))))
  16125. (org-defkey minibuffer-local-map ">"
  16126. (lambda () (interactive)
  16127. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16128. (org-defkey minibuffer-local-map "<"
  16129. (lambda () (interactive)
  16130. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16131. (unwind-protect
  16132. (progn
  16133. (use-local-map map)
  16134. (add-hook 'post-command-hook 'org-read-date-display)
  16135. (setq org-ans0 (read-string prompt default-input nil nil))
  16136. ;; org-ans0: from prompt
  16137. ;; org-ans1: from mouse click
  16138. ;; org-ans2: from calendar motion
  16139. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16140. (remove-hook 'post-command-hook 'org-read-date-display)
  16141. (use-local-map old-map)
  16142. (when org-read-date-overlay
  16143. (org-delete-overlay org-read-date-overlay)
  16144. (setq org-read-date-overlay nil)))))))
  16145. (t ; Naked prompt only
  16146. (unwind-protect
  16147. (setq ans (read-string prompt default-input nil timestr))
  16148. (when org-read-date-overlay
  16149. (org-delete-overlay org-read-date-overlay)
  16150. (setq org-read-date-overlay nil)))))
  16151. (setq final (org-read-date-analyze ans def defdecode))
  16152. (if to-time
  16153. (apply 'encode-time final)
  16154. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16155. (format "%04d-%02d-%02d %02d:%02d"
  16156. (nth 5 final) (nth 4 final) (nth 3 final)
  16157. (nth 2 final) (nth 1 final))
  16158. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16159. (defvar def)
  16160. (defvar defdecode)
  16161. (defvar with-time)
  16162. (defun org-read-date-display ()
  16163. "Display the currrent date prompt interpretation in the minibuffer."
  16164. (when org-read-date-display-live
  16165. (when org-read-date-overlay
  16166. (org-delete-overlay org-read-date-overlay))
  16167. (let ((p (point)))
  16168. (end-of-line 1)
  16169. (while (not (equal (buffer-substring
  16170. (max (point-min) (- (point) 4)) (point))
  16171. " "))
  16172. (insert " "))
  16173. (goto-char p))
  16174. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16175. " " (or org-ans1 org-ans2)))
  16176. (org-end-time-was-given nil)
  16177. (f (org-read-date-analyze ans def defdecode))
  16178. (fmts (if org-dcst
  16179. org-time-stamp-custom-formats
  16180. org-time-stamp-formats))
  16181. (fmt (if (or with-time
  16182. (and (boundp 'org-time-was-given) org-time-was-given))
  16183. (cdr fmts)
  16184. (car fmts)))
  16185. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16186. (when (and org-end-time-was-given
  16187. (string-match org-plain-time-of-day-regexp txt))
  16188. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16189. org-end-time-was-given
  16190. (substring txt (match-end 0)))))
  16191. (setq org-read-date-overlay
  16192. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16193. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16194. (defun org-read-date-analyze (ans def defdecode)
  16195. "Analyze the combined answer of the date prompt."
  16196. ;; FIXME: cleanup and comment
  16197. (let (delta deltan deltaw deltadef year month day
  16198. hour minute second wday pm h2 m2 tl wday1)
  16199. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16200. (setq ans (replace-match "" t t ans)
  16201. deltan (car delta)
  16202. deltaw (nth 1 delta)
  16203. deltadef (nth 2 delta)))
  16204. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16205. (when (string-match
  16206. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16207. (setq year (if (match-end 2)
  16208. (string-to-number (match-string 2 ans))
  16209. (string-to-number (format-time-string "%Y")))
  16210. month (string-to-number (match-string 3 ans))
  16211. day (string-to-number (match-string 4 ans)))
  16212. (if (< year 100) (setq year (+ 2000 year)))
  16213. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16214. t nil ans)))
  16215. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16216. ;; If there is a time with am/pm, and *no* time without it, we convert
  16217. ;; so that matching will be successful.
  16218. (loop for i from 1 to 2 do ; twice, for end time as well
  16219. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16220. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16221. (setq hour (string-to-number (match-string 1 ans))
  16222. minute (if (match-end 3)
  16223. (string-to-number (match-string 3 ans))
  16224. 0)
  16225. pm (equal ?p
  16226. (string-to-char (downcase (match-string 4 ans)))))
  16227. (if (and (= hour 12) (not pm))
  16228. (setq hour 0)
  16229. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16230. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16231. t t ans))))
  16232. ;; Check if a time range is given as a duration
  16233. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16234. (setq hour (string-to-number (match-string 1 ans))
  16235. h2 (+ hour (string-to-number (match-string 3 ans)))
  16236. minute (string-to-number (match-string 2 ans))
  16237. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16238. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16239. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16240. ;; Check if there is a time range
  16241. (when (boundp 'org-end-time-was-given)
  16242. (setq org-time-was-given nil)
  16243. (when (and (string-match org-plain-time-of-day-regexp ans)
  16244. (match-end 8))
  16245. (setq org-end-time-was-given (match-string 8 ans))
  16246. (setq ans (concat (substring ans 0 (match-beginning 7))
  16247. (substring ans (match-end 7))))))
  16248. (setq tl (parse-time-string ans)
  16249. day (or (nth 3 tl) (nth 3 defdecode))
  16250. month (or (nth 4 tl)
  16251. (if (and org-read-date-prefer-future
  16252. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16253. (1+ (nth 4 defdecode))
  16254. (nth 4 defdecode)))
  16255. year (or (nth 5 tl)
  16256. (if (and org-read-date-prefer-future
  16257. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16258. (1+ (nth 5 defdecode))
  16259. (nth 5 defdecode)))
  16260. hour (or (nth 2 tl) (nth 2 defdecode))
  16261. minute (or (nth 1 tl) (nth 1 defdecode))
  16262. second (or (nth 0 tl) 0)
  16263. wday (nth 6 tl))
  16264. (when deltan
  16265. (unless deltadef
  16266. (let ((now (decode-time (current-time))))
  16267. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16268. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16269. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16270. ((equal deltaw "m") (setq month (+ month deltan)))
  16271. ((equal deltaw "y") (setq year (+ year deltan)))))
  16272. (when (and wday (not (nth 3 tl)))
  16273. ;; Weekday was given, but no day, so pick that day in the week
  16274. ;; on or after the derived date.
  16275. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16276. (unless (equal wday wday1)
  16277. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16278. (if (and (boundp 'org-time-was-given)
  16279. (nth 2 tl))
  16280. (setq org-time-was-given t))
  16281. (if (< year 100) (setq year (+ 2000 year)))
  16282. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16283. (list second minute hour day month year)))
  16284. (defvar parse-time-weekdays)
  16285. (defun org-read-date-get-relative (s today default)
  16286. "Check string S for special relative date string.
  16287. TODAY and DEFAULT are internal times, for today and for a default.
  16288. Return shift list (N what def-flag)
  16289. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16290. N is the number of WHATs to shift.
  16291. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16292. the DEFAULT date rather than TODAY."
  16293. (when (string-match
  16294. (concat
  16295. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16296. "\\([0-9]+\\)?"
  16297. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16298. "\\([ \t]\\|$\\)") s)
  16299. (let* ((dir (if (match-end 1)
  16300. (string-to-char (substring (match-string 1 s) -1))
  16301. ?+))
  16302. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16303. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16304. (what (if (match-end 3) (match-string 3 s) "d"))
  16305. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16306. (date (if rel default today))
  16307. (wday (nth 6 (decode-time date)))
  16308. delta)
  16309. (if wday1
  16310. (progn
  16311. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16312. (if (= dir ?-) (setq delta (- delta 7)))
  16313. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16314. (list delta "d" rel))
  16315. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16316. (defun org-eval-in-calendar (form &optional keepdate)
  16317. "Eval FORM in the calendar window and return to current window.
  16318. Also, store the cursor date in variable org-ans2."
  16319. (let ((sw (selected-window)))
  16320. (select-window (get-buffer-window "*Calendar*"))
  16321. (eval form)
  16322. (when (and (not keepdate) (calendar-cursor-to-date))
  16323. (let* ((date (calendar-cursor-to-date))
  16324. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16325. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16326. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16327. (select-window sw)))
  16328. ; ;; Update the prompt to show new default date
  16329. ; (save-excursion
  16330. ; (goto-char (point-min))
  16331. ; (when (and org-ans2
  16332. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16333. ; (get-text-property (match-end 0) 'field))
  16334. ; (let ((inhibit-read-only t))
  16335. ; (replace-match (concat "[" org-ans2 "]") t t)
  16336. ; (add-text-properties (point-min) (1+ (match-end 0))
  16337. ; (text-properties-at (1+ (point-min)))))))))
  16338. (defun org-calendar-select ()
  16339. "Return to `org-read-date' with the date currently selected.
  16340. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16341. (interactive)
  16342. (when (calendar-cursor-to-date)
  16343. (let* ((date (calendar-cursor-to-date))
  16344. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16345. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16346. (if (active-minibuffer-window) (exit-minibuffer))))
  16347. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16348. "Insert a date stamp for the date given by the internal TIME.
  16349. WITH-HM means, use the stamp format that includes the time of the day.
  16350. INACTIVE means use square brackets instead of angular ones, so that the
  16351. stamp will not contribute to the agenda.
  16352. PRE and POST are optional strings to be inserted before and after the
  16353. stamp.
  16354. The command returns the inserted time stamp."
  16355. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16356. stamp)
  16357. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16358. (insert-before-markers (or pre ""))
  16359. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16360. (when (listp extra)
  16361. (setq extra (car extra))
  16362. (if (and (stringp extra)
  16363. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16364. (setq extra (format "-%02d:%02d"
  16365. (string-to-number (match-string 1 extra))
  16366. (string-to-number (match-string 2 extra))))
  16367. (setq extra nil)))
  16368. (when extra
  16369. (backward-char 1)
  16370. (insert-before-markers extra)
  16371. (forward-char 1))
  16372. (insert-before-markers (or post ""))
  16373. stamp))
  16374. (defun org-toggle-time-stamp-overlays ()
  16375. "Toggle the use of custom time stamp formats."
  16376. (interactive)
  16377. (setq org-display-custom-times (not org-display-custom-times))
  16378. (unless org-display-custom-times
  16379. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16380. (while (setq p (next-single-property-change p 'display))
  16381. (if (and (get-text-property p 'display)
  16382. (eq (get-text-property p 'face) 'org-date))
  16383. (remove-text-properties
  16384. p (setq p (next-single-property-change p 'display))
  16385. '(display t))))
  16386. (set-buffer-modified-p bmp)))
  16387. (if (featurep 'xemacs)
  16388. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16389. (org-restart-font-lock)
  16390. (setq org-table-may-need-update t)
  16391. (if org-display-custom-times
  16392. (message "Time stamps are overlayed with custom format")
  16393. (message "Time stamp overlays removed")))
  16394. (defun org-display-custom-time (beg end)
  16395. "Overlay modified time stamp format over timestamp between BED and END."
  16396. (let* ((ts (buffer-substring beg end))
  16397. t1 w1 with-hm tf time str w2 (off 0))
  16398. (save-match-data
  16399. (setq t1 (org-parse-time-string ts t))
  16400. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16401. (setq off (- (match-end 0) (match-beginning 0)))))
  16402. (setq end (- end off))
  16403. (setq w1 (- end beg)
  16404. with-hm (and (nth 1 t1) (nth 2 t1))
  16405. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16406. time (org-fix-decoded-time t1)
  16407. str (org-add-props
  16408. (format-time-string
  16409. (substring tf 1 -1) (apply 'encode-time time))
  16410. nil 'mouse-face 'highlight)
  16411. w2 (length str))
  16412. (if (not (= w2 w1))
  16413. (add-text-properties (1+ beg) (+ 2 beg)
  16414. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16415. (if (featurep 'xemacs)
  16416. (progn
  16417. (put-text-property beg end 'invisible t)
  16418. (put-text-property beg end 'end-glyph (make-glyph str)))
  16419. (put-text-property beg end 'display str))))
  16420. (defun org-translate-time (string)
  16421. "Translate all timestamps in STRING to custom format.
  16422. But do this only if the variable `org-display-custom-times' is set."
  16423. (when org-display-custom-times
  16424. (save-match-data
  16425. (let* ((start 0)
  16426. (re org-ts-regexp-both)
  16427. t1 with-hm inactive tf time str beg end)
  16428. (while (setq start (string-match re string start))
  16429. (setq beg (match-beginning 0)
  16430. end (match-end 0)
  16431. t1 (save-match-data
  16432. (org-parse-time-string (substring string beg end) t))
  16433. with-hm (and (nth 1 t1) (nth 2 t1))
  16434. inactive (equal (substring string beg (1+ beg)) "[")
  16435. tf (funcall (if with-hm 'cdr 'car)
  16436. org-time-stamp-custom-formats)
  16437. time (org-fix-decoded-time t1)
  16438. str (format-time-string
  16439. (concat
  16440. (if inactive "[" "<") (substring tf 1 -1)
  16441. (if inactive "]" ">"))
  16442. (apply 'encode-time time))
  16443. string (replace-match str t t string)
  16444. start (+ start (length str)))))))
  16445. string)
  16446. (defun org-fix-decoded-time (time)
  16447. "Set 0 instead of nil for the first 6 elements of time.
  16448. Don't touch the rest."
  16449. (let ((n 0))
  16450. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16451. (defun org-days-to-time (timestamp-string)
  16452. "Difference between TIMESTAMP-STRING and now in days."
  16453. (- (time-to-days (org-time-string-to-time timestamp-string))
  16454. (time-to-days (current-time))))
  16455. (defun org-deadline-close (timestamp-string &optional ndays)
  16456. "Is the time in TIMESTAMP-STRING close to the current date?"
  16457. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16458. (and (< (org-days-to-time timestamp-string) ndays)
  16459. (not (org-entry-is-done-p))))
  16460. (defun org-get-wdays (ts)
  16461. "Get the deadline lead time appropriate for timestring TS."
  16462. (cond
  16463. ((<= org-deadline-warning-days 0)
  16464. ;; 0 or negative, enforce this value no matter what
  16465. (- org-deadline-warning-days))
  16466. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16467. ;; lead time is specified.
  16468. (floor (* (string-to-number (match-string 1 ts))
  16469. (cdr (assoc (match-string 2 ts)
  16470. '(("d" . 1) ("w" . 7)
  16471. ("m" . 30.4) ("y" . 365.25)))))))
  16472. ;; go for the default.
  16473. (t org-deadline-warning-days)))
  16474. (defun org-calendar-select-mouse (ev)
  16475. "Return to `org-read-date' with the date currently selected.
  16476. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16477. (interactive "e")
  16478. (mouse-set-point ev)
  16479. (when (calendar-cursor-to-date)
  16480. (let* ((date (calendar-cursor-to-date))
  16481. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16482. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16483. (if (active-minibuffer-window) (exit-minibuffer))))
  16484. (defun org-check-deadlines (ndays)
  16485. "Check if there are any deadlines due or past due.
  16486. A deadline is considered due if it happens within `org-deadline-warning-days'
  16487. days from today's date. If the deadline appears in an entry marked DONE,
  16488. it is not shown. The prefix arg NDAYS can be used to test that many
  16489. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16490. (interactive "P")
  16491. (let* ((org-warn-days
  16492. (cond
  16493. ((equal ndays '(4)) 100000)
  16494. (ndays (prefix-numeric-value ndays))
  16495. (t (abs org-deadline-warning-days))))
  16496. (case-fold-search nil)
  16497. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16498. (callback
  16499. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16500. (message "%d deadlines past-due or due within %d days"
  16501. (org-occur regexp nil callback)
  16502. org-warn-days)))
  16503. (defun org-check-before-date (date)
  16504. "Check if there are deadlines or scheduled entries before DATE."
  16505. (interactive (list (org-read-date)))
  16506. (let ((case-fold-search nil)
  16507. (regexp (concat "\\<\\(" org-deadline-string
  16508. "\\|" org-scheduled-string
  16509. "\\) *<\\([^>]+\\)>"))
  16510. (callback
  16511. (lambda () (time-less-p
  16512. (org-time-string-to-time (match-string 2))
  16513. (org-time-string-to-time date)))))
  16514. (message "%d entries before %s"
  16515. (org-occur regexp nil callback) date)))
  16516. (defun org-evaluate-time-range (&optional to-buffer)
  16517. "Evaluate a time range by computing the difference between start and end.
  16518. Normally the result is just printed in the echo area, but with prefix arg
  16519. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16520. If the time range is actually in a table, the result is inserted into the
  16521. next column.
  16522. For time difference computation, a year is assumed to be exactly 365
  16523. days in order to avoid rounding problems."
  16524. (interactive "P")
  16525. (or
  16526. (org-clock-update-time-maybe)
  16527. (save-excursion
  16528. (unless (org-at-date-range-p t)
  16529. (goto-char (point-at-bol))
  16530. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16531. (if (not (org-at-date-range-p t))
  16532. (error "Not at a time-stamp range, and none found in current line")))
  16533. (let* ((ts1 (match-string 1))
  16534. (ts2 (match-string 2))
  16535. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16536. (match-end (match-end 0))
  16537. (time1 (org-time-string-to-time ts1))
  16538. (time2 (org-time-string-to-time ts2))
  16539. (t1 (time-to-seconds time1))
  16540. (t2 (time-to-seconds time2))
  16541. (diff (abs (- t2 t1)))
  16542. (negative (< (- t2 t1) 0))
  16543. ;; (ys (floor (* 365 24 60 60)))
  16544. (ds (* 24 60 60))
  16545. (hs (* 60 60))
  16546. (fy "%dy %dd %02d:%02d")
  16547. (fy1 "%dy %dd")
  16548. (fd "%dd %02d:%02d")
  16549. (fd1 "%dd")
  16550. (fh "%02d:%02d")
  16551. y d h m align)
  16552. (if havetime
  16553. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16554. y 0
  16555. d (floor (/ diff ds)) diff (mod diff ds)
  16556. h (floor (/ diff hs)) diff (mod diff hs)
  16557. m (floor (/ diff 60)))
  16558. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16559. y 0
  16560. d (floor (+ (/ diff ds) 0.5))
  16561. h 0 m 0))
  16562. (if (not to-buffer)
  16563. (message "%s" (org-make-tdiff-string y d h m))
  16564. (if (org-at-table-p)
  16565. (progn
  16566. (goto-char match-end)
  16567. (setq align t)
  16568. (and (looking-at " *|") (goto-char (match-end 0))))
  16569. (goto-char match-end))
  16570. (if (looking-at
  16571. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16572. (replace-match ""))
  16573. (if negative (insert " -"))
  16574. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16575. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16576. (insert " " (format fh h m))))
  16577. (if align (org-table-align))
  16578. (message "Time difference inserted")))))
  16579. (defun org-make-tdiff-string (y d h m)
  16580. (let ((fmt "")
  16581. (l nil))
  16582. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16583. l (push y l)))
  16584. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16585. l (push d l)))
  16586. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16587. l (push h l)))
  16588. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16589. l (push m l)))
  16590. (apply 'format fmt (nreverse l))))
  16591. (defun org-time-string-to-time (s)
  16592. (apply 'encode-time (org-parse-time-string s)))
  16593. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16594. "Convert a time stamp to an absolute day number.
  16595. If there is a specifyer for a cyclic time stamp, get the closest date to
  16596. DAYNR."
  16597. (cond
  16598. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16599. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16600. daynr
  16601. (+ daynr 1000)))
  16602. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16603. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16604. (time-to-days (current-time))) (match-string 0 s)
  16605. prefer))
  16606. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16607. (defun org-time-from-absolute (d)
  16608. "Return the time corresponding to date D.
  16609. D may be an absolute day number, or a calendar-type list (month day year)."
  16610. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16611. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16612. (defun org-calendar-holiday ()
  16613. "List of holidays, for Diary display in Org-mode."
  16614. (require 'holidays)
  16615. (let ((hl (funcall
  16616. (if (fboundp 'calendar-check-holidays)
  16617. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16618. (if hl (mapconcat 'identity hl "; "))))
  16619. (defun org-diary-sexp-entry (sexp entry date)
  16620. "Process a SEXP diary ENTRY for DATE."
  16621. (require 'diary-lib)
  16622. (let ((result (if calendar-debug-sexp
  16623. (let ((stack-trace-on-error t))
  16624. (eval (car (read-from-string sexp))))
  16625. (condition-case nil
  16626. (eval (car (read-from-string sexp)))
  16627. (error
  16628. (beep)
  16629. (message "Bad sexp at line %d in %s: %s"
  16630. (org-current-line)
  16631. (buffer-file-name) sexp)
  16632. (sleep-for 2))))))
  16633. (cond ((stringp result) result)
  16634. ((and (consp result)
  16635. (stringp (cdr result))) (cdr result))
  16636. (result entry)
  16637. (t nil))))
  16638. (defun org-diary-to-ical-string (frombuf)
  16639. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16640. This uses the icalendar.el library."
  16641. (let* ((tmpdir (if (featurep 'xemacs)
  16642. (temp-directory)
  16643. temporary-file-directory))
  16644. (tmpfile (make-temp-name
  16645. (expand-file-name "orgics" tmpdir)))
  16646. buf rtn b e)
  16647. (save-excursion
  16648. (set-buffer frombuf)
  16649. (icalendar-export-region (point-min) (point-max) tmpfile)
  16650. (setq buf (find-buffer-visiting tmpfile))
  16651. (set-buffer buf)
  16652. (goto-char (point-min))
  16653. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16654. (setq b (match-beginning 0)))
  16655. (goto-char (point-max))
  16656. (if (re-search-backward "^END:VEVENT" nil t)
  16657. (setq e (match-end 0)))
  16658. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16659. (kill-buffer buf)
  16660. (kill-buffer frombuf)
  16661. (delete-file tmpfile)
  16662. rtn))
  16663. (defun org-closest-date (start current change prefer)
  16664. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16665. When PREFER is `past' return a date that is either CURRENT or past.
  16666. When PREFER is `future', return a date that is either CURRENT or future."
  16667. ;; Make the proper lists from the dates
  16668. (catch 'exit
  16669. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16670. dn dw sday cday n1 n2
  16671. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16672. (setq start (org-date-to-gregorian start)
  16673. current (org-date-to-gregorian
  16674. (if org-agenda-repeating-timestamp-show-all
  16675. current
  16676. (time-to-days (current-time))))
  16677. sday (calendar-absolute-from-gregorian start)
  16678. cday (calendar-absolute-from-gregorian current))
  16679. (if (<= cday sday) (throw 'exit sday))
  16680. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16681. (setq dn (string-to-number (match-string 1 change))
  16682. dw (cdr (assoc (match-string 2 change) a1)))
  16683. (error "Invalid change specifyer: %s" change))
  16684. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16685. (cond
  16686. ((eq dw 'day)
  16687. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16688. n2 (+ n1 dn)))
  16689. ((eq dw 'year)
  16690. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16691. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16692. (setq date1 (list m d y1)
  16693. n1 (calendar-absolute-from-gregorian date1)
  16694. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16695. n2 (calendar-absolute-from-gregorian date2)))
  16696. ((eq dw 'month)
  16697. ;; approx number of month between the tow dates
  16698. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16699. ;; How often does dn fit in there?
  16700. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16701. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16702. m (+ m nm)
  16703. ny (floor (/ m 12))
  16704. y (+ y ny)
  16705. m (- m (* ny 12)))
  16706. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16707. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16708. (setq m2 (+ m dn) y2 y)
  16709. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16710. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16711. (while (< n2 cday)
  16712. (setq n1 n2 m m2 y y2)
  16713. (setq m2 (+ m dn) y2 y)
  16714. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16715. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16716. (if org-agenda-repeating-timestamp-show-all
  16717. (cond
  16718. ((eq prefer 'past) n1)
  16719. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16720. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16721. (cond
  16722. ((eq prefer 'past) n1)
  16723. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16724. (t (if (= cday n1) n1 n2)))))))
  16725. (defun org-date-to-gregorian (date)
  16726. "Turn any specification of DATE into a gregorian date for the calendar."
  16727. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16728. ((and (listp date) (= (length date) 3)) date)
  16729. ((stringp date)
  16730. (setq date (org-parse-time-string date))
  16731. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16732. ((listp date)
  16733. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16734. (defun org-parse-time-string (s &optional nodefault)
  16735. "Parse the standard Org-mode time string.
  16736. This should be a lot faster than the normal `parse-time-string'.
  16737. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16738. hour and minute fields will be nil if not given."
  16739. (if (string-match org-ts-regexp0 s)
  16740. (list 0
  16741. (if (or (match-beginning 8) (not nodefault))
  16742. (string-to-number (or (match-string 8 s) "0")))
  16743. (if (or (match-beginning 7) (not nodefault))
  16744. (string-to-number (or (match-string 7 s) "0")))
  16745. (string-to-number (match-string 4 s))
  16746. (string-to-number (match-string 3 s))
  16747. (string-to-number (match-string 2 s))
  16748. nil nil nil)
  16749. (make-list 9 0)))
  16750. (defun org-timestamp-up (&optional arg)
  16751. "Increase the date item at the cursor by one.
  16752. If the cursor is on the year, change the year. If it is on the month or
  16753. the day, change that.
  16754. With prefix ARG, change by that many units."
  16755. (interactive "p")
  16756. (org-timestamp-change (prefix-numeric-value arg)))
  16757. (defun org-timestamp-down (&optional arg)
  16758. "Decrease the date item at the cursor by one.
  16759. If the cursor is on the year, change the year. If it is on the month or
  16760. the day, change that.
  16761. With prefix ARG, change by that many units."
  16762. (interactive "p")
  16763. (org-timestamp-change (- (prefix-numeric-value arg))))
  16764. (defun org-timestamp-up-day (&optional arg)
  16765. "Increase the date in the time stamp by one day.
  16766. With prefix ARG, change that many days."
  16767. (interactive "p")
  16768. (if (and (not (org-at-timestamp-p t))
  16769. (org-on-heading-p))
  16770. (org-todo 'up)
  16771. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16772. (defun org-timestamp-down-day (&optional arg)
  16773. "Decrease the date in the time stamp by one day.
  16774. With prefix ARG, change that many days."
  16775. (interactive "p")
  16776. (if (and (not (org-at-timestamp-p t))
  16777. (org-on-heading-p))
  16778. (org-todo 'down)
  16779. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16780. (defsubst org-pos-in-match-range (pos n)
  16781. (and (match-beginning n)
  16782. (<= (match-beginning n) pos)
  16783. (>= (match-end n) pos)))
  16784. (defun org-at-timestamp-p (&optional inactive-ok)
  16785. "Determine if the cursor is in or at a timestamp."
  16786. (interactive)
  16787. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16788. (pos (point))
  16789. (ans (or (looking-at tsr)
  16790. (save-excursion
  16791. (skip-chars-backward "^[<\n\r\t")
  16792. (if (> (point) (point-min)) (backward-char 1))
  16793. (and (looking-at tsr)
  16794. (> (- (match-end 0) pos) -1))))))
  16795. (and ans
  16796. (boundp 'org-ts-what)
  16797. (setq org-ts-what
  16798. (cond
  16799. ((= pos (match-beginning 0)) 'bracket)
  16800. ((= pos (1- (match-end 0))) 'bracket)
  16801. ((org-pos-in-match-range pos 2) 'year)
  16802. ((org-pos-in-match-range pos 3) 'month)
  16803. ((org-pos-in-match-range pos 7) 'hour)
  16804. ((org-pos-in-match-range pos 8) 'minute)
  16805. ((or (org-pos-in-match-range pos 4)
  16806. (org-pos-in-match-range pos 5)) 'day)
  16807. ((and (> pos (or (match-end 8) (match-end 5)))
  16808. (< pos (match-end 0)))
  16809. (- pos (or (match-end 8) (match-end 5))))
  16810. (t 'day))))
  16811. ans))
  16812. (defun org-toggle-timestamp-type ()
  16813. ""
  16814. (interactive)
  16815. (when (org-at-timestamp-p t)
  16816. (save-excursion
  16817. (goto-char (match-beginning 0))
  16818. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16819. (goto-char (1- (match-end 0)))
  16820. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16821. (message "Timestamp is now %sactive"
  16822. (if (equal (char-before) ?>) "in" ""))))
  16823. (defun org-timestamp-change (n &optional what)
  16824. "Change the date in the time stamp at point.
  16825. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16826. `year', `minute', `second'. If WHAT is not given, the cursor position
  16827. in the timestamp determines what will be changed."
  16828. (let ((pos (point))
  16829. with-hm inactive
  16830. org-ts-what
  16831. extra
  16832. ts time time0)
  16833. (if (not (org-at-timestamp-p t))
  16834. (error "Not at a timestamp"))
  16835. (if (and (not what) (eq org-ts-what 'bracket))
  16836. (org-toggle-timestamp-type)
  16837. (if (and (not what) (not (eq org-ts-what 'day))
  16838. org-display-custom-times
  16839. (get-text-property (point) 'display)
  16840. (not (get-text-property (1- (point)) 'display)))
  16841. (setq org-ts-what 'day))
  16842. (setq org-ts-what (or what org-ts-what)
  16843. inactive (= (char-after (match-beginning 0)) ?\[)
  16844. ts (match-string 0))
  16845. (replace-match "")
  16846. (if (string-match
  16847. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16848. ts)
  16849. (setq extra (match-string 1 ts)))
  16850. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16851. (setq with-hm t))
  16852. (setq time0 (org-parse-time-string ts))
  16853. (setq time
  16854. (encode-time (or (car time0) 0)
  16855. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16856. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16857. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16858. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16859. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16860. (nthcdr 6 time0)))
  16861. (when (integerp org-ts-what)
  16862. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16863. (if (eq what 'calendar)
  16864. (let ((cal-date (org-get-date-from-calendar)))
  16865. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16866. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16867. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16868. (setcar time0 (or (car time0) 0))
  16869. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16870. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16871. (setq time (apply 'encode-time time0))))
  16872. (setq org-last-changed-timestamp
  16873. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16874. (org-clock-update-time-maybe)
  16875. (goto-char pos)
  16876. ;; Try to recenter the calendar window, if any
  16877. (if (and org-calendar-follow-timestamp-change
  16878. (get-buffer-window "*Calendar*" t)
  16879. (memq org-ts-what '(day month year)))
  16880. (org-recenter-calendar (time-to-days time))))))
  16881. ;; FIXME: does not yet work for lead times
  16882. (defun org-modify-ts-extra (s pos n)
  16883. "Change the different parts of the lead-time and repeat fields in timestamp."
  16884. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16885. ng h m new)
  16886. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16887. (cond
  16888. ((or (org-pos-in-match-range pos 2)
  16889. (org-pos-in-match-range pos 3))
  16890. (setq m (string-to-number (match-string 3 s))
  16891. h (string-to-number (match-string 2 s)))
  16892. (if (org-pos-in-match-range pos 2)
  16893. (setq h (+ h n))
  16894. (setq m (+ m n)))
  16895. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16896. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16897. (setq h (min 24 (max 0 h)))
  16898. (setq ng 1 new (format "-%02d:%02d" h m)))
  16899. ((org-pos-in-match-range pos 6)
  16900. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16901. ((org-pos-in-match-range pos 5)
  16902. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16903. (when ng
  16904. (setq s (concat
  16905. (substring s 0 (match-beginning ng))
  16906. new
  16907. (substring s (match-end ng))))))
  16908. s))
  16909. (defun org-recenter-calendar (date)
  16910. "If the calendar is visible, recenter it to DATE."
  16911. (let* ((win (selected-window))
  16912. (cwin (get-buffer-window "*Calendar*" t))
  16913. (calendar-move-hook nil))
  16914. (when cwin
  16915. (select-window cwin)
  16916. (calendar-goto-date (if (listp date) date
  16917. (calendar-gregorian-from-absolute date)))
  16918. (select-window win))))
  16919. (defun org-goto-calendar (&optional arg)
  16920. "Go to the Emacs calendar at the current date.
  16921. If there is a time stamp in the current line, go to that date.
  16922. A prefix ARG can be used to force the current date."
  16923. (interactive "P")
  16924. (let ((tsr org-ts-regexp) diff
  16925. (calendar-move-hook nil)
  16926. (view-calendar-holidays-initially nil)
  16927. (view-diary-entries-initially nil))
  16928. (if (or (org-at-timestamp-p)
  16929. (save-excursion
  16930. (beginning-of-line 1)
  16931. (looking-at (concat ".*" tsr))))
  16932. (let ((d1 (time-to-days (current-time)))
  16933. (d2 (time-to-days
  16934. (org-time-string-to-time (match-string 1)))))
  16935. (setq diff (- d2 d1))))
  16936. (calendar)
  16937. (calendar-goto-today)
  16938. (if (and diff (not arg)) (calendar-forward-day diff))))
  16939. (defun org-get-date-from-calendar ()
  16940. "Return a list (month day year) of date at point in calendar."
  16941. (with-current-buffer "*Calendar*"
  16942. (save-match-data
  16943. (calendar-cursor-to-date))))
  16944. (defun org-date-from-calendar ()
  16945. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16946. If there is already a time stamp at the cursor position, update it."
  16947. (interactive)
  16948. (if (org-at-timestamp-p t)
  16949. (org-timestamp-change 0 'calendar)
  16950. (let ((cal-date (org-get-date-from-calendar)))
  16951. (org-insert-time-stamp
  16952. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16953. ;; Make appt aware of appointments from the agenda
  16954. ;;;###autoload
  16955. (defun org-agenda-to-appt (&optional filter)
  16956. "Activate appointments found in `org-agenda-files'.
  16957. When prefixed, prompt for a regular expression and use it as a
  16958. filter: only add entries if they match this regular expression.
  16959. FILTER can be a string. In this case, use this string as a
  16960. regular expression to filter results.
  16961. FILTER can also be an alist, with the car of each cell being
  16962. either 'headline or 'category. For example:
  16963. '((headline \"IMPORTANT\")
  16964. (category \"Work\"))
  16965. will only add headlines containing IMPORTANT or headlines
  16966. belonging to the category \"Work\"."
  16967. (interactive "P")
  16968. (require 'calendar)
  16969. (if (equal filter '(4))
  16970. (setq filter (read-from-minibuffer "Regexp filter: ")))
  16971. (let* ((cnt 0) ; count added events
  16972. (org-agenda-new-buffers nil)
  16973. (today (org-date-to-gregorian
  16974. (time-to-days (current-time))))
  16975. (files (org-agenda-files)) entries file)
  16976. ;; Get all entries which may contain an appt
  16977. (while (setq file (pop files))
  16978. (setq entries
  16979. (append entries
  16980. (org-agenda-get-day-entries
  16981. file today
  16982. :timestamp :scheduled :deadline))))
  16983. (setq entries (delq nil entries))
  16984. ;; Map thru entries and find if they pass thru the filter
  16985. (mapc
  16986. (lambda(x)
  16987. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  16988. (cat (get-text-property 1 'org-category x))
  16989. (tod (get-text-property 1 'time-of-day x))
  16990. (ok (or (null filter)
  16991. (and (stringp filter) (string-match filter evt))
  16992. (and (listp filter)
  16993. (or (string-match
  16994. (cadr (assoc 'category filter)) cat)
  16995. (string-match
  16996. (cadr (assoc 'headline filter)) evt))))))
  16997. ;; FIXME: Shall we remove text-properties for the appt text?
  16998. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  16999. (when (and ok tod)
  17000. (setq tod (number-to-string tod)
  17001. tod (when (string-match
  17002. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17003. (concat (match-string 1 tod) ":"
  17004. (match-string 2 tod))))
  17005. (appt-add tod evt)
  17006. (setq cnt (1+ cnt))))) entries)
  17007. (org-release-buffers org-agenda-new-buffers)
  17008. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  17009. ;;; The clock for measuring work time.
  17010. (defvar org-mode-line-string "")
  17011. (put 'org-mode-line-string 'risky-local-variable t)
  17012. (defvar org-mode-line-timer nil)
  17013. (defvar org-clock-heading "")
  17014. (defvar org-clock-start-time "")
  17015. (defun org-update-mode-line ()
  17016. (let* ((delta (- (time-to-seconds (current-time))
  17017. (time-to-seconds org-clock-start-time)))
  17018. (h (floor delta 3600))
  17019. (m (floor (- delta (* 3600 h)) 60)))
  17020. (setq org-mode-line-string
  17021. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17022. 'help-echo "Org-mode clock is running"))
  17023. (force-mode-line-update)))
  17024. (defvar org-clock-marker (make-marker)
  17025. "Marker recording the last clock-in.")
  17026. (defvar org-clock-mode-line-entry nil
  17027. "Information for the modeline about the running clock.")
  17028. (defun org-clock-in ()
  17029. "Start the clock on the current item.
  17030. If necessary, clock-out of the currently active clock."
  17031. (interactive)
  17032. (org-clock-out t)
  17033. (let (ts)
  17034. (save-excursion
  17035. (org-back-to-heading t)
  17036. (when (and org-clock-in-switch-to-state
  17037. (not (looking-at (concat outline-regexp "[ \t]*"
  17038. org-clock-in-switch-to-state
  17039. "\\>"))))
  17040. (org-todo org-clock-in-switch-to-state))
  17041. (if (and org-clock-heading-function
  17042. (functionp org-clock-heading-function))
  17043. (setq org-clock-heading (funcall org-clock-heading-function))
  17044. (if (looking-at org-complex-heading-regexp)
  17045. (setq org-clock-heading (match-string 4))
  17046. (setq org-clock-heading "???")))
  17047. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17048. (org-clock-find-position)
  17049. (insert "\n") (backward-char 1)
  17050. (indent-relative)
  17051. (insert org-clock-string " ")
  17052. (setq org-clock-start-time (current-time))
  17053. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17054. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17055. (or global-mode-string (setq global-mode-string '("")))
  17056. (or (memq 'org-mode-line-string global-mode-string)
  17057. (setq global-mode-string
  17058. (append global-mode-string '(org-mode-line-string))))
  17059. (org-update-mode-line)
  17060. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17061. (message "Clock started at %s" ts))))
  17062. (defun org-clock-find-position ()
  17063. "Find the location where the next clock line should be inserted."
  17064. (org-back-to-heading t)
  17065. (catch 'exit
  17066. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17067. (re (concat "^[ \t]*" org-clock-string))
  17068. (cnt 0)
  17069. first last)
  17070. (goto-char beg)
  17071. (when (eobp) (newline) (setq end (max (point) end)))
  17072. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17073. ;; we seem to have a CLOCK drawer, so go there.
  17074. (beginning-of-line 2)
  17075. (throw 'exit t))
  17076. ;; Lets count the CLOCK lines
  17077. (goto-char beg)
  17078. (while (re-search-forward re end t)
  17079. (setq first (or first (match-beginning 0))
  17080. last (match-beginning 0)
  17081. cnt (1+ cnt)))
  17082. (when (and (integerp org-clock-into-drawer)
  17083. (>= (1+ cnt) org-clock-into-drawer))
  17084. ;; Wrap current entries into a new drawer
  17085. (goto-char last)
  17086. (beginning-of-line 2)
  17087. (if (org-at-item-p) (org-end-of-item))
  17088. (insert ":END:\n")
  17089. (beginning-of-line 0)
  17090. (org-indent-line-function)
  17091. (goto-char first)
  17092. (insert ":CLOCK:\n")
  17093. (beginning-of-line 0)
  17094. (org-indent-line-function)
  17095. (org-flag-drawer t)
  17096. (beginning-of-line 2)
  17097. (throw 'exit nil))
  17098. (goto-char beg)
  17099. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17100. (not (equal (match-string 1) org-clock-string)))
  17101. ;; Planning info, skip to after it
  17102. (beginning-of-line 2)
  17103. (or (bolp) (newline)))
  17104. (when (eq t org-clock-into-drawer)
  17105. (insert ":CLOCK:\n:END:\n")
  17106. (beginning-of-line -1)
  17107. (org-indent-line-function)
  17108. (org-flag-drawer t)
  17109. (beginning-of-line 2)
  17110. (org-indent-line-function)))))
  17111. (defun org-clock-out (&optional fail-quietly)
  17112. "Stop the currently running clock.
  17113. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17114. (interactive)
  17115. (catch 'exit
  17116. (if (not (marker-buffer org-clock-marker))
  17117. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17118. (let (ts te s h m)
  17119. (save-excursion
  17120. (set-buffer (marker-buffer org-clock-marker))
  17121. (goto-char org-clock-marker)
  17122. (beginning-of-line 1)
  17123. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17124. (equal (match-string 1) org-clock-string))
  17125. (setq ts (match-string 2))
  17126. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17127. (goto-char (match-end 0))
  17128. (delete-region (point) (point-at-eol))
  17129. (insert "--")
  17130. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17131. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17132. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17133. h (floor (/ s 3600))
  17134. s (- s (* 3600 h))
  17135. m (floor (/ s 60))
  17136. s (- s (* 60 s)))
  17137. (insert " => " (format "%2d:%02d" h m))
  17138. (move-marker org-clock-marker nil)
  17139. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  17140. (org-log-done (org-parse-local-options logging 'org-log-done))
  17141. (org-log-repeat (org-parse-local-options logging 'org-log-repeat)))
  17142. (org-add-log-maybe 'clock-out))
  17143. (when org-mode-line-timer
  17144. (cancel-timer org-mode-line-timer)
  17145. (setq org-mode-line-timer nil))
  17146. (setq global-mode-string
  17147. (delq 'org-mode-line-string global-mode-string))
  17148. (force-mode-line-update)
  17149. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17150. (defun org-clock-cancel ()
  17151. "Cancel the running clock be removing the start timestamp."
  17152. (interactive)
  17153. (if (not (marker-buffer org-clock-marker))
  17154. (error "No active clock"))
  17155. (save-excursion
  17156. (set-buffer (marker-buffer org-clock-marker))
  17157. (goto-char org-clock-marker)
  17158. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17159. (setq global-mode-string
  17160. (delq 'org-mode-line-string global-mode-string))
  17161. (force-mode-line-update)
  17162. (message "Clock canceled"))
  17163. (defun org-clock-goto (&optional delete-windows)
  17164. "Go to the currently clocked-in entry."
  17165. (interactive "P")
  17166. (if (not (marker-buffer org-clock-marker))
  17167. (error "No active clock"))
  17168. (switch-to-buffer-other-window
  17169. (marker-buffer org-clock-marker))
  17170. (if delete-windows (delete-other-windows))
  17171. (goto-char org-clock-marker)
  17172. (org-show-entry)
  17173. (org-back-to-heading)
  17174. (recenter))
  17175. (defvar org-clock-file-total-minutes nil
  17176. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17177. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17178. (defun org-clock-sum (&optional tstart tend)
  17179. "Sum the times for each subtree.
  17180. Puts the resulting times in minutes as a text property on each headline."
  17181. (interactive)
  17182. (let* ((bmp (buffer-modified-p))
  17183. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17184. org-clock-string
  17185. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17186. (lmax 30)
  17187. (ltimes (make-vector lmax 0))
  17188. (t1 0)
  17189. (level 0)
  17190. ts te dt
  17191. time)
  17192. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17193. (save-excursion
  17194. (goto-char (point-max))
  17195. (while (re-search-backward re nil t)
  17196. (cond
  17197. ((match-end 2)
  17198. ;; Two time stamps
  17199. (setq ts (match-string 2)
  17200. te (match-string 3)
  17201. ts (time-to-seconds
  17202. (apply 'encode-time (org-parse-time-string ts)))
  17203. te (time-to-seconds
  17204. (apply 'encode-time (org-parse-time-string te)))
  17205. ts (if tstart (max ts tstart) ts)
  17206. te (if tend (min te tend) te)
  17207. dt (- te ts)
  17208. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17209. ((match-end 4)
  17210. ;; A naket time
  17211. (setq t1 (+ t1 (string-to-number (match-string 5))
  17212. (* 60 (string-to-number (match-string 4))))))
  17213. (t ;; A headline
  17214. (setq level (- (match-end 1) (match-beginning 1)))
  17215. (when (or (> t1 0) (> (aref ltimes level) 0))
  17216. (loop for l from 0 to level do
  17217. (aset ltimes l (+ (aref ltimes l) t1)))
  17218. (setq t1 0 time (aref ltimes level))
  17219. (loop for l from level to (1- lmax) do
  17220. (aset ltimes l 0))
  17221. (goto-char (match-beginning 0))
  17222. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17223. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17224. (set-buffer-modified-p bmp)))
  17225. (defun org-clock-display (&optional total-only)
  17226. "Show subtree times in the entire buffer.
  17227. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17228. in the echo area."
  17229. (interactive)
  17230. (org-remove-clock-overlays)
  17231. (let (time h m p)
  17232. (org-clock-sum)
  17233. (unless total-only
  17234. (save-excursion
  17235. (goto-char (point-min))
  17236. (while (or (and (equal (setq p (point)) (point-min))
  17237. (get-text-property p :org-clock-minutes))
  17238. (setq p (next-single-property-change
  17239. (point) :org-clock-minutes)))
  17240. (goto-char p)
  17241. (when (setq time (get-text-property p :org-clock-minutes))
  17242. (org-put-clock-overlay time (funcall outline-level))))
  17243. (setq h (/ org-clock-file-total-minutes 60)
  17244. m (- org-clock-file-total-minutes (* 60 h)))
  17245. ;; Arrange to remove the overlays upon next change.
  17246. (when org-remove-highlights-with-change
  17247. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17248. nil 'local))))
  17249. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17250. (defvar org-clock-overlays nil)
  17251. (make-variable-buffer-local 'org-clock-overlays)
  17252. (defun org-put-clock-overlay (time &optional level)
  17253. "Put an overlays on the current line, displaying TIME.
  17254. If LEVEL is given, prefix time with a corresponding number of stars.
  17255. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17256. will be easy to remove."
  17257. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17258. (l (if level (org-get-legal-level level 0) 0))
  17259. (off 0)
  17260. ov tx)
  17261. (move-to-column c)
  17262. (unless (eolp) (skip-chars-backward "^ \t"))
  17263. (skip-chars-backward " \t")
  17264. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17265. tx (concat (buffer-substring (1- (point)) (point))
  17266. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17267. (org-add-props (format "%s %2d:%02d%s"
  17268. (make-string l ?*) h m
  17269. (make-string (- 10 l) ?\ ))
  17270. '(face secondary-selection))
  17271. ""))
  17272. (if (not (featurep 'xemacs))
  17273. (org-overlay-put ov 'display tx)
  17274. (org-overlay-put ov 'invisible t)
  17275. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17276. (push ov org-clock-overlays)))
  17277. (defun org-remove-clock-overlays (&optional beg end noremove)
  17278. "Remove the occur highlights from the buffer.
  17279. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17280. from the `before-change-functions' in the current buffer."
  17281. (interactive)
  17282. (unless org-inhibit-highlight-removal
  17283. (mapc 'org-delete-overlay org-clock-overlays)
  17284. (setq org-clock-overlays nil)
  17285. (unless noremove
  17286. (remove-hook 'before-change-functions
  17287. 'org-remove-clock-overlays 'local))))
  17288. (defun org-clock-out-if-current ()
  17289. "Clock out if the current entry contains the running clock.
  17290. This is used to stop the clock after a TODO entry is marked DONE,
  17291. and is only done if the variable `org-clock-out-when-done' is not nil."
  17292. (when (and org-clock-out-when-done
  17293. (member state org-done-keywords)
  17294. (equal (marker-buffer org-clock-marker) (current-buffer))
  17295. (< (point) org-clock-marker)
  17296. (> (save-excursion (outline-next-heading) (point))
  17297. org-clock-marker))
  17298. ;; Clock out, but don't accept a logging message for this.
  17299. (let ((org-log-done (if (and (listp org-log-done)
  17300. (member 'clock-out org-log-done))
  17301. '(done)
  17302. org-log-done)))
  17303. (org-clock-out))))
  17304. (add-hook 'org-after-todo-state-change-hook
  17305. 'org-clock-out-if-current)
  17306. (defun org-check-running-clock ()
  17307. "Check if the current buffer contains the running clock.
  17308. If yes, offer to stop it and to save the buffer with the changes."
  17309. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17310. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17311. (buffer-name))))
  17312. (org-clock-out)
  17313. (when (y-or-n-p "Save changed buffer?")
  17314. (save-buffer))))
  17315. (defun org-clock-report (&optional arg)
  17316. "Create a table containing a report about clocked time.
  17317. If the cursor is inside an existing clocktable block, then the table
  17318. will be updated. If not, a new clocktable will be inserted.
  17319. When called with a prefix argument, move to the first clock table in the
  17320. buffer and update it."
  17321. (interactive "P")
  17322. (org-remove-clock-overlays)
  17323. (when arg
  17324. (org-find-dblock "clocktable")
  17325. (org-show-entry))
  17326. (if (org-in-clocktable-p)
  17327. (goto-char (org-in-clocktable-p))
  17328. (org-create-dblock (list :name "clocktable"
  17329. :maxlevel 2 :scope 'file)))
  17330. (org-update-dblock))
  17331. (defun org-in-clocktable-p ()
  17332. "Check if the cursor is in a clocktable."
  17333. (let ((pos (point)) start)
  17334. (save-excursion
  17335. (end-of-line 1)
  17336. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17337. (setq start (match-beginning 0))
  17338. (re-search-forward "^#\\+END:.*" nil t)
  17339. (>= (match-end 0) pos)
  17340. start))))
  17341. (defun org-clock-update-time-maybe ()
  17342. "If this is a CLOCK line, update it and return t.
  17343. Otherwise, return nil."
  17344. (interactive)
  17345. (save-excursion
  17346. (beginning-of-line 1)
  17347. (skip-chars-forward " \t")
  17348. (when (looking-at org-clock-string)
  17349. (let ((re (concat "[ \t]*" org-clock-string
  17350. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17351. "\\([ \t]*=>.*\\)?"))
  17352. ts te h m s)
  17353. (if (not (looking-at re))
  17354. nil
  17355. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17356. (end-of-line 1)
  17357. (setq ts (match-string 1)
  17358. te (match-string 2))
  17359. (setq s (- (time-to-seconds
  17360. (apply 'encode-time (org-parse-time-string te)))
  17361. (time-to-seconds
  17362. (apply 'encode-time (org-parse-time-string ts))))
  17363. h (floor (/ s 3600))
  17364. s (- s (* 3600 h))
  17365. m (floor (/ s 60))
  17366. s (- s (* 60 s)))
  17367. (insert " => " (format "%2d:%02d" h m))
  17368. t)))))
  17369. (defun org-clock-special-range (key &optional time as-strings)
  17370. "Return two times bordering a special time range.
  17371. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17372. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17373. A week starts Monday 0:00 and ends Sunday 24:00.
  17374. The range is determined relative to TIME. TIME defaults to the current time.
  17375. The return value is a cons cell with two internal times like the ones
  17376. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17377. the returned times will be formatted strings."
  17378. (let* ((tm (decode-time (or time (current-time))))
  17379. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17380. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17381. (dow (nth 6 tm))
  17382. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17383. (cond
  17384. ((eq key 'today)
  17385. (setq h 0 m 0 h1 24 m1 0))
  17386. ((eq key 'yesterday)
  17387. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17388. ((eq key 'thisweek)
  17389. (setq diff (if (= dow 0) 6 (1- dow))
  17390. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17391. ((eq key 'lastweek)
  17392. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17393. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17394. ((eq key 'thismonth)
  17395. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17396. ((eq key 'lastmonth)
  17397. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17398. ((eq key 'thisyear)
  17399. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17400. ((eq key 'lastyear)
  17401. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17402. (t (error "No such time block %s" key)))
  17403. (setq ts (encode-time s m h d month y)
  17404. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17405. (or d1 d) (or month1 month) (or y1 y)))
  17406. (setq fm (cdr org-time-stamp-formats))
  17407. (if as-strings
  17408. (cons (format-time-string fm ts) (format-time-string fm te))
  17409. (cons ts te))))
  17410. (defun org-dblock-write:clocktable (params)
  17411. "Write the standard clocktable."
  17412. (catch 'exit
  17413. (let* ((hlchars '((1 . "*") (2 . "/")))
  17414. (ins (make-marker))
  17415. (total-time nil)
  17416. (scope (plist-get params :scope))
  17417. (tostring (plist-get params :tostring))
  17418. (multifile (plist-get params :multifile))
  17419. (header (plist-get params :header))
  17420. (maxlevel (or (plist-get params :maxlevel) 3))
  17421. (step (plist-get params :step))
  17422. (emph (plist-get params :emphasize))
  17423. (ts (plist-get params :tstart))
  17424. (te (plist-get params :tend))
  17425. (block (plist-get params :block))
  17426. ipos time h m p level hlc hdl
  17427. cc beg end pos tbl)
  17428. (when step
  17429. (org-clocktable-steps params)
  17430. (throw 'exit nil))
  17431. (when block
  17432. (setq cc (org-clock-special-range block nil t)
  17433. ts (car cc) te (cdr cc)))
  17434. (if ts (setq ts (time-to-seconds
  17435. (apply 'encode-time (org-parse-time-string ts)))))
  17436. (if te (setq te (time-to-seconds
  17437. (apply 'encode-time (org-parse-time-string te)))))
  17438. (move-marker ins (point))
  17439. (setq ipos (point))
  17440. ;; Get the right scope
  17441. (setq pos (point))
  17442. (save-restriction
  17443. (cond
  17444. ((not scope))
  17445. ((eq scope 'file) (widen))
  17446. ((eq scope 'subtree) (org-narrow-to-subtree))
  17447. ((eq scope 'tree)
  17448. (while (org-up-heading-safe))
  17449. (org-narrow-to-subtree))
  17450. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17451. (symbol-name scope)))
  17452. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17453. (catch 'exit
  17454. (while (org-up-heading-safe)
  17455. (looking-at outline-regexp)
  17456. (if (<= (org-reduced-level (funcall outline-level)) level)
  17457. (throw 'exit nil))))
  17458. (org-narrow-to-subtree))
  17459. ((or (listp scope) (eq scope 'agenda))
  17460. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17461. (scope 'agenda)
  17462. (p1 (copy-sequence params))
  17463. file)
  17464. (plist-put p1 :tostring t)
  17465. (plist-put p1 :multifile t)
  17466. (plist-put p1 :scope 'file)
  17467. (org-prepare-agenda-buffers files)
  17468. (while (setq file (pop files))
  17469. (with-current-buffer (find-buffer-visiting file)
  17470. (push (org-clocktable-add-file
  17471. file (org-dblock-write:clocktable p1)) tbl)
  17472. (setq total-time (+ (or total-time 0)
  17473. org-clock-file-total-minutes)))))))
  17474. (goto-char pos)
  17475. (unless (eq scope 'agenda)
  17476. (org-clock-sum ts te)
  17477. (goto-char (point-min))
  17478. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17479. (goto-char p)
  17480. (when (setq time (get-text-property p :org-clock-minutes))
  17481. (save-excursion
  17482. (beginning-of-line 1)
  17483. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17484. (setq level (org-reduced-level
  17485. (- (match-end 1) (match-beginning 1))))
  17486. (<= level maxlevel))
  17487. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17488. hdl (match-string 2)
  17489. h (/ time 60)
  17490. m (- time (* 60 h)))
  17491. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17492. (push (concat
  17493. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17494. (make-string (1- level) ?|)
  17495. hlc (format "%d:%02d" h m) hlc
  17496. " |") tbl))))))
  17497. (setq tbl (nreverse tbl))
  17498. (if tostring
  17499. (if tbl (mapconcat 'identity tbl "\n") nil)
  17500. (goto-char ins)
  17501. (insert-before-markers
  17502. (or header
  17503. (concat
  17504. "Clock summary at ["
  17505. (substring
  17506. (format-time-string (cdr org-time-stamp-formats))
  17507. 1 -1)
  17508. "]."
  17509. (if block
  17510. (format " Considered range is /%s/." block)
  17511. "")
  17512. "\n\n"))
  17513. (if (eq scope 'agenda) "|File" "")
  17514. "|L|Headline|Time|\n")
  17515. (setq total-time (or total-time org-clock-file-total-minutes)
  17516. h (/ total-time 60)
  17517. m (- total-time (* 60 h)))
  17518. (insert-before-markers
  17519. "|-\n|"
  17520. (if (eq scope 'agenda) "|" "")
  17521. "|"
  17522. "*Total time*| "
  17523. (format "*%d:%02d*" h m)
  17524. "|\n|-\n")
  17525. (setq tbl (delq nil tbl))
  17526. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17527. (equal (substring (car tbl) 0 2) "|-"))
  17528. (pop tbl))
  17529. (insert-before-markers (mapconcat
  17530. 'identity (delq nil tbl)
  17531. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17532. (backward-delete-char 1)
  17533. (goto-char ipos)
  17534. (skip-chars-forward "^|")
  17535. (org-table-align))))))
  17536. (defun org-clocktable-steps (params)
  17537. (let* ((p1 (copy-sequence params))
  17538. (ts (plist-get p1 :tstart))
  17539. (te (plist-get p1 :tend))
  17540. (step0 (plist-get p1 :step))
  17541. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17542. (block (plist-get p1 :block))
  17543. cc)
  17544. (when block
  17545. (setq cc (org-clock-special-range block nil t)
  17546. ts (car cc) te (cdr cc)))
  17547. (if ts (setq ts (time-to-seconds
  17548. (apply 'encode-time (org-parse-time-string ts)))))
  17549. (if te (setq te (time-to-seconds
  17550. (apply 'encode-time (org-parse-time-string te)))))
  17551. (plist-put p1 :header "")
  17552. (plist-put p1 :step nil)
  17553. (plist-put p1 :block nil)
  17554. (while (< ts te)
  17555. (or (bolp) (insert "\n"))
  17556. (plist-put p1 :tstart (format-time-string
  17557. (car org-time-stamp-formats)
  17558. (seconds-to-time ts)))
  17559. (plist-put p1 :tend (format-time-string
  17560. (car org-time-stamp-formats)
  17561. (seconds-to-time (setq ts (+ ts step)))))
  17562. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17563. (plist-get p1 :tstart) "\n")
  17564. (org-dblock-write:clocktable p1)
  17565. (re-search-forward "#\\+END:")
  17566. (end-of-line 0))))
  17567. (defun org-clocktable-add-file (file table)
  17568. (if table
  17569. (let ((lines (org-split-string table "\n"))
  17570. (ff (file-name-nondirectory file)))
  17571. (mapconcat 'identity
  17572. (mapcar (lambda (x)
  17573. (if (string-match org-table-dataline-regexp x)
  17574. (concat "|" ff x)
  17575. x))
  17576. lines)
  17577. "\n"))))
  17578. ;; FIXME: I don't think anybody uses this, ask David
  17579. (defun org-collect-clock-time-entries ()
  17580. "Return an internal list with clocking information.
  17581. This list has one entry for each CLOCK interval.
  17582. FIXME: describe the elements."
  17583. (interactive)
  17584. (let ((re (concat "^[ \t]*" org-clock-string
  17585. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17586. rtn beg end next cont level title total closedp leafp
  17587. clockpos titlepos h m donep)
  17588. (save-excursion
  17589. (org-clock-sum)
  17590. (goto-char (point-min))
  17591. (while (re-search-forward re nil t)
  17592. (setq clockpos (match-beginning 0)
  17593. beg (match-string 1) end (match-string 2)
  17594. cont (match-end 0))
  17595. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17596. end (apply 'encode-time (org-parse-time-string end)))
  17597. (org-back-to-heading t)
  17598. (setq donep (org-entry-is-done-p))
  17599. (setq titlepos (point)
  17600. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17601. h (/ total 60) m (- total (* 60 h))
  17602. total (cons h m))
  17603. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17604. (setq level (- (match-end 1) (match-beginning 1))
  17605. title (org-match-string-no-properties 2))
  17606. (save-excursion (outline-next-heading) (setq next (point)))
  17607. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17608. (goto-char next)
  17609. (setq leafp (and (looking-at "^\\*+ ")
  17610. (<= (- (match-end 0) (point)) level)))
  17611. (push (list beg end clockpos closedp donep
  17612. total title titlepos level leafp)
  17613. rtn)
  17614. (goto-char cont)))
  17615. (nreverse rtn)))
  17616. ;;;; Agenda, and Diary Integration
  17617. ;;; Define the Org-agenda-mode
  17618. (defvar org-agenda-mode-map (make-sparse-keymap)
  17619. "Keymap for `org-agenda-mode'.")
  17620. (defvar org-agenda-menu) ; defined later in this file.
  17621. (defvar org-agenda-follow-mode nil)
  17622. (defvar org-agenda-show-log nil)
  17623. (defvar org-agenda-redo-command nil)
  17624. (defvar org-agenda-mode-hook nil)
  17625. (defvar org-agenda-type nil)
  17626. (defvar org-agenda-force-single-file nil)
  17627. (defun org-agenda-mode ()
  17628. "Mode for time-sorted view on action items in Org-mode files.
  17629. The following commands are available:
  17630. \\{org-agenda-mode-map}"
  17631. (interactive)
  17632. (kill-all-local-variables)
  17633. (setq org-agenda-undo-list nil
  17634. org-agenda-pending-undo-list nil)
  17635. (setq major-mode 'org-agenda-mode)
  17636. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17637. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17638. (setq mode-name "Org-Agenda")
  17639. (use-local-map org-agenda-mode-map)
  17640. (easy-menu-add org-agenda-menu)
  17641. (if org-startup-truncated (setq truncate-lines t))
  17642. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17643. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17644. ;; Make sure properties are removed when copying text
  17645. (when (boundp 'buffer-substring-filters)
  17646. (org-set-local 'buffer-substring-filters
  17647. (cons (lambda (x)
  17648. (set-text-properties 0 (length x) nil x) x)
  17649. buffer-substring-filters)))
  17650. (unless org-agenda-keep-modes
  17651. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17652. org-agenda-show-log nil))
  17653. (easy-menu-change
  17654. '("Agenda") "Agenda Files"
  17655. (append
  17656. (list
  17657. (vector
  17658. (if (get 'org-agenda-files 'org-restrict)
  17659. "Restricted to single file"
  17660. "Edit File List")
  17661. '(org-edit-agenda-file-list)
  17662. (not (get 'org-agenda-files 'org-restrict)))
  17663. "--")
  17664. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17665. (org-agenda-set-mode-name)
  17666. (apply
  17667. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17668. (list 'org-agenda-mode-hook)))
  17669. (substitute-key-definition 'undo 'org-agenda-undo
  17670. org-agenda-mode-map global-map)
  17671. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17672. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17673. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17674. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17675. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17676. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17677. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17678. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17679. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17680. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17681. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17682. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17683. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17684. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17685. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17686. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17687. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17688. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17689. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17690. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17691. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17692. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17693. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17694. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17695. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17696. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17697. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17698. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17699. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17700. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17701. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17702. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17703. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17704. (while l (org-defkey org-agenda-mode-map
  17705. (int-to-string (pop l)) 'digit-argument)))
  17706. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17707. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17708. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17709. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17710. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17711. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17712. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17713. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17714. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17715. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17716. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17717. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17718. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17719. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17720. (org-defkey org-agenda-mode-map "n" 'next-line)
  17721. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17722. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17723. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17724. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17725. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17726. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17727. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17728. (eval-after-load "calendar"
  17729. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17730. 'org-calendar-goto-agenda))
  17731. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17732. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17733. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17734. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17735. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17736. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17737. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17738. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17739. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17740. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17741. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17742. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17743. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17744. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17745. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17746. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17747. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17748. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17749. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17750. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17751. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17752. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17753. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17754. "Local keymap for agenda entries from Org-mode.")
  17755. (org-defkey org-agenda-keymap
  17756. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17757. (org-defkey org-agenda-keymap
  17758. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17759. (when org-agenda-mouse-1-follows-link
  17760. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17761. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17762. '("Agenda"
  17763. ("Agenda Files")
  17764. "--"
  17765. ["Show" org-agenda-show t]
  17766. ["Go To (other window)" org-agenda-goto t]
  17767. ["Go To (this window)" org-agenda-switch-to t]
  17768. ["Follow Mode" org-agenda-follow-mode
  17769. :style toggle :selected org-agenda-follow-mode :active t]
  17770. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17771. "--"
  17772. ["Cycle TODO" org-agenda-todo t]
  17773. ["Archive subtree" org-agenda-archive t]
  17774. ["Delete subtree" org-agenda-kill t]
  17775. "--"
  17776. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17777. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17778. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17779. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17780. "--"
  17781. ("Tags and Properties"
  17782. ["Show all Tags" org-agenda-show-tags t]
  17783. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17784. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17785. "--"
  17786. ["Column View" org-columns t])
  17787. ("Date/Schedule"
  17788. ["Schedule" org-agenda-schedule t]
  17789. ["Set Deadline" org-agenda-deadline t]
  17790. "--"
  17791. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17792. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17793. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17794. ("Clock"
  17795. ["Clock in" org-agenda-clock-in t]
  17796. ["Clock out" org-agenda-clock-out t]
  17797. ["Clock cancel" org-agenda-clock-cancel t]
  17798. ["Goto running clock" org-clock-goto t])
  17799. ("Priority"
  17800. ["Set Priority" org-agenda-priority t]
  17801. ["Increase Priority" org-agenda-priority-up t]
  17802. ["Decrease Priority" org-agenda-priority-down t]
  17803. ["Show Priority" org-agenda-show-priority t])
  17804. ("Calendar/Diary"
  17805. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17806. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17807. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17808. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17809. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17810. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17811. "--"
  17812. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17813. "--"
  17814. ("View"
  17815. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17816. :style radio :selected (equal org-agenda-ndays 1)]
  17817. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17818. :style radio :selected (equal org-agenda-ndays 7)]
  17819. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17820. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17821. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17822. :style radio :selected (member org-agenda-ndays '(365 366))]
  17823. "--"
  17824. ["Show Logbook entries" org-agenda-log-mode
  17825. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17826. ["Include Diary" org-agenda-toggle-diary
  17827. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17828. ["Use Time Grid" org-agenda-toggle-time-grid
  17829. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17830. ["Write view to file" org-write-agenda t]
  17831. ["Rebuild buffer" org-agenda-redo t]
  17832. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17833. "--"
  17834. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17835. "--"
  17836. ["Quit" org-agenda-quit t]
  17837. ["Exit and Release Buffers" org-agenda-exit t]
  17838. ))
  17839. ;;; Agenda undo
  17840. (defvar org-agenda-allow-remote-undo t
  17841. "Non-nil means, allow remote undo from the agenda buffer.")
  17842. (defvar org-agenda-undo-list nil
  17843. "List of undoable operations in the agenda since last refresh.")
  17844. (defvar org-agenda-undo-has-started-in nil
  17845. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17846. (defvar org-agenda-pending-undo-list nil
  17847. "In a series of undo commands, this is the list of remaning undo items.")
  17848. (defmacro org-if-unprotected (&rest body)
  17849. "Execute BODY if there is no `org-protected' text property at point."
  17850. (declare (debug t))
  17851. `(unless (get-text-property (point) 'org-protected)
  17852. ,@body))
  17853. (defmacro org-with-remote-undo (_buffer &rest _body)
  17854. "Execute BODY while recording undo information in two buffers."
  17855. (declare (indent 1) (debug t))
  17856. `(let ((_cline (org-current-line))
  17857. (_cmd this-command)
  17858. (_buf1 (current-buffer))
  17859. (_buf2 ,_buffer)
  17860. (_undo1 buffer-undo-list)
  17861. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17862. _c1 _c2)
  17863. ,@_body
  17864. (when org-agenda-allow-remote-undo
  17865. (setq _c1 (org-verify-change-for-undo
  17866. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17867. _c2 (org-verify-change-for-undo
  17868. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17869. (when (or _c1 _c2)
  17870. ;; make sure there are undo boundaries
  17871. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17872. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17873. ;; remember which buffer to undo
  17874. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17875. org-agenda-undo-list)))))
  17876. (defun org-agenda-undo ()
  17877. "Undo a remote editing step in the agenda.
  17878. This undoes changes both in the agenda buffer and in the remote buffer
  17879. that have been changed along."
  17880. (interactive)
  17881. (or org-agenda-allow-remote-undo
  17882. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17883. (if (not (eq this-command last-command))
  17884. (setq org-agenda-undo-has-started-in nil
  17885. org-agenda-pending-undo-list org-agenda-undo-list))
  17886. (if (not org-agenda-pending-undo-list)
  17887. (error "No further undo information"))
  17888. (let* ((entry (pop org-agenda-pending-undo-list))
  17889. buf line cmd rembuf)
  17890. (setq cmd (pop entry) line (pop entry))
  17891. (setq rembuf (nth 2 entry))
  17892. (org-with-remote-undo rembuf
  17893. (while (bufferp (setq buf (pop entry)))
  17894. (if (pop entry)
  17895. (with-current-buffer buf
  17896. (let ((last-undo-buffer buf)
  17897. (inhibit-read-only t))
  17898. (unless (memq buf org-agenda-undo-has-started-in)
  17899. (push buf org-agenda-undo-has-started-in)
  17900. (make-local-variable 'pending-undo-list)
  17901. (undo-start))
  17902. (while (and pending-undo-list
  17903. (listp pending-undo-list)
  17904. (not (car pending-undo-list)))
  17905. (pop pending-undo-list))
  17906. (undo-more 1))))))
  17907. (goto-line line)
  17908. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17909. (defun org-verify-change-for-undo (l1 l2)
  17910. "Verify that a real change occurred between the undo lists L1 and L2."
  17911. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17912. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17913. (not (eq l1 l2)))
  17914. ;;; Agenda dispatch
  17915. (defvar org-agenda-restrict nil)
  17916. (defvar org-agenda-restrict-begin (make-marker))
  17917. (defvar org-agenda-restrict-end (make-marker))
  17918. (defvar org-agenda-last-dispatch-buffer nil)
  17919. (defvar org-agenda-overriding-restriction nil)
  17920. ;;;###autoload
  17921. (defun org-agenda (arg &optional keys restriction)
  17922. "Dispatch agenda commands to collect entries to the agenda buffer.
  17923. Prompts for a command to execute. Any prefix arg will be passed
  17924. on to the selected command. The default selections are:
  17925. a Call `org-agenda-list' to display the agenda for current day or week.
  17926. t Call `org-todo-list' to display the global todo list.
  17927. T Call `org-todo-list' to display the global todo list, select only
  17928. entries with a specific TODO keyword (the user gets a prompt).
  17929. m Call `org-tags-view' to display headlines with tags matching
  17930. a condition (the user is prompted for the condition).
  17931. M Like `m', but select only TODO entries, no ordinary headlines.
  17932. L Create a timeline for the current buffer.
  17933. e Export views to associated files.
  17934. More commands can be added by configuring the variable
  17935. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17936. searches can be pre-defined in this way.
  17937. If the current buffer is in Org-mode and visiting a file, you can also
  17938. first press `<' once to indicate that the agenda should be temporarily
  17939. \(until the next use of \\[org-agenda]) restricted to the current file.
  17940. Pressing `<' twice means to restrict to the current subtree or region
  17941. \(if active)."
  17942. (interactive "P")
  17943. (catch 'exit
  17944. (let* ((prefix-descriptions nil)
  17945. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  17946. (org-agenda-custom-commands
  17947. ;; normalize different versions
  17948. (delq nil
  17949. (mapcar
  17950. (lambda (x)
  17951. (cond ((stringp (cdr x))
  17952. (push x prefix-descriptions)
  17953. nil)
  17954. ((stringp (nth 1 x)) x)
  17955. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17956. (t (cons (car x) (cons "" (cdr x))))))
  17957. org-agenda-custom-commands)))
  17958. (buf (current-buffer))
  17959. (bfn (buffer-file-name (buffer-base-buffer)))
  17960. entry key type match lprops ans)
  17961. ;; Turn off restriction unless there is an overriding one
  17962. (unless org-agenda-overriding-restriction
  17963. (put 'org-agenda-files 'org-restrict nil)
  17964. (setq org-agenda-restrict nil)
  17965. (move-marker org-agenda-restrict-begin nil)
  17966. (move-marker org-agenda-restrict-end nil))
  17967. ;; Delete old local properties
  17968. (put 'org-agenda-redo-command 'org-lprops nil)
  17969. ;; Remember where this call originated
  17970. (setq org-agenda-last-dispatch-buffer (current-buffer))
  17971. (unless keys
  17972. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  17973. keys (car ans)
  17974. restriction (cdr ans)))
  17975. ;; Estabish the restriction, if any
  17976. (when (and (not org-agenda-overriding-restriction) restriction)
  17977. (put 'org-agenda-files 'org-restrict (list bfn))
  17978. (cond
  17979. ((eq restriction 'region)
  17980. (setq org-agenda-restrict t)
  17981. (move-marker org-agenda-restrict-begin (region-beginning))
  17982. (move-marker org-agenda-restrict-end (region-end)))
  17983. ((eq restriction 'subtree)
  17984. (save-excursion
  17985. (setq org-agenda-restrict t)
  17986. (org-back-to-heading t)
  17987. (move-marker org-agenda-restrict-begin (point))
  17988. (move-marker org-agenda-restrict-end
  17989. (progn (org-end-of-subtree t)))))))
  17990. (require 'calendar) ; FIXME: can we avoid this for some commands?
  17991. ;; For example the todo list should not need it (but does...)
  17992. (cond
  17993. ((setq entry (assoc keys org-agenda-custom-commands))
  17994. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  17995. (progn
  17996. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  17997. (put 'org-agenda-redo-command 'org-lprops lprops)
  17998. (cond
  17999. ((eq type 'agenda)
  18000. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18001. ((eq type 'alltodo)
  18002. (org-let lprops '(org-todo-list current-prefix-arg)))
  18003. ((eq type 'stuck)
  18004. (org-let lprops '(org-agenda-list-stuck-projects
  18005. current-prefix-arg)))
  18006. ((eq type 'tags)
  18007. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18008. ((eq type 'tags-todo)
  18009. (org-let lprops '(org-tags-view '(4) match)))
  18010. ((eq type 'todo)
  18011. (org-let lprops '(org-todo-list match)))
  18012. ((eq type 'tags-tree)
  18013. (org-check-for-org-mode)
  18014. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18015. ((eq type 'todo-tree)
  18016. (org-check-for-org-mode)
  18017. (org-let lprops
  18018. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18019. (regexp-quote match) "\\>"))))
  18020. ((eq type 'occur-tree)
  18021. (org-check-for-org-mode)
  18022. (org-let lprops '(org-occur match)))
  18023. ((functionp type)
  18024. (org-let lprops '(funcall type match)))
  18025. ((fboundp type)
  18026. (org-let lprops '(funcall type match)))
  18027. (t (error "Invalid custom agenda command type %s" type))))
  18028. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18029. ((equal keys "C")
  18030. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18031. (customize-variable 'org-agenda-custom-commands))
  18032. ((equal keys "a") (call-interactively 'org-agenda-list))
  18033. ((equal keys "t") (call-interactively 'org-todo-list))
  18034. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18035. ((equal keys "m") (call-interactively 'org-tags-view))
  18036. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18037. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18038. ((equal keys "L")
  18039. (unless (org-mode-p)
  18040. (error "This is not an Org-mode file"))
  18041. (unless restriction
  18042. (put 'org-agenda-files 'org-restrict (list bfn))
  18043. (org-call-with-arg 'org-timeline arg)))
  18044. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18045. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18046. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18047. (t (error "Invalid agenda key"))))))
  18048. (defun org-agenda-normalize-custom-commands (cmds)
  18049. (delq nil
  18050. (mapcar
  18051. (lambda (x)
  18052. (cond ((stringp (cdr x)) nil)
  18053. ((stringp (nth 1 x)) x)
  18054. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18055. (t (cons (car x) (cons "" (cdr x))))))
  18056. cmds)))
  18057. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18058. "The user interface for selecting an agenda command."
  18059. (catch 'exit
  18060. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18061. (restrict-ok (and bfn (org-mode-p)))
  18062. (region-p (org-region-active-p))
  18063. (custom org-agenda-custom-commands)
  18064. (selstring "")
  18065. restriction second-time
  18066. c entry key type match prefixes rmheader header-end custom1 desc)
  18067. (save-window-excursion
  18068. (delete-other-windows)
  18069. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18070. (erase-buffer)
  18071. (insert (eval-when-compile
  18072. (let ((header
  18073. "
  18074. Press key for an agenda command: < Buffer,subtree/region restriction
  18075. -------------------------------- > Remove restriction
  18076. a Agenda for current week or day e Export agenda views
  18077. t List of all TODO entries T Entries with special TODO kwd
  18078. m Match a TAGS query M Like m, but only TODO entries
  18079. L Timeline for current buffer # List stuck projects (!=configure)
  18080. / Multi-occur C Configure custom agenda commands
  18081. ")
  18082. (start 0))
  18083. (while (string-match
  18084. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18085. header start)
  18086. (setq start (match-end 0))
  18087. (add-text-properties (match-beginning 2) (match-end 2)
  18088. '(face bold) header))
  18089. header)))
  18090. (setq header-end (move-marker (make-marker) (point)))
  18091. (while t
  18092. (setq custom1 custom)
  18093. (when (eq rmheader t)
  18094. (goto-line 1)
  18095. (re-search-forward ":" nil t)
  18096. (delete-region (match-end 0) (point-at-eol))
  18097. (forward-char 1)
  18098. (looking-at "-+")
  18099. (delete-region (match-end 0) (point-at-eol))
  18100. (move-marker header-end (match-end 0)))
  18101. (goto-char header-end)
  18102. (delete-region (point) (point-max))
  18103. (while (setq entry (pop custom1))
  18104. (setq key (car entry) desc (nth 1 entry)
  18105. type (nth 2 entry) match (nth 3 entry))
  18106. (if (> (length key) 1)
  18107. (add-to-list 'prefixes (string-to-char key))
  18108. (insert
  18109. (format
  18110. "\n%-4s%-14s: %s"
  18111. (org-add-props (copy-sequence key)
  18112. '(face bold))
  18113. (cond
  18114. ((string-match "\\S-" desc) desc)
  18115. ((eq type 'agenda) "Agenda for current week or day")
  18116. ((eq type 'alltodo) "List of all TODO entries")
  18117. ((eq type 'stuck) "List of stuck projects")
  18118. ((eq type 'todo) "TODO keyword")
  18119. ((eq type 'tags) "Tags query")
  18120. ((eq type 'tags-todo) "Tags (TODO)")
  18121. ((eq type 'tags-tree) "Tags tree")
  18122. ((eq type 'todo-tree) "TODO kwd tree")
  18123. ((eq type 'occur-tree) "Occur tree")
  18124. ((functionp type) (if (symbolp type)
  18125. (symbol-name type)
  18126. "Lambda expression"))
  18127. (t "???"))
  18128. (cond
  18129. ((stringp match)
  18130. (org-add-props match nil 'face 'org-warning))
  18131. (match
  18132. (format "set of %d commands" (length match)))
  18133. (t ""))))))
  18134. (when prefixes
  18135. (mapc (lambda (x)
  18136. (insert
  18137. (format "\n%s %s"
  18138. (org-add-props (char-to-string x)
  18139. nil 'face 'bold)
  18140. (or (cdr (assoc (concat selstring (char-to-string x))
  18141. prefix-descriptions))
  18142. "Prefix key"))))
  18143. prefixes))
  18144. (goto-char (point-min))
  18145. (when (fboundp 'fit-window-to-buffer)
  18146. (if second-time
  18147. (if (not (pos-visible-in-window-p (point-max)))
  18148. (fit-window-to-buffer))
  18149. (setq second-time t)
  18150. (fit-window-to-buffer)))
  18151. (message "Press key for agenda command%s:"
  18152. (if (or restrict-ok org-agenda-overriding-restriction)
  18153. (if org-agenda-overriding-restriction
  18154. " (restriction lock active)"
  18155. (if restriction
  18156. (format " (restricted to %s)" restriction)
  18157. " (unrestricted)"))
  18158. ""))
  18159. (setq c (read-char-exclusive))
  18160. (message "")
  18161. (cond
  18162. ((assoc (char-to-string c) custom)
  18163. (setq selstring (concat selstring (char-to-string c)))
  18164. (throw 'exit (cons selstring restriction)))
  18165. ((memq c prefixes)
  18166. (setq selstring (concat selstring (char-to-string c))
  18167. prefixes nil
  18168. rmheader (or rmheader t)
  18169. custom (delq nil (mapcar
  18170. (lambda (x)
  18171. (if (or (= (length (car x)) 1)
  18172. (/= (string-to-char (car x)) c))
  18173. nil
  18174. (cons (substring (car x) 1) (cdr x))))
  18175. custom))))
  18176. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18177. (message "Restriction is only possible in Org-mode buffers")
  18178. (ding) (sit-for 1))
  18179. ((eq c ?1)
  18180. (org-agenda-remove-restriction-lock 'noupdate)
  18181. (setq restriction 'buffer))
  18182. ((eq c ?0)
  18183. (org-agenda-remove-restriction-lock 'noupdate)
  18184. (setq restriction (if region-p 'region 'subtree)))
  18185. ((eq c ?<)
  18186. (org-agenda-remove-restriction-lock 'noupdate)
  18187. (setq restriction
  18188. (cond
  18189. ((eq restriction 'buffer)
  18190. (if region-p 'region 'subtree))
  18191. ((memq restriction '(subtree region))
  18192. nil)
  18193. (t 'buffer))))
  18194. ((eq c ?>)
  18195. (org-agenda-remove-restriction-lock 'noupdate)
  18196. (setq restriction nil))
  18197. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18198. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18199. ((and (> (length selstring) 0) (eq c ?\d))
  18200. (delete-window)
  18201. (org-agenda-get-restriction-and-command prefix-descriptions))
  18202. ((equal c ?q) (error "Abort"))
  18203. (t (error "Invalid key %c" c))))))))
  18204. (defun org-run-agenda-series (name series)
  18205. (org-prepare-agenda name)
  18206. (let* ((org-agenda-multi t)
  18207. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18208. (cmds (car series))
  18209. (gprops (nth 1 series))
  18210. match ;; The byte compiler incorrectly complains about this. Keep it!
  18211. cmd type lprops)
  18212. (while (setq cmd (pop cmds))
  18213. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18214. (cond
  18215. ((eq type 'agenda)
  18216. (org-let2 gprops lprops
  18217. '(call-interactively 'org-agenda-list)))
  18218. ((eq type 'alltodo)
  18219. (org-let2 gprops lprops
  18220. '(call-interactively 'org-todo-list)))
  18221. ((eq type 'stuck)
  18222. (org-let2 gprops lprops
  18223. '(call-interactively 'org-agenda-list-stuck-projects)))
  18224. ((eq type 'tags)
  18225. (org-let2 gprops lprops
  18226. '(org-tags-view current-prefix-arg match)))
  18227. ((eq type 'tags-todo)
  18228. (org-let2 gprops lprops
  18229. '(org-tags-view '(4) match)))
  18230. ((eq type 'todo)
  18231. (org-let2 gprops lprops
  18232. '(org-todo-list match)))
  18233. ((fboundp type)
  18234. (org-let2 gprops lprops
  18235. '(funcall type match)))
  18236. (t (error "Invalid type in command series"))))
  18237. (widen)
  18238. (setq org-agenda-redo-command redo)
  18239. (goto-char (point-min)))
  18240. (org-finalize-agenda))
  18241. ;;;###autoload
  18242. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18243. "Run an agenda command in batch mode and send the result to STDOUT.
  18244. If CMD-KEY is a string of length 1, it is used as a key in
  18245. `org-agenda-custom-commands' and triggers this command. If it is a
  18246. longer string it is used as a tags/todo match string.
  18247. Paramters are alternating variable names and values that will be bound
  18248. before running the agenda command."
  18249. (let (pars)
  18250. (while parameters
  18251. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18252. (if (> (length cmd-key) 2)
  18253. (eval (list 'let (nreverse pars)
  18254. (list 'org-tags-view nil cmd-key)))
  18255. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18256. (set-buffer org-agenda-buffer-name)
  18257. (princ (org-encode-for-stdout (buffer-string)))))
  18258. (defun org-encode-for-stdout (string)
  18259. (if (fboundp 'encode-coding-string)
  18260. (encode-coding-string string buffer-file-coding-system)
  18261. string))
  18262. (defvar org-agenda-info nil)
  18263. ;;;###autoload
  18264. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18265. "Run an agenda command in batch mode and send the result to STDOUT.
  18266. If CMD-KEY is a string of length 1, it is used as a key in
  18267. `org-agenda-custom-commands' and triggers this command. If it is a
  18268. longer string it is used as a tags/todo match string.
  18269. Paramters are alternating variable names and values that will be bound
  18270. before running the agenda command.
  18271. The output gives a line for each selected agenda item. Each
  18272. item is a list of comma-separated values, like this:
  18273. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18274. category The category of the item
  18275. head The headline, without TODO kwd, TAGS and PRIORITY
  18276. type The type of the agenda entry, can be
  18277. todo selected in TODO match
  18278. tagsmatch selected in tags match
  18279. diary imported from diary
  18280. deadline a deadline on given date
  18281. scheduled scheduled on given date
  18282. timestamp entry has timestamp on given date
  18283. closed entry was closed on given date
  18284. upcoming-deadline warning about deadline
  18285. past-scheduled forwarded scheduled item
  18286. block entry has date block including g. date
  18287. todo The todo keyword, if any
  18288. tags All tags including inherited ones, separated by colons
  18289. date The relevant date, like 2007-2-14
  18290. time The time, like 15:00-16:50
  18291. extra Sting with extra planning info
  18292. priority-l The priority letter if any was given
  18293. priority-n The computed numerical priority
  18294. agenda-day The day in the agenda where this is listed"
  18295. (let (pars)
  18296. (while parameters
  18297. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18298. (push (list 'org-agenda-remove-tags t) pars)
  18299. (if (> (length cmd-key) 2)
  18300. (eval (list 'let (nreverse pars)
  18301. (list 'org-tags-view nil cmd-key)))
  18302. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18303. (set-buffer org-agenda-buffer-name)
  18304. (let* ((lines (org-split-string (buffer-string) "\n"))
  18305. line)
  18306. (while (setq line (pop lines))
  18307. (catch 'next
  18308. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18309. (setq org-agenda-info
  18310. (org-fix-agenda-info (text-properties-at 0 line)))
  18311. (princ
  18312. (org-encode-for-stdout
  18313. (mapconcat 'org-agenda-export-csv-mapper
  18314. '(org-category txt type todo tags date time-of-day extra
  18315. priority-letter priority agenda-day)
  18316. ",")))
  18317. (princ "\n"))))))
  18318. (defun org-fix-agenda-info (props)
  18319. "Make sure all properties on an agenda item have a canonical form,
  18320. so the export commands can easily use it."
  18321. (let (tmp re)
  18322. (when (setq tmp (plist-get props 'tags))
  18323. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18324. (when (setq tmp (plist-get props 'date))
  18325. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18326. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18327. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18328. (setq tmp (calendar-date-string tmp)))
  18329. (setq props (plist-put props 'date tmp)))
  18330. (when (setq tmp (plist-get props 'day))
  18331. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18332. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18333. (setq tmp (calendar-date-string tmp)))
  18334. (setq props (plist-put props 'day tmp))
  18335. (setq props (plist-put props 'agenda-day tmp)))
  18336. (when (setq tmp (plist-get props 'txt))
  18337. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18338. (plist-put props 'priority-letter (match-string 1 tmp))
  18339. (setq tmp (replace-match "" t t tmp)))
  18340. (when (and (setq re (plist-get props 'org-todo-regexp))
  18341. (setq re (concat "\\`\\.*" re " ?"))
  18342. (string-match re tmp))
  18343. (plist-put props 'todo (match-string 1 tmp))
  18344. (setq tmp (replace-match "" t t tmp)))
  18345. (plist-put props 'txt tmp)))
  18346. props)
  18347. (defun org-agenda-export-csv-mapper (prop)
  18348. (let ((res (plist-get org-agenda-info prop)))
  18349. (setq res
  18350. (cond
  18351. ((not res) "")
  18352. ((stringp res) res)
  18353. (t (prin1-to-string res))))
  18354. (while (string-match "," res)
  18355. (setq res (replace-match ";" t t res)))
  18356. (org-trim res)))
  18357. ;;;###autoload
  18358. (defun org-store-agenda-views (&rest parameters)
  18359. (interactive)
  18360. (eval (list 'org-batch-store-agenda-views)))
  18361. ;; FIXME, why is this a macro?????
  18362. ;;;###autoload
  18363. (defmacro org-batch-store-agenda-views (&rest parameters)
  18364. "Run all custom agenda commands that have a file argument."
  18365. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18366. (pop-up-frames nil)
  18367. (dir default-directory)
  18368. pars cmd thiscmdkey files opts)
  18369. (while parameters
  18370. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18371. (setq pars (reverse pars))
  18372. (save-window-excursion
  18373. (while cmds
  18374. (setq cmd (pop cmds)
  18375. thiscmdkey (car cmd)
  18376. opts (nth 4 cmd)
  18377. files (nth 5 cmd))
  18378. (if (stringp files) (setq files (list files)))
  18379. (when files
  18380. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18381. (list 'org-agenda nil thiscmdkey)))
  18382. (set-buffer org-agenda-buffer-name)
  18383. (while files
  18384. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18385. (list 'org-write-agenda
  18386. (expand-file-name (pop files) dir) t))))
  18387. (and (get-buffer org-agenda-buffer-name)
  18388. (kill-buffer org-agenda-buffer-name)))))))
  18389. (defun org-write-agenda (file &optional nosettings)
  18390. "Write the current buffer (an agenda view) as a file.
  18391. Depending on the extension of the file name, plain text (.txt),
  18392. HTML (.html or .htm) or Postscript (.ps) is produced.
  18393. If NOSETTINGS is given, do not scope the settings of
  18394. `org-agenda-exporter-settings' into the export commands. This is used when
  18395. the settings have already been scoped and we do not wish to overrule other,
  18396. higher priority settings."
  18397. (interactive "FWrite agenda to file: ")
  18398. (if (not (file-writable-p file))
  18399. (error "Cannot write agenda to file %s" file))
  18400. (cond
  18401. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18402. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18403. (org-let (if nosettings nil org-agenda-exporter-settings)
  18404. '(save-excursion
  18405. (save-window-excursion
  18406. (cond
  18407. ((string-match "\\.html?\\'" file)
  18408. (set-buffer (htmlize-buffer (current-buffer)))
  18409. (when (and org-agenda-export-html-style
  18410. (string-match "<style>" org-agenda-export-html-style))
  18411. ;; replace <style> section with org-agenda-export-html-style
  18412. (goto-char (point-min))
  18413. (kill-region (- (search-forward "<style") 6)
  18414. (search-forward "</style>"))
  18415. (insert org-agenda-export-html-style))
  18416. (write-file file)
  18417. (kill-buffer (current-buffer))
  18418. (message "HTML written to %s" file))
  18419. ((string-match "\\.ps\\'" file)
  18420. (ps-print-buffer-with-faces file)
  18421. (message "Postscript written to %s" file))
  18422. (t
  18423. (let ((bs (buffer-string)))
  18424. (find-file file)
  18425. (insert bs)
  18426. (save-buffer 0)
  18427. (kill-buffer (current-buffer))
  18428. (message "Plain text written to %s" file))))))
  18429. (set-buffer org-agenda-buffer-name)))
  18430. (defmacro org-no-read-only (&rest body)
  18431. "Inhibit read-only for BODY."
  18432. `(let ((inhibit-read-only t)) ,@body))
  18433. (defun org-check-for-org-mode ()
  18434. "Make sure current buffer is in org-mode. Error if not."
  18435. (or (org-mode-p)
  18436. (error "Cannot execute org-mode agenda command on buffer in %s."
  18437. major-mode)))
  18438. (defun org-fit-agenda-window ()
  18439. "Fit the window to the buffer size."
  18440. (and (memq org-agenda-window-setup '(reorganize-frame))
  18441. (fboundp 'fit-window-to-buffer)
  18442. (fit-window-to-buffer
  18443. nil
  18444. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18445. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18446. ;;; Agenda file list
  18447. (defun org-agenda-files (&optional unrestricted)
  18448. "Get the list of agenda files.
  18449. Optional UNRESTRICTED means return the full list even if a restriction
  18450. is currently in place."
  18451. (let ((files
  18452. (cond
  18453. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18454. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18455. ((listp org-agenda-files) org-agenda-files)
  18456. (t (error "Invalid value of `org-agenda-files'")))))
  18457. (setq files (apply 'append
  18458. (mapcar (lambda (f)
  18459. (if (file-directory-p f)
  18460. (directory-files f t
  18461. org-agenda-file-regexp)
  18462. (list f)))
  18463. files)))
  18464. (if org-agenda-skip-unavailable-files
  18465. (delq nil
  18466. (mapcar (function
  18467. (lambda (file)
  18468. (and (file-readable-p file) file)))
  18469. files))
  18470. files))) ; `org-check-agenda-file' will remove them from the list
  18471. (defun org-edit-agenda-file-list ()
  18472. "Edit the list of agenda files.
  18473. Depending on setup, this either uses customize to edit the variable
  18474. `org-agenda-files', or it visits the file that is holding the list. In the
  18475. latter case, the buffer is set up in a way that saving it automatically kills
  18476. the buffer and restores the previous window configuration."
  18477. (interactive)
  18478. (if (stringp org-agenda-files)
  18479. (let ((cw (current-window-configuration)))
  18480. (find-file org-agenda-files)
  18481. (org-set-local 'org-window-configuration cw)
  18482. (org-add-hook 'after-save-hook
  18483. (lambda ()
  18484. (set-window-configuration
  18485. (prog1 org-window-configuration
  18486. (kill-buffer (current-buffer))))
  18487. (org-install-agenda-files-menu)
  18488. (message "New agenda file list installed"))
  18489. nil 'local)
  18490. (message "%s" (substitute-command-keys
  18491. "Edit list and finish with \\[save-buffer]")))
  18492. (customize-variable 'org-agenda-files)))
  18493. (defun org-store-new-agenda-file-list (list)
  18494. "Set new value for the agenda file list and save it correcly."
  18495. (if (stringp org-agenda-files)
  18496. (let ((f org-agenda-files) b)
  18497. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18498. (with-temp-file f
  18499. (insert (mapconcat 'identity list "\n") "\n")))
  18500. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18501. (setq org-agenda-files list)
  18502. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18503. (defun org-read-agenda-file-list ()
  18504. "Read the list of agenda files from a file."
  18505. (when (stringp org-agenda-files)
  18506. (with-temp-buffer
  18507. (insert-file-contents org-agenda-files)
  18508. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18509. ;;;###autoload
  18510. (defun org-cycle-agenda-files ()
  18511. "Cycle through the files in `org-agenda-files'.
  18512. If the current buffer visits an agenda file, find the next one in the list.
  18513. If the current buffer does not, find the first agenda file."
  18514. (interactive)
  18515. (let* ((fs (org-agenda-files t))
  18516. (files (append fs (list (car fs))))
  18517. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18518. file)
  18519. (unless files (error "No agenda files"))
  18520. (catch 'exit
  18521. (while (setq file (pop files))
  18522. (if (equal (file-truename file) tcf)
  18523. (when (car files)
  18524. (find-file (car files))
  18525. (throw 'exit t))))
  18526. (find-file (car fs)))
  18527. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18528. (defun org-agenda-file-to-front (&optional to-end)
  18529. "Move/add the current file to the top of the agenda file list.
  18530. If the file is not present in the list, it is added to the front. If it is
  18531. present, it is moved there. With optional argument TO-END, add/move to the
  18532. end of the list."
  18533. (interactive "P")
  18534. (let ((org-agenda-skip-unavailable-files nil)
  18535. (file-alist (mapcar (lambda (x)
  18536. (cons (file-truename x) x))
  18537. (org-agenda-files t)))
  18538. (ctf (file-truename buffer-file-name))
  18539. x had)
  18540. (setq x (assoc ctf file-alist) had x)
  18541. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18542. (if to-end
  18543. (setq file-alist (append (delq x file-alist) (list x)))
  18544. (setq file-alist (cons x (delq x file-alist))))
  18545. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18546. (org-install-agenda-files-menu)
  18547. (message "File %s to %s of agenda file list"
  18548. (if had "moved" "added") (if to-end "end" "front"))))
  18549. (defun org-remove-file (&optional file)
  18550. "Remove current file from the list of files in variable `org-agenda-files'.
  18551. These are the files which are being checked for agenda entries.
  18552. Optional argument FILE means, use this file instead of the current."
  18553. (interactive)
  18554. (let* ((org-agenda-skip-unavailable-files nil)
  18555. (file (or file buffer-file-name))
  18556. (true-file (file-truename file))
  18557. (afile (abbreviate-file-name file))
  18558. (files (delq nil (mapcar
  18559. (lambda (x)
  18560. (if (equal true-file
  18561. (file-truename x))
  18562. nil x))
  18563. (org-agenda-files t)))))
  18564. (if (not (= (length files) (length (org-agenda-files t))))
  18565. (progn
  18566. (org-store-new-agenda-file-list files)
  18567. (org-install-agenda-files-menu)
  18568. (message "Removed file: %s" afile))
  18569. (message "File was not in list: %s (not removed)" afile))))
  18570. (defun org-file-menu-entry (file)
  18571. (vector file (list 'find-file file) t))
  18572. (defun org-check-agenda-file (file)
  18573. "Make sure FILE exists. If not, ask user what to do."
  18574. (when (not (file-exists-p file))
  18575. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18576. (abbreviate-file-name file))
  18577. (let ((r (downcase (read-char-exclusive))))
  18578. (cond
  18579. ((equal r ?r)
  18580. (org-remove-file file)
  18581. (throw 'nextfile t))
  18582. (t (error "Abort"))))))
  18583. ;;; Agenda prepare and finalize
  18584. (defvar org-agenda-multi nil) ; dynammically scoped
  18585. (defvar org-agenda-buffer-name "*Org Agenda*")
  18586. (defvar org-pre-agenda-window-conf nil)
  18587. (defvar org-agenda-name nil)
  18588. (defun org-prepare-agenda (&optional name)
  18589. (setq org-todo-keywords-for-agenda nil)
  18590. (setq org-done-keywords-for-agenda nil)
  18591. (if org-agenda-multi
  18592. (progn
  18593. (setq buffer-read-only nil)
  18594. (goto-char (point-max))
  18595. (unless (or (bobp) org-agenda-compact-blocks)
  18596. (insert "\n" (make-string (window-width) ?=) "\n"))
  18597. (narrow-to-region (point) (point-max)))
  18598. (org-agenda-reset-markers)
  18599. (org-prepare-agenda-buffers (org-agenda-files))
  18600. (setq org-todo-keywords-for-agenda
  18601. (org-uniquify org-todo-keywords-for-agenda))
  18602. (setq org-done-keywords-for-agenda
  18603. (org-uniquify org-done-keywords-for-agenda))
  18604. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18605. (awin (get-buffer-window abuf)))
  18606. (cond
  18607. ((equal (current-buffer) abuf) nil)
  18608. (awin (select-window awin))
  18609. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18610. ((equal org-agenda-window-setup 'current-window)
  18611. (switch-to-buffer abuf))
  18612. ((equal org-agenda-window-setup 'other-window)
  18613. (org-switch-to-buffer-other-window abuf))
  18614. ((equal org-agenda-window-setup 'other-frame)
  18615. (switch-to-buffer-other-frame abuf))
  18616. ((equal org-agenda-window-setup 'reorganize-frame)
  18617. (delete-other-windows)
  18618. (org-switch-to-buffer-other-window abuf))))
  18619. (setq buffer-read-only nil)
  18620. (erase-buffer)
  18621. (org-agenda-mode)
  18622. (and name (not org-agenda-name)
  18623. (org-set-local 'org-agenda-name name)))
  18624. (setq buffer-read-only nil))
  18625. (defun org-finalize-agenda ()
  18626. "Finishing touch for the agenda buffer, called just before displaying it."
  18627. (unless org-agenda-multi
  18628. (save-excursion
  18629. (let ((inhibit-read-only t))
  18630. (goto-char (point-min))
  18631. (while (org-activate-bracket-links (point-max))
  18632. (add-text-properties (match-beginning 0) (match-end 0)
  18633. '(face org-link)))
  18634. (org-agenda-align-tags)
  18635. (unless org-agenda-with-colors
  18636. (remove-text-properties (point-min) (point-max) '(face nil))))
  18637. (if (and (boundp 'org-overriding-columns-format)
  18638. org-overriding-columns-format)
  18639. (org-set-local 'org-overriding-columns-format
  18640. org-overriding-columns-format))
  18641. (if (and (boundp 'org-agenda-view-columns-initially)
  18642. org-agenda-view-columns-initially)
  18643. (org-agenda-columns))
  18644. (when org-agenda-fontify-priorities
  18645. (org-fontify-priorities))
  18646. (run-hooks 'org-finalize-agenda-hook)
  18647. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18648. )))
  18649. (defun org-fontify-priorities ()
  18650. "Make highest priority lines bold, and lowest italic."
  18651. (interactive)
  18652. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18653. (org-delete-overlay o)))
  18654. (org-overlays-in (point-min) (point-max)))
  18655. (save-excursion
  18656. (let ((inhibit-read-only t)
  18657. b e p ov h l)
  18658. (goto-char (point-min))
  18659. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18660. (setq h (or (get-char-property (point) 'org-highest-priority)
  18661. org-highest-priority)
  18662. l (or (get-char-property (point) 'org-lowest-priority)
  18663. org-lowest-priority)
  18664. p (string-to-char (match-string 1))
  18665. b (match-beginning 0) e (point-at-eol)
  18666. ov (org-make-overlay b e))
  18667. (org-overlay-put
  18668. ov 'face
  18669. (cond ((listp org-agenda-fontify-priorities)
  18670. (cdr (assoc p org-agenda-fontify-priorities)))
  18671. ((equal p l) 'italic)
  18672. ((equal p h) 'bold)))
  18673. (org-overlay-put ov 'org-type 'org-priority)))))
  18674. (defun org-prepare-agenda-buffers (files)
  18675. "Create buffers for all agenda files, protect archived trees and comments."
  18676. (interactive)
  18677. (let ((pa '(:org-archived t))
  18678. (pc '(:org-comment t))
  18679. (pall '(:org-archived t :org-comment t))
  18680. (inhibit-read-only t)
  18681. (rea (concat ":" org-archive-tag ":"))
  18682. bmp file re)
  18683. (save-excursion
  18684. (save-restriction
  18685. (while (setq file (pop files))
  18686. (if (bufferp file)
  18687. (set-buffer file)
  18688. (org-check-agenda-file file)
  18689. (set-buffer (org-get-agenda-file-buffer file)))
  18690. (widen)
  18691. (setq bmp (buffer-modified-p))
  18692. (org-refresh-category-properties)
  18693. (setq org-todo-keywords-for-agenda
  18694. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18695. (setq org-done-keywords-for-agenda
  18696. (append org-done-keywords-for-agenda org-done-keywords))
  18697. (save-excursion
  18698. (remove-text-properties (point-min) (point-max) pall)
  18699. (when org-agenda-skip-archived-trees
  18700. (goto-char (point-min))
  18701. (while (re-search-forward rea nil t)
  18702. (if (org-on-heading-p t)
  18703. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18704. (goto-char (point-min))
  18705. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18706. (while (re-search-forward re nil t)
  18707. (add-text-properties
  18708. (match-beginning 0) (org-end-of-subtree t) pc)))
  18709. (set-buffer-modified-p bmp))))))
  18710. (defvar org-agenda-skip-function nil
  18711. "Function to be called at each match during agenda construction.
  18712. If this function returns nil, the current match should not be skipped.
  18713. Otherwise, the function must return a position from where the search
  18714. should be continued.
  18715. This may also be a Lisp form, it will be evaluated.
  18716. Never set this variable using `setq' or so, because then it will apply
  18717. to all future agenda commands. Instead, bind it with `let' to scope
  18718. it dynamically into the agenda-constructing command. A good way to set
  18719. it is through options in org-agenda-custom-commands.")
  18720. (defun org-agenda-skip ()
  18721. "Throw to `:skip' in places that should be skipped.
  18722. Also moves point to the end of the skipped region, so that search can
  18723. continue from there."
  18724. (let ((p (point-at-bol)) to fp)
  18725. (and org-agenda-skip-archived-trees
  18726. (get-text-property p :org-archived)
  18727. (org-end-of-subtree t)
  18728. (throw :skip t))
  18729. (and (get-text-property p :org-comment)
  18730. (org-end-of-subtree t)
  18731. (throw :skip t))
  18732. (if (equal (char-after p) ?#) (throw :skip t))
  18733. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18734. (consp org-agenda-skip-function))
  18735. (setq to (save-excursion
  18736. (save-match-data
  18737. (if fp
  18738. (funcall org-agenda-skip-function)
  18739. (eval org-agenda-skip-function))))))
  18740. (goto-char to)
  18741. (throw :skip t))))
  18742. (defvar org-agenda-markers nil
  18743. "List of all currently active markers created by `org-agenda'.")
  18744. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18745. "Creation time of the last agenda marker.")
  18746. (defun org-agenda-new-marker (&optional pos)
  18747. "Return a new agenda marker.
  18748. Org-mode keeps a list of these markers and resets them when they are
  18749. no longer in use."
  18750. (let ((m (copy-marker (or pos (point)))))
  18751. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18752. (push m org-agenda-markers)
  18753. m))
  18754. (defun org-agenda-reset-markers ()
  18755. "Reset markers created by `org-agenda'."
  18756. (while org-agenda-markers
  18757. (move-marker (pop org-agenda-markers) nil)))
  18758. (defun org-get-agenda-file-buffer (file)
  18759. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18760. it to the list of buffers which might be released later."
  18761. (let ((buf (org-find-base-buffer-visiting file)))
  18762. (if buf
  18763. buf ; just return it
  18764. ;; Make a new buffer and remember it
  18765. (setq buf (find-file-noselect file))
  18766. (if buf (push buf org-agenda-new-buffers))
  18767. buf)))
  18768. (defun org-release-buffers (blist)
  18769. "Release all buffers in list, asking the user for confirmation when needed.
  18770. When a buffer is unmodified, it is just killed. When modified, it is saved
  18771. \(if the user agrees) and then killed."
  18772. (let (buf file)
  18773. (while (setq buf (pop blist))
  18774. (setq file (buffer-file-name buf))
  18775. (when (and (buffer-modified-p buf)
  18776. file
  18777. (y-or-n-p (format "Save file %s? " file)))
  18778. (with-current-buffer buf (save-buffer)))
  18779. (kill-buffer buf))))
  18780. (defun org-get-category (&optional pos)
  18781. "Get the category applying to position POS."
  18782. (get-text-property (or pos (point)) 'org-category))
  18783. ;;; Agenda timeline
  18784. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18785. (defun org-timeline (&optional include-all)
  18786. "Show a time-sorted view of the entries in the current org file.
  18787. Only entries with a time stamp of today or later will be listed. With
  18788. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18789. under the current date.
  18790. If the buffer contains an active region, only check the region for
  18791. dates."
  18792. (interactive "P")
  18793. (require 'calendar)
  18794. (org-compile-prefix-format 'timeline)
  18795. (org-set-sorting-strategy 'timeline)
  18796. (let* ((dopast t)
  18797. (dotodo include-all)
  18798. (doclosed org-agenda-show-log)
  18799. (entry buffer-file-name)
  18800. (date (calendar-current-date))
  18801. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18802. (end (if (org-region-active-p) (region-end) (point-max)))
  18803. (day-numbers (org-get-all-dates beg end 'no-ranges
  18804. t doclosed ; always include today
  18805. org-timeline-show-empty-dates))
  18806. (org-deadline-warning-days 0)
  18807. (org-agenda-only-exact-dates t)
  18808. (today (time-to-days (current-time)))
  18809. (past t)
  18810. args
  18811. s e rtn d emptyp)
  18812. (setq org-agenda-redo-command
  18813. (list 'progn
  18814. (list 'org-switch-to-buffer-other-window (current-buffer))
  18815. (list 'org-timeline (list 'quote include-all))))
  18816. (if (not dopast)
  18817. ;; Remove past dates from the list of dates.
  18818. (setq day-numbers (delq nil (mapcar (lambda(x)
  18819. (if (>= x today) x nil))
  18820. day-numbers))))
  18821. (org-prepare-agenda (concat "Timeline "
  18822. (file-name-nondirectory buffer-file-name)))
  18823. (if doclosed (push :closed args))
  18824. (push :timestamp args)
  18825. (push :deadline args)
  18826. (push :scheduled args)
  18827. (push :sexp args)
  18828. (if dotodo (push :todo args))
  18829. (while (setq d (pop day-numbers))
  18830. (if (and (listp d) (eq (car d) :omitted))
  18831. (progn
  18832. (setq s (point))
  18833. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18834. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18835. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18836. (if (and (>= d today)
  18837. dopast
  18838. past)
  18839. (progn
  18840. (setq past nil)
  18841. (insert (make-string 79 ?-) "\n")))
  18842. (setq date (calendar-gregorian-from-absolute d))
  18843. (setq s (point))
  18844. (setq rtn (and (not emptyp)
  18845. (apply 'org-agenda-get-day-entries entry
  18846. date args)))
  18847. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18848. (progn
  18849. (insert
  18850. (if (stringp org-agenda-format-date)
  18851. (format-time-string org-agenda-format-date
  18852. (org-time-from-absolute date))
  18853. (funcall org-agenda-format-date date))
  18854. "\n")
  18855. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18856. (put-text-property s (1- (point)) 'org-date-line t)
  18857. (if (equal d today)
  18858. (put-text-property s (1- (point)) 'org-today t))
  18859. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18860. (put-text-property s (1- (point)) 'day d)))))
  18861. (goto-char (point-min))
  18862. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18863. (point-min)))
  18864. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18865. (org-finalize-agenda)
  18866. (setq buffer-read-only t)))
  18867. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  18868. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18869. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18870. not every single day in the range. If FORCE-TODAY is non-nil, make
  18871. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18872. inactive time stamps (those in square brackets) are included.
  18873. When EMPTY is non-nil, also include days without any entries."
  18874. (let ((re (concat
  18875. (if pre-re pre-re "")
  18876. (if inactive org-ts-regexp-both org-ts-regexp)))
  18877. dates dates1 date day day1 day2 ts1 ts2)
  18878. (if force-today
  18879. (setq dates (list (time-to-days (current-time)))))
  18880. (save-excursion
  18881. (goto-char beg)
  18882. (while (re-search-forward re end t)
  18883. (setq day (time-to-days (org-time-string-to-time
  18884. (substring (match-string 1) 0 10))))
  18885. (or (memq day dates) (push day dates)))
  18886. (unless no-ranges
  18887. (goto-char beg)
  18888. (while (re-search-forward org-tr-regexp end t)
  18889. (setq ts1 (substring (match-string 1) 0 10)
  18890. ts2 (substring (match-string 2) 0 10)
  18891. day1 (time-to-days (org-time-string-to-time ts1))
  18892. day2 (time-to-days (org-time-string-to-time ts2)))
  18893. (while (< (setq day1 (1+ day1)) day2)
  18894. (or (memq day1 dates) (push day1 dates)))))
  18895. (setq dates (sort dates '<))
  18896. (when empty
  18897. (while (setq day (pop dates))
  18898. (setq day2 (car dates))
  18899. (push day dates1)
  18900. (when (and day2 empty)
  18901. (if (or (eq empty t)
  18902. (and (numberp empty) (<= (- day2 day) empty)))
  18903. (while (< (setq day (1+ day)) day2)
  18904. (push (list day) dates1))
  18905. (push (cons :omitted (- day2 day)) dates1))))
  18906. (setq dates (nreverse dates1)))
  18907. dates)))
  18908. ;;; Agenda Daily/Weekly
  18909. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18910. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18911. (defvar org-agenda-last-arguments nil
  18912. "The arguments of the previous call to org-agenda")
  18913. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18914. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18915. (defvar org-include-all-loc nil) ; local variable
  18916. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18917. ;;;###autoload
  18918. (defun org-agenda-list (&optional include-all start-day ndays)
  18919. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18920. The view will be for the current day or week, but from the overview buffer
  18921. you will be able to go to other days/weeks.
  18922. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18923. all unfinished TODO items will also be shown, before the agenda.
  18924. This feature is considered obsolete, please use the TODO list or a block
  18925. agenda instead.
  18926. With a numeric prefix argument in an interactive call, the agenda will
  18927. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18928. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18929. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18930. given in `org-agenda-start-on-weekday'."
  18931. (interactive "P")
  18932. (if (and (integerp include-all) (> include-all 0))
  18933. (setq ndays include-all include-all nil))
  18934. (setq ndays (or ndays org-agenda-ndays)
  18935. start-day (or start-day org-agenda-start-day))
  18936. (if org-agenda-overriding-arguments
  18937. (setq include-all (car org-agenda-overriding-arguments)
  18938. start-day (nth 1 org-agenda-overriding-arguments)
  18939. ndays (nth 2 org-agenda-overriding-arguments)))
  18940. (if (stringp start-day)
  18941. ;; Convert to an absolute day number
  18942. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18943. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18944. (org-compile-prefix-format 'agenda)
  18945. (org-set-sorting-strategy 'agenda)
  18946. (require 'calendar)
  18947. (let* ((org-agenda-start-on-weekday
  18948. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  18949. org-agenda-start-on-weekday nil))
  18950. (thefiles (org-agenda-files))
  18951. (files thefiles)
  18952. (today (time-to-days
  18953. (time-subtract (current-time)
  18954. (list 0 (* 3600 org-extend-today-until) 0))))
  18955. (sd (or start-day today))
  18956. (start (if (or (null org-agenda-start-on-weekday)
  18957. (< org-agenda-ndays 7))
  18958. sd
  18959. (let* ((nt (calendar-day-of-week
  18960. (calendar-gregorian-from-absolute sd)))
  18961. (n1 org-agenda-start-on-weekday)
  18962. (d (- nt n1)))
  18963. (- sd (+ (if (< d 0) 7 0) d)))))
  18964. (day-numbers (list start))
  18965. (day-cnt 0)
  18966. (inhibit-redisplay (not debug-on-error))
  18967. s e rtn rtnall file date d start-pos end-pos todayp nd)
  18968. (setq org-agenda-redo-command
  18969. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  18970. ;; Make the list of days
  18971. (setq ndays (or ndays org-agenda-ndays)
  18972. nd ndays)
  18973. (while (> ndays 1)
  18974. (push (1+ (car day-numbers)) day-numbers)
  18975. (setq ndays (1- ndays)))
  18976. (setq day-numbers (nreverse day-numbers))
  18977. (org-prepare-agenda "Day/Week")
  18978. (org-set-local 'org-starting-day (car day-numbers))
  18979. (org-set-local 'org-include-all-loc include-all)
  18980. (org-set-local 'org-agenda-span
  18981. (org-agenda-ndays-to-span nd))
  18982. (when (and (or include-all org-agenda-include-all-todo)
  18983. (member today day-numbers))
  18984. (setq files thefiles
  18985. rtnall nil)
  18986. (while (setq file (pop files))
  18987. (catch 'nextfile
  18988. (org-check-agenda-file file)
  18989. (setq date (calendar-gregorian-from-absolute today)
  18990. rtn (org-agenda-get-day-entries
  18991. file date :todo))
  18992. (setq rtnall (append rtnall rtn))))
  18993. (when rtnall
  18994. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  18995. (add-text-properties (point-min) (1- (point))
  18996. (list 'face 'org-agenda-structure))
  18997. (insert (org-finalize-agenda-entries rtnall) "\n")))
  18998. (unless org-agenda-compact-blocks
  18999. (setq s (point))
  19000. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19001. "-agenda:\n")
  19002. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19003. 'org-date-line t)))
  19004. (while (setq d (pop day-numbers))
  19005. (setq date (calendar-gregorian-from-absolute d)
  19006. s (point))
  19007. (if (or (setq todayp (= d today))
  19008. (and (not start-pos) (= d sd)))
  19009. (setq start-pos (point))
  19010. (if (and start-pos (not end-pos))
  19011. (setq end-pos (point))))
  19012. (setq files thefiles
  19013. rtnall nil)
  19014. (while (setq file (pop files))
  19015. (catch 'nextfile
  19016. (org-check-agenda-file file)
  19017. (if org-agenda-show-log
  19018. (setq rtn (org-agenda-get-day-entries
  19019. file date
  19020. :deadline :scheduled :timestamp :sexp :closed))
  19021. (setq rtn (org-agenda-get-day-entries
  19022. file date
  19023. :deadline :scheduled :sexp :timestamp)))
  19024. (setq rtnall (append rtnall rtn))))
  19025. (if org-agenda-include-diary
  19026. (progn
  19027. (require 'diary-lib)
  19028. (setq rtn (org-get-entries-from-diary date))
  19029. (setq rtnall (append rtnall rtn))))
  19030. (if (or rtnall org-agenda-show-all-dates)
  19031. (progn
  19032. (setq day-cnt (1+ day-cnt))
  19033. (insert
  19034. (if (stringp org-agenda-format-date)
  19035. (format-time-string org-agenda-format-date
  19036. (org-time-from-absolute date))
  19037. (funcall org-agenda-format-date date))
  19038. "\n")
  19039. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19040. (put-text-property s (1- (point)) 'org-date-line t)
  19041. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19042. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19043. (if rtnall (insert
  19044. (org-finalize-agenda-entries
  19045. (org-agenda-add-time-grid-maybe
  19046. rtnall nd todayp))
  19047. "\n"))
  19048. (put-text-property s (1- (point)) 'day d)
  19049. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19050. (goto-char (point-min))
  19051. (org-fit-agenda-window)
  19052. (unless (and (pos-visible-in-window-p (point-min))
  19053. (pos-visible-in-window-p (point-max)))
  19054. (goto-char (1- (point-max)))
  19055. (recenter -1)
  19056. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19057. (progn
  19058. (goto-char (or start-pos 1))
  19059. (recenter 1))))
  19060. (goto-char (or start-pos 1))
  19061. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19062. (org-finalize-agenda)
  19063. (setq buffer-read-only t)
  19064. (message "")))
  19065. (defun org-agenda-ndays-to-span (n)
  19066. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19067. ;;; Agenda TODO list
  19068. (defvar org-select-this-todo-keyword nil)
  19069. (defvar org-last-arg nil)
  19070. ;;;###autoload
  19071. (defun org-todo-list (arg)
  19072. "Show all TODO entries from all agenda file in a single list.
  19073. The prefix arg can be used to select a specific TODO keyword and limit
  19074. the list to these. When using \\[universal-argument], you will be prompted
  19075. for a keyword. A numeric prefix directly selects the Nth keyword in
  19076. `org-todo-keywords-1'."
  19077. (interactive "P")
  19078. (require 'calendar)
  19079. (org-compile-prefix-format 'todo)
  19080. (org-set-sorting-strategy 'todo)
  19081. (org-prepare-agenda "TODO")
  19082. (let* ((today (time-to-days (current-time)))
  19083. (date (calendar-gregorian-from-absolute today))
  19084. (kwds org-todo-keywords-for-agenda)
  19085. (completion-ignore-case t)
  19086. (org-select-this-todo-keyword
  19087. (if (stringp arg) arg
  19088. (and arg (integerp arg) (> arg 0)
  19089. (nth (1- arg) kwds))))
  19090. rtn rtnall files file pos)
  19091. (when (equal arg '(4))
  19092. (setq org-select-this-todo-keyword
  19093. (completing-read "Keyword (or KWD1|K2D2|...): "
  19094. (mapcar 'list kwds) nil nil)))
  19095. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19096. (org-set-local 'org-last-arg arg)
  19097. (setq org-agenda-redo-command
  19098. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19099. (setq files (org-agenda-files)
  19100. rtnall nil)
  19101. (while (setq file (pop files))
  19102. (catch 'nextfile
  19103. (org-check-agenda-file file)
  19104. (setq rtn (org-agenda-get-day-entries file date :todo))
  19105. (setq rtnall (append rtnall rtn))))
  19106. (if org-agenda-overriding-header
  19107. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19108. nil 'face 'org-agenda-structure) "\n")
  19109. (insert "Global list of TODO items of type: ")
  19110. (add-text-properties (point-min) (1- (point))
  19111. (list 'face 'org-agenda-structure))
  19112. (setq pos (point))
  19113. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19114. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19115. (setq pos (point))
  19116. (unless org-agenda-multi
  19117. (insert "Available with `N r': (0)ALL")
  19118. (let ((n 0) s)
  19119. (mapc (lambda (x)
  19120. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19121. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19122. (insert "\n "))
  19123. (insert " " s))
  19124. kwds))
  19125. (insert "\n"))
  19126. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19127. (when rtnall
  19128. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19129. (goto-char (point-min))
  19130. (org-fit-agenda-window)
  19131. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19132. (org-finalize-agenda)
  19133. (setq buffer-read-only t)))
  19134. ;;; Agenda tags match
  19135. ;;;###autoload
  19136. (defun org-tags-view (&optional todo-only match)
  19137. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19138. The prefix arg TODO-ONLY limits the search to TODO entries."
  19139. (interactive "P")
  19140. (org-compile-prefix-format 'tags)
  19141. (org-set-sorting-strategy 'tags)
  19142. (let* ((org-tags-match-list-sublevels
  19143. (if todo-only t org-tags-match-list-sublevels))
  19144. (completion-ignore-case t)
  19145. rtn rtnall files file pos matcher
  19146. buffer)
  19147. (setq matcher (org-make-tags-matcher match)
  19148. match (car matcher) matcher (cdr matcher))
  19149. (org-prepare-agenda (concat "TAGS " match))
  19150. (setq org-agenda-redo-command
  19151. (list 'org-tags-view (list 'quote todo-only)
  19152. (list 'if 'current-prefix-arg nil match)))
  19153. (setq files (org-agenda-files)
  19154. rtnall nil)
  19155. (while (setq file (pop files))
  19156. (catch 'nextfile
  19157. (org-check-agenda-file file)
  19158. (setq buffer (if (file-exists-p file)
  19159. (org-get-agenda-file-buffer file)
  19160. (error "No such file %s" file)))
  19161. (if (not buffer)
  19162. ;; If file does not exist, merror message to agenda
  19163. (setq rtn (list
  19164. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19165. rtnall (append rtnall rtn))
  19166. (with-current-buffer buffer
  19167. (unless (org-mode-p)
  19168. (error "Agenda file %s is not in `org-mode'" file))
  19169. (save-excursion
  19170. (save-restriction
  19171. (if org-agenda-restrict
  19172. (narrow-to-region org-agenda-restrict-begin
  19173. org-agenda-restrict-end)
  19174. (widen))
  19175. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19176. (setq rtnall (append rtnall rtn))))))))
  19177. (if org-agenda-overriding-header
  19178. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19179. nil 'face 'org-agenda-structure) "\n")
  19180. (insert "Headlines with TAGS match: ")
  19181. (add-text-properties (point-min) (1- (point))
  19182. (list 'face 'org-agenda-structure))
  19183. (setq pos (point))
  19184. (insert match "\n")
  19185. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19186. (setq pos (point))
  19187. (unless org-agenda-multi
  19188. (insert "Press `C-u r' to search again with new search string\n"))
  19189. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19190. (when rtnall
  19191. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19192. (goto-char (point-min))
  19193. (org-fit-agenda-window)
  19194. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19195. (org-finalize-agenda)
  19196. (setq buffer-read-only t)))
  19197. ;;; Agenda Finding stuck projects
  19198. (defvar org-agenda-skip-regexp nil
  19199. "Regular expression used in skipping subtrees for the agenda.
  19200. This is basically a temporary global variable that can be set and then
  19201. used by user-defined selections using `org-agenda-skip-function'.")
  19202. (defvar org-agenda-overriding-header nil
  19203. "When this is set during todo and tags searches, will replace header.")
  19204. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19205. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19206. If yes, it returns the end position of this tree, causing agenda commands
  19207. to skip this subtree. This is a function that can be put into
  19208. `org-agenda-skip-function' for the duration of a command."
  19209. (let ((end (save-excursion (org-end-of-subtree t)))
  19210. skip)
  19211. (save-excursion
  19212. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19213. (and skip end)))
  19214. (defun org-agenda-skip-entry-if (&rest conditions)
  19215. "Skip entry if any of CONDITIONS is true.
  19216. See `org-agenda-skip-if' for details."
  19217. (org-agenda-skip-if nil conditions))
  19218. (defun org-agenda-skip-subtree-if (&rest conditions)
  19219. "Skip entry if any of CONDITIONS is true.
  19220. See `org-agenda-skip-if' for details."
  19221. (org-agenda-skip-if t conditions))
  19222. (defun org-agenda-skip-if (subtree conditions)
  19223. "Checks current entity for CONDITIONS.
  19224. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19225. the entry, i.e. the text before the next heading is checked.
  19226. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19227. from different tests. Valid conditions are:
  19228. scheduled Check if there is a scheduled cookie
  19229. notscheduled Check if there is no scheduled cookie
  19230. deadline Check if there is a deadline
  19231. notdeadline Check if there is no deadline
  19232. regexp Check if regexp matches
  19233. notregexp Check if regexp does not match.
  19234. The regexp is taken from the conditions list, it must come right after
  19235. the `regexp' or `notregexp' element.
  19236. If any of these conditions is met, this function returns the end point of
  19237. the entity, causing the search to continue from there. This is a function
  19238. that can be put into `org-agenda-skip-function' for the duration of a command."
  19239. (let (beg end m)
  19240. (org-back-to-heading t)
  19241. (setq beg (point)
  19242. end (if subtree
  19243. (progn (org-end-of-subtree t) (point))
  19244. (progn (outline-next-heading) (1- (point)))))
  19245. (goto-char beg)
  19246. (and
  19247. (or
  19248. (and (memq 'scheduled conditions)
  19249. (re-search-forward org-scheduled-time-regexp end t))
  19250. (and (memq 'notscheduled conditions)
  19251. (not (re-search-forward org-scheduled-time-regexp end t)))
  19252. (and (memq 'deadline conditions)
  19253. (re-search-forward org-deadline-time-regexp end t))
  19254. (and (memq 'notdeadline conditions)
  19255. (not (re-search-forward org-deadline-time-regexp end t)))
  19256. (and (setq m (memq 'regexp conditions))
  19257. (stringp (nth 1 m))
  19258. (re-search-forward (nth 1 m) end t))
  19259. (and (setq m (memq 'notregexp conditions))
  19260. (stringp (nth 1 m))
  19261. (not (re-search-forward (nth 1 m) end t))))
  19262. end)))
  19263. ;;;###autoload
  19264. (defun org-agenda-list-stuck-projects (&rest ignore)
  19265. "Create agenda view for projects that are stuck.
  19266. Stuck projects are project that have no next actions. For the definitions
  19267. of what a project is and how to check if it stuck, customize the variable
  19268. `org-stuck-projects'.
  19269. MATCH is being ignored."
  19270. (interactive)
  19271. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19272. ;; FIXME: we could have used org-agenda-skip-if here.
  19273. (org-agenda-overriding-header "List of stuck projects: ")
  19274. (matcher (nth 0 org-stuck-projects))
  19275. (todo (nth 1 org-stuck-projects))
  19276. (todo-wds (if (member "*" todo)
  19277. (progn
  19278. (org-prepare-agenda-buffers (org-agenda-files))
  19279. (org-delete-all
  19280. org-done-keywords-for-agenda
  19281. (copy-sequence org-todo-keywords-for-agenda)))
  19282. todo))
  19283. (todo-re (concat "^\\*+[ \t]+\\("
  19284. (mapconcat 'identity todo-wds "\\|")
  19285. "\\)\\>"))
  19286. (tags (nth 2 org-stuck-projects))
  19287. (tags-re (if (member "*" tags)
  19288. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19289. (concat "^\\*+ .*:\\("
  19290. (mapconcat 'identity tags "\\|")
  19291. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19292. (gen-re (nth 3 org-stuck-projects))
  19293. (re-list
  19294. (delq nil
  19295. (list
  19296. (if todo todo-re)
  19297. (if tags tags-re)
  19298. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19299. gen-re)))))
  19300. (setq org-agenda-skip-regexp
  19301. (if re-list
  19302. (mapconcat 'identity re-list "\\|")
  19303. (error "No information how to identify unstuck projects")))
  19304. (org-tags-view nil matcher)
  19305. (with-current-buffer org-agenda-buffer-name
  19306. (setq org-agenda-redo-command
  19307. '(org-agenda-list-stuck-projects
  19308. (or current-prefix-arg org-last-arg))))))
  19309. ;;; Diary integration
  19310. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19311. (defun org-get-entries-from-diary (date)
  19312. "Get the (Emacs Calendar) diary entries for DATE."
  19313. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19314. (diary-display-hook '(fancy-diary-display))
  19315. (pop-up-frames nil)
  19316. (list-diary-entries-hook
  19317. (cons 'org-diary-default-entry list-diary-entries-hook))
  19318. (diary-file-name-prefix-function nil) ; turn this feature off
  19319. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19320. entries
  19321. (org-disable-agenda-to-diary t))
  19322. (save-excursion
  19323. (save-window-excursion
  19324. (funcall (if (fboundp 'diary-list-entries)
  19325. 'diary-list-entries 'list-diary-entries)
  19326. date 1)))
  19327. (if (not (get-buffer fancy-diary-buffer))
  19328. (setq entries nil)
  19329. (with-current-buffer fancy-diary-buffer
  19330. (setq buffer-read-only nil)
  19331. (if (zerop (buffer-size))
  19332. ;; No entries
  19333. (setq entries nil)
  19334. ;; Omit the date and other unnecessary stuff
  19335. (org-agenda-cleanup-fancy-diary)
  19336. ;; Add prefix to each line and extend the text properties
  19337. (if (zerop (buffer-size))
  19338. (setq entries nil)
  19339. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19340. (set-buffer-modified-p nil)
  19341. (kill-buffer fancy-diary-buffer)))
  19342. (when entries
  19343. (setq entries (org-split-string entries "\n"))
  19344. (setq entries
  19345. (mapcar
  19346. (lambda (x)
  19347. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19348. ;; Extend the text properties to the beginning of the line
  19349. (org-add-props x (text-properties-at (1- (length x)) x)
  19350. 'type "diary" 'date date))
  19351. entries)))))
  19352. (defun org-agenda-cleanup-fancy-diary ()
  19353. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19354. This gets rid of the date, the underline under the date, and
  19355. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19356. date. It also removes lines that contain only whitespace."
  19357. (goto-char (point-min))
  19358. (if (looking-at ".*?:[ \t]*")
  19359. (progn
  19360. (replace-match "")
  19361. (re-search-forward "\n=+$" nil t)
  19362. (replace-match "")
  19363. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19364. (re-search-forward "\n=+$" nil t)
  19365. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19366. (goto-char (point-min))
  19367. (while (re-search-forward "^ +\n" nil t)
  19368. (replace-match ""))
  19369. (goto-char (point-min))
  19370. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19371. (replace-match "")))
  19372. ;; Make sure entries from the diary have the right text properties.
  19373. (eval-after-load "diary-lib"
  19374. '(if (boundp 'diary-modify-entry-list-string-function)
  19375. ;; We can rely on the hook, nothing to do
  19376. nil
  19377. ;; Hook not avaiable, must use advice to make this work
  19378. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19379. "Make the position visible."
  19380. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19381. (stringp string)
  19382. buffer-file-name)
  19383. (setq string (org-modify-diary-entry-string string))))))
  19384. (defun org-modify-diary-entry-string (string)
  19385. "Add text properties to string, allowing org-mode to act on it."
  19386. (org-add-props string nil
  19387. 'mouse-face 'highlight
  19388. 'keymap org-agenda-keymap
  19389. 'help-echo (if buffer-file-name
  19390. (format "mouse-2 or RET jump to diary file %s"
  19391. (abbreviate-file-name buffer-file-name))
  19392. "")
  19393. 'org-agenda-diary-link t
  19394. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19395. (defun org-diary-default-entry ()
  19396. "Add a dummy entry to the diary.
  19397. Needed to avoid empty dates which mess up holiday display."
  19398. ;; Catch the error if dealing with the new add-to-diary-alist
  19399. (when org-disable-agenda-to-diary
  19400. (condition-case nil
  19401. (add-to-diary-list original-date "Org-mode dummy" "")
  19402. (error
  19403. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19404. ;;;###autoload
  19405. (defun org-diary (&rest args)
  19406. "Return diary information from org-files.
  19407. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19408. It accesses org files and extracts information from those files to be
  19409. listed in the diary. The function accepts arguments specifying what
  19410. items should be listed. The following arguments are allowed:
  19411. :timestamp List the headlines of items containing a date stamp or
  19412. date range matching the selected date. Deadlines will
  19413. also be listed, on the expiration day.
  19414. :sexp List entries resulting from diary-like sexps.
  19415. :deadline List any deadlines past due, or due within
  19416. `org-deadline-warning-days'. The listing occurs only
  19417. in the diary for *today*, not at any other date. If
  19418. an entry is marked DONE, it is no longer listed.
  19419. :scheduled List all items which are scheduled for the given date.
  19420. The diary for *today* also contains items which were
  19421. scheduled earlier and are not yet marked DONE.
  19422. :todo List all TODO items from the org-file. This may be a
  19423. long list - so this is not turned on by default.
  19424. Like deadlines, these entries only show up in the
  19425. diary for *today*, not at any other date.
  19426. The call in the diary file should look like this:
  19427. &%%(org-diary) ~/path/to/some/orgfile.org
  19428. Use a separate line for each org file to check. Or, if you omit the file name,
  19429. all files listed in `org-agenda-files' will be checked automatically:
  19430. &%%(org-diary)
  19431. If you don't give any arguments (as in the example above), the default
  19432. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19433. So the example above may also be written as
  19434. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19435. The function expects the lisp variables `entry' and `date' to be provided
  19436. by the caller, because this is how the calendar works. Don't use this
  19437. function from a program - use `org-agenda-get-day-entries' instead."
  19438. (when (> (- (time-to-seconds (current-time))
  19439. org-agenda-last-marker-time)
  19440. 5)
  19441. (org-agenda-reset-markers))
  19442. (org-compile-prefix-format 'agenda)
  19443. (org-set-sorting-strategy 'agenda)
  19444. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19445. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19446. (list entry)
  19447. (org-agenda-files t)))
  19448. file rtn results)
  19449. (org-prepare-agenda-buffers files)
  19450. ;; If this is called during org-agenda, don't return any entries to
  19451. ;; the calendar. Org Agenda will list these entries itself.
  19452. (if org-disable-agenda-to-diary (setq files nil))
  19453. (while (setq file (pop files))
  19454. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19455. (setq results (append results rtn)))
  19456. (if results
  19457. (concat (org-finalize-agenda-entries results) "\n"))))
  19458. ;;; Agenda entry finders
  19459. (defun org-agenda-get-day-entries (file date &rest args)
  19460. "Does the work for `org-diary' and `org-agenda'.
  19461. FILE is the path to a file to be checked for entries. DATE is date like
  19462. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19463. which kind of entries should be extracted. For details about these, see
  19464. the documentation of `org-diary'."
  19465. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19466. (let* ((org-startup-folded nil)
  19467. (org-startup-align-all-tables nil)
  19468. (buffer (if (file-exists-p file)
  19469. (org-get-agenda-file-buffer file)
  19470. (error "No such file %s" file)))
  19471. arg results rtn)
  19472. (if (not buffer)
  19473. ;; If file does not exist, make sure an error message ends up in diary
  19474. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19475. (with-current-buffer buffer
  19476. (unless (org-mode-p)
  19477. (error "Agenda file %s is not in `org-mode'" file))
  19478. (let ((case-fold-search nil))
  19479. (save-excursion
  19480. (save-restriction
  19481. (if org-agenda-restrict
  19482. (narrow-to-region org-agenda-restrict-begin
  19483. org-agenda-restrict-end)
  19484. (widen))
  19485. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19486. (while (setq arg (pop args))
  19487. (cond
  19488. ((and (eq arg :todo)
  19489. (equal date (calendar-current-date)))
  19490. (setq rtn (org-agenda-get-todos))
  19491. (setq results (append results rtn)))
  19492. ((eq arg :timestamp)
  19493. (setq rtn (org-agenda-get-blocks))
  19494. (setq results (append results rtn))
  19495. (setq rtn (org-agenda-get-timestamps))
  19496. (setq results (append results rtn)))
  19497. ((eq arg :sexp)
  19498. (setq rtn (org-agenda-get-sexps))
  19499. (setq results (append results rtn)))
  19500. ((eq arg :scheduled)
  19501. (setq rtn (org-agenda-get-scheduled))
  19502. (setq results (append results rtn)))
  19503. ((eq arg :closed)
  19504. (setq rtn (org-agenda-get-closed))
  19505. (setq results (append results rtn)))
  19506. ((eq arg :deadline)
  19507. (setq rtn (org-agenda-get-deadlines))
  19508. (setq results (append results rtn))))))))
  19509. results))))
  19510. (defun org-entry-is-todo-p ()
  19511. (member (org-get-todo-state) org-not-done-keywords))
  19512. (defun org-entry-is-done-p ()
  19513. (member (org-get-todo-state) org-done-keywords))
  19514. (defun org-get-todo-state ()
  19515. (save-excursion
  19516. (org-back-to-heading t)
  19517. (and (looking-at org-todo-line-regexp)
  19518. (match-end 2)
  19519. (match-string 2))))
  19520. (defun org-at-date-range-p (&optional inactive-ok)
  19521. "Is the cursor inside a date range?"
  19522. (interactive)
  19523. (save-excursion
  19524. (catch 'exit
  19525. (let ((pos (point)))
  19526. (skip-chars-backward "^[<\r\n")
  19527. (skip-chars-backward "<[")
  19528. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19529. (>= (match-end 0) pos)
  19530. (throw 'exit t))
  19531. (skip-chars-backward "^<[\r\n")
  19532. (skip-chars-backward "<[")
  19533. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19534. (>= (match-end 0) pos)
  19535. (throw 'exit t)))
  19536. nil)))
  19537. (defun org-agenda-get-todos ()
  19538. "Return the TODO information for agenda display."
  19539. (let* ((props (list 'face nil
  19540. 'done-face 'org-done
  19541. 'org-not-done-regexp org-not-done-regexp
  19542. 'org-todo-regexp org-todo-regexp
  19543. 'mouse-face 'highlight
  19544. 'keymap org-agenda-keymap
  19545. 'help-echo
  19546. (format "mouse-2 or RET jump to org file %s"
  19547. (abbreviate-file-name buffer-file-name))))
  19548. ;; FIXME: get rid of the \n at some point but watch out
  19549. (regexp (concat "^\\*+[ \t]+\\("
  19550. (if org-select-this-todo-keyword
  19551. (if (equal org-select-this-todo-keyword "*")
  19552. org-todo-regexp
  19553. (concat "\\<\\("
  19554. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19555. "\\)\\>"))
  19556. org-not-done-regexp)
  19557. "[^\n\r]*\\)"))
  19558. marker priority category tags
  19559. ee txt beg end)
  19560. (goto-char (point-min))
  19561. (while (re-search-forward regexp nil t)
  19562. (catch :skip
  19563. (save-match-data
  19564. (beginning-of-line)
  19565. (setq beg (point) end (progn (outline-next-heading) (point)))
  19566. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19567. (re-search-forward org-ts-regexp end t))
  19568. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19569. (re-search-forward org-scheduled-time-regexp end t))
  19570. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19571. (re-search-forward org-deadline-time-regexp end t)
  19572. (org-deadline-close (match-string 1))))
  19573. (goto-char (1+ beg))
  19574. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19575. (throw :skip nil)))
  19576. (goto-char beg)
  19577. (org-agenda-skip)
  19578. (goto-char (match-beginning 1))
  19579. (setq marker (org-agenda-new-marker (match-beginning 0))
  19580. category (org-get-category)
  19581. tags (org-get-tags-at (point))
  19582. txt (org-format-agenda-item "" (match-string 1) category tags)
  19583. priority (1+ (org-get-priority txt)))
  19584. (org-add-props txt props
  19585. 'org-marker marker 'org-hd-marker marker
  19586. 'priority priority 'org-category category
  19587. 'type "todo")
  19588. (push txt ee)
  19589. (if org-agenda-todo-list-sublevels
  19590. (goto-char (match-end 1))
  19591. (org-end-of-subtree 'invisible))))
  19592. (nreverse ee)))
  19593. (defconst org-agenda-no-heading-message
  19594. "No heading for this item in buffer or region.")
  19595. (defun org-agenda-get-timestamps ()
  19596. "Return the date stamp information for agenda display."
  19597. (let* ((props (list 'face nil
  19598. 'org-not-done-regexp org-not-done-regexp
  19599. 'org-todo-regexp org-todo-regexp
  19600. 'mouse-face 'highlight
  19601. 'keymap org-agenda-keymap
  19602. 'help-echo
  19603. (format "mouse-2 or RET jump to org file %s"
  19604. (abbreviate-file-name buffer-file-name))))
  19605. (d1 (calendar-absolute-from-gregorian date))
  19606. (remove-re
  19607. (concat
  19608. (regexp-quote
  19609. (format-time-string
  19610. "<%Y-%m-%d"
  19611. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19612. ".*?>"))
  19613. (regexp
  19614. (concat
  19615. (regexp-quote
  19616. (substring
  19617. (format-time-string
  19618. (car org-time-stamp-formats)
  19619. (apply 'encode-time ; DATE bound by calendar
  19620. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19621. 0 11))
  19622. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19623. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19624. marker hdmarker deadlinep scheduledp donep tmp priority category
  19625. ee txt timestr tags b0 b3 e3 head)
  19626. (goto-char (point-min))
  19627. (while (re-search-forward regexp nil t)
  19628. (setq b0 (match-beginning 0)
  19629. b3 (match-beginning 3) e3 (match-end 3))
  19630. (catch :skip
  19631. (and (org-at-date-range-p) (throw :skip nil))
  19632. (org-agenda-skip)
  19633. (if (and (match-end 1)
  19634. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19635. (throw :skip nil))
  19636. (if (and e3
  19637. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19638. (throw :skip nil))
  19639. (setq marker (org-agenda-new-marker b0)
  19640. category (org-get-category b0)
  19641. tmp (buffer-substring (max (point-min)
  19642. (- b0 org-ds-keyword-length))
  19643. b0)
  19644. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19645. deadlinep (string-match org-deadline-regexp tmp)
  19646. scheduledp (string-match org-scheduled-regexp tmp)
  19647. donep (org-entry-is-done-p))
  19648. (if (or scheduledp deadlinep) (throw :skip t))
  19649. (if (string-match ">" timestr)
  19650. ;; substring should only run to end of time stamp
  19651. (setq timestr (substring timestr 0 (match-end 0))))
  19652. (save-excursion
  19653. (if (re-search-backward "^\\*+ " nil t)
  19654. (progn
  19655. (goto-char (match-beginning 0))
  19656. (setq hdmarker (org-agenda-new-marker)
  19657. tags (org-get-tags-at))
  19658. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19659. (setq head (match-string 1))
  19660. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19661. (setq txt (org-format-agenda-item
  19662. nil head category tags timestr nil
  19663. remove-re)))
  19664. (setq txt org-agenda-no-heading-message))
  19665. (setq priority (org-get-priority txt))
  19666. (org-add-props txt props
  19667. 'org-marker marker 'org-hd-marker hdmarker)
  19668. (org-add-props txt nil 'priority priority
  19669. 'org-category category 'date date
  19670. 'type "timestamp")
  19671. (push txt ee))
  19672. (outline-next-heading)))
  19673. (nreverse ee)))
  19674. (defun org-agenda-get-sexps ()
  19675. "Return the sexp information for agenda display."
  19676. (require 'diary-lib)
  19677. (let* ((props (list 'face nil
  19678. 'mouse-face 'highlight
  19679. 'keymap org-agenda-keymap
  19680. 'help-echo
  19681. (format "mouse-2 or RET jump to org file %s"
  19682. (abbreviate-file-name buffer-file-name))))
  19683. (regexp "^&?%%(")
  19684. marker category ee txt tags entry result beg b sexp sexp-entry)
  19685. (goto-char (point-min))
  19686. (while (re-search-forward regexp nil t)
  19687. (catch :skip
  19688. (org-agenda-skip)
  19689. (setq beg (match-beginning 0))
  19690. (goto-char (1- (match-end 0)))
  19691. (setq b (point))
  19692. (forward-sexp 1)
  19693. (setq sexp (buffer-substring b (point)))
  19694. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19695. (org-trim (match-string 1))
  19696. ""))
  19697. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19698. (when result
  19699. (setq marker (org-agenda-new-marker beg)
  19700. category (org-get-category beg))
  19701. (if (string-match "\\S-" result)
  19702. (setq txt result)
  19703. (setq txt "SEXP entry returned empty string"))
  19704. (setq txt (org-format-agenda-item
  19705. "" txt category tags 'time))
  19706. (org-add-props txt props 'org-marker marker)
  19707. (org-add-props txt nil
  19708. 'org-category category 'date date
  19709. 'type "sexp")
  19710. (push txt ee))))
  19711. (nreverse ee)))
  19712. (defun org-agenda-get-closed ()
  19713. "Return the logged TODO entries for agenda display."
  19714. (let* ((props (list 'mouse-face 'highlight
  19715. 'org-not-done-regexp org-not-done-regexp
  19716. 'org-todo-regexp org-todo-regexp
  19717. 'keymap org-agenda-keymap
  19718. 'help-echo
  19719. (format "mouse-2 or RET jump to org file %s"
  19720. (abbreviate-file-name buffer-file-name))))
  19721. (regexp (concat
  19722. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19723. (regexp-quote
  19724. (substring
  19725. (format-time-string
  19726. (car org-time-stamp-formats)
  19727. (apply 'encode-time ; DATE bound by calendar
  19728. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19729. 1 11))))
  19730. marker hdmarker priority category tags closedp
  19731. ee txt timestr)
  19732. (goto-char (point-min))
  19733. (while (re-search-forward regexp nil t)
  19734. (catch :skip
  19735. (org-agenda-skip)
  19736. (setq marker (org-agenda-new-marker (match-beginning 0))
  19737. closedp (equal (match-string 1) org-closed-string)
  19738. category (org-get-category (match-beginning 0))
  19739. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19740. ;; donep (org-entry-is-done-p)
  19741. )
  19742. (if (string-match "\\]" timestr)
  19743. ;; substring should only run to end of time stamp
  19744. (setq timestr (substring timestr 0 (match-end 0))))
  19745. (save-excursion
  19746. (if (re-search-backward "^\\*+ " nil t)
  19747. (progn
  19748. (goto-char (match-beginning 0))
  19749. (setq hdmarker (org-agenda-new-marker)
  19750. tags (org-get-tags-at))
  19751. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19752. (setq txt (org-format-agenda-item
  19753. (if closedp "Closed: " "Clocked: ")
  19754. (match-string 1) category tags timestr)))
  19755. (setq txt org-agenda-no-heading-message))
  19756. (setq priority 100000)
  19757. (org-add-props txt props
  19758. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19759. 'priority priority 'org-category category
  19760. 'type "closed" 'date date
  19761. 'undone-face 'org-warning 'done-face 'org-done)
  19762. (push txt ee))
  19763. (goto-char (point-at-eol))))
  19764. (nreverse ee)))
  19765. (defun org-agenda-get-deadlines ()
  19766. "Return the deadline information for agenda display."
  19767. (let* ((props (list 'mouse-face 'highlight
  19768. 'org-not-done-regexp org-not-done-regexp
  19769. 'org-todo-regexp org-todo-regexp
  19770. 'keymap org-agenda-keymap
  19771. 'help-echo
  19772. (format "mouse-2 or RET jump to org file %s"
  19773. (abbreviate-file-name buffer-file-name))))
  19774. (regexp org-deadline-time-regexp)
  19775. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19776. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19777. d2 diff dfrac wdays pos pos1 category tags
  19778. ee txt head face s upcomingp donep timestr)
  19779. (goto-char (point-min))
  19780. (while (re-search-forward regexp nil t)
  19781. (catch :skip
  19782. (org-agenda-skip)
  19783. (setq s (match-string 1)
  19784. pos (1- (match-beginning 1))
  19785. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19786. diff (- d2 d1)
  19787. wdays (org-get-wdays s)
  19788. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  19789. upcomingp (and todayp (> diff 0)))
  19790. ;; When to show a deadline in the calendar:
  19791. ;; If the expiration is within wdays warning time.
  19792. ;; Past-due deadlines are only shown on the current date
  19793. (if (or (and (<= diff wdays)
  19794. (and todayp (not org-agenda-only-exact-dates)))
  19795. (= diff 0))
  19796. (save-excursion
  19797. (setq category (org-get-category))
  19798. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19799. (progn
  19800. (goto-char (match-end 0))
  19801. (setq pos1 (match-beginning 0))
  19802. (setq tags (org-get-tags-at pos1))
  19803. (setq head (buffer-substring-no-properties
  19804. (point)
  19805. (progn (skip-chars-forward "^\r\n")
  19806. (point))))
  19807. (setq donep (string-match org-looking-at-done-regexp head))
  19808. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19809. (setq timestr
  19810. (concat (substring s (match-beginning 1)) " "))
  19811. (setq timestr 'time))
  19812. (if (and donep
  19813. (or org-agenda-skip-deadline-if-done
  19814. (not (= diff 0))))
  19815. (setq txt nil)
  19816. (setq txt (org-format-agenda-item
  19817. (if (= diff 0)
  19818. (car org-agenda-deadline-leaders)
  19819. (format (nth 1 org-agenda-deadline-leaders)
  19820. diff))
  19821. head category tags timestr))))
  19822. (setq txt org-agenda-no-heading-message))
  19823. (when txt
  19824. (setq face (org-agenda-deadline-face dfrac))
  19825. (org-add-props txt props
  19826. 'org-marker (org-agenda-new-marker pos)
  19827. 'org-hd-marker (org-agenda-new-marker pos1)
  19828. 'priority (+ (- diff)
  19829. (org-get-priority txt))
  19830. 'org-category category
  19831. 'type (if upcomingp "upcoming-deadline" "deadline")
  19832. 'date (if upcomingp date d2)
  19833. 'face (if donep 'org-done face)
  19834. 'undone-face face 'done-face 'org-done)
  19835. (push txt ee))))))
  19836. (nreverse ee)))
  19837. (defun org-agenda-deadline-face (fraction)
  19838. "Return the face to displaying a deadline item.
  19839. FRACTION is what fraction of the head-warning time has passed."
  19840. (let ((faces org-agenda-deadline-faces) f)
  19841. (catch 'exit
  19842. (while (setq f (pop faces))
  19843. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19844. (defun org-agenda-get-scheduled ()
  19845. "Return the scheduled information for agenda display."
  19846. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19847. 'org-todo-regexp org-todo-regexp
  19848. 'done-face 'org-done
  19849. 'mouse-face 'highlight
  19850. 'keymap org-agenda-keymap
  19851. 'help-echo
  19852. (format "mouse-2 or RET jump to org file %s"
  19853. (abbreviate-file-name buffer-file-name))))
  19854. (regexp org-scheduled-time-regexp)
  19855. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19856. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19857. d2 diff pos pos1 category tags
  19858. ee txt head pastschedp donep face timestr s)
  19859. (goto-char (point-min))
  19860. (while (re-search-forward regexp nil t)
  19861. (catch :skip
  19862. (org-agenda-skip)
  19863. (setq s (match-string 1)
  19864. pos (1- (match-beginning 1))
  19865. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19866. ;;; is this right?
  19867. ;;; do we need to do this for deadleine too????
  19868. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  19869. diff (- d2 d1))
  19870. (setq pastschedp (and todayp (< diff 0)))
  19871. ;; When to show a scheduled item in the calendar:
  19872. ;; If it is on or past the date.
  19873. (if (or (and (< diff 0)
  19874. (and todayp (not org-agenda-only-exact-dates)))
  19875. (= diff 0))
  19876. (save-excursion
  19877. (setq category (org-get-category))
  19878. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19879. (progn
  19880. (goto-char (match-end 0))
  19881. (setq pos1 (match-beginning 0))
  19882. (setq tags (org-get-tags-at))
  19883. (setq head (buffer-substring-no-properties
  19884. (point)
  19885. (progn (skip-chars-forward "^\r\n") (point))))
  19886. (setq donep (string-match org-looking-at-done-regexp head))
  19887. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19888. (setq timestr
  19889. (concat (substring s (match-beginning 1)) " "))
  19890. (setq timestr 'time))
  19891. (if (and donep
  19892. (or org-agenda-skip-scheduled-if-done
  19893. (not (= diff 0))))
  19894. (setq txt nil)
  19895. (setq txt (org-format-agenda-item
  19896. (if (= diff 0)
  19897. (car org-agenda-scheduled-leaders)
  19898. (format (nth 1 org-agenda-scheduled-leaders)
  19899. (- 1 diff)))
  19900. head category tags timestr))))
  19901. (setq txt org-agenda-no-heading-message))
  19902. (when txt
  19903. (setq face (if pastschedp
  19904. 'org-scheduled-previously
  19905. 'org-scheduled-today))
  19906. (org-add-props txt props
  19907. 'undone-face face
  19908. 'face (if donep 'org-done face)
  19909. 'org-marker (org-agenda-new-marker pos)
  19910. 'org-hd-marker (org-agenda-new-marker pos1)
  19911. 'type (if pastschedp "past-scheduled" "scheduled")
  19912. 'date (if pastschedp d2 date)
  19913. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19914. 'org-category category)
  19915. (push txt ee))))))
  19916. (nreverse ee)))
  19917. (defun org-agenda-get-blocks ()
  19918. "Return the date-range information for agenda display."
  19919. (let* ((props (list 'face nil
  19920. 'org-not-done-regexp org-not-done-regexp
  19921. 'org-todo-regexp org-todo-regexp
  19922. 'mouse-face 'highlight
  19923. 'keymap org-agenda-keymap
  19924. 'help-echo
  19925. (format "mouse-2 or RET jump to org file %s"
  19926. (abbreviate-file-name buffer-file-name))))
  19927. (regexp org-tr-regexp)
  19928. (d0 (calendar-absolute-from-gregorian date))
  19929. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19930. donep head)
  19931. (goto-char (point-min))
  19932. (while (re-search-forward regexp nil t)
  19933. (catch :skip
  19934. (org-agenda-skip)
  19935. (setq pos (point))
  19936. (setq timestr (match-string 0)
  19937. s1 (match-string 1)
  19938. s2 (match-string 2)
  19939. d1 (time-to-days (org-time-string-to-time s1))
  19940. d2 (time-to-days (org-time-string-to-time s2)))
  19941. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19942. ;; Only allow days between the limits, because the normal
  19943. ;; date stamps will catch the limits.
  19944. (save-excursion
  19945. (setq marker (org-agenda-new-marker (point)))
  19946. (setq category (org-get-category))
  19947. (if (re-search-backward "^\\*+ " nil t)
  19948. (progn
  19949. (goto-char (match-beginning 0))
  19950. (setq hdmarker (org-agenda-new-marker (point)))
  19951. (setq tags (org-get-tags-at))
  19952. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19953. (setq head (match-string 1))
  19954. (and org-agenda-skip-timestamp-if-done
  19955. (org-entry-is-done-p)
  19956. (throw :skip t))
  19957. (setq txt (org-format-agenda-item
  19958. (format (if (= d1 d2) "" "(%d/%d): ")
  19959. (1+ (- d0 d1)) (1+ (- d2 d1)))
  19960. head category tags
  19961. (if (= d0 d1) timestr))))
  19962. (setq txt org-agenda-no-heading-message))
  19963. (org-add-props txt props
  19964. 'org-marker marker 'org-hd-marker hdmarker
  19965. 'type "block" 'date date
  19966. 'priority (org-get-priority txt) 'org-category category)
  19967. (push txt ee)))
  19968. (goto-char pos)))
  19969. ;; Sort the entries by expiration date.
  19970. (nreverse ee)))
  19971. ;;; Agenda presentation and sorting
  19972. (defconst org-plain-time-of-day-regexp
  19973. (concat
  19974. "\\(\\<[012]?[0-9]"
  19975. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19976. "\\(--?"
  19977. "\\(\\<[012]?[0-9]"
  19978. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19979. "\\)?")
  19980. "Regular expression to match a plain time or time range.
  19981. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19982. groups carry important information:
  19983. 0 the full match
  19984. 1 the first time, range or not
  19985. 8 the second time, if it is a range.")
  19986. (defconst org-plain-time-extension-regexp
  19987. (concat
  19988. "\\(\\<[012]?[0-9]"
  19989. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19990. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  19991. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  19992. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19993. groups carry important information:
  19994. 0 the full match
  19995. 7 hours of duration
  19996. 9 minutes of duration")
  19997. (defconst org-stamp-time-of-day-regexp
  19998. (concat
  19999. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20000. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20001. "\\(--?"
  20002. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20003. "Regular expression to match a timestamp time or time range.
  20004. After a match, the following groups carry important information:
  20005. 0 the full match
  20006. 1 date plus weekday, for backreferencing to make sure both times on same day
  20007. 2 the first time, range or not
  20008. 4 the second time, if it is a range.")
  20009. (defvar org-prefix-has-time nil
  20010. "A flag, set by `org-compile-prefix-format'.
  20011. The flag is set if the currently compiled format contains a `%t'.")
  20012. (defvar org-prefix-has-tag nil
  20013. "A flag, set by `org-compile-prefix-format'.
  20014. The flag is set if the currently compiled format contains a `%T'.")
  20015. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20016. noprefix remove-re)
  20017. "Format TXT to be inserted into the agenda buffer.
  20018. In particular, it adds the prefix and corresponding text properties. EXTRA
  20019. must be a string and replaces the `%s' specifier in the prefix format.
  20020. CATEGORY (string, symbol or nil) may be used to overrule the default
  20021. category taken from local variable or file name. It will replace the `%c'
  20022. specifier in the format. DOTIME, when non-nil, indicates that a
  20023. time-of-day should be extracted from TXT for sorting of this entry, and for
  20024. the `%t' specifier in the format. When DOTIME is a string, this string is
  20025. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20026. only the correctly processes TXT should be returned - this is used by
  20027. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20028. Any match of REMOVE-RE will be removed from TXT."
  20029. (save-match-data
  20030. ;; Diary entries sometimes have extra whitespace at the beginning
  20031. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20032. (let* ((category (or category
  20033. org-category
  20034. (if buffer-file-name
  20035. (file-name-sans-extension
  20036. (file-name-nondirectory buffer-file-name))
  20037. "")))
  20038. (tag (if tags (nth (1- (length tags)) tags) ""))
  20039. time ; time and tag are needed for the eval of the prefix format
  20040. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20041. (time-of-day (and dotime (org-get-time-of-day ts)))
  20042. stamp plain s0 s1 s2 rtn srp)
  20043. (when (and dotime time-of-day org-prefix-has-time)
  20044. ;; Extract starting and ending time and move them to prefix
  20045. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20046. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20047. (setq s0 (match-string 0 ts)
  20048. srp (and stamp (match-end 3))
  20049. s1 (match-string (if plain 1 2) ts)
  20050. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20051. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20052. ;; them, we might want to remove them there to avoid duplication.
  20053. ;; The user can turn this off with a variable.
  20054. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20055. (string-match (concat (regexp-quote s0) " *") txt)
  20056. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20057. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20058. (= (match-beginning 0) 0)
  20059. t))
  20060. (setq txt (replace-match "" nil nil txt))))
  20061. ;; Normalize the time(s) to 24 hour
  20062. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20063. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20064. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20065. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20066. (let ((m (+ (string-to-number (match-string 2 s1))
  20067. (* 60 (string-to-number (match-string 1 s1)))
  20068. org-agenda-default-appointment-duration))
  20069. h)
  20070. (setq h (/ m 60) m (- m (* h 60)))
  20071. (setq s2 (format "%02d:%02d" h m))))
  20072. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20073. txt)
  20074. ;; Tags are in the string
  20075. (if (or (eq org-agenda-remove-tags t)
  20076. (and org-agenda-remove-tags
  20077. org-prefix-has-tag))
  20078. (setq txt (replace-match "" t t txt))
  20079. (setq txt (replace-match
  20080. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20081. (match-string 2 txt))
  20082. t t txt))))
  20083. (when remove-re
  20084. (while (string-match remove-re txt)
  20085. (setq txt (replace-match "" t t txt))))
  20086. ;; Create the final string
  20087. (if noprefix
  20088. (setq rtn txt)
  20089. ;; Prepare the variables needed in the eval of the compiled format
  20090. (setq time (cond (s2 (concat s1 "-" s2))
  20091. (s1 (concat s1 "......"))
  20092. (t ""))
  20093. extra (or extra "")
  20094. category (if (symbolp category) (symbol-name category) category))
  20095. ;; Evaluate the compiled format
  20096. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20097. ;; And finally add the text properties
  20098. (org-add-props rtn nil
  20099. 'org-category (downcase category) 'tags tags
  20100. 'org-highest-priority org-highest-priority
  20101. 'org-lowest-priority org-lowest-priority
  20102. 'prefix-length (- (length rtn) (length txt))
  20103. 'time-of-day time-of-day
  20104. 'txt txt
  20105. 'time time
  20106. 'extra extra
  20107. 'dotime dotime))))
  20108. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20109. (defvar org-agenda-sorting-strategy-selected nil)
  20110. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20111. (catch 'exit
  20112. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20113. ((and todayp (member 'today (car org-agenda-time-grid))))
  20114. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20115. ((member 'weekly (car org-agenda-time-grid)))
  20116. (t (throw 'exit list)))
  20117. (let* ((have (delq nil (mapcar
  20118. (lambda (x) (get-text-property 1 'time-of-day x))
  20119. list)))
  20120. (string (nth 1 org-agenda-time-grid))
  20121. (gridtimes (nth 2 org-agenda-time-grid))
  20122. (req (car org-agenda-time-grid))
  20123. (remove (member 'remove-match req))
  20124. new time)
  20125. (if (and (member 'require-timed req) (not have))
  20126. ;; don't show empty grid
  20127. (throw 'exit list))
  20128. (while (setq time (pop gridtimes))
  20129. (unless (and remove (member time have))
  20130. (setq time (int-to-string time))
  20131. (push (org-format-agenda-item
  20132. nil string "" nil
  20133. (concat (substring time 0 -2) ":" (substring time -2)))
  20134. new)
  20135. (put-text-property
  20136. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20137. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20138. (append new list)
  20139. (append list new)))))
  20140. (defun org-compile-prefix-format (key)
  20141. "Compile the prefix format into a Lisp form that can be evaluated.
  20142. The resulting form is returned and stored in the variable
  20143. `org-prefix-format-compiled'."
  20144. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20145. (let ((s (cond
  20146. ((stringp org-agenda-prefix-format)
  20147. org-agenda-prefix-format)
  20148. ((assq key org-agenda-prefix-format)
  20149. (cdr (assq key org-agenda-prefix-format)))
  20150. (t " %-12:c%?-12t% s")))
  20151. (start 0)
  20152. varform vars var e c f opt)
  20153. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20154. s start)
  20155. (setq var (cdr (assoc (match-string 4 s)
  20156. '(("c" . category) ("t" . time) ("s" . extra)
  20157. ("T" . tag))))
  20158. c (or (match-string 3 s) "")
  20159. opt (match-beginning 1)
  20160. start (1+ (match-beginning 0)))
  20161. (if (equal var 'time) (setq org-prefix-has-time t))
  20162. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20163. (setq f (concat "%" (match-string 2 s) "s"))
  20164. (if opt
  20165. (setq varform
  20166. `(if (equal "" ,var)
  20167. ""
  20168. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20169. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20170. (setq s (replace-match "%s" t nil s))
  20171. (push varform vars))
  20172. (setq vars (nreverse vars))
  20173. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20174. (defun org-set-sorting-strategy (key)
  20175. (if (symbolp (car org-agenda-sorting-strategy))
  20176. ;; the old format
  20177. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20178. (setq org-agenda-sorting-strategy-selected
  20179. (or (cdr (assq key org-agenda-sorting-strategy))
  20180. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20181. '(time-up category-keep priority-down)))))
  20182. (defun org-get-time-of-day (s &optional string mod24)
  20183. "Check string S for a time of day.
  20184. If found, return it as a military time number between 0 and 2400.
  20185. If not found, return nil.
  20186. The optional STRING argument forces conversion into a 5 character wide string
  20187. HH:MM."
  20188. (save-match-data
  20189. (when
  20190. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20191. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20192. (let* ((h (string-to-number (match-string 1 s)))
  20193. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20194. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20195. (am-p (equal ampm "am"))
  20196. (h1 (cond ((not ampm) h)
  20197. ((= h 12) (if am-p 0 12))
  20198. (t (+ h (if am-p 0 12)))))
  20199. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20200. (mod h1 24) h1))
  20201. (t0 (+ (* 100 h2) m))
  20202. (t1 (concat (if (>= h1 24) "+" " ")
  20203. (if (< t0 100) "0" "")
  20204. (if (< t0 10) "0" "")
  20205. (int-to-string t0))))
  20206. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20207. (defun org-finalize-agenda-entries (list &optional nosort)
  20208. "Sort and concatenate the agenda items."
  20209. (setq list (mapcar 'org-agenda-highlight-todo list))
  20210. (if nosort
  20211. list
  20212. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20213. (defun org-agenda-highlight-todo (x)
  20214. (let (re pl)
  20215. (if (eq x 'line)
  20216. (save-excursion
  20217. (beginning-of-line 1)
  20218. (setq re (get-text-property (point) 'org-todo-regexp))
  20219. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20220. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20221. (add-text-properties (match-beginning 0) (match-end 0)
  20222. (list 'face (org-get-todo-face 0)))
  20223. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20224. (delete-region (match-beginning 1) (1- (match-end 0)))
  20225. (goto-char (match-beginning 1))
  20226. (insert (format org-agenda-todo-keyword-format s)))))
  20227. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20228. pl (get-text-property 0 'prefix-length x))
  20229. ; (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  20230. ; (add-text-properties
  20231. ; (or (match-end 1) (match-end 0)) (match-end 0)
  20232. ; (list 'face (org-get-todo-face (match-string 2 x)))
  20233. ; x))
  20234. (when (and re
  20235. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20236. x (or pl 0)) pl))
  20237. (add-text-properties
  20238. (or (match-end 1) (match-end 0)) (match-end 0)
  20239. (list 'face (org-get-todo-face (match-string 2 x)))
  20240. x)
  20241. (setq x (concat (substring x 0 (match-end 1))
  20242. (format org-agenda-todo-keyword-format
  20243. (match-string 2 x))
  20244. " "
  20245. (substring x (match-end 3)))))
  20246. x)))
  20247. (defsubst org-cmp-priority (a b)
  20248. "Compare the priorities of string A and B."
  20249. (let ((pa (or (get-text-property 1 'priority a) 0))
  20250. (pb (or (get-text-property 1 'priority b) 0)))
  20251. (cond ((> pa pb) +1)
  20252. ((< pa pb) -1)
  20253. (t nil))))
  20254. (defsubst org-cmp-category (a b)
  20255. "Compare the string values of categories of strings A and B."
  20256. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20257. (cb (or (get-text-property 1 'org-category b) "")))
  20258. (cond ((string-lessp ca cb) -1)
  20259. ((string-lessp cb ca) +1)
  20260. (t nil))))
  20261. (defsubst org-cmp-tag (a b)
  20262. "Compare the string values of categories of strings A and B."
  20263. (let ((ta (car (last (get-text-property 1 'tags a))))
  20264. (tb (car (last (get-text-property 1 'tags b)))))
  20265. (cond ((not ta) +1)
  20266. ((not tb) -1)
  20267. ((string-lessp ta tb) -1)
  20268. ((string-lessp tb ta) +1)
  20269. (t nil))))
  20270. (defsubst org-cmp-time (a b)
  20271. "Compare the time-of-day values of strings A and B."
  20272. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20273. (ta (or (get-text-property 1 'time-of-day a) def))
  20274. (tb (or (get-text-property 1 'time-of-day b) def)))
  20275. (cond ((< ta tb) -1)
  20276. ((< tb ta) +1)
  20277. (t nil))))
  20278. (defun org-entries-lessp (a b)
  20279. "Predicate for sorting agenda entries."
  20280. ;; The following variables will be used when the form is evaluated.
  20281. ;; So even though the compiler complains, keep them.
  20282. (let* ((time-up (org-cmp-time a b))
  20283. (time-down (if time-up (- time-up) nil))
  20284. (priority-up (org-cmp-priority a b))
  20285. (priority-down (if priority-up (- priority-up) nil))
  20286. (category-up (org-cmp-category a b))
  20287. (category-down (if category-up (- category-up) nil))
  20288. (category-keep (if category-up +1 nil))
  20289. (tag-up (org-cmp-tag a b))
  20290. (tag-down (if tag-up (- tag-up) nil)))
  20291. (cdr (assoc
  20292. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20293. '((-1 . t) (1 . nil) (nil . nil))))))
  20294. ;;; Agenda restriction lock
  20295. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20296. "Overlay to mark the headline to which arenda commands are restricted.")
  20297. (org-overlay-put org-agenda-restriction-lock-overlay
  20298. 'face 'org-agenda-restriction-lock)
  20299. (org-overlay-put org-agenda-restriction-lock-overlay
  20300. 'help-echo "Agendas are currently limited to this subtree.")
  20301. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20302. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20303. "Overlay marking the agenda restriction line in speedbar.")
  20304. (org-overlay-put org-speedbar-restriction-lock-overlay
  20305. 'face 'org-agenda-restriction-lock)
  20306. (org-overlay-put org-speedbar-restriction-lock-overlay
  20307. 'help-echo "Agendas are currently limited to this item.")
  20308. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20309. (defun org-agenda-set-restriction-lock (&optional type)
  20310. "Set restriction lock for agenda, to current subtree or file.
  20311. Restriction will be the file if TYPE is `file', or if type is the
  20312. universal prefix '(4), or if the cursor is before the first headline
  20313. in the file. Otherwise, restriction will be to the current subtree."
  20314. (interactive "P")
  20315. (and (equal type '(4)) (setq type 'file))
  20316. (setq type (cond
  20317. (type type)
  20318. ((org-at-heading-p) 'subtree)
  20319. ((condition-case nil (org-back-to-heading t) (error nil))
  20320. 'subtree)
  20321. (t 'file)))
  20322. (if (eq type 'subtree)
  20323. (progn
  20324. (setq org-agenda-restrict t)
  20325. (setq org-agenda-overriding-restriction 'subtree)
  20326. (put 'org-agenda-files 'org-restrict
  20327. (list (buffer-file-name (buffer-base-buffer))))
  20328. (org-back-to-heading t)
  20329. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20330. (move-marker org-agenda-restrict-begin (point))
  20331. (move-marker org-agenda-restrict-end
  20332. (save-excursion (org-end-of-subtree t)))
  20333. (message "Locking agenda restriction to subtree"))
  20334. (put 'org-agenda-files 'org-restrict
  20335. (list (buffer-file-name (buffer-base-buffer))))
  20336. (setq org-agenda-restrict nil)
  20337. (setq org-agenda-overriding-restriction 'file)
  20338. (move-marker org-agenda-restrict-begin nil)
  20339. (move-marker org-agenda-restrict-end nil)
  20340. (message "Locking agenda restriction to file"))
  20341. (setq current-prefix-arg nil)
  20342. (org-agenda-maybe-redo))
  20343. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20344. "Remove the agenda restriction lock."
  20345. (interactive "P")
  20346. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20347. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20348. (setq org-agenda-overriding-restriction nil)
  20349. (setq org-agenda-restrict nil)
  20350. (put 'org-agenda-files 'org-restrict nil)
  20351. (move-marker org-agenda-restrict-begin nil)
  20352. (move-marker org-agenda-restrict-end nil)
  20353. (setq current-prefix-arg nil)
  20354. (message "Agenda restriction lock removed")
  20355. (or noupdate (org-agenda-maybe-redo)))
  20356. (defun org-agenda-maybe-redo ()
  20357. "If there is any window showing the agenda view, update it."
  20358. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20359. (w0 (selected-window)))
  20360. (when w
  20361. (select-window w)
  20362. (org-agenda-redo)
  20363. (select-window w0)
  20364. (if org-agenda-overriding-restriction
  20365. (message "Agenda view shifted to new %s restriction"
  20366. org-agenda-overriding-restriction)
  20367. (message "Agenda restriction lock removed")))))
  20368. ;;; Agenda commands
  20369. (defun org-agenda-check-type (error &rest types)
  20370. "Check if agenda buffer is of allowed type.
  20371. If ERROR is non-nil, throw an error, otherwise just return nil."
  20372. (if (memq org-agenda-type types)
  20373. t
  20374. (if error
  20375. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20376. nil)))
  20377. (defun org-agenda-quit ()
  20378. "Exit agenda by removing the window or the buffer."
  20379. (interactive)
  20380. (let ((buf (current-buffer)))
  20381. (if (not (one-window-p)) (delete-window))
  20382. (kill-buffer buf)
  20383. (org-agenda-reset-markers)
  20384. (org-columns-remove-overlays))
  20385. ;; Maybe restore the pre-agenda window configuration.
  20386. (and org-agenda-restore-windows-after-quit
  20387. (not (eq org-agenda-window-setup 'other-frame))
  20388. org-pre-agenda-window-conf
  20389. (set-window-configuration org-pre-agenda-window-conf)))
  20390. (defun org-agenda-exit ()
  20391. "Exit agenda by removing the window or the buffer.
  20392. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20393. Org-mode buffers visited directly by the user will not be touched."
  20394. (interactive)
  20395. (org-release-buffers org-agenda-new-buffers)
  20396. (setq org-agenda-new-buffers nil)
  20397. (org-agenda-quit))
  20398. (defun org-agenda-execute (arg)
  20399. "Execute another agenda command, keeping same window.\\<global-map>
  20400. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20401. (interactive "P")
  20402. (let ((org-agenda-window-setup 'current-window))
  20403. (org-agenda arg)))
  20404. (defun org-save-all-org-buffers ()
  20405. "Save all Org-mode buffers without user confirmation."
  20406. (interactive)
  20407. (message "Saving all Org-mode buffers...")
  20408. (save-some-buffers t 'org-mode-p)
  20409. (message "Saving all Org-mode buffers... done"))
  20410. (defun org-agenda-redo ()
  20411. "Rebuild Agenda.
  20412. When this is the global TODO list, a prefix argument will be interpreted."
  20413. (interactive)
  20414. (let* ((org-agenda-keep-modes t)
  20415. (line (org-current-line))
  20416. (window-line (- line (org-current-line (window-start))))
  20417. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20418. (message "Rebuilding agenda buffer...")
  20419. (org-let lprops '(eval org-agenda-redo-command))
  20420. (setq org-agenda-undo-list nil
  20421. org-agenda-pending-undo-list nil)
  20422. (message "Rebuilding agenda buffer...done")
  20423. (goto-line line)
  20424. (recenter window-line)))
  20425. (defun org-agenda-goto-date (date)
  20426. "Jump to DATE in agenda."
  20427. (interactive (list (org-read-date)))
  20428. (org-agenda-list nil date))
  20429. (defun org-agenda-goto-today ()
  20430. "Go to today."
  20431. (interactive)
  20432. (org-agenda-check-type t 'timeline 'agenda)
  20433. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20434. (cond
  20435. (tdpos (goto-char tdpos))
  20436. ((eq org-agenda-type 'agenda)
  20437. (let* ((sd (time-to-days
  20438. (time-subtract (current-time)
  20439. (list 0 (* 3600 org-extend-today-until) 0))))
  20440. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20441. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20442. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20443. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20444. (org-agenda-redo)
  20445. (org-agenda-find-same-or-today-or-agenda)))
  20446. (t (error "Cannot find today")))))
  20447. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20448. (goto-char
  20449. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20450. (text-property-any (point-min) (point-max) 'org-today t)
  20451. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20452. (point-min))))
  20453. (defun org-agenda-later (arg)
  20454. "Go forward in time by thee current span.
  20455. With prefix ARG, go forward that many times the current span."
  20456. (interactive "p")
  20457. (org-agenda-check-type t 'agenda)
  20458. (let* ((span org-agenda-span)
  20459. (sd org-starting-day)
  20460. (greg (calendar-gregorian-from-absolute sd))
  20461. (cnt (get-text-property (point) 'org-day-cnt))
  20462. greg2 nd)
  20463. (cond
  20464. ((eq span 'day)
  20465. (setq sd (+ arg sd) nd 1))
  20466. ((eq span 'week)
  20467. (setq sd (+ (* 7 arg) sd) nd 7))
  20468. ((eq span 'month)
  20469. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20470. sd (calendar-absolute-from-gregorian greg2))
  20471. (setcar greg2 (1+ (car greg2)))
  20472. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20473. ((eq span 'year)
  20474. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20475. sd (calendar-absolute-from-gregorian greg2))
  20476. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20477. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20478. (let ((org-agenda-overriding-arguments
  20479. (list (car org-agenda-last-arguments) sd nd t)))
  20480. (org-agenda-redo)
  20481. (org-agenda-find-same-or-today-or-agenda cnt))))
  20482. (defun org-agenda-earlier (arg)
  20483. "Go backward in time by the current span.
  20484. With prefix ARG, go backward that many times the current span."
  20485. (interactive "p")
  20486. (org-agenda-later (- arg)))
  20487. (defun org-agenda-day-view ()
  20488. "Switch to daily view for agenda."
  20489. (interactive)
  20490. (setq org-agenda-ndays 1)
  20491. (org-agenda-change-time-span 'day))
  20492. (defun org-agenda-week-view ()
  20493. "Switch to daily view for agenda."
  20494. (interactive)
  20495. (setq org-agenda-ndays 7)
  20496. (org-agenda-change-time-span 'week))
  20497. (defun org-agenda-month-view ()
  20498. "Switch to daily view for agenda."
  20499. (interactive)
  20500. (org-agenda-change-time-span 'month))
  20501. (defun org-agenda-year-view ()
  20502. "Switch to daily view for agenda."
  20503. (interactive)
  20504. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20505. (org-agenda-change-time-span 'year)
  20506. (error "Abort")))
  20507. (defun org-agenda-change-time-span (span)
  20508. "Change the agenda view to SPAN.
  20509. SPAN may be `day', `week', `month', `year'."
  20510. (org-agenda-check-type t 'agenda)
  20511. (if (equal org-agenda-span span)
  20512. (error "Viewing span is already \"%s\"" span))
  20513. (let* ((sd (or (get-text-property (point) 'day)
  20514. org-starting-day))
  20515. (computed (org-agenda-compute-time-span sd span))
  20516. (org-agenda-overriding-arguments
  20517. (list (car org-agenda-last-arguments)
  20518. (car computed) (cdr computed) t)))
  20519. (org-agenda-redo)
  20520. (org-agenda-find-same-or-today-or-agenda))
  20521. (org-agenda-set-mode-name)
  20522. (message "Switched to %s view" span))
  20523. (defun org-agenda-compute-time-span (sd span)
  20524. "Compute starting date and number of days for agenda.
  20525. SPAN may be `day', `week', `month', `year'. The return value
  20526. is a cons cell with the starting date and the number of days,
  20527. so that the date SD will be in that range."
  20528. (let* ((greg (calendar-gregorian-from-absolute sd))
  20529. nd)
  20530. (cond
  20531. ((eq span 'day)
  20532. (setq nd 1))
  20533. ((eq span 'week)
  20534. (let* ((nt (calendar-day-of-week
  20535. (calendar-gregorian-from-absolute sd)))
  20536. (d (if org-agenda-start-on-weekday
  20537. (- nt org-agenda-start-on-weekday)
  20538. 0)))
  20539. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20540. (setq nd 7)))
  20541. ((eq span 'month)
  20542. (setq sd (calendar-absolute-from-gregorian
  20543. (list (car greg) 1 (nth 2 greg)))
  20544. nd (- (calendar-absolute-from-gregorian
  20545. (list (1+ (car greg)) 1 (nth 2 greg)))
  20546. sd)))
  20547. ((eq span 'year)
  20548. (setq sd (calendar-absolute-from-gregorian
  20549. (list 1 1 (nth 2 greg)))
  20550. nd (- (calendar-absolute-from-gregorian
  20551. (list 1 1 (1+ (nth 2 greg))))
  20552. sd))))
  20553. (cons sd nd)))
  20554. ;; FIXME: does not work if user makes date format that starts with a blank
  20555. (defun org-agenda-next-date-line (&optional arg)
  20556. "Jump to the next line indicating a date in agenda buffer."
  20557. (interactive "p")
  20558. (org-agenda-check-type t 'agenda 'timeline)
  20559. (beginning-of-line 1)
  20560. (if (looking-at "^\\S-") (forward-char 1))
  20561. (if (not (re-search-forward "^\\S-" nil t arg))
  20562. (progn
  20563. (backward-char 1)
  20564. (error "No next date after this line in this buffer")))
  20565. (goto-char (match-beginning 0)))
  20566. (defun org-agenda-previous-date-line (&optional arg)
  20567. "Jump to the previous line indicating a date in agenda buffer."
  20568. (interactive "p")
  20569. (org-agenda-check-type t 'agenda 'timeline)
  20570. (beginning-of-line 1)
  20571. (if (not (re-search-backward "^\\S-" nil t arg))
  20572. (error "No previous date before this line in this buffer")))
  20573. ;; Initialize the highlight
  20574. (defvar org-hl (org-make-overlay 1 1))
  20575. (org-overlay-put org-hl 'face 'highlight)
  20576. (defun org-highlight (begin end &optional buffer)
  20577. "Highlight a region with overlay."
  20578. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20579. org-hl begin end (or buffer (current-buffer))))
  20580. (defun org-unhighlight ()
  20581. "Detach overlay INDEX."
  20582. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20583. ;; FIXME this is currently not used.
  20584. (defun org-highlight-until-next-command (beg end &optional buffer)
  20585. (org-highlight beg end buffer)
  20586. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20587. (defun org-unhighlight-once ()
  20588. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20589. (org-unhighlight))
  20590. (defun org-agenda-follow-mode ()
  20591. "Toggle follow mode in an agenda buffer."
  20592. (interactive)
  20593. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20594. (org-agenda-set-mode-name)
  20595. (message "Follow mode is %s"
  20596. (if org-agenda-follow-mode "on" "off")))
  20597. (defun org-agenda-log-mode ()
  20598. "Toggle log mode in an agenda buffer."
  20599. (interactive)
  20600. (org-agenda-check-type t 'agenda 'timeline)
  20601. (setq org-agenda-show-log (not org-agenda-show-log))
  20602. (org-agenda-set-mode-name)
  20603. (org-agenda-redo)
  20604. (message "Log mode is %s"
  20605. (if org-agenda-show-log "on" "off")))
  20606. (defun org-agenda-toggle-diary ()
  20607. "Toggle diary inclusion in an agenda buffer."
  20608. (interactive)
  20609. (org-agenda-check-type t 'agenda)
  20610. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20611. (org-agenda-redo)
  20612. (org-agenda-set-mode-name)
  20613. (message "Diary inclusion turned %s"
  20614. (if org-agenda-include-diary "on" "off")))
  20615. (defun org-agenda-toggle-time-grid ()
  20616. "Toggle time grid in an agenda buffer."
  20617. (interactive)
  20618. (org-agenda-check-type t 'agenda)
  20619. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20620. (org-agenda-redo)
  20621. (org-agenda-set-mode-name)
  20622. (message "Time-grid turned %s"
  20623. (if org-agenda-use-time-grid "on" "off")))
  20624. (defun org-agenda-set-mode-name ()
  20625. "Set the mode name to indicate all the small mode settings."
  20626. (setq mode-name
  20627. (concat "Org-Agenda"
  20628. (if (equal org-agenda-ndays 1) " Day" "")
  20629. (if (equal org-agenda-ndays 7) " Week" "")
  20630. (if org-agenda-follow-mode " Follow" "")
  20631. (if org-agenda-include-diary " Diary" "")
  20632. (if org-agenda-use-time-grid " Grid" "")
  20633. (if org-agenda-show-log " Log" "")))
  20634. (force-mode-line-update))
  20635. (defun org-agenda-post-command-hook ()
  20636. (and (eolp) (not (bolp)) (backward-char 1))
  20637. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20638. (if (and org-agenda-follow-mode
  20639. (get-text-property (point) 'org-marker))
  20640. (org-agenda-show)))
  20641. (defun org-agenda-show-priority ()
  20642. "Show the priority of the current item.
  20643. This priority is composed of the main priority given with the [#A] cookies,
  20644. and by additional input from the age of a schedules or deadline entry."
  20645. (interactive)
  20646. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20647. (message "Priority is %d" (if pri pri -1000))))
  20648. (defun org-agenda-show-tags ()
  20649. "Show the tags applicable to the current item."
  20650. (interactive)
  20651. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20652. (if tags
  20653. (message "Tags are :%s:"
  20654. (org-no-properties (mapconcat 'identity tags ":")))
  20655. (message "No tags associated with this line"))))
  20656. (defun org-agenda-goto (&optional highlight)
  20657. "Go to the Org-mode file which contains the item at point."
  20658. (interactive)
  20659. (let* ((marker (or (get-text-property (point) 'org-marker)
  20660. (org-agenda-error)))
  20661. (buffer (marker-buffer marker))
  20662. (pos (marker-position marker)))
  20663. (switch-to-buffer-other-window buffer)
  20664. (widen)
  20665. (goto-char pos)
  20666. (when (org-mode-p)
  20667. (org-show-context 'agenda)
  20668. (save-excursion
  20669. (and (outline-next-heading)
  20670. (org-flag-heading nil)))) ; show the next heading
  20671. (recenter (/ (window-height) 2))
  20672. (run-hooks 'org-agenda-after-show-hook)
  20673. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20674. (defvar org-agenda-after-show-hook nil
  20675. "Normal hook run after an item has been shown from the agenda.
  20676. Point is in the buffer where the item originated.")
  20677. (defun org-agenda-kill ()
  20678. "Kill the entry or subtree belonging to the current agenda entry."
  20679. (interactive)
  20680. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20681. (let* ((marker (or (get-text-property (point) 'org-marker)
  20682. (org-agenda-error)))
  20683. (buffer (marker-buffer marker))
  20684. (pos (marker-position marker))
  20685. (type (get-text-property (point) 'type))
  20686. dbeg dend (n 0) conf)
  20687. (org-with-remote-undo buffer
  20688. (with-current-buffer buffer
  20689. (save-excursion
  20690. (goto-char pos)
  20691. (if (and (org-mode-p) (not (member type '("sexp"))))
  20692. (setq dbeg (progn (org-back-to-heading t) (point))
  20693. dend (org-end-of-subtree t t))
  20694. (setq dbeg (point-at-bol)
  20695. dend (min (point-max) (1+ (point-at-eol)))))
  20696. (goto-char dbeg)
  20697. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20698. (setq conf (or (eq t org-agenda-confirm-kill)
  20699. (and (numberp org-agenda-confirm-kill)
  20700. (> n org-agenda-confirm-kill))))
  20701. (and conf
  20702. (not (y-or-n-p
  20703. (format "Delete entry with %d lines in buffer \"%s\"? "
  20704. n (buffer-name buffer))))
  20705. (error "Abort"))
  20706. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20707. (with-current-buffer buffer (delete-region dbeg dend))
  20708. (message "Agenda item and source killed"))))
  20709. (defun org-agenda-archive ()
  20710. "Kill the entry or subtree belonging to the current agenda entry."
  20711. (interactive)
  20712. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20713. (let* ((marker (or (get-text-property (point) 'org-marker)
  20714. (org-agenda-error)))
  20715. (buffer (marker-buffer marker))
  20716. (pos (marker-position marker)))
  20717. (org-with-remote-undo buffer
  20718. (with-current-buffer buffer
  20719. (if (org-mode-p)
  20720. (save-excursion
  20721. (goto-char pos)
  20722. (org-remove-subtree-entries-from-agenda)
  20723. (org-back-to-heading t)
  20724. (org-archive-subtree))
  20725. (error "Archiving works only in Org-mode files"))))))
  20726. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20727. "Remove all lines in the agenda that correspond to a given subtree.
  20728. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20729. If this information is not given, the function uses the tree at point."
  20730. (let ((buf (or buf (current-buffer))) m p)
  20731. (save-excursion
  20732. (unless (and beg end)
  20733. (org-back-to-heading t)
  20734. (setq beg (point))
  20735. (org-end-of-subtree t)
  20736. (setq end (point)))
  20737. (set-buffer (get-buffer org-agenda-buffer-name))
  20738. (save-excursion
  20739. (goto-char (point-max))
  20740. (beginning-of-line 1)
  20741. (while (not (bobp))
  20742. (when (and (setq m (get-text-property (point) 'org-marker))
  20743. (equal buf (marker-buffer m))
  20744. (setq p (marker-position m))
  20745. (>= p beg)
  20746. (<= p end))
  20747. (let ((inhibit-read-only t))
  20748. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20749. (beginning-of-line 0))))))
  20750. (defun org-agenda-open-link ()
  20751. "Follow the link in the current line, if any."
  20752. (interactive)
  20753. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20754. (save-excursion
  20755. (save-restriction
  20756. (narrow-to-region (point-at-bol) (point-at-eol))
  20757. (org-open-at-point))))
  20758. (defun org-agenda-copy-local-variable (var)
  20759. "Get a variable from a referenced buffer and install it here."
  20760. (let ((m (get-text-property (point) 'org-marker)))
  20761. (when (and m (buffer-live-p (marker-buffer m)))
  20762. (org-set-local var (with-current-buffer (marker-buffer m)
  20763. (symbol-value var))))))
  20764. (defun org-agenda-switch-to (&optional delete-other-windows)
  20765. "Go to the Org-mode file which contains the item at point."
  20766. (interactive)
  20767. (let* ((marker (or (get-text-property (point) 'org-marker)
  20768. (org-agenda-error)))
  20769. (buffer (marker-buffer marker))
  20770. (pos (marker-position marker)))
  20771. (switch-to-buffer buffer)
  20772. (and delete-other-windows (delete-other-windows))
  20773. (widen)
  20774. (goto-char pos)
  20775. (when (org-mode-p)
  20776. (org-show-context 'agenda)
  20777. (save-excursion
  20778. (and (outline-next-heading)
  20779. (org-flag-heading nil)))))) ; show the next heading
  20780. (defun org-agenda-goto-mouse (ev)
  20781. "Go to the Org-mode file which contains the item at the mouse click."
  20782. (interactive "e")
  20783. (mouse-set-point ev)
  20784. (org-agenda-goto))
  20785. (defun org-agenda-show ()
  20786. "Display the Org-mode file which contains the item at point."
  20787. (interactive)
  20788. (let ((win (selected-window)))
  20789. (org-agenda-goto t)
  20790. (select-window win)))
  20791. (defun org-agenda-recenter (arg)
  20792. "Display the Org-mode file which contains the item at point and recenter."
  20793. (interactive "P")
  20794. (let ((win (selected-window)))
  20795. (org-agenda-goto t)
  20796. (recenter arg)
  20797. (select-window win)))
  20798. (defun org-agenda-show-mouse (ev)
  20799. "Display the Org-mode file which contains the item at the mouse click."
  20800. (interactive "e")
  20801. (mouse-set-point ev)
  20802. (org-agenda-show))
  20803. (defun org-agenda-check-no-diary ()
  20804. "Check if the entry is a diary link and abort if yes."
  20805. (if (get-text-property (point) 'org-agenda-diary-link)
  20806. (org-agenda-error)))
  20807. (defun org-agenda-error ()
  20808. (error "Command not allowed in this line"))
  20809. (defun org-agenda-tree-to-indirect-buffer ()
  20810. "Show the subtree corresponding to the current entry in an indirect buffer.
  20811. This calls the command `org-tree-to-indirect-buffer' from the original
  20812. Org-mode buffer.
  20813. With numerical prefix arg ARG, go up to this level and then take that tree.
  20814. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20815. dedicated frame)."
  20816. (interactive)
  20817. (org-agenda-check-no-diary)
  20818. (let* ((marker (or (get-text-property (point) 'org-marker)
  20819. (org-agenda-error)))
  20820. (buffer (marker-buffer marker))
  20821. (pos (marker-position marker)))
  20822. (with-current-buffer buffer
  20823. (save-excursion
  20824. (goto-char pos)
  20825. (call-interactively 'org-tree-to-indirect-buffer)))))
  20826. (defvar org-last-heading-marker (make-marker)
  20827. "Marker pointing to the headline that last changed its TODO state
  20828. by a remote command from the agenda.")
  20829. (defun org-agenda-todo-nextset ()
  20830. "Switch TODO entry to next sequence."
  20831. (interactive)
  20832. (org-agenda-todo 'nextset))
  20833. (defun org-agenda-todo-previousset ()
  20834. "Switch TODO entry to previous sequence."
  20835. (interactive)
  20836. (org-agenda-todo 'previousset))
  20837. (defun org-agenda-todo (&optional arg)
  20838. "Cycle TODO state of line at point, also in Org-mode file.
  20839. This changes the line at point, all other lines in the agenda referring to
  20840. the same tree node, and the headline of the tree node in the Org-mode file."
  20841. (interactive "P")
  20842. (org-agenda-check-no-diary)
  20843. (let* ((col (current-column))
  20844. (marker (or (get-text-property (point) 'org-marker)
  20845. (org-agenda-error)))
  20846. (buffer (marker-buffer marker))
  20847. (pos (marker-position marker))
  20848. (hdmarker (get-text-property (point) 'org-hd-marker))
  20849. (inhibit-read-only t)
  20850. newhead)
  20851. (org-with-remote-undo buffer
  20852. (with-current-buffer buffer
  20853. (widen)
  20854. (goto-char pos)
  20855. (org-show-context 'agenda)
  20856. (save-excursion
  20857. (and (outline-next-heading)
  20858. (org-flag-heading nil))) ; show the next heading
  20859. (org-todo arg)
  20860. (and (bolp) (forward-char 1))
  20861. (setq newhead (org-get-heading))
  20862. (save-excursion
  20863. (org-back-to-heading)
  20864. (move-marker org-last-heading-marker (point))))
  20865. (beginning-of-line 1)
  20866. (save-excursion
  20867. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20868. (move-to-column col))))
  20869. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20870. "Change all lines in the agenda buffer which match HDMARKER.
  20871. The new content of the line will be NEWHEAD (as modified by
  20872. `org-format-agenda-item'). HDMARKER is checked with
  20873. `equal' against all `org-hd-marker' text properties in the file.
  20874. If FIXFACE is non-nil, the face of each item is modified acording to
  20875. the new TODO state."
  20876. (let* ((inhibit-read-only t)
  20877. props m pl undone-face done-face finish new dotime cat tags)
  20878. (save-excursion
  20879. (goto-char (point-max))
  20880. (beginning-of-line 1)
  20881. (while (not finish)
  20882. (setq finish (bobp))
  20883. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20884. (equal m hdmarker))
  20885. (setq props (text-properties-at (point))
  20886. dotime (get-text-property (point) 'dotime)
  20887. cat (get-text-property (point) 'org-category)
  20888. tags (get-text-property (point) 'tags)
  20889. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20890. pl (get-text-property (point) 'prefix-length)
  20891. undone-face (get-text-property (point) 'undone-face)
  20892. done-face (get-text-property (point) 'done-face))
  20893. (move-to-column pl)
  20894. (cond
  20895. ((equal new "")
  20896. (beginning-of-line 1)
  20897. (and (looking-at ".*\n?") (replace-match "")))
  20898. ((looking-at ".*")
  20899. (replace-match new t t)
  20900. (beginning-of-line 1)
  20901. (add-text-properties (point-at-bol) (point-at-eol) props)
  20902. (when fixface
  20903. (add-text-properties
  20904. (point-at-bol) (point-at-eol)
  20905. (list 'face
  20906. (if org-last-todo-state-is-todo
  20907. undone-face done-face))))
  20908. (org-agenda-highlight-todo 'line)
  20909. (beginning-of-line 1))
  20910. (t (error "Line update did not work"))))
  20911. (beginning-of-line 0)))
  20912. (org-finalize-agenda)))
  20913. (defun org-agenda-align-tags (&optional line)
  20914. "Align all tags in agenda items to `org-agenda-tags-column'."
  20915. (let ((inhibit-read-only t) l c)
  20916. (save-excursion
  20917. (goto-char (if line (point-at-bol) (point-min)))
  20918. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20919. (if line (point-at-eol) nil) t)
  20920. (add-text-properties
  20921. (match-beginning 2) (match-end 2)
  20922. (list 'face (list 'org-tag (get-text-property
  20923. (match-beginning 2) 'face))))
  20924. (setq l (- (match-end 2) (match-beginning 2))
  20925. c (if (< org-agenda-tags-column 0)
  20926. (- (abs org-agenda-tags-column) l)
  20927. org-agenda-tags-column))
  20928. (delete-region (match-beginning 1) (match-end 1))
  20929. (goto-char (match-beginning 1))
  20930. (insert (org-add-props
  20931. (make-string (max 1 (- c (current-column))) ?\ )
  20932. (text-properties-at (point))))))))
  20933. (defun org-agenda-priority-up ()
  20934. "Increase the priority of line at point, also in Org-mode file."
  20935. (interactive)
  20936. (org-agenda-priority 'up))
  20937. (defun org-agenda-priority-down ()
  20938. "Decrease the priority of line at point, also in Org-mode file."
  20939. (interactive)
  20940. (org-agenda-priority 'down))
  20941. (defun org-agenda-priority (&optional force-direction)
  20942. "Set the priority of line at point, also in Org-mode file.
  20943. This changes the line at point, all other lines in the agenda referring to
  20944. the same tree node, and the headline of the tree node in the Org-mode file."
  20945. (interactive)
  20946. (org-agenda-check-no-diary)
  20947. (let* ((marker (or (get-text-property (point) 'org-marker)
  20948. (org-agenda-error)))
  20949. (hdmarker (get-text-property (point) 'org-hd-marker))
  20950. (buffer (marker-buffer hdmarker))
  20951. (pos (marker-position hdmarker))
  20952. (inhibit-read-only t)
  20953. newhead)
  20954. (org-with-remote-undo buffer
  20955. (with-current-buffer buffer
  20956. (widen)
  20957. (goto-char pos)
  20958. (org-show-context 'agenda)
  20959. (save-excursion
  20960. (and (outline-next-heading)
  20961. (org-flag-heading nil))) ; show the next heading
  20962. (funcall 'org-priority force-direction)
  20963. (end-of-line 1)
  20964. (setq newhead (org-get-heading)))
  20965. (org-agenda-change-all-lines newhead hdmarker)
  20966. (beginning-of-line 1))))
  20967. (defun org-get-tags-at (&optional pos)
  20968. "Get a list of all headline tags applicable at POS.
  20969. POS defaults to point. If tags are inherited, the list contains
  20970. the targets in the same sequence as the headlines appear, i.e.
  20971. the tags of the current headline come last."
  20972. (interactive)
  20973. (let (tags lastpos)
  20974. (save-excursion
  20975. (save-restriction
  20976. (widen)
  20977. (goto-char (or pos (point)))
  20978. (save-match-data
  20979. (org-back-to-heading t)
  20980. (condition-case nil
  20981. (while (not (equal lastpos (point)))
  20982. (setq lastpos (point))
  20983. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  20984. (setq tags (append (org-split-string
  20985. (org-match-string-no-properties 1) ":")
  20986. tags)))
  20987. (or org-use-tag-inheritance (error ""))
  20988. (org-up-heading-all 1))
  20989. (error nil))))
  20990. tags)))
  20991. ;; FIXME: should fix the tags property of the agenda line.
  20992. (defun org-agenda-set-tags ()
  20993. "Set tags for the current headline."
  20994. (interactive)
  20995. (org-agenda-check-no-diary)
  20996. (if (and (org-region-active-p) (interactive-p))
  20997. (call-interactively 'org-change-tag-in-region)
  20998. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20999. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21000. (org-agenda-error)))
  21001. (buffer (marker-buffer hdmarker))
  21002. (pos (marker-position hdmarker))
  21003. (inhibit-read-only t)
  21004. newhead)
  21005. (org-with-remote-undo buffer
  21006. (with-current-buffer buffer
  21007. (widen)
  21008. (goto-char pos)
  21009. (save-excursion
  21010. (org-show-context 'agenda))
  21011. (save-excursion
  21012. (and (outline-next-heading)
  21013. (org-flag-heading nil))) ; show the next heading
  21014. (goto-char pos)
  21015. (call-interactively 'org-set-tags)
  21016. (end-of-line 1)
  21017. (setq newhead (org-get-heading)))
  21018. (org-agenda-change-all-lines newhead hdmarker)
  21019. (beginning-of-line 1)))))
  21020. (defun org-agenda-toggle-archive-tag ()
  21021. "Toggle the archive tag for the current entry."
  21022. (interactive)
  21023. (org-agenda-check-no-diary)
  21024. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21025. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21026. (org-agenda-error)))
  21027. (buffer (marker-buffer hdmarker))
  21028. (pos (marker-position hdmarker))
  21029. (inhibit-read-only t)
  21030. newhead)
  21031. (org-with-remote-undo buffer
  21032. (with-current-buffer buffer
  21033. (widen)
  21034. (goto-char pos)
  21035. (org-show-context 'agenda)
  21036. (save-excursion
  21037. (and (outline-next-heading)
  21038. (org-flag-heading nil))) ; show the next heading
  21039. (call-interactively 'org-toggle-archive-tag)
  21040. (end-of-line 1)
  21041. (setq newhead (org-get-heading)))
  21042. (org-agenda-change-all-lines newhead hdmarker)
  21043. (beginning-of-line 1))))
  21044. (defun org-agenda-date-later (arg &optional what)
  21045. "Change the date of this item to one day later."
  21046. (interactive "p")
  21047. (org-agenda-check-type t 'agenda 'timeline)
  21048. (org-agenda-check-no-diary)
  21049. (let* ((marker (or (get-text-property (point) 'org-marker)
  21050. (org-agenda-error)))
  21051. (buffer (marker-buffer marker))
  21052. (pos (marker-position marker)))
  21053. (org-with-remote-undo buffer
  21054. (with-current-buffer buffer
  21055. (widen)
  21056. (goto-char pos)
  21057. (if (not (org-at-timestamp-p))
  21058. (error "Cannot find time stamp"))
  21059. (org-timestamp-change arg (or what 'day)))
  21060. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21061. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21062. (defun org-agenda-date-earlier (arg &optional what)
  21063. "Change the date of this item to one day earlier."
  21064. (interactive "p")
  21065. (org-agenda-date-later (- arg) what))
  21066. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21067. "Show new date stamp via text properties."
  21068. ;; We use text properties to make this undoable
  21069. (let ((inhibit-read-only t))
  21070. (setq stamp (concat " " prefix " => " stamp))
  21071. (save-excursion
  21072. (goto-char (point-max))
  21073. (while (not (bobp))
  21074. (when (equal marker (get-text-property (point) 'org-marker))
  21075. (move-to-column (- (window-width) (length stamp)) t)
  21076. (if (featurep 'xemacs)
  21077. ;; Use `duplicable' property to trigger undo recording
  21078. (let ((ex (make-extent nil nil))
  21079. (gl (make-glyph stamp)))
  21080. (set-glyph-face gl 'secondary-selection)
  21081. (set-extent-properties
  21082. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21083. (insert-extent ex (1- (point)) (point-at-eol)))
  21084. (add-text-properties
  21085. (1- (point)) (point-at-eol)
  21086. (list 'display (org-add-props stamp nil
  21087. 'face 'secondary-selection))))
  21088. (beginning-of-line 1))
  21089. (beginning-of-line 0)))))
  21090. (defun org-agenda-date-prompt (arg)
  21091. "Change the date of this item. Date is prompted for, with default today.
  21092. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21093. be used to request time specification in the time stamp."
  21094. (interactive "P")
  21095. (org-agenda-check-type t 'agenda 'timeline)
  21096. (org-agenda-check-no-diary)
  21097. (let* ((marker (or (get-text-property (point) 'org-marker)
  21098. (org-agenda-error)))
  21099. (buffer (marker-buffer marker))
  21100. (pos (marker-position marker)))
  21101. (org-with-remote-undo buffer
  21102. (with-current-buffer buffer
  21103. (widen)
  21104. (goto-char pos)
  21105. (if (not (org-at-timestamp-p))
  21106. (error "Cannot find time stamp"))
  21107. (org-time-stamp arg)
  21108. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21109. (defun org-agenda-schedule (arg)
  21110. "Schedule the item at point."
  21111. (interactive "P")
  21112. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21113. (org-agenda-check-no-diary)
  21114. (let* ((marker (or (get-text-property (point) 'org-marker)
  21115. (org-agenda-error)))
  21116. (buffer (marker-buffer marker))
  21117. (pos (marker-position marker))
  21118. (org-insert-labeled-timestamps-at-point nil)
  21119. ts)
  21120. (org-with-remote-undo buffer
  21121. (with-current-buffer buffer
  21122. (widen)
  21123. (goto-char pos)
  21124. (setq ts (org-schedule arg)))
  21125. (org-agenda-show-new-time marker ts "S"))
  21126. (message "Item scheduled for %s" ts)))
  21127. (defun org-agenda-deadline (arg)
  21128. "Schedule the item at point."
  21129. (interactive "P")
  21130. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21131. (org-agenda-check-no-diary)
  21132. (let* ((marker (or (get-text-property (point) 'org-marker)
  21133. (org-agenda-error)))
  21134. (buffer (marker-buffer marker))
  21135. (pos (marker-position marker))
  21136. (org-insert-labeled-timestamps-at-point nil)
  21137. ts)
  21138. (org-with-remote-undo buffer
  21139. (with-current-buffer buffer
  21140. (widen)
  21141. (goto-char pos)
  21142. (setq ts (org-deadline arg)))
  21143. (org-agenda-show-new-time marker ts "S"))
  21144. (message "Deadline for this item set to %s" ts)))
  21145. (defun org-get-heading (&optional no-tags)
  21146. "Return the heading of the current entry, without the stars."
  21147. (save-excursion
  21148. (org-back-to-heading t)
  21149. (if (looking-at
  21150. (if no-tags
  21151. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21152. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21153. (match-string 1) "")))
  21154. (defun org-agenda-clock-in (&optional arg)
  21155. "Start the clock on the currently selected item."
  21156. (interactive "P")
  21157. (org-agenda-check-no-diary)
  21158. (let* ((marker (or (get-text-property (point) 'org-marker)
  21159. (org-agenda-error)))
  21160. (pos (marker-position marker)))
  21161. (org-with-remote-undo (marker-buffer marker)
  21162. (with-current-buffer (marker-buffer marker)
  21163. (widen)
  21164. (goto-char pos)
  21165. (org-clock-in)))))
  21166. (defun org-agenda-clock-out (&optional arg)
  21167. "Stop the currently running clock."
  21168. (interactive "P")
  21169. (unless (marker-buffer org-clock-marker)
  21170. (error "No running clock"))
  21171. (org-with-remote-undo (marker-buffer org-clock-marker)
  21172. (org-clock-out)))
  21173. (defun org-agenda-clock-cancel (&optional arg)
  21174. "Cancel the currently running clock."
  21175. (interactive "P")
  21176. (unless (marker-buffer org-clock-marker)
  21177. (error "No running clock"))
  21178. (org-with-remote-undo (marker-buffer org-clock-marker)
  21179. (org-clock-cancel)))
  21180. (defun org-agenda-diary-entry ()
  21181. "Make a diary entry, like the `i' command from the calendar.
  21182. All the standard commands work: block, weekly etc."
  21183. (interactive)
  21184. (org-agenda-check-type t 'agenda 'timeline)
  21185. (require 'diary-lib)
  21186. (let* ((char (progn
  21187. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21188. (read-char-exclusive)))
  21189. (cmd (cdr (assoc char
  21190. '((?d . insert-diary-entry)
  21191. (?w . insert-weekly-diary-entry)
  21192. (?m . insert-monthly-diary-entry)
  21193. (?y . insert-yearly-diary-entry)
  21194. (?a . insert-anniversary-diary-entry)
  21195. (?b . insert-block-diary-entry)
  21196. (?c . insert-cyclic-diary-entry)))))
  21197. (oldf (symbol-function 'calendar-cursor-to-date))
  21198. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21199. (point (point))
  21200. (mark (or (mark t) (point))))
  21201. (unless cmd
  21202. (error "No command associated with <%c>" char))
  21203. (unless (and (get-text-property point 'day)
  21204. (or (not (equal ?b char))
  21205. (get-text-property mark 'day)))
  21206. (error "Don't know which date to use for diary entry"))
  21207. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21208. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21209. (let ((calendar-mark-ring
  21210. (list (calendar-gregorian-from-absolute
  21211. (or (get-text-property mark 'day)
  21212. (get-text-property point 'day))))))
  21213. (unwind-protect
  21214. (progn
  21215. (fset 'calendar-cursor-to-date
  21216. (lambda (&optional error)
  21217. (calendar-gregorian-from-absolute
  21218. (get-text-property point 'day))))
  21219. (call-interactively cmd))
  21220. (fset 'calendar-cursor-to-date oldf)))))
  21221. (defun org-agenda-execute-calendar-command (cmd)
  21222. "Execute a calendar command from the agenda, with the date associated to
  21223. the cursor position."
  21224. (org-agenda-check-type t 'agenda 'timeline)
  21225. (require 'diary-lib)
  21226. (unless (get-text-property (point) 'day)
  21227. (error "Don't know which date to use for calendar command"))
  21228. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21229. (point (point))
  21230. (date (calendar-gregorian-from-absolute
  21231. (get-text-property point 'day)))
  21232. ;; the following 3 vars are needed in the calendar
  21233. (displayed-day (extract-calendar-day date))
  21234. (displayed-month (extract-calendar-month date))
  21235. (displayed-year (extract-calendar-year date)))
  21236. (unwind-protect
  21237. (progn
  21238. (fset 'calendar-cursor-to-date
  21239. (lambda (&optional error)
  21240. (calendar-gregorian-from-absolute
  21241. (get-text-property point 'day))))
  21242. (call-interactively cmd))
  21243. (fset 'calendar-cursor-to-date oldf))))
  21244. (defun org-agenda-phases-of-moon ()
  21245. "Display the phases of the moon for the 3 months around the cursor date."
  21246. (interactive)
  21247. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21248. (defun org-agenda-holidays ()
  21249. "Display the holidays for the 3 months around the cursor date."
  21250. (interactive)
  21251. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21252. (defun org-agenda-sunrise-sunset (arg)
  21253. "Display sunrise and sunset for the cursor date.
  21254. Latitude and longitude can be specified with the variables
  21255. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21256. argument, latitude and longitude will be prompted for."
  21257. (interactive "P")
  21258. (let ((calendar-longitude (if arg nil calendar-longitude))
  21259. (calendar-latitude (if arg nil calendar-latitude))
  21260. (calendar-location-name
  21261. (if arg "the given coordinates" calendar-location-name)))
  21262. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21263. (defun org-agenda-goto-calendar ()
  21264. "Open the Emacs calendar with the date at the cursor."
  21265. (interactive)
  21266. (org-agenda-check-type t 'agenda 'timeline)
  21267. (let* ((day (or (get-text-property (point) 'day)
  21268. (error "Don't know which date to open in calendar")))
  21269. (date (calendar-gregorian-from-absolute day))
  21270. (calendar-move-hook nil)
  21271. (view-calendar-holidays-initially nil)
  21272. (view-diary-entries-initially nil))
  21273. (calendar)
  21274. (calendar-goto-date date)))
  21275. (defun org-calendar-goto-agenda ()
  21276. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21277. This is a command that has to be installed in `calendar-mode-map'."
  21278. (interactive)
  21279. (org-agenda-list nil (calendar-absolute-from-gregorian
  21280. (calendar-cursor-to-date))
  21281. nil))
  21282. (defun org-agenda-convert-date ()
  21283. (interactive)
  21284. (org-agenda-check-type t 'agenda 'timeline)
  21285. (let ((day (get-text-property (point) 'day))
  21286. date s)
  21287. (unless day
  21288. (error "Don't know which date to convert"))
  21289. (setq date (calendar-gregorian-from-absolute day))
  21290. (setq s (concat
  21291. "Gregorian: " (calendar-date-string date) "\n"
  21292. "ISO: " (calendar-iso-date-string date) "\n"
  21293. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21294. "Julian: " (calendar-julian-date-string date) "\n"
  21295. "Astron. JD: " (calendar-astro-date-string date)
  21296. " (Julian date number at noon UTC)\n"
  21297. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21298. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21299. "French: " (calendar-french-date-string date) "\n"
  21300. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21301. "Mayan: " (calendar-mayan-date-string date) "\n"
  21302. "Coptic: " (calendar-coptic-date-string date) "\n"
  21303. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21304. "Persian: " (calendar-persian-date-string date) "\n"
  21305. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21306. (with-output-to-temp-buffer "*Dates*"
  21307. (princ s))
  21308. (if (fboundp 'fit-window-to-buffer)
  21309. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21310. ;;;; Embedded LaTeX
  21311. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21312. "Keymap for the minor `org-cdlatex-mode'.")
  21313. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21314. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21315. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21316. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21317. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21318. (defvar org-cdlatex-texmathp-advice-is-done nil
  21319. "Flag remembering if we have applied the advice to texmathp already.")
  21320. (define-minor-mode org-cdlatex-mode
  21321. "Toggle the minor `org-cdlatex-mode'.
  21322. This mode supports entering LaTeX environment and math in LaTeX fragments
  21323. in Org-mode.
  21324. \\{org-cdlatex-mode-map}"
  21325. nil " OCDL" nil
  21326. (when org-cdlatex-mode (require 'cdlatex))
  21327. (unless org-cdlatex-texmathp-advice-is-done
  21328. (setq org-cdlatex-texmathp-advice-is-done t)
  21329. (defadvice texmathp (around org-math-always-on activate)
  21330. "Always return t in org-mode buffers.
  21331. This is because we want to insert math symbols without dollars even outside
  21332. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21333. en embedded LaTeX fragement, let texmathp do its job.
  21334. \\[org-cdlatex-mode-map]"
  21335. (interactive)
  21336. (let (p)
  21337. (cond
  21338. ((not (org-mode-p)) ad-do-it)
  21339. ((eq this-command 'cdlatex-math-symbol)
  21340. (setq ad-return-value t
  21341. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21342. (t
  21343. (let ((p (org-inside-LaTeX-fragment-p)))
  21344. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21345. (setq ad-return-value t
  21346. texmathp-why '("Org-mode embedded math" . 0))
  21347. (if p ad-do-it)))))))))
  21348. (defun turn-on-org-cdlatex ()
  21349. "Unconditionally turn on `org-cdlatex-mode'."
  21350. (org-cdlatex-mode 1))
  21351. (defun org-inside-LaTeX-fragment-p ()
  21352. "Test if point is inside a LaTeX fragment.
  21353. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21354. sequence appearing also before point.
  21355. Even though the matchers for math are configurable, this function assumes
  21356. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21357. delimiters are skipped when they have been removed by customization.
  21358. The return value is nil, or a cons cell with the delimiter and
  21359. and the position of this delimiter.
  21360. This function does a reasonably good job, but can locally be fooled by
  21361. for example currency specifications. For example it will assume being in
  21362. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21363. fragments that are properly closed, but during editing, we have to live
  21364. with the uncertainty caused by missing closing delimiters. This function
  21365. looks only before point, not after."
  21366. (catch 'exit
  21367. (let ((pos (point))
  21368. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21369. (lim (progn
  21370. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21371. (point)))
  21372. dd-on str (start 0) m re)
  21373. (goto-char pos)
  21374. (when dodollar
  21375. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21376. re (nth 1 (assoc "$" org-latex-regexps)))
  21377. (while (string-match re str start)
  21378. (cond
  21379. ((= (match-end 0) (length str))
  21380. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21381. ((= (match-end 0) (- (length str) 5))
  21382. (throw 'exit nil))
  21383. (t (setq start (match-end 0))))))
  21384. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21385. (goto-char pos)
  21386. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21387. (and (match-beginning 2) (throw 'exit nil))
  21388. ;; count $$
  21389. (while (re-search-backward "\\$\\$" lim t)
  21390. (setq dd-on (not dd-on)))
  21391. (goto-char pos)
  21392. (if dd-on (cons "$$" m))))))
  21393. (defun org-try-cdlatex-tab ()
  21394. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21395. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21396. - inside a LaTeX fragment, or
  21397. - after the first word in a line, where an abbreviation expansion could
  21398. insert a LaTeX environment."
  21399. (when org-cdlatex-mode
  21400. (cond
  21401. ((save-excursion
  21402. (skip-chars-backward "a-zA-Z0-9*")
  21403. (skip-chars-backward " \t")
  21404. (bolp))
  21405. (cdlatex-tab) t)
  21406. ((org-inside-LaTeX-fragment-p)
  21407. (cdlatex-tab) t)
  21408. (t nil))))
  21409. (defun org-cdlatex-underscore-caret (&optional arg)
  21410. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21411. Revert to the normal definition outside of these fragments."
  21412. (interactive "P")
  21413. (if (org-inside-LaTeX-fragment-p)
  21414. (call-interactively 'cdlatex-sub-superscript)
  21415. (let (org-cdlatex-mode)
  21416. (call-interactively (key-binding (vector last-input-event))))))
  21417. (defun org-cdlatex-math-modify (&optional arg)
  21418. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21419. Revert to the normal definition outside of these fragments."
  21420. (interactive "P")
  21421. (if (org-inside-LaTeX-fragment-p)
  21422. (call-interactively 'cdlatex-math-modify)
  21423. (let (org-cdlatex-mode)
  21424. (call-interactively (key-binding (vector last-input-event))))))
  21425. (defvar org-latex-fragment-image-overlays nil
  21426. "List of overlays carrying the images of latex fragments.")
  21427. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21428. (defun org-remove-latex-fragment-image-overlays ()
  21429. "Remove all overlays with LaTeX fragment images in current buffer."
  21430. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21431. (setq org-latex-fragment-image-overlays nil))
  21432. (defun org-preview-latex-fragment (&optional subtree)
  21433. "Preview the LaTeX fragment at point, or all locally or globally.
  21434. If the cursor is in a LaTeX fragment, create the image and overlay
  21435. it over the source code. If there is no fragment at point, display
  21436. all fragments in the current text, from one headline to the next. With
  21437. prefix SUBTREE, display all fragments in the current subtree. With a
  21438. double prefix `C-u C-u', or when the cursor is before the first headline,
  21439. display all fragments in the buffer.
  21440. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21441. (interactive "P")
  21442. (org-remove-latex-fragment-image-overlays)
  21443. (save-excursion
  21444. (save-restriction
  21445. (let (beg end at msg)
  21446. (cond
  21447. ((or (equal subtree '(16))
  21448. (not (save-excursion
  21449. (re-search-backward (concat "^" outline-regexp) nil t))))
  21450. (setq beg (point-min) end (point-max)
  21451. msg "Creating images for buffer...%s"))
  21452. ((equal subtree '(4))
  21453. (org-back-to-heading)
  21454. (setq beg (point) end (org-end-of-subtree t)
  21455. msg "Creating images for subtree...%s"))
  21456. (t
  21457. (if (setq at (org-inside-LaTeX-fragment-p))
  21458. (goto-char (max (point-min) (- (cdr at) 2)))
  21459. (org-back-to-heading))
  21460. (setq beg (point) end (progn (outline-next-heading) (point))
  21461. msg (if at "Creating image...%s"
  21462. "Creating images for entry...%s"))))
  21463. (message msg "")
  21464. (narrow-to-region beg end)
  21465. (goto-char beg)
  21466. (org-format-latex
  21467. (concat "ltxpng/" (file-name-sans-extension
  21468. (file-name-nondirectory
  21469. buffer-file-name)))
  21470. default-directory 'overlays msg at 'forbuffer)
  21471. (message msg "done. Use `C-c C-c' to remove images.")))))
  21472. (defvar org-latex-regexps
  21473. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21474. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21475. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21476. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21477. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21478. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21479. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21480. "Regular expressions for matching embedded LaTeX.")
  21481. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21482. "Replace LaTeX fragments with links to an image, and produce images."
  21483. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21484. (let* ((prefixnodir (file-name-nondirectory prefix))
  21485. (absprefix (expand-file-name prefix dir))
  21486. (todir (file-name-directory absprefix))
  21487. (opt org-format-latex-options)
  21488. (matchers (plist-get opt :matchers))
  21489. (re-list org-latex-regexps)
  21490. (cnt 0) txt link beg end re e checkdir
  21491. m n block linkfile movefile ov)
  21492. ;; Check if there are old images files with this prefix, and remove them
  21493. (when (file-directory-p todir)
  21494. (mapc 'delete-file
  21495. (directory-files
  21496. todir 'full
  21497. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21498. ;; Check the different regular expressions
  21499. (while (setq e (pop re-list))
  21500. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21501. block (if (nth 3 e) "\n\n" ""))
  21502. (when (member m matchers)
  21503. (goto-char (point-min))
  21504. (while (re-search-forward re nil t)
  21505. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21506. (setq txt (match-string n)
  21507. beg (match-beginning n) end (match-end n)
  21508. cnt (1+ cnt)
  21509. linkfile (format "%s_%04d.png" prefix cnt)
  21510. movefile (format "%s_%04d.png" absprefix cnt)
  21511. link (concat block "[[file:" linkfile "]]" block))
  21512. (if msg (message msg cnt))
  21513. (goto-char beg)
  21514. (unless checkdir ; make sure the directory exists
  21515. (setq checkdir t)
  21516. (or (file-directory-p todir) (make-directory todir)))
  21517. (org-create-formula-image
  21518. txt movefile opt forbuffer)
  21519. (if overlays
  21520. (progn
  21521. (setq ov (org-make-overlay beg end))
  21522. (if (featurep 'xemacs)
  21523. (progn
  21524. (org-overlay-put ov 'invisible t)
  21525. (org-overlay-put
  21526. ov 'end-glyph
  21527. (make-glyph (vector 'png :file movefile))))
  21528. (org-overlay-put
  21529. ov 'display
  21530. (list 'image :type 'png :file movefile :ascent 'center)))
  21531. (push ov org-latex-fragment-image-overlays)
  21532. (goto-char end))
  21533. (delete-region beg end)
  21534. (insert link))))))))
  21535. ;; This function borrows from Ganesh Swami's latex2png.el
  21536. (defun org-create-formula-image (string tofile options buffer)
  21537. (let* ((tmpdir (if (featurep 'xemacs)
  21538. (temp-directory)
  21539. temporary-file-directory))
  21540. (texfilebase (make-temp-name
  21541. (expand-file-name "orgtex" tmpdir)))
  21542. (texfile (concat texfilebase ".tex"))
  21543. (dvifile (concat texfilebase ".dvi"))
  21544. (pngfile (concat texfilebase ".png"))
  21545. (fnh (face-attribute 'default :height nil))
  21546. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21547. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21548. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21549. "Black"))
  21550. (bg (or (plist-get options (if buffer :background :html-background))
  21551. "Transparent")))
  21552. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21553. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21554. (with-temp-file texfile
  21555. (insert org-format-latex-header
  21556. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21557. (let ((dir default-directory))
  21558. (condition-case nil
  21559. (progn
  21560. (cd tmpdir)
  21561. (call-process "latex" nil nil nil texfile))
  21562. (error nil))
  21563. (cd dir))
  21564. (if (not (file-exists-p dvifile))
  21565. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21566. (call-process "dvipng" nil nil nil
  21567. "-E" "-fg" fg "-bg" bg
  21568. "-D" dpi
  21569. ;;"-x" scale "-y" scale
  21570. "-T" "tight"
  21571. "-o" pngfile
  21572. dvifile)
  21573. (if (not (file-exists-p pngfile))
  21574. (progn (message "Failed to create png file from %s" texfile) nil)
  21575. ;; Use the requested file name and clean up
  21576. (copy-file pngfile tofile 'replace)
  21577. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21578. (delete-file (concat texfilebase e)))
  21579. pngfile))))
  21580. (defun org-dvipng-color (attr)
  21581. "Return an rgb color specification for dvipng."
  21582. (apply 'format "rgb %s %s %s"
  21583. (mapcar 'org-normalize-color
  21584. (color-values (face-attribute 'default attr nil)))))
  21585. (defun org-normalize-color (value)
  21586. "Return string to be used as color value for an RGB component."
  21587. (format "%g" (/ value 65535.0)))
  21588. ;;;; Exporting
  21589. ;;; Variables, constants, and parameter plists
  21590. (defconst org-level-max 20)
  21591. (defvar org-export-html-preamble nil
  21592. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21593. (defvar org-export-html-postamble nil
  21594. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21595. (defvar org-export-html-auto-preamble t
  21596. "Should default preamble be inserted? Set by publishing functions.")
  21597. (defvar org-export-html-auto-postamble t
  21598. "Should default postamble be inserted? Set by publishing functions.")
  21599. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21600. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21601. (defconst org-export-plist-vars
  21602. '((:language . org-export-default-language)
  21603. (:customtime . org-display-custom-times)
  21604. (:headline-levels . org-export-headline-levels)
  21605. (:section-numbers . org-export-with-section-numbers)
  21606. (:table-of-contents . org-export-with-toc)
  21607. (:preserve-breaks . org-export-preserve-breaks)
  21608. (:archived-trees . org-export-with-archived-trees)
  21609. (:emphasize . org-export-with-emphasize)
  21610. (:sub-superscript . org-export-with-sub-superscripts)
  21611. (:special-strings . org-export-with-special-strings)
  21612. (:footnotes . org-export-with-footnotes)
  21613. (:drawers . org-export-with-drawers)
  21614. (:tags . org-export-with-tags)
  21615. (:TeX-macros . org-export-with-TeX-macros)
  21616. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21617. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21618. (:fixed-width . org-export-with-fixed-width)
  21619. (:timestamps . org-export-with-timestamps)
  21620. (:author-info . org-export-author-info)
  21621. (:time-stamp-file . org-export-time-stamp-file)
  21622. (:tables . org-export-with-tables)
  21623. (:table-auto-headline . org-export-highlight-first-table-line)
  21624. (:style . org-export-html-style)
  21625. (:agenda-style . org-agenda-export-html-style)
  21626. (:convert-org-links . org-export-html-link-org-files-as-html)
  21627. (:inline-images . org-export-html-inline-images)
  21628. (:html-extension . org-export-html-extension)
  21629. (:html-table-tag . org-export-html-table-tag)
  21630. (:expand-quoted-html . org-export-html-expand)
  21631. (:timestamp . org-export-html-with-timestamp)
  21632. (:publishing-directory . org-export-publishing-directory)
  21633. (:preamble . org-export-html-preamble)
  21634. (:postamble . org-export-html-postamble)
  21635. (:auto-preamble . org-export-html-auto-preamble)
  21636. (:auto-postamble . org-export-html-auto-postamble)
  21637. (:author . user-full-name)
  21638. (:email . user-mail-address)))
  21639. (defun org-default-export-plist ()
  21640. "Return the property list with default settings for the export variables."
  21641. (let ((l org-export-plist-vars) rtn e)
  21642. (while (setq e (pop l))
  21643. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21644. rtn))
  21645. (defun org-infile-export-plist ()
  21646. "Return the property list with file-local settings for export."
  21647. (save-excursion
  21648. (save-restriction
  21649. (widen)
  21650. (goto-char 0)
  21651. (let ((re (org-make-options-regexp
  21652. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21653. p key val text options)
  21654. (while (re-search-forward re nil t)
  21655. (setq key (org-match-string-no-properties 1)
  21656. val (org-match-string-no-properties 2))
  21657. (cond
  21658. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21659. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21660. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21661. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21662. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21663. ((string-equal key "TEXT")
  21664. (setq text (if text (concat text "\n" val) val)))
  21665. ((string-equal key "OPTIONS") (setq options val))))
  21666. (setq p (plist-put p :text text))
  21667. (when options
  21668. (let ((op '(("H" . :headline-levels)
  21669. ("num" . :section-numbers)
  21670. ("toc" . :table-of-contents)
  21671. ("\\n" . :preserve-breaks)
  21672. ("@" . :expand-quoted-html)
  21673. (":" . :fixed-width)
  21674. ("|" . :tables)
  21675. ("^" . :sub-superscript)
  21676. ("-" . :special-strings)
  21677. ("f" . :footnotes)
  21678. ("d" . :drawers)
  21679. ("tags" . :tags)
  21680. ("*" . :emphasize)
  21681. ("TeX" . :TeX-macros)
  21682. ("LaTeX" . :LaTeX-fragments)
  21683. ("skip" . :skip-before-1st-heading)
  21684. ("author" . :author-info)
  21685. ("timestamp" . :time-stamp-file)))
  21686. o)
  21687. (while (setq o (pop op))
  21688. (if (string-match (concat (regexp-quote (car o))
  21689. ":\\([^ \t\n\r;,.]*\\)")
  21690. options)
  21691. (setq p (plist-put p (cdr o)
  21692. (car (read-from-string
  21693. (match-string 1 options)))))))))
  21694. p))))
  21695. (defun org-export-directory (type plist)
  21696. (let* ((val (plist-get plist :publishing-directory))
  21697. (dir (if (listp val)
  21698. (or (cdr (assoc type val)) ".")
  21699. val)))
  21700. dir))
  21701. (defun org-skip-comments (lines)
  21702. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21703. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21704. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21705. (case-fold-search nil)
  21706. rtn line level)
  21707. (while (setq line (pop lines))
  21708. (cond
  21709. ((and (string-match re1 line)
  21710. (setq level (- (match-end 1) (match-beginning 1))))
  21711. ;; Beginning of a COMMENT subtree. Skip it.
  21712. (while (and (setq line (pop lines))
  21713. (or (not (string-match re2 line))
  21714. (> (- (match-end 1) (match-beginning 1)) level))))
  21715. (setq lines (cons line lines)))
  21716. ((string-match "^#" line)
  21717. ;; an ordinary comment line
  21718. )
  21719. ((and org-export-table-remove-special-lines
  21720. (string-match "^[ \t]*|" line)
  21721. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21722. (and (string-match "| *<[0-9]+> *|" line)
  21723. (not (string-match "| *[^ <|]" line)))))
  21724. ;; a special table line that should be removed
  21725. )
  21726. (t (setq rtn (cons line rtn)))))
  21727. (nreverse rtn)))
  21728. (defun org-export (&optional arg)
  21729. (interactive)
  21730. (let ((help "[t] insert the export option template
  21731. \[v] limit export to visible part of outline tree
  21732. \[a] export as ASCII
  21733. \[h] export as HTML
  21734. \[H] export as HTML to temporary buffer
  21735. \[R] export region as HTML
  21736. \[b] export as HTML and browse immediately
  21737. \[x] export as XOXO
  21738. \[l] export as LaTeX
  21739. \[L] export as LaTeX to temporary buffer
  21740. \[i] export current file as iCalendar file
  21741. \[I] export all agenda files as iCalendar files
  21742. \[c] export agenda files into combined iCalendar file
  21743. \[F] publish current file
  21744. \[P] publish current project
  21745. \[X] publish... (project will be prompted for)
  21746. \[A] publish all projects")
  21747. (cmds
  21748. '((?t . org-insert-export-options-template)
  21749. (?v . org-export-visible)
  21750. (?a . org-export-as-ascii)
  21751. (?h . org-export-as-html)
  21752. (?b . org-export-as-html-and-open)
  21753. (?H . org-export-as-html-to-buffer)
  21754. (?R . org-export-region-as-html)
  21755. (?x . org-export-as-xoxo)
  21756. (?l . org-export-as-latex)
  21757. (?L . org-export-as-latex-to-buffer)
  21758. (?i . org-export-icalendar-this-file)
  21759. (?I . org-export-icalendar-all-agenda-files)
  21760. (?c . org-export-icalendar-combine-agenda-files)
  21761. (?F . org-publish-current-file)
  21762. (?P . org-publish-current-project)
  21763. (?X . org-publish)
  21764. (?A . org-publish-all)))
  21765. r1 r2 ass)
  21766. (save-window-excursion
  21767. (delete-other-windows)
  21768. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21769. (princ help))
  21770. (message "Select command: ")
  21771. (setq r1 (read-char-exclusive)))
  21772. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21773. (if (setq ass (assq r2 cmds))
  21774. (call-interactively (cdr ass))
  21775. (error "No command associated with key %c" r1))))
  21776. (defconst org-html-entities
  21777. '(("nbsp")
  21778. ("iexcl")
  21779. ("cent")
  21780. ("pound")
  21781. ("curren")
  21782. ("yen")
  21783. ("brvbar")
  21784. ("vert" . "&#124;")
  21785. ("sect")
  21786. ("uml")
  21787. ("copy")
  21788. ("ordf")
  21789. ("laquo")
  21790. ("not")
  21791. ("shy")
  21792. ("reg")
  21793. ("macr")
  21794. ("deg")
  21795. ("plusmn")
  21796. ("sup2")
  21797. ("sup3")
  21798. ("acute")
  21799. ("micro")
  21800. ("para")
  21801. ("middot")
  21802. ("odot"."o")
  21803. ("star"."*")
  21804. ("cedil")
  21805. ("sup1")
  21806. ("ordm")
  21807. ("raquo")
  21808. ("frac14")
  21809. ("frac12")
  21810. ("frac34")
  21811. ("iquest")
  21812. ("Agrave")
  21813. ("Aacute")
  21814. ("Acirc")
  21815. ("Atilde")
  21816. ("Auml")
  21817. ("Aring") ("AA"."&Aring;")
  21818. ("AElig")
  21819. ("Ccedil")
  21820. ("Egrave")
  21821. ("Eacute")
  21822. ("Ecirc")
  21823. ("Euml")
  21824. ("Igrave")
  21825. ("Iacute")
  21826. ("Icirc")
  21827. ("Iuml")
  21828. ("ETH")
  21829. ("Ntilde")
  21830. ("Ograve")
  21831. ("Oacute")
  21832. ("Ocirc")
  21833. ("Otilde")
  21834. ("Ouml")
  21835. ("times")
  21836. ("Oslash")
  21837. ("Ugrave")
  21838. ("Uacute")
  21839. ("Ucirc")
  21840. ("Uuml")
  21841. ("Yacute")
  21842. ("THORN")
  21843. ("szlig")
  21844. ("agrave")
  21845. ("aacute")
  21846. ("acirc")
  21847. ("atilde")
  21848. ("auml")
  21849. ("aring")
  21850. ("aelig")
  21851. ("ccedil")
  21852. ("egrave")
  21853. ("eacute")
  21854. ("ecirc")
  21855. ("euml")
  21856. ("igrave")
  21857. ("iacute")
  21858. ("icirc")
  21859. ("iuml")
  21860. ("eth")
  21861. ("ntilde")
  21862. ("ograve")
  21863. ("oacute")
  21864. ("ocirc")
  21865. ("otilde")
  21866. ("ouml")
  21867. ("divide")
  21868. ("oslash")
  21869. ("ugrave")
  21870. ("uacute")
  21871. ("ucirc")
  21872. ("uuml")
  21873. ("yacute")
  21874. ("thorn")
  21875. ("yuml")
  21876. ("fnof")
  21877. ("Alpha")
  21878. ("Beta")
  21879. ("Gamma")
  21880. ("Delta")
  21881. ("Epsilon")
  21882. ("Zeta")
  21883. ("Eta")
  21884. ("Theta")
  21885. ("Iota")
  21886. ("Kappa")
  21887. ("Lambda")
  21888. ("Mu")
  21889. ("Nu")
  21890. ("Xi")
  21891. ("Omicron")
  21892. ("Pi")
  21893. ("Rho")
  21894. ("Sigma")
  21895. ("Tau")
  21896. ("Upsilon")
  21897. ("Phi")
  21898. ("Chi")
  21899. ("Psi")
  21900. ("Omega")
  21901. ("alpha")
  21902. ("beta")
  21903. ("gamma")
  21904. ("delta")
  21905. ("epsilon")
  21906. ("varepsilon"."&epsilon;")
  21907. ("zeta")
  21908. ("eta")
  21909. ("theta")
  21910. ("iota")
  21911. ("kappa")
  21912. ("lambda")
  21913. ("mu")
  21914. ("nu")
  21915. ("xi")
  21916. ("omicron")
  21917. ("pi")
  21918. ("rho")
  21919. ("sigmaf") ("varsigma"."&sigmaf;")
  21920. ("sigma")
  21921. ("tau")
  21922. ("upsilon")
  21923. ("phi")
  21924. ("chi")
  21925. ("psi")
  21926. ("omega")
  21927. ("thetasym") ("vartheta"."&thetasym;")
  21928. ("upsih")
  21929. ("piv")
  21930. ("bull") ("bullet"."&bull;")
  21931. ("hellip") ("dots"."&hellip;")
  21932. ("prime")
  21933. ("Prime")
  21934. ("oline")
  21935. ("frasl")
  21936. ("weierp")
  21937. ("image")
  21938. ("real")
  21939. ("trade")
  21940. ("alefsym")
  21941. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  21942. ("uarr") ("uparrow"."&uarr;")
  21943. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  21944. ("darr")("downarrow"."&darr;")
  21945. ("harr") ("leftrightarrow"."&harr;")
  21946. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  21947. ("lArr") ("Leftarrow"."&lArr;")
  21948. ("uArr") ("Uparrow"."&uArr;")
  21949. ("rArr") ("Rightarrow"."&rArr;")
  21950. ("dArr") ("Downarrow"."&dArr;")
  21951. ("hArr") ("Leftrightarrow"."&hArr;")
  21952. ("forall")
  21953. ("part") ("partial"."&part;")
  21954. ("exist") ("exists"."&exist;")
  21955. ("empty") ("emptyset"."&empty;")
  21956. ("nabla")
  21957. ("isin") ("in"."&isin;")
  21958. ("notin")
  21959. ("ni")
  21960. ("prod")
  21961. ("sum")
  21962. ("minus")
  21963. ("lowast") ("ast"."&lowast;")
  21964. ("radic")
  21965. ("prop") ("proptp"."&prop;")
  21966. ("infin") ("infty"."&infin;")
  21967. ("ang") ("angle"."&ang;")
  21968. ("and") ("wedge"."&and;")
  21969. ("or") ("vee"."&or;")
  21970. ("cap")
  21971. ("cup")
  21972. ("int")
  21973. ("there4")
  21974. ("sim")
  21975. ("cong") ("simeq"."&cong;")
  21976. ("asymp")("approx"."&asymp;")
  21977. ("ne") ("neq"."&ne;")
  21978. ("equiv")
  21979. ("le")
  21980. ("ge")
  21981. ("sub") ("subset"."&sub;")
  21982. ("sup") ("supset"."&sup;")
  21983. ("nsub")
  21984. ("sube")
  21985. ("supe")
  21986. ("oplus")
  21987. ("otimes")
  21988. ("perp")
  21989. ("sdot") ("cdot"."&sdot;")
  21990. ("lceil")
  21991. ("rceil")
  21992. ("lfloor")
  21993. ("rfloor")
  21994. ("lang")
  21995. ("rang")
  21996. ("loz") ("Diamond"."&loz;")
  21997. ("spades") ("spadesuit"."&spades;")
  21998. ("clubs") ("clubsuit"."&clubs;")
  21999. ("hearts") ("diamondsuit"."&hearts;")
  22000. ("diams") ("diamondsuit"."&diams;")
  22001. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22002. ("quot")
  22003. ("amp")
  22004. ("lt")
  22005. ("gt")
  22006. ("OElig")
  22007. ("oelig")
  22008. ("Scaron")
  22009. ("scaron")
  22010. ("Yuml")
  22011. ("circ")
  22012. ("tilde")
  22013. ("ensp")
  22014. ("emsp")
  22015. ("thinsp")
  22016. ("zwnj")
  22017. ("zwj")
  22018. ("lrm")
  22019. ("rlm")
  22020. ("ndash")
  22021. ("mdash")
  22022. ("lsquo")
  22023. ("rsquo")
  22024. ("sbquo")
  22025. ("ldquo")
  22026. ("rdquo")
  22027. ("bdquo")
  22028. ("dagger")
  22029. ("Dagger")
  22030. ("permil")
  22031. ("lsaquo")
  22032. ("rsaquo")
  22033. ("euro")
  22034. ("arccos"."arccos")
  22035. ("arcsin"."arcsin")
  22036. ("arctan"."arctan")
  22037. ("arg"."arg")
  22038. ("cos"."cos")
  22039. ("cosh"."cosh")
  22040. ("cot"."cot")
  22041. ("coth"."coth")
  22042. ("csc"."csc")
  22043. ("deg"."deg")
  22044. ("det"."det")
  22045. ("dim"."dim")
  22046. ("exp"."exp")
  22047. ("gcd"."gcd")
  22048. ("hom"."hom")
  22049. ("inf"."inf")
  22050. ("ker"."ker")
  22051. ("lg"."lg")
  22052. ("lim"."lim")
  22053. ("liminf"."liminf")
  22054. ("limsup"."limsup")
  22055. ("ln"."ln")
  22056. ("log"."log")
  22057. ("max"."max")
  22058. ("min"."min")
  22059. ("Pr"."Pr")
  22060. ("sec"."sec")
  22061. ("sin"."sin")
  22062. ("sinh"."sinh")
  22063. ("sup"."sup")
  22064. ("tan"."tan")
  22065. ("tanh"."tanh")
  22066. )
  22067. "Entities for TeX->HTML translation.
  22068. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22069. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22070. In that case, \"\\ent\" will be translated to \"&other;\".
  22071. The list contains HTML entities for Latin-1, Greek and other symbols.
  22072. It is supplemented by a number of commonly used TeX macros with appropriate
  22073. translations. There is currently no way for users to extend this.")
  22074. ;;; General functions for all backends
  22075. (defun org-cleaned-string-for-export (string &rest parameters)
  22076. "Cleanup a buffer STRING so that links can be created safely."
  22077. (interactive)
  22078. (let* ((re-radio (and org-target-link-regexp
  22079. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22080. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22081. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22082. (re-archive (concat ":" org-archive-tag ":"))
  22083. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22084. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22085. (htmlp (plist-get parameters :for-html))
  22086. (asciip (plist-get parameters :for-ascii))
  22087. (latexp (plist-get parameters :for-LaTeX))
  22088. (commentsp (plist-get parameters :comments))
  22089. (archived-trees (plist-get parameters :archived-trees))
  22090. (inhibit-read-only t)
  22091. (drawers org-drawers)
  22092. (exp-drawers (plist-get parameters :drawers))
  22093. (outline-regexp "\\*+ ")
  22094. a b xx
  22095. rtn p)
  22096. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22097. (erase-buffer)
  22098. (insert string)
  22099. ;; Remove license-to-kill stuff
  22100. (while (setq p (text-property-any (point-min) (point-max)
  22101. :org-license-to-kill t))
  22102. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22103. (let ((org-inhibit-startup t)) (org-mode))
  22104. (untabify (point-min) (point-max))
  22105. ;; Get rid of drawers
  22106. (unless (eq t exp-drawers)
  22107. (goto-char (point-min))
  22108. (let ((re (concat "^[ \t]*:\\("
  22109. (mapconcat
  22110. 'identity
  22111. (org-delete-all exp-drawers
  22112. (copy-sequence drawers))
  22113. "\\|")
  22114. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22115. (while (re-search-forward re nil t)
  22116. (replace-match ""))))
  22117. ;; Get the correct stuff before the first headline
  22118. (when (plist-get parameters :skip-before-1st-heading)
  22119. (goto-char (point-min))
  22120. (when (re-search-forward "^\\*+[ \t]" nil t)
  22121. (delete-region (point-min) (match-beginning 0))
  22122. (goto-char (point-min))
  22123. (insert "\n")))
  22124. (when (plist-get parameters :add-text)
  22125. (goto-char (point-min))
  22126. (insert (plist-get parameters :add-text) "\n"))
  22127. ;; Get rid of archived trees
  22128. (when (not (eq archived-trees t))
  22129. (goto-char (point-min))
  22130. (while (re-search-forward re-archive nil t)
  22131. (if (not (org-on-heading-p t))
  22132. (org-end-of-subtree t)
  22133. (beginning-of-line 1)
  22134. (setq a (if archived-trees
  22135. (1+ (point-at-eol)) (point))
  22136. b (org-end-of-subtree t))
  22137. (if (> b a) (delete-region a b)))))
  22138. ;; Find targets in comments and move them out of comments,
  22139. ;; but mark them as targets that should be invisible
  22140. (goto-char (point-min))
  22141. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22142. (replace-match "\\1(INVISIBLE)"))
  22143. ;; Protect backend specific stuff, throw away the others.
  22144. (let ((formatters
  22145. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22146. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22147. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22148. fmt)
  22149. (goto-char (point-min))
  22150. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22151. (goto-char (match-end 0))
  22152. (while (not (looking-at "#\\+END_EXAMPLE"))
  22153. (insert ": ")
  22154. (beginning-of-line 2)))
  22155. (goto-char (point-min))
  22156. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22157. (add-text-properties (match-beginning 0) (match-end 0)
  22158. '(org-protected t)))
  22159. (while formatters
  22160. (setq fmt (pop formatters))
  22161. (when (car fmt)
  22162. (goto-char (point-min))
  22163. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22164. ":[ \t]*\\(.*\\)") nil t)
  22165. (replace-match "\\1" t)
  22166. (add-text-properties
  22167. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22168. '(org-protected t))))
  22169. (goto-char (point-min))
  22170. (while (re-search-forward
  22171. (concat "^#\\+"
  22172. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22173. (cadddr fmt) "\\>.*\n?") nil t)
  22174. (if (car fmt)
  22175. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22176. '(org-protected t))
  22177. (delete-region (match-beginning 0) (match-end 0))))))
  22178. ;; Protect quoted subtrees
  22179. (goto-char (point-min))
  22180. (while (re-search-forward re-quote nil t)
  22181. (goto-char (match-beginning 0))
  22182. (end-of-line 1)
  22183. (add-text-properties (point) (org-end-of-subtree t)
  22184. '(org-protected t)))
  22185. ;; Protect verbatim elements
  22186. (goto-char (point-min))
  22187. (while (re-search-forward org-verbatim-re nil t)
  22188. (add-text-properties (match-beginning 4) (match-end 4)
  22189. '(org-protected t))
  22190. (goto-char (1+ (match-end 4))))
  22191. ;; Remove subtrees that are commented
  22192. (goto-char (point-min))
  22193. (while (re-search-forward re-commented nil t)
  22194. (goto-char (match-beginning 0))
  22195. (delete-region (point) (org-end-of-subtree t)))
  22196. ;; Remove special table lines
  22197. (when org-export-table-remove-special-lines
  22198. (goto-char (point-min))
  22199. (while (re-search-forward "^[ \t]*|" nil t)
  22200. (beginning-of-line 1)
  22201. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22202. (and (looking-at ".*?| *<[0-9]+> *|")
  22203. (not (looking-at ".*?| *[^ <|]"))))
  22204. (delete-region (max (point-min) (1- (point-at-bol)))
  22205. (point-at-eol))
  22206. (end-of-line 1))))
  22207. ;; Specific LaTeX stuff
  22208. (when latexp
  22209. (require 'org-export-latex nil)
  22210. (org-export-latex-cleaned-string))
  22211. (when asciip
  22212. (org-export-ascii-clean-string))
  22213. ;; Specific HTML stuff
  22214. (when htmlp
  22215. ;; Convert LaTeX fragments to images
  22216. (when (plist-get parameters :LaTeX-fragments)
  22217. (org-format-latex
  22218. (concat "ltxpng/" (file-name-sans-extension
  22219. (file-name-nondirectory
  22220. org-current-export-file)))
  22221. org-current-export-dir nil "Creating LaTeX image %s"))
  22222. (message "Exporting..."))
  22223. ;; Remove or replace comments
  22224. (goto-char (point-min))
  22225. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22226. (if commentsp
  22227. (progn (add-text-properties
  22228. (match-beginning 0) (match-end 0) '(org-protected t))
  22229. (replace-match (format commentsp (match-string 1)) t t))
  22230. (replace-match "")))
  22231. ;; Find matches for radio targets and turn them into internal links
  22232. (goto-char (point-min))
  22233. (when re-radio
  22234. (while (re-search-forward re-radio nil t)
  22235. (org-if-unprotected
  22236. (replace-match "\\1[[\\2]]"))))
  22237. ;; Find all links that contain a newline and put them into a single line
  22238. (goto-char (point-min))
  22239. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22240. (org-if-unprotected
  22241. (replace-match "\\1 \\3")
  22242. (goto-char (match-beginning 0))))
  22243. ;; Normalize links: Convert angle and plain links into bracket links
  22244. ;; Expand link abbreviations
  22245. (goto-char (point-min))
  22246. (while (re-search-forward re-plain-link nil t)
  22247. (goto-char (1- (match-end 0)))
  22248. (org-if-unprotected
  22249. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22250. ":" (match-string 3) "]]")))
  22251. ;; added 'org-link face to links
  22252. (put-text-property 0 (length s) 'face 'org-link s)
  22253. (replace-match s t t))))
  22254. (goto-char (point-min))
  22255. (while (re-search-forward re-angle-link nil t)
  22256. (goto-char (1- (match-end 0)))
  22257. (org-if-unprotected
  22258. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22259. ":" (match-string 3) "]]")))
  22260. (put-text-property 0 (length s) 'face 'org-link s)
  22261. (replace-match s t t))))
  22262. (goto-char (point-min))
  22263. (while (re-search-forward org-bracket-link-regexp nil t)
  22264. (org-if-unprotected
  22265. (let* ((s (concat "[[" (setq xx (save-match-data
  22266. (org-link-expand-abbrev (match-string 1))))
  22267. "]"
  22268. (if (match-end 3)
  22269. (match-string 2)
  22270. (concat "[" xx "]"))
  22271. "]")))
  22272. (put-text-property 0 (length s) 'face 'org-link s)
  22273. (replace-match s t t))))
  22274. ;; Find multiline emphasis and put them into single line
  22275. (when (plist-get parameters :emph-multiline)
  22276. (goto-char (point-min))
  22277. (while (re-search-forward org-emph-re nil t)
  22278. (if (not (= (char-after (match-beginning 3))
  22279. (char-after (match-beginning 4))))
  22280. (org-if-unprotected
  22281. (subst-char-in-region (match-beginning 0) (match-end 0)
  22282. ?\n ?\ t)
  22283. (goto-char (1- (match-end 0))))
  22284. (goto-char (1+ (match-beginning 0))))))
  22285. (setq rtn (buffer-string)))
  22286. (kill-buffer " org-mode-tmp")
  22287. rtn))
  22288. (defun org-export-grab-title-from-buffer ()
  22289. "Get a title for the current document, from looking at the buffer."
  22290. (let ((inhibit-read-only t))
  22291. (save-excursion
  22292. (goto-char (point-min))
  22293. (let ((end (save-excursion (outline-next-heading) (point))))
  22294. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22295. ;; Mark the line so that it will not be exported as normal text.
  22296. (org-unmodified
  22297. (add-text-properties (match-beginning 0) (match-end 0)
  22298. (list :org-license-to-kill t)))
  22299. ;; Return the title string
  22300. (org-trim (match-string 0)))))))
  22301. (defun org-export-get-title-from-subtree ()
  22302. "Return subtree title and exclude it from export."
  22303. (let (title (m (mark)))
  22304. (save-excursion
  22305. (goto-char (region-beginning))
  22306. (when (and (org-at-heading-p)
  22307. (>= (org-end-of-subtree t t) (region-end)))
  22308. ;; This is a subtree, we take the title from the first heading
  22309. (goto-char (region-beginning))
  22310. (looking-at org-todo-line-regexp)
  22311. (setq title (match-string 3))
  22312. (org-unmodified
  22313. (add-text-properties (point) (1+ (point-at-eol))
  22314. (list :org-license-to-kill t)))))
  22315. title))
  22316. (defun org-solidify-link-text (s &optional alist)
  22317. "Take link text and make a safe target out of it."
  22318. (save-match-data
  22319. (let* ((rtn
  22320. (mapconcat
  22321. 'identity
  22322. (org-split-string s "[ \t\r\n]+") "--"))
  22323. (a (assoc rtn alist)))
  22324. (or (cdr a) rtn))))
  22325. (defun org-get-min-level (lines)
  22326. "Get the minimum level in LINES."
  22327. (let ((re "^\\(\\*+\\) ") l min)
  22328. (catch 'exit
  22329. (while (setq l (pop lines))
  22330. (if (string-match re l)
  22331. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22332. 1)))
  22333. ;; Variable holding the vector with section numbers
  22334. (defvar org-section-numbers (make-vector org-level-max 0))
  22335. (defun org-init-section-numbers ()
  22336. "Initialize the vector for the section numbers."
  22337. (let* ((level -1)
  22338. (numbers (nreverse (org-split-string "" "\\.")))
  22339. (depth (1- (length org-section-numbers)))
  22340. (i depth) number-string)
  22341. (while (>= i 0)
  22342. (if (> i level)
  22343. (aset org-section-numbers i 0)
  22344. (setq number-string (or (car numbers) "0"))
  22345. (if (string-match "\\`[A-Z]\\'" number-string)
  22346. (aset org-section-numbers i
  22347. (- (string-to-char number-string) ?A -1))
  22348. (aset org-section-numbers i (string-to-number number-string)))
  22349. (pop numbers))
  22350. (setq i (1- i)))))
  22351. (defun org-section-number (&optional level)
  22352. "Return a string with the current section number.
  22353. When LEVEL is non-nil, increase section numbers on that level."
  22354. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22355. (when level
  22356. (when (> level -1)
  22357. (aset org-section-numbers
  22358. level (1+ (aref org-section-numbers level))))
  22359. (setq idx (1+ level))
  22360. (while (<= idx depth)
  22361. (if (not (= idx 1))
  22362. (aset org-section-numbers idx 0))
  22363. (setq idx (1+ idx))))
  22364. (setq idx 0)
  22365. (while (<= idx depth)
  22366. (setq n (aref org-section-numbers idx))
  22367. (setq string (concat string (if (not (string= string "")) "." "")
  22368. (int-to-string n)))
  22369. (setq idx (1+ idx)))
  22370. (save-match-data
  22371. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22372. (setq string (replace-match "" t nil string)))
  22373. (if (string-match "\\(\\.0\\)+\\'" string)
  22374. (setq string (replace-match "" t nil string))))
  22375. string))
  22376. ;;; ASCII export
  22377. (defvar org-last-level nil) ; dynamically scoped variable
  22378. (defvar org-min-level nil) ; dynamically scoped variable
  22379. (defvar org-levels-open nil) ; dynamically scoped parameter
  22380. (defvar org-ascii-current-indentation nil) ; For communication
  22381. (defun org-export-as-ascii (arg)
  22382. "Export the outline as a pretty ASCII file.
  22383. If there is an active region, export only the region.
  22384. The prefix ARG specifies how many levels of the outline should become
  22385. underlined headlines. The default is 3."
  22386. (interactive "P")
  22387. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22388. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22389. (org-infile-export-plist)))
  22390. (region-p (org-region-active-p))
  22391. (subtree-p
  22392. (when region-p
  22393. (save-excursion
  22394. (goto-char (region-beginning))
  22395. (and (org-at-heading-p)
  22396. (>= (org-end-of-subtree t t) (region-end))))))
  22397. (custom-times org-display-custom-times)
  22398. (org-ascii-current-indentation '(0 . 0))
  22399. (level 0) line txt
  22400. (umax nil)
  22401. (umax-toc nil)
  22402. (case-fold-search nil)
  22403. (filename (concat (file-name-as-directory
  22404. (org-export-directory :ascii opt-plist))
  22405. (file-name-sans-extension
  22406. (or (and subtree-p
  22407. (org-entry-get (region-beginning)
  22408. "EXPORT_FILE_NAME" t))
  22409. (file-name-nondirectory buffer-file-name)))
  22410. ".txt"))
  22411. (filename (if (equal (file-truename filename)
  22412. (file-truename buffer-file-name))
  22413. (concat filename ".txt")
  22414. filename))
  22415. (buffer (find-file-noselect filename))
  22416. (org-levels-open (make-vector org-level-max nil))
  22417. (odd org-odd-levels-only)
  22418. (date (plist-get opt-plist :date))
  22419. (author (plist-get opt-plist :author))
  22420. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22421. (plist-get opt-plist :title)
  22422. (and (not
  22423. (plist-get opt-plist :skip-before-1st-heading))
  22424. (org-export-grab-title-from-buffer))
  22425. (file-name-sans-extension
  22426. (file-name-nondirectory buffer-file-name))))
  22427. (email (plist-get opt-plist :email))
  22428. (language (plist-get opt-plist :language))
  22429. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22430. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22431. (todo nil)
  22432. (lang-words nil)
  22433. (region
  22434. (buffer-substring
  22435. (if (org-region-active-p) (region-beginning) (point-min))
  22436. (if (org-region-active-p) (region-end) (point-max))))
  22437. (lines (org-split-string
  22438. (org-cleaned-string-for-export
  22439. region
  22440. :for-ascii t
  22441. :skip-before-1st-heading
  22442. (plist-get opt-plist :skip-before-1st-heading)
  22443. :drawers (plist-get opt-plist :drawers)
  22444. :verbatim-multiline t
  22445. :archived-trees
  22446. (plist-get opt-plist :archived-trees)
  22447. :add-text (plist-get opt-plist :text))
  22448. "\n"))
  22449. thetoc have-headings first-heading-pos
  22450. table-open table-buffer)
  22451. (let ((inhibit-read-only t))
  22452. (org-unmodified
  22453. (remove-text-properties (point-min) (point-max)
  22454. '(:org-license-to-kill t))))
  22455. (setq org-min-level (org-get-min-level lines))
  22456. (setq org-last-level org-min-level)
  22457. (org-init-section-numbers)
  22458. (find-file-noselect filename)
  22459. (setq lang-words (or (assoc language org-export-language-setup)
  22460. (assoc "en" org-export-language-setup)))
  22461. (switch-to-buffer-other-window buffer)
  22462. (erase-buffer)
  22463. (fundamental-mode)
  22464. ;; create local variables for all options, to make sure all called
  22465. ;; functions get the correct information
  22466. (mapc (lambda (x)
  22467. (set (make-local-variable (cdr x))
  22468. (plist-get opt-plist (car x))))
  22469. org-export-plist-vars)
  22470. (org-set-local 'org-odd-levels-only odd)
  22471. (setq umax (if arg (prefix-numeric-value arg)
  22472. org-export-headline-levels))
  22473. (setq umax-toc (if (integerp org-export-with-toc)
  22474. (min org-export-with-toc umax)
  22475. umax))
  22476. ;; File header
  22477. (if title (org-insert-centered title ?=))
  22478. (insert "\n")
  22479. (if (and (or author email)
  22480. org-export-author-info)
  22481. (insert (concat (nth 1 lang-words) ": " (or author "")
  22482. (if email (concat " <" email ">") "")
  22483. "\n")))
  22484. (cond
  22485. ((and date (string-match "%" date))
  22486. (setq date (format-time-string date (current-time))))
  22487. (date)
  22488. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22489. (if (and date org-export-time-stamp-file)
  22490. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22491. (insert "\n\n")
  22492. (if org-export-with-toc
  22493. (progn
  22494. (push (concat (nth 3 lang-words) "\n") thetoc)
  22495. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22496. (mapc '(lambda (line)
  22497. (if (string-match org-todo-line-regexp
  22498. line)
  22499. ;; This is a headline
  22500. (progn
  22501. (setq have-headings t)
  22502. (setq level (- (match-end 1) (match-beginning 1))
  22503. level (org-tr-level level)
  22504. txt (match-string 3 line)
  22505. todo
  22506. (or (and org-export-mark-todo-in-toc
  22507. (match-beginning 2)
  22508. (not (member (match-string 2 line)
  22509. org-done-keywords)))
  22510. ; TODO, not DONE
  22511. (and org-export-mark-todo-in-toc
  22512. (= level umax-toc)
  22513. (org-search-todo-below
  22514. line lines level))))
  22515. (setq txt (org-html-expand-for-ascii txt))
  22516. (while (string-match org-bracket-link-regexp txt)
  22517. (setq txt
  22518. (replace-match
  22519. (match-string (if (match-end 2) 3 1) txt)
  22520. t t txt)))
  22521. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22522. (string-match
  22523. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22524. txt))
  22525. (setq txt (replace-match "" t t txt)))
  22526. (if (string-match quote-re0 txt)
  22527. (setq txt (replace-match "" t t txt)))
  22528. (if org-export-with-section-numbers
  22529. (setq txt (concat (org-section-number level)
  22530. " " txt)))
  22531. (if (<= level umax-toc)
  22532. (progn
  22533. (push
  22534. (concat
  22535. (make-string
  22536. (* (max 0 (- level org-min-level)) 4) ?\ )
  22537. (format (if todo "%s (*)\n" "%s\n") txt))
  22538. thetoc)
  22539. (setq org-last-level level))
  22540. ))))
  22541. lines)
  22542. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22543. (org-init-section-numbers)
  22544. (while (setq line (pop lines))
  22545. ;; Remove the quoted HTML tags.
  22546. (setq line (org-html-expand-for-ascii line))
  22547. ;; Remove targets
  22548. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22549. (setq line (replace-match "" t t line)))
  22550. ;; Replace internal links
  22551. (while (string-match org-bracket-link-regexp line)
  22552. (setq line (replace-match
  22553. (if (match-end 3) "[\\3]" "[\\1]")
  22554. t nil line)))
  22555. (when custom-times
  22556. (setq line (org-translate-time line)))
  22557. (cond
  22558. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22559. ;; a Headline
  22560. (setq first-heading-pos (or first-heading-pos (point)))
  22561. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22562. txt (match-string 2 line))
  22563. (org-ascii-level-start level txt umax lines))
  22564. ((and org-export-with-tables
  22565. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22566. (if (not table-open)
  22567. ;; New table starts
  22568. (setq table-open t table-buffer nil))
  22569. ;; Accumulate lines
  22570. (setq table-buffer (cons line table-buffer))
  22571. (when (or (not lines)
  22572. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22573. (car lines))))
  22574. (setq table-open nil
  22575. table-buffer (nreverse table-buffer))
  22576. (insert (mapconcat
  22577. (lambda (x)
  22578. (org-fix-indentation x org-ascii-current-indentation))
  22579. (org-format-table-ascii table-buffer)
  22580. "\n") "\n")))
  22581. (t
  22582. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22583. (if (and org-export-with-fixed-width
  22584. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22585. (setq line (replace-match "\\1" nil nil line)))
  22586. (insert line "\n"))))
  22587. (normal-mode)
  22588. ;; insert the table of contents
  22589. (when thetoc
  22590. (goto-char (point-min))
  22591. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22592. (progn
  22593. (goto-char (match-beginning 0))
  22594. (replace-match ""))
  22595. (goto-char first-heading-pos))
  22596. (mapc 'insert thetoc)
  22597. (or (looking-at "[ \t]*\n[ \t]*\n")
  22598. (insert "\n\n")))
  22599. ;; Convert whitespace place holders
  22600. (goto-char (point-min))
  22601. (let (beg end)
  22602. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22603. (setq end (next-single-property-change beg 'org-whitespace))
  22604. (goto-char beg)
  22605. (delete-region beg end)
  22606. (insert (make-string (- end beg) ?\ ))))
  22607. (save-buffer)
  22608. ;; remove display and invisible chars
  22609. (let (beg end)
  22610. (goto-char (point-min))
  22611. (while (setq beg (next-single-property-change (point) 'display))
  22612. (setq end (next-single-property-change beg 'display))
  22613. (delete-region beg end)
  22614. (goto-char beg)
  22615. (insert "=>"))
  22616. (goto-char (point-min))
  22617. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22618. (setq end (next-single-property-change beg 'org-cwidth))
  22619. (delete-region beg end)
  22620. (goto-char beg)))
  22621. (goto-char (point-min))))
  22622. (defun org-export-ascii-clean-string ()
  22623. "Do extra work for ASCII export"
  22624. (goto-char (point-min))
  22625. (while (re-search-forward org-verbatim-re nil t)
  22626. (goto-char (match-end 2))
  22627. (backward-delete-char 1) (insert "'")
  22628. (goto-char (match-beginning 2))
  22629. (delete-char 1) (insert "`")
  22630. (goto-char (match-end 2))))
  22631. (defun org-search-todo-below (line lines level)
  22632. "Search the subtree below LINE for any TODO entries."
  22633. (let ((rest (cdr (memq line lines)))
  22634. (re org-todo-line-regexp)
  22635. line lv todo)
  22636. (catch 'exit
  22637. (while (setq line (pop rest))
  22638. (if (string-match re line)
  22639. (progn
  22640. (setq lv (- (match-end 1) (match-beginning 1))
  22641. todo (and (match-beginning 2)
  22642. (not (member (match-string 2 line)
  22643. org-done-keywords))))
  22644. ; TODO, not DONE
  22645. (if (<= lv level) (throw 'exit nil))
  22646. (if todo (throw 'exit t))))))))
  22647. (defun org-html-expand-for-ascii (line)
  22648. "Handle quoted HTML for ASCII export."
  22649. (if org-export-html-expand
  22650. (while (string-match "@<[^<>\n]*>" line)
  22651. ;; We just remove the tags for now.
  22652. (setq line (replace-match "" nil nil line))))
  22653. line)
  22654. (defun org-insert-centered (s &optional underline)
  22655. "Insert the string S centered and underline it with character UNDERLINE."
  22656. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22657. (insert (make-string ind ?\ ) s "\n")
  22658. (if underline
  22659. (insert (make-string ind ?\ )
  22660. (make-string (string-width s) underline)
  22661. "\n"))))
  22662. (defun org-ascii-level-start (level title umax &optional lines)
  22663. "Insert a new level in ASCII export."
  22664. (let (char (n (- level umax 1)) (ind 0))
  22665. (if (> level umax)
  22666. (progn
  22667. (insert (make-string (* 2 n) ?\ )
  22668. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22669. org-export-ascii-bullets))
  22670. " " title "\n")
  22671. ;; find the indentation of the next non-empty line
  22672. (catch 'stop
  22673. (while lines
  22674. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22675. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22676. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22677. (pop lines)))
  22678. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22679. (if (or (not (equal (char-before) ?\n))
  22680. (not (equal (char-before (1- (point))) ?\n)))
  22681. (insert "\n"))
  22682. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22683. (unless org-export-with-tags
  22684. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22685. (setq title (replace-match "" t t title))))
  22686. (if org-export-with-section-numbers
  22687. (setq title (concat (org-section-number level) " " title)))
  22688. (insert title "\n" (make-string (string-width title) char) "\n")
  22689. (setq org-ascii-current-indentation '(0 . 0)))))
  22690. (defun org-export-visible (type arg)
  22691. "Create a copy of the visible part of the current buffer, and export it.
  22692. The copy is created in a temporary buffer and removed after use.
  22693. TYPE is the final key (as a string) that also select the export command in
  22694. the `C-c C-e' export dispatcher.
  22695. As a special case, if the you type SPC at the prompt, the temporary
  22696. org-mode file will not be removed but presented to you so that you can
  22697. continue to use it. The prefix arg ARG is passed through to the exporting
  22698. command."
  22699. (interactive
  22700. (list (progn
  22701. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22702. (read-char-exclusive))
  22703. current-prefix-arg))
  22704. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22705. (error "Invalid export key"))
  22706. (let* ((binding (cdr (assoc type
  22707. '((?a . org-export-as-ascii)
  22708. (?\C-a . org-export-as-ascii)
  22709. (?b . org-export-as-html-and-open)
  22710. (?\C-b . org-export-as-html-and-open)
  22711. (?h . org-export-as-html)
  22712. (?H . org-export-as-html-to-buffer)
  22713. (?R . org-export-region-as-html)
  22714. (?x . org-export-as-xoxo)))))
  22715. (keepp (equal type ?\ ))
  22716. (file buffer-file-name)
  22717. (buffer (get-buffer-create "*Org Export Visible*"))
  22718. s e)
  22719. ;; Need to hack the drawers here.
  22720. (save-excursion
  22721. (goto-char (point-min))
  22722. (while (re-search-forward org-drawer-regexp nil t)
  22723. (goto-char (match-beginning 1))
  22724. (or (org-invisible-p) (org-flag-drawer nil))))
  22725. (with-current-buffer buffer (erase-buffer))
  22726. (save-excursion
  22727. (setq s (goto-char (point-min)))
  22728. (while (not (= (point) (point-max)))
  22729. (goto-char (org-find-invisible))
  22730. (append-to-buffer buffer s (point))
  22731. (setq s (goto-char (org-find-visible))))
  22732. (org-cycle-hide-drawers 'all)
  22733. (goto-char (point-min))
  22734. (unless keepp
  22735. ;; Copy all comment lines to the end, to make sure #+ settings are
  22736. ;; still available for the second export step. Kind of a hack, but
  22737. ;; does do the trick.
  22738. (if (looking-at "#[^\r\n]*")
  22739. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22740. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22741. (append-to-buffer buffer (1+ (match-beginning 0))
  22742. (min (point-max) (1+ (match-end 0))))))
  22743. (set-buffer buffer)
  22744. (let ((buffer-file-name file)
  22745. (org-inhibit-startup t))
  22746. (org-mode)
  22747. (show-all)
  22748. (unless keepp (funcall binding arg))))
  22749. (if (not keepp)
  22750. (kill-buffer buffer)
  22751. (switch-to-buffer-other-window buffer)
  22752. (goto-char (point-min)))))
  22753. (defun org-find-visible ()
  22754. (let ((s (point)))
  22755. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22756. (get-char-property s 'invisible)))
  22757. s))
  22758. (defun org-find-invisible ()
  22759. (let ((s (point)))
  22760. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22761. (not (get-char-property s 'invisible))))
  22762. s))
  22763. ;;; HTML export
  22764. (defun org-get-current-options ()
  22765. "Return a string with current options as keyword options.
  22766. Does include HTML export options as well as TODO and CATEGORY stuff."
  22767. (format
  22768. "#+TITLE: %s
  22769. #+AUTHOR: %s
  22770. #+EMAIL: %s
  22771. #+LANGUAGE: %s
  22772. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22773. #+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
  22774. #+CATEGORY: %s
  22775. #+SEQ_TODO: %s
  22776. #+TYP_TODO: %s
  22777. #+PRIORITIES: %c %c %c
  22778. #+DRAWERS: %s
  22779. #+STARTUP: %s %s %s %s %s
  22780. #+TAGS: %s
  22781. #+ARCHIVE: %s
  22782. #+LINK: %s
  22783. "
  22784. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22785. org-export-headline-levels
  22786. org-export-with-section-numbers
  22787. org-export-with-toc
  22788. org-export-preserve-breaks
  22789. org-export-html-expand
  22790. org-export-with-fixed-width
  22791. org-export-with-tables
  22792. org-export-with-sub-superscripts
  22793. org-export-with-special-strings
  22794. org-export-with-footnotes
  22795. org-export-with-emphasize
  22796. org-export-with-TeX-macros
  22797. org-export-with-LaTeX-fragments
  22798. org-export-skip-text-before-1st-heading
  22799. org-export-with-drawers
  22800. org-export-with-tags
  22801. (file-name-nondirectory buffer-file-name)
  22802. "TODO FEEDBACK VERIFY DONE"
  22803. "Me Jason Marie DONE"
  22804. org-highest-priority org-lowest-priority org-default-priority
  22805. (mapconcat 'identity org-drawers " ")
  22806. (cdr (assoc org-startup-folded
  22807. '((nil . "showall") (t . "overview") (content . "content"))))
  22808. (if org-odd-levels-only "odd" "oddeven")
  22809. (if org-hide-leading-stars "hidestars" "showstars")
  22810. (if org-startup-align-all-tables "align" "noalign")
  22811. (cond ((eq t org-log-done) "logdone")
  22812. ((not org-log-done) "nologging")
  22813. ((listp org-log-done)
  22814. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  22815. org-log-done " ")))
  22816. (or (mapconcat (lambda (x)
  22817. (cond
  22818. ((equal '(:startgroup) x) "{")
  22819. ((equal '(:endgroup) x) "}")
  22820. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22821. (t (car x))))
  22822. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22823. org-archive-location
  22824. "org file:~/org/%s.org"
  22825. ))
  22826. (defun org-insert-export-options-template ()
  22827. "Insert into the buffer a template with information for exporting."
  22828. (interactive)
  22829. (if (not (bolp)) (newline))
  22830. (let ((s (org-get-current-options)))
  22831. (and (string-match "#\\+CATEGORY" s)
  22832. (setq s (substring s 0 (match-beginning 0))))
  22833. (insert s)))
  22834. (defun org-toggle-fixed-width-section (arg)
  22835. "Toggle the fixed-width export.
  22836. If there is no active region, the QUOTE keyword at the current headline is
  22837. inserted or removed. When present, it causes the text between this headline
  22838. and the next to be exported as fixed-width text, and unmodified.
  22839. If there is an active region, this command adds or removes a colon as the
  22840. first character of this line. If the first character of a line is a colon,
  22841. this line is also exported in fixed-width font."
  22842. (interactive "P")
  22843. (let* ((cc 0)
  22844. (regionp (org-region-active-p))
  22845. (beg (if regionp (region-beginning) (point)))
  22846. (end (if regionp (region-end)))
  22847. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22848. (case-fold-search nil)
  22849. (re "[ \t]*\\(:\\)")
  22850. off)
  22851. (if regionp
  22852. (save-excursion
  22853. (goto-char beg)
  22854. (setq cc (current-column))
  22855. (beginning-of-line 1)
  22856. (setq off (looking-at re))
  22857. (while (> nlines 0)
  22858. (setq nlines (1- nlines))
  22859. (beginning-of-line 1)
  22860. (cond
  22861. (arg
  22862. (move-to-column cc t)
  22863. (insert ":\n")
  22864. (forward-line -1))
  22865. ((and off (looking-at re))
  22866. (replace-match "" t t nil 1))
  22867. ((not off) (move-to-column cc t) (insert ":")))
  22868. (forward-line 1)))
  22869. (save-excursion
  22870. (org-back-to-heading)
  22871. (if (looking-at (concat outline-regexp
  22872. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22873. (replace-match "" t t nil 1)
  22874. (if (looking-at outline-regexp)
  22875. (progn
  22876. (goto-char (match-end 0))
  22877. (insert org-quote-string " "))))))))
  22878. (defun org-export-as-html-and-open (arg)
  22879. "Export the outline as HTML and immediately open it with a browser.
  22880. If there is an active region, export only the region.
  22881. The prefix ARG specifies how many levels of the outline should become
  22882. headlines. The default is 3. Lower levels will become bulleted lists."
  22883. (interactive "P")
  22884. (org-export-as-html arg 'hidden)
  22885. (org-open-file buffer-file-name))
  22886. (defun org-export-as-html-batch ()
  22887. "Call `org-export-as-html', may be used in batch processing as
  22888. emacs --batch
  22889. --load=$HOME/lib/emacs/org.el
  22890. --eval \"(setq org-export-headline-levels 2)\"
  22891. --visit=MyFile --funcall org-export-as-html-batch"
  22892. (org-export-as-html org-export-headline-levels 'hidden))
  22893. (defun org-export-as-html-to-buffer (arg)
  22894. "Call `org-exort-as-html` with output to a temporary buffer.
  22895. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22896. (interactive "P")
  22897. (org-export-as-html arg nil nil "*Org HTML Export*")
  22898. (switch-to-buffer-other-window "*Org HTML Export*"))
  22899. (defun org-replace-region-by-html (beg end)
  22900. "Assume the current region has org-mode syntax, and convert it to HTML.
  22901. This can be used in any buffer. For example, you could write an
  22902. itemized list in org-mode syntax in an HTML buffer and then use this
  22903. command to convert it."
  22904. (interactive "r")
  22905. (let (reg html buf pop-up-frames)
  22906. (save-window-excursion
  22907. (if (org-mode-p)
  22908. (setq html (org-export-region-as-html
  22909. beg end t 'string))
  22910. (setq reg (buffer-substring beg end)
  22911. buf (get-buffer-create "*Org tmp*"))
  22912. (with-current-buffer buf
  22913. (erase-buffer)
  22914. (insert reg)
  22915. (org-mode)
  22916. (setq html (org-export-region-as-html
  22917. (point-min) (point-max) t 'string)))
  22918. (kill-buffer buf)))
  22919. (delete-region beg end)
  22920. (insert html)))
  22921. (defun org-export-region-as-html (beg end &optional body-only buffer)
  22922. "Convert region from BEG to END in org-mode buffer to HTML.
  22923. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  22924. contents, and only produce the region of converted text, useful for
  22925. cut-and-paste operations.
  22926. If BUFFER is a buffer or a string, use/create that buffer as a target
  22927. of the converted HTML. If BUFFER is the symbol `string', return the
  22928. produced HTML as a string and leave not buffer behind. For example,
  22929. a Lisp program could call this function in the following way:
  22930. (setq html (org-export-region-as-html beg end t 'string))
  22931. When called interactively, the output buffer is selected, and shown
  22932. in a window. A non-interactive call will only retunr the buffer."
  22933. (interactive "r\nP")
  22934. (when (interactive-p)
  22935. (setq buffer "*Org HTML Export*"))
  22936. (let ((transient-mark-mode t) (zmacs-regions t)
  22937. rtn)
  22938. (goto-char end)
  22939. (set-mark (point)) ;; to activate the region
  22940. (goto-char beg)
  22941. (setq rtn (org-export-as-html
  22942. nil nil nil
  22943. buffer body-only))
  22944. (if (fboundp 'deactivate-mark) (deactivate-mark))
  22945. (if (and (interactive-p) (bufferp rtn))
  22946. (switch-to-buffer-other-window rtn)
  22947. rtn)))
  22948. (defvar html-table-tag nil) ; dynamically scoped into this.
  22949. (defun org-export-as-html (arg &optional hidden ext-plist
  22950. to-buffer body-only)
  22951. "Export the outline as a pretty HTML file.
  22952. If there is an active region, export only the region. The prefix
  22953. ARG specifies how many levels of the outline should become
  22954. headlines. The default is 3. Lower levels will become bulleted
  22955. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  22956. EXT-PLIST is a property list with external parameters overriding
  22957. org-mode's default settings, but still inferior to file-local
  22958. settings. When TO-BUFFER is non-nil, create a buffer with that
  22959. name and export to that buffer. If TO-BUFFER is the symbol `string',
  22960. don't leave any buffer behind but just return the resulting HTML as
  22961. a string. When BODY-ONLY is set, don't produce the file header and footer,
  22962. simply return the content of <body>...</body>, without even
  22963. the body tags themselves."
  22964. (interactive "P")
  22965. ;; Make sure we have a file name when we need it.
  22966. (when (and (not (or to-buffer body-only))
  22967. (not buffer-file-name))
  22968. (if (buffer-base-buffer)
  22969. (org-set-local 'buffer-file-name
  22970. (with-current-buffer (buffer-base-buffer)
  22971. buffer-file-name))
  22972. (error "Need a file name to be able to export.")))
  22973. (message "Exporting...")
  22974. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22975. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  22976. (setq-default org-done-keywords org-done-keywords)
  22977. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  22978. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22979. ext-plist
  22980. (org-infile-export-plist)))
  22981. (style (plist-get opt-plist :style))
  22982. (html-extension (plist-get opt-plist :html-extension))
  22983. (link-validate (plist-get opt-plist :link-validation-function))
  22984. valid thetoc have-headings first-heading-pos
  22985. (odd org-odd-levels-only)
  22986. (region-p (org-region-active-p))
  22987. (subtree-p
  22988. (when region-p
  22989. (save-excursion
  22990. (goto-char (region-beginning))
  22991. (and (org-at-heading-p)
  22992. (>= (org-end-of-subtree t t) (region-end))))))
  22993. ;; The following two are dynamically scoped into other
  22994. ;; routines below.
  22995. (org-current-export-dir (org-export-directory :html opt-plist))
  22996. (org-current-export-file buffer-file-name)
  22997. (level 0) (line "") (origline "") txt todo
  22998. (umax nil)
  22999. (umax-toc nil)
  23000. (filename (if to-buffer nil
  23001. (expand-file-name
  23002. (concat
  23003. (file-name-sans-extension
  23004. (or (and subtree-p
  23005. (org-entry-get (region-beginning)
  23006. "EXPORT_FILE_NAME" t))
  23007. (file-name-nondirectory buffer-file-name)))
  23008. "." html-extension)
  23009. (file-name-as-directory
  23010. (org-export-directory :html opt-plist)))))
  23011. (current-dir (if buffer-file-name
  23012. (file-name-directory buffer-file-name)
  23013. default-directory))
  23014. (buffer (if to-buffer
  23015. (cond
  23016. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23017. (t (get-buffer-create to-buffer)))
  23018. (find-file-noselect filename)))
  23019. (org-levels-open (make-vector org-level-max nil))
  23020. (date (plist-get opt-plist :date))
  23021. (author (plist-get opt-plist :author))
  23022. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23023. (plist-get opt-plist :title)
  23024. (and (not
  23025. (plist-get opt-plist :skip-before-1st-heading))
  23026. (org-export-grab-title-from-buffer))
  23027. (and buffer-file-name
  23028. (file-name-sans-extension
  23029. (file-name-nondirectory buffer-file-name)))
  23030. "UNTITLED"))
  23031. (html-table-tag (plist-get opt-plist :html-table-tag))
  23032. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23033. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23034. (inquote nil)
  23035. (infixed nil)
  23036. (in-local-list nil)
  23037. (local-list-num nil)
  23038. (local-list-indent nil)
  23039. (llt org-plain-list-ordered-item-terminator)
  23040. (email (plist-get opt-plist :email))
  23041. (language (plist-get opt-plist :language))
  23042. (lang-words nil)
  23043. (target-alist nil) tg
  23044. (head-count 0) cnt
  23045. (start 0)
  23046. (coding-system (and (boundp 'buffer-file-coding-system)
  23047. buffer-file-coding-system))
  23048. (coding-system-for-write (or org-export-html-coding-system
  23049. coding-system))
  23050. (save-buffer-coding-system (or org-export-html-coding-system
  23051. coding-system))
  23052. (charset (and coding-system-for-write
  23053. (fboundp 'coding-system-get)
  23054. (coding-system-get coding-system-for-write
  23055. 'mime-charset)))
  23056. (region
  23057. (buffer-substring
  23058. (if region-p (region-beginning) (point-min))
  23059. (if region-p (region-end) (point-max))))
  23060. (lines
  23061. (org-split-string
  23062. (org-cleaned-string-for-export
  23063. region
  23064. :emph-multiline t
  23065. :for-html t
  23066. :skip-before-1st-heading
  23067. (plist-get opt-plist :skip-before-1st-heading)
  23068. :drawers (plist-get opt-plist :drawers)
  23069. :archived-trees
  23070. (plist-get opt-plist :archived-trees)
  23071. :add-text
  23072. (plist-get opt-plist :text)
  23073. :LaTeX-fragments
  23074. (plist-get opt-plist :LaTeX-fragments))
  23075. "[\r\n]"))
  23076. table-open type
  23077. table-buffer table-orig-buffer
  23078. ind start-is-num starter didclose
  23079. rpl path desc descp desc1 desc2 link
  23080. )
  23081. (let ((inhibit-read-only t))
  23082. (org-unmodified
  23083. (remove-text-properties (point-min) (point-max)
  23084. '(:org-license-to-kill t))))
  23085. (message "Exporting...")
  23086. (setq org-min-level (org-get-min-level lines))
  23087. (setq org-last-level org-min-level)
  23088. (org-init-section-numbers)
  23089. (cond
  23090. ((and date (string-match "%" date))
  23091. (setq date (format-time-string date (current-time))))
  23092. (date)
  23093. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23094. ;; Get the language-dependent settings
  23095. (setq lang-words (or (assoc language org-export-language-setup)
  23096. (assoc "en" org-export-language-setup)))
  23097. ;; Switch to the output buffer
  23098. (set-buffer buffer)
  23099. (let ((inhibit-read-only t)) (erase-buffer))
  23100. (fundamental-mode)
  23101. (and (fboundp 'set-buffer-file-coding-system)
  23102. (set-buffer-file-coding-system coding-system-for-write))
  23103. (let ((case-fold-search nil)
  23104. (org-odd-levels-only odd))
  23105. ;; create local variables for all options, to make sure all called
  23106. ;; functions get the correct information
  23107. (mapc (lambda (x)
  23108. (set (make-local-variable (cdr x))
  23109. (plist-get opt-plist (car x))))
  23110. org-export-plist-vars)
  23111. (setq umax (if arg (prefix-numeric-value arg)
  23112. org-export-headline-levels))
  23113. (setq umax-toc (if (integerp org-export-with-toc)
  23114. (min org-export-with-toc umax)
  23115. umax))
  23116. (unless body-only
  23117. ;; File header
  23118. (insert (format
  23119. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23120. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23121. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23122. lang=\"%s\" xml:lang=\"%s\">
  23123. <head>
  23124. <title>%s</title>
  23125. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23126. <meta name=\"generator\" content=\"Org-mode\"/>
  23127. <meta name=\"generated\" content=\"%s\"/>
  23128. <meta name=\"author\" content=\"%s\"/>
  23129. %s
  23130. </head><body>
  23131. "
  23132. language language (org-html-expand title)
  23133. (or charset "iso-8859-1") date author style))
  23134. (insert (or (plist-get opt-plist :preamble) ""))
  23135. (when (plist-get opt-plist :auto-preamble)
  23136. (if title (insert (format org-export-html-title-format
  23137. (org-html-expand title))))))
  23138. (if (and org-export-with-toc (not body-only))
  23139. (progn
  23140. (push (format "<h%d>%s</h%d>\n"
  23141. org-export-html-toplevel-hlevel
  23142. (nth 3 lang-words)
  23143. org-export-html-toplevel-hlevel)
  23144. thetoc)
  23145. (push "<ul>\n<li>" thetoc)
  23146. (setq lines
  23147. (mapcar '(lambda (line)
  23148. (if (string-match org-todo-line-regexp line)
  23149. ;; This is a headline
  23150. (progn
  23151. (setq have-headings t)
  23152. (setq level (- (match-end 1) (match-beginning 1))
  23153. level (org-tr-level level)
  23154. txt (save-match-data
  23155. (org-html-expand
  23156. (org-export-cleanup-toc-line
  23157. (match-string 3 line))))
  23158. todo
  23159. (or (and org-export-mark-todo-in-toc
  23160. (match-beginning 2)
  23161. (not (member (match-string 2 line)
  23162. org-done-keywords)))
  23163. ; TODO, not DONE
  23164. (and org-export-mark-todo-in-toc
  23165. (= level umax-toc)
  23166. (org-search-todo-below
  23167. line lines level))))
  23168. (if (string-match
  23169. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23170. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23171. (if (string-match quote-re0 txt)
  23172. (setq txt (replace-match "" t t txt)))
  23173. (if org-export-with-section-numbers
  23174. (setq txt (concat (org-section-number level)
  23175. " " txt)))
  23176. (if (<= level (max umax umax-toc))
  23177. (setq head-count (+ head-count 1)))
  23178. (if (<= level umax-toc)
  23179. (progn
  23180. (if (> level org-last-level)
  23181. (progn
  23182. (setq cnt (- level org-last-level))
  23183. (while (>= (setq cnt (1- cnt)) 0)
  23184. (push "\n<ul>\n<li>" thetoc))
  23185. (push "\n" thetoc)))
  23186. (if (< level org-last-level)
  23187. (progn
  23188. (setq cnt (- org-last-level level))
  23189. (while (>= (setq cnt (1- cnt)) 0)
  23190. (push "</li>\n</ul>" thetoc))
  23191. (push "\n" thetoc)))
  23192. ;; Check for targets
  23193. (while (string-match org-target-regexp line)
  23194. (setq tg (match-string 1 line)
  23195. line (replace-match
  23196. (concat "@<span class=\"target\">" tg "@</span> ")
  23197. t t line))
  23198. (push (cons (org-solidify-link-text tg)
  23199. (format "sec-%d" head-count))
  23200. target-alist))
  23201. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23202. (setq txt (replace-match "" t t txt)))
  23203. (push
  23204. (format
  23205. (if todo
  23206. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23207. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23208. head-count txt) thetoc)
  23209. (setq org-last-level level))
  23210. )))
  23211. line)
  23212. lines))
  23213. (while (> org-last-level (1- org-min-level))
  23214. (setq org-last-level (1- org-last-level))
  23215. (push "</li>\n</ul>\n" thetoc))
  23216. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23217. (setq head-count 0)
  23218. (org-init-section-numbers)
  23219. (while (setq line (pop lines) origline line)
  23220. (catch 'nextline
  23221. ;; end of quote section?
  23222. (when (and inquote (string-match "^\\*+ " line))
  23223. (insert "</pre>\n")
  23224. (setq inquote nil))
  23225. ;; inside a quote section?
  23226. (when inquote
  23227. (insert (org-html-protect line) "\n")
  23228. (throw 'nextline nil))
  23229. ;; verbatim lines
  23230. (when (and org-export-with-fixed-width
  23231. (string-match "^[ \t]*:\\(.*\\)" line))
  23232. (when (not infixed)
  23233. (setq infixed t)
  23234. (insert "<pre>\n"))
  23235. (insert (org-html-protect (match-string 1 line)) "\n")
  23236. (when (and lines
  23237. (not (string-match "^[ \t]*\\(:.*\\)"
  23238. (car lines))))
  23239. (setq infixed nil)
  23240. (insert "</pre>\n"))
  23241. (throw 'nextline nil))
  23242. ;; Protected HTML
  23243. (when (get-text-property 0 'org-protected line)
  23244. (let (par)
  23245. (when (re-search-backward
  23246. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23247. (setq par (match-string 1))
  23248. (replace-match "\\2\n"))
  23249. (insert line "\n")
  23250. (while (and lines
  23251. (or (= (length (car lines)) 0)
  23252. (get-text-property 0 'org-protected (car lines))))
  23253. (insert (pop lines) "\n"))
  23254. (and par (insert "<p>\n")))
  23255. (throw 'nextline nil))
  23256. ;; Horizontal line
  23257. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23258. (insert "\n<hr/>\n")
  23259. (throw 'nextline nil))
  23260. ;; make targets to anchors
  23261. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23262. (cond
  23263. ((match-end 2)
  23264. (setq line (replace-match
  23265. (concat "@<a name=\""
  23266. (org-solidify-link-text (match-string 1 line))
  23267. "\">\\nbsp@</a>")
  23268. t t line)))
  23269. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23270. (setq line (replace-match
  23271. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23272. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23273. t t line)))
  23274. (t
  23275. (setq line (replace-match
  23276. (concat "@<a name=\""
  23277. (org-solidify-link-text (match-string 1 line))
  23278. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23279. t t line)))))
  23280. (setq line (org-html-handle-time-stamps line))
  23281. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23282. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23283. ;; Also handle sub_superscripts and checkboxes
  23284. (or (string-match org-table-hline-regexp line)
  23285. (setq line (org-html-expand line)))
  23286. ;; Format the links
  23287. (setq start 0)
  23288. (while (string-match org-bracket-link-analytic-regexp line start)
  23289. (setq start (match-beginning 0))
  23290. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23291. (setq path (match-string 3 line))
  23292. (setq desc1 (if (match-end 5) (match-string 5 line))
  23293. desc2 (if (match-end 2) (concat type ":" path) path)
  23294. descp (and desc1 (not (equal desc1 desc2)))
  23295. desc (or desc1 desc2))
  23296. ;; Make an image out of the description if that is so wanted
  23297. (when (and descp (org-file-image-p desc))
  23298. (save-match-data
  23299. (if (string-match "^file:" desc)
  23300. (setq desc (substring desc (match-end 0)))))
  23301. (setq desc (concat "<img src=\"" desc "\"/>")))
  23302. ;; FIXME: do we need to unescape here somewhere?
  23303. (cond
  23304. ((equal type "internal")
  23305. (setq rpl
  23306. (concat
  23307. "<a href=\"#"
  23308. (org-solidify-link-text
  23309. (save-match-data (org-link-unescape path)) target-alist)
  23310. "\">" desc "</a>")))
  23311. ((member type '("http" "https"))
  23312. ;; standard URL, just check if we need to inline an image
  23313. (if (and (or (eq t org-export-html-inline-images)
  23314. (and org-export-html-inline-images (not descp)))
  23315. (org-file-image-p path))
  23316. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23317. (setq link (concat type ":" path))
  23318. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23319. ((member type '("ftp" "mailto" "news"))
  23320. ;; standard URL
  23321. (setq link (concat type ":" path))
  23322. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23323. ((string= type "file")
  23324. ;; FILE link
  23325. (let* ((filename path)
  23326. (abs-p (file-name-absolute-p filename))
  23327. thefile file-is-image-p search)
  23328. (save-match-data
  23329. (if (string-match "::\\(.*\\)" filename)
  23330. (setq search (match-string 1 filename)
  23331. filename (replace-match "" t nil filename)))
  23332. (setq valid
  23333. (if (functionp link-validate)
  23334. (funcall link-validate filename current-dir)
  23335. t))
  23336. (setq file-is-image-p (org-file-image-p filename))
  23337. (setq thefile (if abs-p (expand-file-name filename) filename))
  23338. (when (and org-export-html-link-org-files-as-html
  23339. (string-match "\\.org$" thefile))
  23340. (setq thefile (concat (substring thefile 0
  23341. (match-beginning 0))
  23342. "." html-extension))
  23343. (if (and search
  23344. ;; make sure this is can be used as target search
  23345. (not (string-match "^[0-9]*$" search))
  23346. (not (string-match "^\\*" search))
  23347. (not (string-match "^/.*/$" search)))
  23348. (setq thefile (concat thefile "#"
  23349. (org-solidify-link-text
  23350. (org-link-unescape search)))))
  23351. (when (string-match "^file:" desc)
  23352. (setq desc (replace-match "" t t desc))
  23353. (if (string-match "\\.org$" desc)
  23354. (setq desc (replace-match "" t t desc))))))
  23355. (setq rpl (if (and file-is-image-p
  23356. (or (eq t org-export-html-inline-images)
  23357. (and org-export-html-inline-images
  23358. (not descp))))
  23359. (concat "<img src=\"" thefile "\"/>")
  23360. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23361. (if (not valid) (setq rpl desc))))
  23362. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23363. (setq rpl (concat "<i>&lt;" type ":"
  23364. (save-match-data (org-link-unescape path))
  23365. "&gt;</i>"))))
  23366. (setq line (replace-match rpl t t line)
  23367. start (+ start (length rpl))))
  23368. ;; TODO items
  23369. (if (and (string-match org-todo-line-regexp line)
  23370. (match-beginning 2))
  23371. (setq line
  23372. (concat (substring line 0 (match-beginning 2))
  23373. "<span class=\""
  23374. (if (member (match-string 2 line)
  23375. org-done-keywords)
  23376. "done" "todo")
  23377. "\">" (match-string 2 line)
  23378. "</span>" (substring line (match-end 2)))))
  23379. ;; Does this contain a reference to a footnote?
  23380. (when org-export-with-footnotes
  23381. (setq start 0)
  23382. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23383. (if (get-text-property (match-beginning 2) 'org-protected line)
  23384. (setq start (match-end 2))
  23385. (let ((n (match-string 2 line)))
  23386. (setq line
  23387. (replace-match
  23388. (format
  23389. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23390. (match-string 1 line) n n n)
  23391. t t line))))))
  23392. (cond
  23393. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23394. ;; This is a headline
  23395. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23396. txt (match-string 2 line))
  23397. (if (string-match quote-re0 txt)
  23398. (setq txt (replace-match "" t t txt)))
  23399. (if (<= level (max umax umax-toc))
  23400. (setq head-count (+ head-count 1)))
  23401. (when in-local-list
  23402. ;; Close any local lists before inserting a new header line
  23403. (while local-list-num
  23404. (org-close-li)
  23405. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23406. (pop local-list-num))
  23407. (setq local-list-indent nil
  23408. in-local-list nil))
  23409. (setq first-heading-pos (or first-heading-pos (point)))
  23410. (org-html-level-start level txt umax
  23411. (and org-export-with-toc (<= level umax))
  23412. head-count)
  23413. ;; QUOTES
  23414. (when (string-match quote-re line)
  23415. (insert "<pre>")
  23416. (setq inquote t)))
  23417. ((and org-export-with-tables
  23418. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23419. (if (not table-open)
  23420. ;; New table starts
  23421. (setq table-open t table-buffer nil table-orig-buffer nil))
  23422. ;; Accumulate lines
  23423. (setq table-buffer (cons line table-buffer)
  23424. table-orig-buffer (cons origline table-orig-buffer))
  23425. (when (or (not lines)
  23426. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23427. (car lines))))
  23428. (setq table-open nil
  23429. table-buffer (nreverse table-buffer)
  23430. table-orig-buffer (nreverse table-orig-buffer))
  23431. (org-close-par-maybe)
  23432. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23433. (t
  23434. ;; Normal lines
  23435. (when (string-match
  23436. (cond
  23437. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23438. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23439. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23440. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23441. line)
  23442. (setq ind (org-get-string-indentation line)
  23443. start-is-num (match-beginning 4)
  23444. starter (if (match-beginning 2)
  23445. (substring (match-string 2 line) 0 -1))
  23446. line (substring line (match-beginning 5)))
  23447. (unless (string-match "[^ \t]" line)
  23448. ;; empty line. Pretend indentation is large.
  23449. (setq ind (if org-empty-line-terminates-plain-lists
  23450. 0
  23451. (1+ (or (car local-list-indent) 1)))))
  23452. (setq didclose nil)
  23453. (while (and in-local-list
  23454. (or (and (= ind (car local-list-indent))
  23455. (not starter))
  23456. (< ind (car local-list-indent))))
  23457. (setq didclose t)
  23458. (org-close-li)
  23459. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23460. (pop local-list-num) (pop local-list-indent)
  23461. (setq in-local-list local-list-indent))
  23462. (cond
  23463. ((and starter
  23464. (or (not in-local-list)
  23465. (> ind (car local-list-indent))))
  23466. ;; Start new (level of) list
  23467. (org-close-par-maybe)
  23468. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23469. (push start-is-num local-list-num)
  23470. (push ind local-list-indent)
  23471. (setq in-local-list t))
  23472. (starter
  23473. ;; continue current list
  23474. (org-close-li)
  23475. (insert "<li>\n"))
  23476. (didclose
  23477. ;; we did close a list, normal text follows: need <p>
  23478. (org-open-par)))
  23479. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23480. (setq line
  23481. (replace-match
  23482. (if (equal (match-string 1 line) "X")
  23483. "<b>[X]</b>"
  23484. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23485. t t line))))
  23486. ;; Empty lines start a new paragraph. If hand-formatted lists
  23487. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23488. ;; also start a new paragraph.
  23489. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23490. ;; Is this the start of a footnote?
  23491. (when org-export-with-footnotes
  23492. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23493. (org-close-par-maybe)
  23494. (let ((n (match-string 1 line)))
  23495. (setq line (replace-match
  23496. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23497. ;; Check if the line break needs to be conserved
  23498. (cond
  23499. ((string-match "\\\\\\\\[ \t]*$" line)
  23500. (setq line (replace-match "<br/>" t t line)))
  23501. (org-export-preserve-breaks
  23502. (setq line (concat line "<br/>"))))
  23503. (insert line "\n")))))
  23504. ;; Properly close all local lists and other lists
  23505. (when inquote (insert "</pre>\n"))
  23506. (when in-local-list
  23507. ;; Close any local lists before inserting a new header line
  23508. (while local-list-num
  23509. (org-close-li)
  23510. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23511. (pop local-list-num))
  23512. (setq local-list-indent nil
  23513. in-local-list nil))
  23514. (org-html-level-start 1 nil umax
  23515. (and org-export-with-toc (<= level umax))
  23516. head-count)
  23517. (unless body-only
  23518. (when (plist-get opt-plist :auto-postamble)
  23519. (insert "<div id=\"postamble\">")
  23520. (when (and org-export-author-info author)
  23521. (insert "<p class=\"author\"> "
  23522. (nth 1 lang-words) ": " author "\n")
  23523. (when email
  23524. (if (listp (split-string email ",+ *"))
  23525. (mapc (lambda(e)
  23526. (insert "<a href=\"mailto:" e "\">&lt;"
  23527. e "&gt;</a>\n"))
  23528. (split-string email ",+ *"))
  23529. (insert "<a href=\"mailto:" email "\">&lt;"
  23530. email "&gt;</a>\n")))
  23531. (insert "</p>\n"))
  23532. (when (and date org-export-time-stamp-file)
  23533. (insert "<p class=\"date\"> "
  23534. (nth 2 lang-words) ": "
  23535. date "</p>\n"))
  23536. (insert "</div>"))
  23537. (if org-export-html-with-timestamp
  23538. (insert org-export-html-html-helper-timestamp))
  23539. (insert (or (plist-get opt-plist :postamble) ""))
  23540. (insert "</body>\n</html>\n"))
  23541. (normal-mode)
  23542. (if (eq major-mode default-major-mode) (html-mode))
  23543. ;; insert the table of contents
  23544. (goto-char (point-min))
  23545. (when thetoc
  23546. (if (or (re-search-forward
  23547. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23548. (re-search-forward
  23549. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23550. (progn
  23551. (goto-char (match-beginning 0))
  23552. (replace-match ""))
  23553. (goto-char first-heading-pos)
  23554. (when (looking-at "\\s-*</p>")
  23555. (goto-char (match-end 0))
  23556. (insert "\n")))
  23557. (insert "<div id=\"table-of-contents\">\n")
  23558. (mapc 'insert thetoc)
  23559. (insert "</div>\n"))
  23560. ;; remove empty paragraphs and lists
  23561. (goto-char (point-min))
  23562. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23563. (replace-match ""))
  23564. (goto-char (point-min))
  23565. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23566. (replace-match ""))
  23567. (goto-char (point-min))
  23568. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23569. (replace-match ""))
  23570. ;; Convert whitespace place holders
  23571. (goto-char (point-min))
  23572. (let (beg end n)
  23573. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23574. (setq n (get-text-property beg 'org-whitespace)
  23575. end (next-single-property-change beg 'org-whitespace))
  23576. (goto-char beg)
  23577. (delete-region beg end)
  23578. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23579. (make-string n ?x)))))
  23580. (or to-buffer (save-buffer))
  23581. (goto-char (point-min))
  23582. (message "Exporting... done")
  23583. (if (eq to-buffer 'string)
  23584. (prog1 (buffer-substring (point-min) (point-max))
  23585. (kill-buffer (current-buffer)))
  23586. (current-buffer)))))
  23587. (defvar org-table-colgroup-info nil)
  23588. (defun org-format-table-ascii (lines)
  23589. "Format a table for ascii export."
  23590. (if (stringp lines)
  23591. (setq lines (org-split-string lines "\n")))
  23592. (if (not (string-match "^[ \t]*|" (car lines)))
  23593. ;; Table made by table.el - test for spanning
  23594. lines
  23595. ;; A normal org table
  23596. ;; Get rid of hlines at beginning and end
  23597. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23598. (setq lines (nreverse lines))
  23599. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23600. (setq lines (nreverse lines))
  23601. (when org-export-table-remove-special-lines
  23602. ;; Check if the table has a marking column. If yes remove the
  23603. ;; column and the special lines
  23604. (setq lines (org-table-clean-before-export lines)))
  23605. ;; Get rid of the vertical lines except for grouping
  23606. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23607. rtn line vl1 start)
  23608. (while (setq line (pop lines))
  23609. (if (string-match org-table-hline-regexp line)
  23610. (and (string-match "|\\(.*\\)|" line)
  23611. (setq line (replace-match " \\1" t nil line)))
  23612. (setq start 0 vl1 vl)
  23613. (while (string-match "|" line start)
  23614. (setq start (match-end 0))
  23615. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23616. (push line rtn))
  23617. (nreverse rtn))))
  23618. (defun org-colgroup-info-to-vline-list (info)
  23619. (let (vl new last)
  23620. (while info
  23621. (setq last new new (pop info))
  23622. (if (or (memq last '(:end :startend))
  23623. (memq new '(:start :startend)))
  23624. (push t vl)
  23625. (push nil vl)))
  23626. (setq vl (nreverse vl))
  23627. (and vl (setcar vl nil))
  23628. vl))
  23629. (defun org-format-table-html (lines olines)
  23630. "Find out which HTML converter to use and return the HTML code."
  23631. (if (stringp lines)
  23632. (setq lines (org-split-string lines "\n")))
  23633. (if (string-match "^[ \t]*|" (car lines))
  23634. ;; A normal org table
  23635. (org-format-org-table-html lines)
  23636. ;; Table made by table.el - test for spanning
  23637. (let* ((hlines (delq nil (mapcar
  23638. (lambda (x)
  23639. (if (string-match "^[ \t]*\\+-" x) x
  23640. nil))
  23641. lines)))
  23642. (first (car hlines))
  23643. (ll (and (string-match "\\S-+" first)
  23644. (match-string 0 first)))
  23645. (re (concat "^[ \t]*" (regexp-quote ll)))
  23646. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23647. hlines))))
  23648. (if (and (not spanning)
  23649. (not org-export-prefer-native-exporter-for-tables))
  23650. ;; We can use my own converter with HTML conversions
  23651. (org-format-table-table-html lines)
  23652. ;; Need to use the code generator in table.el, with the original text.
  23653. (org-format-table-table-html-using-table-generate-source olines)))))
  23654. (defun org-format-org-table-html (lines &optional splice)
  23655. "Format a table into HTML."
  23656. ;; Get rid of hlines at beginning and end
  23657. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23658. (setq lines (nreverse lines))
  23659. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23660. (setq lines (nreverse lines))
  23661. (when org-export-table-remove-special-lines
  23662. ;; Check if the table has a marking column. If yes remove the
  23663. ;; column and the special lines
  23664. (setq lines (org-table-clean-before-export lines)))
  23665. (let ((head (and org-export-highlight-first-table-line
  23666. (delq nil (mapcar
  23667. (lambda (x) (string-match "^[ \t]*|-" x))
  23668. (cdr lines)))))
  23669. (nlines 0) fnum i
  23670. tbopen line fields html gr colgropen)
  23671. (if splice (setq head nil))
  23672. (unless splice (push (if head "<thead>" "<tbody>") html))
  23673. (setq tbopen t)
  23674. (while (setq line (pop lines))
  23675. (catch 'next-line
  23676. (if (string-match "^[ \t]*|-" line)
  23677. (progn
  23678. (unless splice
  23679. (push (if head "</thead>" "</tbody>") html)
  23680. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23681. (setq head nil) ;; head ends here, first time around
  23682. ;; ignore this line
  23683. (throw 'next-line t)))
  23684. ;; Break the line into fields
  23685. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23686. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23687. (setq nlines (1+ nlines) i -1)
  23688. (push (concat "<tr>"
  23689. (mapconcat
  23690. (lambda (x)
  23691. (setq i (1+ i))
  23692. (if (and (< i nlines)
  23693. (string-match org-table-number-regexp x))
  23694. (incf (aref fnum i)))
  23695. (if head
  23696. (concat (car org-export-table-header-tags) x
  23697. (cdr org-export-table-header-tags))
  23698. (concat (car org-export-table-data-tags) x
  23699. (cdr org-export-table-data-tags))))
  23700. fields "")
  23701. "</tr>")
  23702. html)))
  23703. (unless splice (if tbopen (push "</tbody>" html)))
  23704. (unless splice (push "</table>\n" html))
  23705. (setq html (nreverse html))
  23706. (unless splice
  23707. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23708. (push (mapconcat
  23709. (lambda (x)
  23710. (setq gr (pop org-table-colgroup-info))
  23711. (format "%s<col align=\"%s\"></col>%s"
  23712. (if (memq gr '(:start :startend))
  23713. (prog1
  23714. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23715. (setq colgropen t))
  23716. "")
  23717. (if (> (/ (float x) nlines) org-table-number-fraction)
  23718. "right" "left")
  23719. (if (memq gr '(:end :startend))
  23720. (progn (setq colgropen nil) "</colgroup>")
  23721. "")))
  23722. fnum "")
  23723. html)
  23724. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23725. (push html-table-tag html))
  23726. (concat (mapconcat 'identity html "\n") "\n")))
  23727. (defun org-table-clean-before-export (lines)
  23728. "Check if the table has a marking column.
  23729. If yes remove the column and the special lines."
  23730. (setq org-table-colgroup-info nil)
  23731. (if (memq nil
  23732. (mapcar
  23733. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23734. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23735. lines))
  23736. (progn
  23737. (setq org-table-clean-did-remove-column nil)
  23738. (delq nil
  23739. (mapcar
  23740. (lambda (x)
  23741. (cond
  23742. ((string-match "^[ \t]*| */ *|" x)
  23743. (setq org-table-colgroup-info
  23744. (mapcar (lambda (x)
  23745. (cond ((member x '("<" "&lt;")) :start)
  23746. ((member x '(">" "&gt;")) :end)
  23747. ((member x '("<>" "&lt;&gt;")) :startend)
  23748. (t nil)))
  23749. (org-split-string x "[ \t]*|[ \t]*")))
  23750. nil)
  23751. (t x)))
  23752. lines)))
  23753. (setq org-table-clean-did-remove-column t)
  23754. (delq nil
  23755. (mapcar
  23756. (lambda (x)
  23757. (cond
  23758. ((string-match "^[ \t]*| */ *|" x)
  23759. (setq org-table-colgroup-info
  23760. (mapcar (lambda (x)
  23761. (cond ((member x '("<" "&lt;")) :start)
  23762. ((member x '(">" "&gt;")) :end)
  23763. ((member x '("<>" "&lt;&gt;")) :startend)
  23764. (t nil)))
  23765. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23766. nil)
  23767. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23768. nil) ; ignore this line
  23769. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23770. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23771. ;; remove the first column
  23772. (replace-match "\\1|" t nil x))))
  23773. lines))))
  23774. (defun org-format-table-table-html (lines)
  23775. "Format a table generated by table.el into HTML.
  23776. This conversion does *not* use `table-generate-source' from table.el.
  23777. This has the advantage that Org-mode's HTML conversions can be used.
  23778. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23779. (let (line field-buffer
  23780. (head org-export-highlight-first-table-line)
  23781. fields html empty)
  23782. (setq html (concat html-table-tag "\n"))
  23783. (while (setq line (pop lines))
  23784. (setq empty "&nbsp;")
  23785. (catch 'next-line
  23786. (if (string-match "^[ \t]*\\+-" line)
  23787. (progn
  23788. (if field-buffer
  23789. (progn
  23790. (setq
  23791. html
  23792. (concat
  23793. html
  23794. "<tr>"
  23795. (mapconcat
  23796. (lambda (x)
  23797. (if (equal x "") (setq x empty))
  23798. (if head
  23799. (concat (car org-export-table-header-tags) x
  23800. (cdr org-export-table-header-tags))
  23801. (concat (car org-export-table-data-tags) x
  23802. (cdr org-export-table-data-tags))))
  23803. field-buffer "\n")
  23804. "</tr>\n"))
  23805. (setq head nil)
  23806. (setq field-buffer nil)))
  23807. ;; Ignore this line
  23808. (throw 'next-line t)))
  23809. ;; Break the line into fields and store the fields
  23810. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23811. (if field-buffer
  23812. (setq field-buffer (mapcar
  23813. (lambda (x)
  23814. (concat x "<br/>" (pop fields)))
  23815. field-buffer))
  23816. (setq field-buffer fields))))
  23817. (setq html (concat html "</table>\n"))
  23818. html))
  23819. (defun org-format-table-table-html-using-table-generate-source (lines)
  23820. "Format a table into html, using `table-generate-source' from table.el.
  23821. This has the advantage that cell- or row-spanning is allowed.
  23822. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23823. (require 'table)
  23824. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23825. (erase-buffer)
  23826. (insert (mapconcat 'identity lines "\n"))
  23827. (goto-char (point-min))
  23828. (if (not (re-search-forward "|[^+]" nil t))
  23829. (error "Error processing table"))
  23830. (table-recognize-table)
  23831. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23832. (table-generate-source 'html " org-tmp2 ")
  23833. (set-buffer " org-tmp2 ")
  23834. (buffer-substring (point-min) (point-max))))
  23835. (defun org-html-handle-time-stamps (s)
  23836. "Format time stamps in string S, or remove them."
  23837. (catch 'exit
  23838. (let (r b)
  23839. (while (string-match org-maybe-keyword-time-regexp s)
  23840. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23841. ;; never export CLOCK
  23842. (throw 'exit ""))
  23843. (or b (setq b (substring s 0 (match-beginning 0))))
  23844. (if (not org-export-with-timestamps)
  23845. (setq r (concat r (substring s 0 (match-beginning 0)))
  23846. s (substring s (match-end 0)))
  23847. (setq r (concat
  23848. r (substring s 0 (match-beginning 0))
  23849. (if (match-end 1)
  23850. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23851. (match-string 1 s)))
  23852. (format " @<span class=\"timestamp\">%s@</span>"
  23853. (substring
  23854. (org-translate-time (match-string 3 s)) 1 -1)))
  23855. s (substring s (match-end 0)))))
  23856. ;; Line break if line started and ended with time stamp stuff
  23857. (if (not r)
  23858. s
  23859. (setq r (concat r s))
  23860. (unless (string-match "\\S-" (concat b s))
  23861. (setq r (concat r "@<br/>")))
  23862. r))))
  23863. (defun org-html-protect (s)
  23864. ;; convert & to &amp;, < to &lt; and > to &gt;
  23865. (let ((start 0))
  23866. (while (string-match "&" s start)
  23867. (setq s (replace-match "&amp;" t t s)
  23868. start (1+ (match-beginning 0))))
  23869. (while (string-match "<" s)
  23870. (setq s (replace-match "&lt;" t t s)))
  23871. (while (string-match ">" s)
  23872. (setq s (replace-match "&gt;" t t s))))
  23873. s)
  23874. (defun org-export-cleanup-toc-line (s)
  23875. "Remove tags and time staps from lines going into the toc."
  23876. (when (memq org-export-with-tags '(not-in-toc nil))
  23877. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23878. (setq s (replace-match "" t t s))))
  23879. (when org-export-remove-timestamps-from-toc
  23880. (while (string-match org-maybe-keyword-time-regexp s)
  23881. (setq s (replace-match "" t t s))))
  23882. (while (string-match org-bracket-link-regexp s)
  23883. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23884. t t s)))
  23885. s)
  23886. (defun org-html-expand (string)
  23887. "Prepare STRING for HTML export. Applies all active conversions.
  23888. If there are links in the string, don't modify these."
  23889. (let* ((re (concat org-bracket-link-regexp "\\|"
  23890. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23891. m s l res)
  23892. (while (setq m (string-match re string))
  23893. (setq s (substring string 0 m)
  23894. l (match-string 0 string)
  23895. string (substring string (match-end 0)))
  23896. (push (org-html-do-expand s) res)
  23897. (push l res))
  23898. (push (org-html-do-expand string) res)
  23899. (apply 'concat (nreverse res))))
  23900. (defun org-html-do-expand (s)
  23901. "Apply all active conversions to translate special ASCII to HTML."
  23902. (setq s (org-html-protect s))
  23903. (if org-export-html-expand
  23904. (let ((start 0))
  23905. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23906. (setq s (replace-match "<\\1>" t nil s)))))
  23907. (if org-export-with-emphasize
  23908. (setq s (org-export-html-convert-emphasize s)))
  23909. (if org-export-with-special-strings
  23910. (setq s (org-export-html-convert-special-strings s)))
  23911. (if org-export-with-sub-superscripts
  23912. (setq s (org-export-html-convert-sub-super s)))
  23913. (if org-export-with-TeX-macros
  23914. (let ((start 0) wd ass)
  23915. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  23916. (if (get-text-property (match-beginning 0) 'org-protected s)
  23917. (setq start (match-end 0))
  23918. (setq wd (match-string 1 s))
  23919. (if (setq ass (assoc wd org-html-entities))
  23920. (setq s (replace-match (or (cdr ass)
  23921. (concat "&" (car ass) ";"))
  23922. t t s))
  23923. (setq start (+ start (length wd))))))))
  23924. s)
  23925. (defun org-create-multibrace-regexp (left right n)
  23926. "Create a regular expression which will match a balanced sexp.
  23927. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  23928. as single character strings.
  23929. The regexp returned will match the entire expression including the
  23930. delimiters. It will also define a single group which contains the
  23931. match except for the outermost delimiters. The maximum depth of
  23932. stacked delimiters is N. Escaping delimiters is not possible."
  23933. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  23934. (or "\\|")
  23935. (re nothing)
  23936. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  23937. (while (> n 1)
  23938. (setq n (1- n)
  23939. re (concat re or next)
  23940. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  23941. (concat left "\\(" re "\\)" right)))
  23942. (defvar org-match-substring-regexp
  23943. (concat
  23944. "\\([^\\]\\)\\([_^]\\)\\("
  23945. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23946. "\\|"
  23947. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  23948. "\\|"
  23949. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  23950. "The regular expression matching a sub- or superscript.")
  23951. (defvar org-match-substring-with-braces-regexp
  23952. (concat
  23953. "\\([^\\]\\)\\([_^]\\)\\("
  23954. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23955. "\\)")
  23956. "The regular expression matching a sub- or superscript, forcing braces.")
  23957. (defconst org-export-html-special-string-regexps
  23958. '(("\\\\-" . "&shy;")
  23959. ("---\\([^-]\\)" . "&mdash;\\1")
  23960. ("--\\([^-]\\)" . "&ndash;\\1")
  23961. ("\\.\\.\\." . "&hellip;"))
  23962. "Regular expressions for special string conversion.")
  23963. (defun org-export-html-convert-special-strings (string)
  23964. "Convert special characters in STRING to HTML."
  23965. (let ((all org-export-html-special-string-regexps)
  23966. e a re rpl start)
  23967. (while (setq a (pop all))
  23968. (setq re (car a) rpl (cdr a) start 0)
  23969. (while (string-match re string start)
  23970. (if (get-text-property (match-beginning 0) 'org-protected string)
  23971. (setq start (match-end 0))
  23972. (setq string (replace-match rpl t nil string)))))
  23973. string))
  23974. (defun org-export-html-convert-sub-super (string)
  23975. "Convert sub- and superscripts in STRING to HTML."
  23976. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  23977. (while (string-match org-match-substring-regexp string s)
  23978. (cond
  23979. ((and requireb (match-end 8)) (setq s (match-end 2)))
  23980. ((get-text-property (match-beginning 2) 'org-protected string)
  23981. (setq s (match-end 2)))
  23982. (t
  23983. (setq s (match-end 1)
  23984. key (if (string= (match-string 2 string) "_") "sub" "sup")
  23985. c (or (match-string 8 string)
  23986. (match-string 6 string)
  23987. (match-string 5 string))
  23988. string (replace-match
  23989. (concat (match-string 1 string)
  23990. "<" key ">" c "</" key ">")
  23991. t t string)))))
  23992. (while (string-match "\\\\\\([_^]\\)" string)
  23993. (setq string (replace-match (match-string 1 string) t t string)))
  23994. string))
  23995. (defun org-export-html-convert-emphasize (string)
  23996. "Apply emphasis."
  23997. (let ((s 0) rpl)
  23998. (while (string-match org-emph-re string s)
  23999. (if (not (equal
  24000. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24001. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24002. (setq s (match-beginning 0)
  24003. rpl
  24004. (concat
  24005. (match-string 1 string)
  24006. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24007. (match-string 4 string)
  24008. (nth 3 (assoc (match-string 3 string)
  24009. org-emphasis-alist))
  24010. (match-string 5 string))
  24011. string (replace-match rpl t t string)
  24012. s (+ s (- (length rpl) 2)))
  24013. (setq s (1+ s))))
  24014. string))
  24015. (defvar org-par-open nil)
  24016. (defun org-open-par ()
  24017. "Insert <p>, but first close previous paragraph if any."
  24018. (org-close-par-maybe)
  24019. (insert "\n<p>")
  24020. (setq org-par-open t))
  24021. (defun org-close-par-maybe ()
  24022. "Close paragraph if there is one open."
  24023. (when org-par-open
  24024. (insert "</p>")
  24025. (setq org-par-open nil)))
  24026. (defun org-close-li ()
  24027. "Close <li> if necessary."
  24028. (org-close-par-maybe)
  24029. (insert "</li>\n"))
  24030. (defvar body-only) ; dynamically scoped into this.
  24031. (defun org-html-level-start (level title umax with-toc head-count)
  24032. "Insert a new level in HTML export.
  24033. When TITLE is nil, just close all open levels."
  24034. (org-close-par-maybe)
  24035. (let ((l org-level-max))
  24036. (while (>= l level)
  24037. (if (aref org-levels-open (1- l))
  24038. (progn
  24039. (org-html-level-close l umax)
  24040. (aset org-levels-open (1- l) nil)))
  24041. (setq l (1- l)))
  24042. (when title
  24043. ;; If title is nil, this means this function is called to close
  24044. ;; all levels, so the rest is done only if title is given
  24045. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24046. (setq title (replace-match
  24047. (if org-export-with-tags
  24048. (save-match-data
  24049. (concat
  24050. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24051. (mapconcat 'identity (org-split-string
  24052. (match-string 1 title) ":")
  24053. "&nbsp;")
  24054. "</span>"))
  24055. "")
  24056. t t title)))
  24057. (if (> level umax)
  24058. (progn
  24059. (if (aref org-levels-open (1- level))
  24060. (progn
  24061. (org-close-li)
  24062. (insert "<li>" title "<br/>\n"))
  24063. (aset org-levels-open (1- level) t)
  24064. (org-close-par-maybe)
  24065. (insert "<ul>\n<li>" title "<br/>\n")))
  24066. (aset org-levels-open (1- level) t)
  24067. (if (and org-export-with-section-numbers (not body-only))
  24068. (setq title (concat (org-section-number level) " " title)))
  24069. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24070. (if with-toc
  24071. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24072. level level head-count title level))
  24073. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24074. (org-open-par)))))
  24075. (defun org-html-level-close (level max-outline-level)
  24076. "Terminate one level in HTML export."
  24077. (if (<= level max-outline-level)
  24078. (insert "</div>\n")
  24079. (org-close-li)
  24080. (insert "</ul>\n")))
  24081. ;;; iCalendar export
  24082. ;;;###autoload
  24083. (defun org-export-icalendar-this-file ()
  24084. "Export current file as an iCalendar file.
  24085. The iCalendar file will be located in the same directory as the Org-mode
  24086. file, but with extension `.ics'."
  24087. (interactive)
  24088. (org-export-icalendar nil buffer-file-name))
  24089. ;;;###autoload
  24090. (defun org-export-icalendar-all-agenda-files ()
  24091. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24092. Each iCalendar file will be located in the same directory as the Org-mode
  24093. file, but with extension `.ics'."
  24094. (interactive)
  24095. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24096. ;;;###autoload
  24097. (defun org-export-icalendar-combine-agenda-files ()
  24098. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24099. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24100. (interactive)
  24101. (apply 'org-export-icalendar t (org-agenda-files t)))
  24102. (defun org-export-icalendar (combine &rest files)
  24103. "Create iCalendar files for all elements of FILES.
  24104. If COMBINE is non-nil, combine all calendar entries into a single large
  24105. file and store it under the name `org-combined-agenda-icalendar-file'."
  24106. (save-excursion
  24107. (org-prepare-agenda-buffers files)
  24108. (let* ((dir (org-export-directory
  24109. :ical (list :publishing-directory
  24110. org-export-publishing-directory)))
  24111. file ical-file ical-buffer category started org-agenda-new-buffers)
  24112. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24113. (when combine
  24114. (setq ical-file
  24115. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24116. org-combined-agenda-icalendar-file
  24117. (expand-file-name org-combined-agenda-icalendar-file dir))
  24118. ical-buffer (org-get-agenda-file-buffer ical-file))
  24119. (set-buffer ical-buffer) (erase-buffer))
  24120. (while (setq file (pop files))
  24121. (catch 'nextfile
  24122. (org-check-agenda-file file)
  24123. (set-buffer (org-get-agenda-file-buffer file))
  24124. (unless combine
  24125. (setq ical-file (concat (file-name-as-directory dir)
  24126. (file-name-sans-extension
  24127. (file-name-nondirectory buffer-file-name))
  24128. ".ics"))
  24129. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24130. (with-current-buffer ical-buffer (erase-buffer)))
  24131. (setq category (or org-category
  24132. (file-name-sans-extension
  24133. (file-name-nondirectory buffer-file-name))))
  24134. (if (symbolp category) (setq category (symbol-name category)))
  24135. (let ((standard-output ical-buffer))
  24136. (if combine
  24137. (and (not started) (setq started t)
  24138. (org-start-icalendar-file org-icalendar-combined-name))
  24139. (org-start-icalendar-file category))
  24140. (org-print-icalendar-entries combine)
  24141. (when (or (and combine (not files)) (not combine))
  24142. (org-finish-icalendar-file)
  24143. (set-buffer ical-buffer)
  24144. (save-buffer)
  24145. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24146. (org-release-buffers org-agenda-new-buffers))))
  24147. (defvar org-after-save-iCalendar-file-hook nil
  24148. "Hook run after an iCalendar file has been saved.
  24149. The iCalendar buffer is still current when this hook is run.
  24150. A good way to use this is to tell a desktop calenndar application to re-read
  24151. the iCalendar file.")
  24152. (defun org-print-icalendar-entries (&optional combine)
  24153. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24154. When COMBINE is non nil, add the category to each line."
  24155. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24156. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24157. (dts (org-ical-ts-to-string
  24158. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24159. "DTSTART"))
  24160. hd ts ts2 state status (inc t) pos b sexp rrule
  24161. scheduledp deadlinep tmp pri category entry location summary desc
  24162. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24163. (org-refresh-category-properties)
  24164. (save-excursion
  24165. (goto-char (point-min))
  24166. (while (re-search-forward re1 nil t)
  24167. (catch :skip
  24168. (org-agenda-skip)
  24169. (setq pos (match-beginning 0)
  24170. ts (match-string 0)
  24171. inc t
  24172. hd (org-get-heading)
  24173. summary (org-icalendar-cleanup-string
  24174. (org-entry-get nil "SUMMARY"))
  24175. desc (org-icalendar-cleanup-string
  24176. (or (org-entry-get nil "DESCRIPTION")
  24177. (and org-icalendar-include-body (org-get-entry)))
  24178. t org-icalendar-include-body)
  24179. location (org-icalendar-cleanup-string
  24180. (org-entry-get nil "LOCATION"))
  24181. category (org-get-category))
  24182. (if (looking-at re2)
  24183. (progn
  24184. (goto-char (match-end 0))
  24185. (setq ts2 (match-string 1) inc nil))
  24186. (setq tmp (buffer-substring (max (point-min)
  24187. (- pos org-ds-keyword-length))
  24188. pos)
  24189. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24190. (progn
  24191. (setq inc nil)
  24192. (replace-match "\\1" t nil ts))
  24193. ts)
  24194. deadlinep (string-match org-deadline-regexp tmp)
  24195. scheduledp (string-match org-scheduled-regexp tmp)
  24196. ;; donep (org-entry-is-done-p)
  24197. ))
  24198. (if (or (string-match org-tr-regexp hd)
  24199. (string-match org-ts-regexp hd))
  24200. (setq hd (replace-match "" t t hd)))
  24201. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24202. (setq rrule
  24203. (concat "\nRRULE:FREQ="
  24204. (cdr (assoc
  24205. (match-string 2 ts)
  24206. '(("d" . "DAILY")("w" . "WEEKLY")
  24207. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24208. ";INTERVAL=" (match-string 1 ts)))
  24209. (setq rrule ""))
  24210. (setq summary (or summary hd))
  24211. (if (string-match org-bracket-link-regexp summary)
  24212. (setq summary
  24213. (replace-match (if (match-end 3)
  24214. (match-string 3 summary)
  24215. (match-string 1 summary))
  24216. t t summary)))
  24217. (if deadlinep (setq summary (concat "DL: " summary)))
  24218. (if scheduledp (setq summary (concat "S: " summary)))
  24219. (if (string-match "\\`<%%" ts)
  24220. (with-current-buffer sexp-buffer
  24221. (insert (substring ts 1 -1) " " summary "\n"))
  24222. (princ (format "BEGIN:VEVENT
  24223. %s
  24224. %s%s
  24225. SUMMARY:%s%s%s
  24226. CATEGORIES:%s
  24227. END:VEVENT\n"
  24228. (org-ical-ts-to-string ts "DTSTART")
  24229. (org-ical-ts-to-string ts2 "DTEND" inc)
  24230. rrule summary
  24231. (if (and desc (string-match "\\S-" desc))
  24232. (concat "\nDESCRIPTION: " desc) "")
  24233. (if (and location (string-match "\\S-" location))
  24234. (concat "\nLOCATION: " location) "")
  24235. category)))))
  24236. (when (and org-icalendar-include-sexps
  24237. (condition-case nil (require 'icalendar) (error nil))
  24238. (fboundp 'icalendar-export-region))
  24239. ;; Get all the literal sexps
  24240. (goto-char (point-min))
  24241. (while (re-search-forward "^&?%%(" nil t)
  24242. (catch :skip
  24243. (org-agenda-skip)
  24244. (setq b (match-beginning 0))
  24245. (goto-char (1- (match-end 0)))
  24246. (forward-sexp 1)
  24247. (end-of-line 1)
  24248. (setq sexp (buffer-substring b (point)))
  24249. (with-current-buffer sexp-buffer
  24250. (insert sexp "\n"))
  24251. (princ (org-diary-to-ical-string sexp-buffer)))))
  24252. (when org-icalendar-include-todo
  24253. (goto-char (point-min))
  24254. (while (re-search-forward org-todo-line-regexp nil t)
  24255. (catch :skip
  24256. (org-agenda-skip)
  24257. (setq state (match-string 2))
  24258. (setq status (if (member state org-done-keywords)
  24259. "COMPLETED" "NEEDS-ACTION"))
  24260. (when (and state
  24261. (or (not (member state org-done-keywords))
  24262. (eq org-icalendar-include-todo 'all))
  24263. (not (member org-archive-tag (org-get-tags-at)))
  24264. )
  24265. (setq hd (match-string 3)
  24266. summary (org-icalendar-cleanup-string
  24267. (org-entry-get nil "SUMMARY"))
  24268. desc (org-icalendar-cleanup-string
  24269. (or (org-entry-get nil "DESCRIPTION")
  24270. (and org-icalendar-include-body (org-get-entry)))
  24271. t org-icalendar-include-body)
  24272. location (org-icalendar-cleanup-string
  24273. (org-entry-get nil "LOCATION")))
  24274. (if (string-match org-bracket-link-regexp hd)
  24275. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24276. (match-string 1 hd))
  24277. t t hd)))
  24278. (if (string-match org-priority-regexp hd)
  24279. (setq pri (string-to-char (match-string 2 hd))
  24280. hd (concat (substring hd 0 (match-beginning 1))
  24281. (substring hd (match-end 1))))
  24282. (setq pri org-default-priority))
  24283. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24284. (- org-lowest-priority org-highest-priority))))))
  24285. (princ (format "BEGIN:VTODO
  24286. %s
  24287. SUMMARY:%s%s%s
  24288. CATEGORIES:%s
  24289. SEQUENCE:1
  24290. PRIORITY:%d
  24291. STATUS:%s
  24292. END:VTODO\n"
  24293. dts
  24294. (or summary hd)
  24295. (if (and location (string-match "\\S-" location))
  24296. (concat "\nLOCATION: " location) "")
  24297. (if (and desc (string-match "\\S-" desc))
  24298. (concat "\nDESCRIPTION: " desc) "")
  24299. category pri status)))))))))
  24300. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24301. "Take out stuff and quote what needs to be quoted.
  24302. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24303. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24304. characters."
  24305. (if (not s)
  24306. nil
  24307. (when is-body
  24308. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24309. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24310. (while (string-match re s) (setq s (replace-match "" t t s)))
  24311. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24312. (let ((start 0))
  24313. (while (string-match "\\([,;\\]\\)" s start)
  24314. (setq start (+ (match-beginning 0) 2)
  24315. s (replace-match "\\\\\\1" nil nil s))))
  24316. (when is-body
  24317. (while (string-match "[ \t]*\n[ \t]*" s)
  24318. (setq s (replace-match "\\n" t t s))))
  24319. (setq s (org-trim s))
  24320. (if is-body
  24321. (if maxlength
  24322. (if (and (numberp maxlength)
  24323. (> (length s) maxlength))
  24324. (setq s (substring s 0 maxlength)))))
  24325. s))
  24326. (defun org-get-entry ()
  24327. "Clean-up description string."
  24328. (save-excursion
  24329. (org-back-to-heading t)
  24330. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24331. (defun org-start-icalendar-file (name)
  24332. "Start an iCalendar file by inserting the header."
  24333. (let ((user user-full-name)
  24334. (name (or name "unknown"))
  24335. (timezone (cadr (current-time-zone))))
  24336. (princ
  24337. (format "BEGIN:VCALENDAR
  24338. VERSION:2.0
  24339. X-WR-CALNAME:%s
  24340. PRODID:-//%s//Emacs with Org-mode//EN
  24341. X-WR-TIMEZONE:%s
  24342. CALSCALE:GREGORIAN\n" name user timezone))))
  24343. (defun org-finish-icalendar-file ()
  24344. "Finish an iCalendar file by inserting the END statement."
  24345. (princ "END:VCALENDAR\n"))
  24346. (defun org-ical-ts-to-string (s keyword &optional inc)
  24347. "Take a time string S and convert it to iCalendar format.
  24348. KEYWORD is added in front, to make a complete line like DTSTART....
  24349. When INC is non-nil, increase the hour by two (if time string contains
  24350. a time), or the day by one (if it does not contain a time)."
  24351. (let ((t1 (org-parse-time-string s 'nodefault))
  24352. t2 fmt have-time time)
  24353. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24354. (setq t2 t1 have-time t)
  24355. (setq t2 (org-parse-time-string s)))
  24356. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24357. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24358. (when inc
  24359. (if have-time
  24360. (if org-agenda-default-appointment-duration
  24361. (setq mi (+ org-agenda-default-appointment-duration mi))
  24362. (setq h (+ 2 h)))
  24363. (setq d (1+ d))))
  24364. (setq time (encode-time s mi h d m y)))
  24365. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24366. (concat keyword (format-time-string fmt time))))
  24367. ;;; XOXO export
  24368. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24369. (with-current-buffer buffer
  24370. (apply 'insert output)))
  24371. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24372. (defun org-export-as-xoxo (&optional buffer)
  24373. "Export the org buffer as XOXO.
  24374. The XOXO buffer is named *xoxo-<source buffer name>*"
  24375. (interactive (list (current-buffer)))
  24376. ;; A quickie abstraction
  24377. ;; Output everything as XOXO
  24378. (with-current-buffer (get-buffer buffer)
  24379. (let* ((pos (point))
  24380. (opt-plist (org-combine-plists (org-default-export-plist)
  24381. (org-infile-export-plist)))
  24382. (filename (concat (file-name-as-directory
  24383. (org-export-directory :xoxo opt-plist))
  24384. (file-name-sans-extension
  24385. (file-name-nondirectory buffer-file-name))
  24386. ".html"))
  24387. (out (find-file-noselect filename))
  24388. (last-level 1)
  24389. (hanging-li nil))
  24390. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24391. ;; Check the output buffer is empty.
  24392. (with-current-buffer out (erase-buffer))
  24393. ;; Kick off the output
  24394. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24395. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24396. (let* ((hd (match-string-no-properties 1))
  24397. (level (length hd))
  24398. (text (concat
  24399. (match-string-no-properties 2)
  24400. (save-excursion
  24401. (goto-char (match-end 0))
  24402. (let ((str ""))
  24403. (catch 'loop
  24404. (while 't
  24405. (forward-line)
  24406. (if (looking-at "^[ \t]\\(.*\\)")
  24407. (setq str (concat str (match-string-no-properties 1)))
  24408. (throw 'loop str)))))))))
  24409. ;; Handle level rendering
  24410. (cond
  24411. ((> level last-level)
  24412. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24413. ((< level last-level)
  24414. (dotimes (- (- last-level level) 1)
  24415. (if hanging-li
  24416. (org-export-as-xoxo-insert-into out "</li>\n"))
  24417. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24418. (when hanging-li
  24419. (org-export-as-xoxo-insert-into out "</li>\n")
  24420. (setq hanging-li nil)))
  24421. ((equal level last-level)
  24422. (if hanging-li
  24423. (org-export-as-xoxo-insert-into out "</li>\n")))
  24424. )
  24425. (setq last-level level)
  24426. ;; And output the new li
  24427. (setq hanging-li 't)
  24428. (if (equal ?+ (elt text 0))
  24429. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24430. (org-export-as-xoxo-insert-into out "<li>" text))))
  24431. ;; Finally finish off the ol
  24432. (dotimes (- last-level 1)
  24433. (if hanging-li
  24434. (org-export-as-xoxo-insert-into out "</li>\n"))
  24435. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24436. (goto-char pos)
  24437. ;; Finish the buffer off and clean it up.
  24438. (switch-to-buffer-other-window out)
  24439. (indent-region (point-min) (point-max) nil)
  24440. (save-buffer)
  24441. (goto-char (point-min))
  24442. )))
  24443. ;;;; Key bindings
  24444. ;; Make `C-c C-x' a prefix key
  24445. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24446. ;; TAB key with modifiers
  24447. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24448. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24449. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24450. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24451. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24452. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24453. ;; The following line is necessary under Suse GNU/Linux
  24454. (unless (featurep 'xemacs)
  24455. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24456. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24457. (define-key org-mode-map [backtab] 'org-shifttab)
  24458. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24459. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24460. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24461. ;; Cursor keys with modifiers
  24462. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24463. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24464. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24465. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24466. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24467. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24468. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24469. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24470. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24471. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24472. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24473. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24474. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24475. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24476. ;;; Extra keys for tty access.
  24477. ;; We only set them when really needed because otherwise the
  24478. ;; menus don't show the simple keys
  24479. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24480. (not window-system))
  24481. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24482. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24483. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24484. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24485. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24486. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24487. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24488. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24489. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24490. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24491. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24492. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24493. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24494. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24495. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24496. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24497. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24498. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24499. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24500. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24501. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24502. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24503. ;; All the other keys
  24504. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24505. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24506. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24507. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24508. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24509. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24510. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24511. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24512. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24513. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24514. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24515. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24516. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24517. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24518. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24519. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24520. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24521. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24522. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24523. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24524. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24525. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24526. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24527. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24528. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24529. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24530. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24531. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24532. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24533. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24534. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24535. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24536. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24537. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24538. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24539. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24540. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24541. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24542. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24543. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24544. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24545. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24546. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24547. (org-defkey org-mode-map "\C-m" 'org-return)
  24548. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24549. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24550. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24551. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24552. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24553. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24554. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24555. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24556. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24557. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24558. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24559. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24560. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24561. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24562. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24563. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24564. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24565. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24566. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24567. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24568. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24569. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24570. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24571. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24572. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24573. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24574. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24575. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24576. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24577. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24578. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24579. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24580. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24581. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24582. (when (featurep 'xemacs)
  24583. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24584. (defsubst org-table-p () (org-at-table-p))
  24585. (defun org-self-insert-command (N)
  24586. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24587. If the cursor is in a table looking at whitespace, the whitespace is
  24588. overwritten, and the table is not marked as requiring realignment."
  24589. (interactive "p")
  24590. (if (and (org-table-p)
  24591. (progn
  24592. ;; check if we blank the field, and if that triggers align
  24593. (and org-table-auto-blank-field
  24594. (member last-command
  24595. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24596. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24597. ;; got extra space, this field does not determine column width
  24598. (let (org-table-may-need-update) (org-table-blank-field))
  24599. ;; no extra space, this field may determine column width
  24600. (org-table-blank-field)))
  24601. t)
  24602. (eq N 1)
  24603. (looking-at "[^|\n]* |"))
  24604. (let (org-table-may-need-update)
  24605. (goto-char (1- (match-end 0)))
  24606. (delete-backward-char 1)
  24607. (goto-char (match-beginning 0))
  24608. (self-insert-command N))
  24609. (setq org-table-may-need-update t)
  24610. (self-insert-command N)
  24611. (org-fix-tags-on-the-fly)))
  24612. (defun org-fix-tags-on-the-fly ()
  24613. (when (and (equal (char-after (point-at-bol)) ?*)
  24614. (org-on-heading-p))
  24615. (org-align-tags-here org-tags-column)))
  24616. (defun org-delete-backward-char (N)
  24617. "Like `delete-backward-char', insert whitespace at field end in tables.
  24618. When deleting backwards, in tables this function will insert whitespace in
  24619. front of the next \"|\" separator, to keep the table aligned. The table will
  24620. still be marked for re-alignment if the field did fill the entire column,
  24621. because, in this case the deletion might narrow the column."
  24622. (interactive "p")
  24623. (if (and (org-table-p)
  24624. (eq N 1)
  24625. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24626. (looking-at ".*?|"))
  24627. (let ((pos (point))
  24628. (noalign (looking-at "[^|\n\r]* |"))
  24629. (c org-table-may-need-update))
  24630. (backward-delete-char N)
  24631. (skip-chars-forward "^|")
  24632. (insert " ")
  24633. (goto-char (1- pos))
  24634. ;; noalign: if there were two spaces at the end, this field
  24635. ;; does not determine the width of the column.
  24636. (if noalign (setq org-table-may-need-update c)))
  24637. (backward-delete-char N)
  24638. (org-fix-tags-on-the-fly)))
  24639. (defun org-delete-char (N)
  24640. "Like `delete-char', but insert whitespace at field end in tables.
  24641. When deleting characters, in tables this function will insert whitespace in
  24642. front of the next \"|\" separator, to keep the table aligned. The table will
  24643. still be marked for re-alignment if the field did fill the entire column,
  24644. because, in this case the deletion might narrow the column."
  24645. (interactive "p")
  24646. (if (and (org-table-p)
  24647. (not (bolp))
  24648. (not (= (char-after) ?|))
  24649. (eq N 1))
  24650. (if (looking-at ".*?|")
  24651. (let ((pos (point))
  24652. (noalign (looking-at "[^|\n\r]* |"))
  24653. (c org-table-may-need-update))
  24654. (replace-match (concat
  24655. (substring (match-string 0) 1 -1)
  24656. " |"))
  24657. (goto-char pos)
  24658. ;; noalign: if there were two spaces at the end, this field
  24659. ;; does not determine the width of the column.
  24660. (if noalign (setq org-table-may-need-update c)))
  24661. (delete-char N))
  24662. (delete-char N)
  24663. (org-fix-tags-on-the-fly)))
  24664. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24665. (put 'org-self-insert-command 'delete-selection t)
  24666. (put 'orgtbl-self-insert-command 'delete-selection t)
  24667. (put 'org-delete-char 'delete-selection 'supersede)
  24668. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24669. ;; Make `flyspell-mode' delay after some commands
  24670. (put 'org-self-insert-command 'flyspell-delayed t)
  24671. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24672. (put 'org-delete-char 'flyspell-delayed t)
  24673. (put 'org-delete-backward-char 'flyspell-delayed t)
  24674. ;; Make pabbrev-mode expand after org-mode commands
  24675. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24676. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24677. ;; How to do this: Measure non-white length of current string
  24678. ;; If equal to column width, we should realign.
  24679. (defun org-remap (map &rest commands)
  24680. "In MAP, remap the functions given in COMMANDS.
  24681. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24682. (let (new old)
  24683. (while commands
  24684. (setq old (pop commands) new (pop commands))
  24685. (if (fboundp 'command-remapping)
  24686. (org-defkey map (vector 'remap old) new)
  24687. (substitute-key-definition old new map global-map)))))
  24688. (when (eq org-enable-table-editor 'optimized)
  24689. ;; If the user wants maximum table support, we need to hijack
  24690. ;; some standard editing functions
  24691. (org-remap org-mode-map
  24692. 'self-insert-command 'org-self-insert-command
  24693. 'delete-char 'org-delete-char
  24694. 'delete-backward-char 'org-delete-backward-char)
  24695. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24696. (defun org-shiftcursor-error ()
  24697. "Throw an error because Shift-Cursor command was applied in wrong context."
  24698. (error "This command is active in special context like tables, headlines or timestamps"))
  24699. (defun org-shifttab (&optional arg)
  24700. "Global visibility cycling or move to previous table field.
  24701. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24702. on context.
  24703. See the individual commands for more information."
  24704. (interactive "P")
  24705. (cond
  24706. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24707. (arg (message "Content view to level: ")
  24708. (org-content (prefix-numeric-value arg))
  24709. (setq org-cycle-global-status 'overview))
  24710. (t (call-interactively 'org-global-cycle))))
  24711. (defun org-shiftmetaleft ()
  24712. "Promote subtree or delete table column.
  24713. Calls `org-promote-subtree', `org-outdent-item',
  24714. or `org-table-delete-column', depending on context.
  24715. See the individual commands for more information."
  24716. (interactive)
  24717. (cond
  24718. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24719. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24720. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24721. (t (org-shiftcursor-error))))
  24722. (defun org-shiftmetaright ()
  24723. "Demote subtree or insert table column.
  24724. Calls `org-demote-subtree', `org-indent-item',
  24725. or `org-table-insert-column', depending on context.
  24726. See the individual commands for more information."
  24727. (interactive)
  24728. (cond
  24729. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24730. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24731. ((org-at-item-p) (call-interactively 'org-indent-item))
  24732. (t (org-shiftcursor-error))))
  24733. (defun org-shiftmetaup (&optional arg)
  24734. "Move subtree up or kill table row.
  24735. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24736. `org-move-item-up' depending on context. See the individual commands
  24737. for more information."
  24738. (interactive "P")
  24739. (cond
  24740. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24741. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24742. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24743. (t (org-shiftcursor-error))))
  24744. (defun org-shiftmetadown (&optional arg)
  24745. "Move subtree down or insert table row.
  24746. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24747. `org-move-item-down', depending on context. See the individual
  24748. commands for more information."
  24749. (interactive "P")
  24750. (cond
  24751. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24752. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24753. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24754. (t (org-shiftcursor-error))))
  24755. (defun org-metaleft (&optional arg)
  24756. "Promote heading or move table column to left.
  24757. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24758. With no specific context, calls the Emacs default `backward-word'.
  24759. See the individual commands for more information."
  24760. (interactive "P")
  24761. (cond
  24762. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24763. ((or (org-on-heading-p) (org-region-active-p))
  24764. (call-interactively 'org-do-promote))
  24765. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24766. (t (call-interactively 'backward-word))))
  24767. (defun org-metaright (&optional arg)
  24768. "Demote subtree or move table column to right.
  24769. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24770. With no specific context, calls the Emacs default `forward-word'.
  24771. See the individual commands for more information."
  24772. (interactive "P")
  24773. (cond
  24774. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24775. ((or (org-on-heading-p) (org-region-active-p))
  24776. (call-interactively 'org-do-demote))
  24777. ((org-at-item-p) (call-interactively 'org-indent-item))
  24778. (t (call-interactively 'forward-word))))
  24779. (defun org-metaup (&optional arg)
  24780. "Move subtree up or move table row up.
  24781. Calls `org-move-subtree-up' or `org-table-move-row' or
  24782. `org-move-item-up', depending on context. See the individual commands
  24783. for more information."
  24784. (interactive "P")
  24785. (cond
  24786. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24787. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24788. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24789. (t (transpose-lines 1) (beginning-of-line -1))))
  24790. (defun org-metadown (&optional arg)
  24791. "Move subtree down or move table row down.
  24792. Calls `org-move-subtree-down' or `org-table-move-row' or
  24793. `org-move-item-down', depending on context. See the individual
  24794. commands for more information."
  24795. (interactive "P")
  24796. (cond
  24797. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24798. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24799. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24800. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24801. (defun org-shiftup (&optional arg)
  24802. "Increase item in timestamp or increase priority of current headline.
  24803. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24804. depending on context. See the individual commands for more information."
  24805. (interactive "P")
  24806. (cond
  24807. ((org-at-timestamp-p t)
  24808. (call-interactively (if org-edit-timestamp-down-means-later
  24809. 'org-timestamp-down 'org-timestamp-up)))
  24810. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24811. ((org-at-item-p) (call-interactively 'org-previous-item))
  24812. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24813. (defun org-shiftdown (&optional arg)
  24814. "Decrease item in timestamp or decrease priority of current headline.
  24815. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24816. depending on context. See the individual commands for more information."
  24817. (interactive "P")
  24818. (cond
  24819. ((org-at-timestamp-p t)
  24820. (call-interactively (if org-edit-timestamp-down-means-later
  24821. 'org-timestamp-up 'org-timestamp-down)))
  24822. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24823. (t (call-interactively 'org-next-item))))
  24824. (defun org-shiftright ()
  24825. "Next TODO keyword or timestamp one day later, depending on context."
  24826. (interactive)
  24827. (cond
  24828. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24829. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24830. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24831. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24832. (t (org-shiftcursor-error))))
  24833. (defun org-shiftleft ()
  24834. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24835. (interactive)
  24836. (cond
  24837. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24838. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24839. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24840. ((org-at-property-p)
  24841. (call-interactively 'org-property-previous-allowed-value))
  24842. (t (org-shiftcursor-error))))
  24843. (defun org-shiftcontrolright ()
  24844. "Switch to next TODO set."
  24845. (interactive)
  24846. (cond
  24847. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24848. (t (org-shiftcursor-error))))
  24849. (defun org-shiftcontrolleft ()
  24850. "Switch to previous TODO set."
  24851. (interactive)
  24852. (cond
  24853. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24854. (t (org-shiftcursor-error))))
  24855. (defun org-ctrl-c-ret ()
  24856. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24857. (interactive)
  24858. (cond
  24859. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24860. (t (call-interactively 'org-insert-heading))))
  24861. (defun org-copy-special ()
  24862. "Copy region in table or copy current subtree.
  24863. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24864. See the individual commands for more information."
  24865. (interactive)
  24866. (call-interactively
  24867. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24868. (defun org-cut-special ()
  24869. "Cut region in table or cut current subtree.
  24870. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24871. See the individual commands for more information."
  24872. (interactive)
  24873. (call-interactively
  24874. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24875. (defun org-paste-special (arg)
  24876. "Paste rectangular region into table, or past subtree relative to level.
  24877. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24878. See the individual commands for more information."
  24879. (interactive "P")
  24880. (if (org-at-table-p)
  24881. (org-table-paste-rectangle)
  24882. (org-paste-subtree arg)))
  24883. (defun org-ctrl-c-ctrl-c (&optional arg)
  24884. "Set tags in headline, or update according to changed information at point.
  24885. This command does many different things, depending on context:
  24886. - If the cursor is in a headline, prompt for tags and insert them
  24887. into the current line, aligned to `org-tags-column'. When called
  24888. with prefix arg, realign all tags in the current buffer.
  24889. - If the cursor is in one of the special #+KEYWORD lines, this
  24890. triggers scanning the buffer for these lines and updating the
  24891. information.
  24892. - If the cursor is inside a table, realign the table. This command
  24893. works even if the automatic table editor has been turned off.
  24894. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24895. the entire table.
  24896. - If the cursor is a the beginning of a dynamic block, update it.
  24897. - If the cursor is inside a table created by the table.el package,
  24898. activate that table.
  24899. - If the current buffer is a remember buffer, close note and file it.
  24900. with a prefix argument, file it without further interaction to the default
  24901. location.
  24902. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24903. links in this buffer.
  24904. - If the cursor is on a numbered item in a plain list, renumber the
  24905. ordered list.
  24906. - If the cursor is on a checkbox, toggle it."
  24907. (interactive "P")
  24908. (let ((org-enable-table-editor t))
  24909. (cond
  24910. ((or org-clock-overlays
  24911. org-occur-highlights
  24912. org-latex-fragment-image-overlays)
  24913. (org-remove-clock-overlays)
  24914. (org-remove-occur-highlights)
  24915. (org-remove-latex-fragment-image-overlays)
  24916. (message "Temporary highlights/overlays removed from current buffer"))
  24917. ((and (local-variable-p 'org-finish-function (current-buffer))
  24918. (fboundp org-finish-function))
  24919. (funcall org-finish-function))
  24920. ((org-at-property-p)
  24921. (call-interactively 'org-property-action))
  24922. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  24923. ((org-on-heading-p) (call-interactively 'org-set-tags))
  24924. ((org-at-table.el-p)
  24925. (require 'table)
  24926. (beginning-of-line 1)
  24927. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  24928. (call-interactively 'table-recognize-table))
  24929. ((org-at-table-p)
  24930. (org-table-maybe-eval-formula)
  24931. (if arg
  24932. (call-interactively 'org-table-recalculate)
  24933. (org-table-maybe-recalculate-line))
  24934. (call-interactively 'org-table-align))
  24935. ((org-at-item-checkbox-p)
  24936. (call-interactively 'org-toggle-checkbox))
  24937. ((org-at-item-p)
  24938. (call-interactively 'org-maybe-renumber-ordered-list))
  24939. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  24940. ;; Dynamic block
  24941. (beginning-of-line 1)
  24942. (org-update-dblock))
  24943. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  24944. (cond
  24945. ((equal (match-string 1) "TBLFM")
  24946. ;; Recalculate the table before this line
  24947. (save-excursion
  24948. (beginning-of-line 1)
  24949. (skip-chars-backward " \r\n\t")
  24950. (if (org-at-table-p)
  24951. (org-call-with-arg 'org-table-recalculate t))))
  24952. (t
  24953. (call-interactively 'org-mode-restart))))
  24954. (t (error "C-c C-c can do nothing useful at this location.")))))
  24955. (defun org-mode-restart ()
  24956. "Restart Org-mode, to scan again for special lines.
  24957. Also updates the keyword regular expressions."
  24958. (interactive)
  24959. (let ((org-inhibit-startup t)) (org-mode))
  24960. (message "Org-mode restarted to refresh keyword and special line setup"))
  24961. (defun org-kill-note-or-show-branches ()
  24962. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  24963. (interactive)
  24964. (if (not org-finish-function)
  24965. (call-interactively 'show-branches)
  24966. (let ((org-note-abort t))
  24967. (funcall org-finish-function))))
  24968. (defun org-return (&optional indent)
  24969. "Goto next table row or insert a newline.
  24970. Calls `org-table-next-row' or `newline', depending on context.
  24971. See the individual commands for more information."
  24972. (interactive)
  24973. (cond
  24974. ((bobp) (if indent (newline-and-indent) (newline)))
  24975. ((and (org-at-heading-p)
  24976. (looking-at
  24977. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  24978. (org-show-entry)
  24979. (end-of-line 1)
  24980. (newline))
  24981. ((org-at-table-p)
  24982. (org-table-justify-field-maybe)
  24983. (call-interactively 'org-table-next-row))
  24984. (t (if indent (newline-and-indent) (newline)))))
  24985. (defun org-return-indent ()
  24986. (interactive)
  24987. "Goto next table row or insert a newline and indent.
  24988. Calls `org-table-next-row' or `newline-and-indent', depending on
  24989. context. See the individual commands for more information."
  24990. (org-return t))
  24991. (defun org-ctrl-c-minus ()
  24992. "Insert separator line in table or modify bullet type in list.
  24993. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  24994. depending on context."
  24995. (interactive)
  24996. (cond
  24997. ((org-at-table-p)
  24998. (call-interactively 'org-table-insert-hline))
  24999. ((org-on-heading-p)
  25000. ;; Convert to item
  25001. (save-excursion
  25002. (beginning-of-line 1)
  25003. (if (looking-at "\\*+ ")
  25004. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25005. ((org-in-item-p)
  25006. (call-interactively 'org-cycle-list-bullet))
  25007. (t (error "`C-c -' does have no function here."))))
  25008. (defun org-meta-return (&optional arg)
  25009. "Insert a new heading or wrap a region in a table.
  25010. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25011. See the individual commands for more information."
  25012. (interactive "P")
  25013. (cond
  25014. ((org-at-table-p)
  25015. (call-interactively 'org-table-wrap-region))
  25016. (t (call-interactively 'org-insert-heading))))
  25017. ;;; Menu entries
  25018. ;; Define the Org-mode menus
  25019. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25020. '("Tbl"
  25021. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25022. ["Next Field" org-cycle (org-at-table-p)]
  25023. ["Previous Field" org-shifttab (org-at-table-p)]
  25024. ["Next Row" org-return (org-at-table-p)]
  25025. "--"
  25026. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25027. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25028. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25029. "--"
  25030. ("Column"
  25031. ["Move Column Left" org-metaleft (org-at-table-p)]
  25032. ["Move Column Right" org-metaright (org-at-table-p)]
  25033. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25034. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25035. ("Row"
  25036. ["Move Row Up" org-metaup (org-at-table-p)]
  25037. ["Move Row Down" org-metadown (org-at-table-p)]
  25038. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25039. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25040. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25041. "--"
  25042. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25043. ("Rectangle"
  25044. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25045. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25046. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25047. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25048. "--"
  25049. ("Calculate"
  25050. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25051. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25052. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25053. "--"
  25054. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25055. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25056. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25057. "--"
  25058. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25059. "--"
  25060. ["Sum Column/Rectangle" org-table-sum
  25061. (or (org-at-table-p) (org-region-active-p))]
  25062. ["Which Column?" org-table-current-column (org-at-table-p)])
  25063. ["Debug Formulas"
  25064. org-table-toggle-formula-debugger
  25065. :style toggle :selected org-table-formula-debug]
  25066. ["Show Col/Row Numbers"
  25067. org-table-toggle-coordinate-overlays
  25068. :style toggle :selected org-table-overlay-coordinates]
  25069. "--"
  25070. ["Create" org-table-create (and (not (org-at-table-p))
  25071. org-enable-table-editor)]
  25072. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25073. ["Import from File" org-table-import (not (org-at-table-p))]
  25074. ["Export to File" org-table-export (org-at-table-p)]
  25075. "--"
  25076. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25077. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25078. '("Org"
  25079. ("Show/Hide"
  25080. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25081. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25082. ["Sparse Tree" org-occur t]
  25083. ["Reveal Context" org-reveal t]
  25084. ["Show All" show-all t]
  25085. "--"
  25086. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25087. "--"
  25088. ["New Heading" org-insert-heading t]
  25089. ("Navigate Headings"
  25090. ["Up" outline-up-heading t]
  25091. ["Next" outline-next-visible-heading t]
  25092. ["Previous" outline-previous-visible-heading t]
  25093. ["Next Same Level" outline-forward-same-level t]
  25094. ["Previous Same Level" outline-backward-same-level t]
  25095. "--"
  25096. ["Jump" org-goto t])
  25097. ("Edit Structure"
  25098. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25099. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25100. "--"
  25101. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25102. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25103. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25104. "--"
  25105. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25106. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25107. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25108. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25109. "--"
  25110. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25111. "--"
  25112. ["Convert to odd levels" org-convert-to-odd-levels t]
  25113. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25114. ("Editing"
  25115. ["Emphasis..." org-emphasize t])
  25116. ("Archive"
  25117. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25118. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25119. ; :active t :keys "C-u C-c C-x C-a"]
  25120. ["Sparse trees open ARCHIVE trees"
  25121. (setq org-sparse-tree-open-archived-trees
  25122. (not org-sparse-tree-open-archived-trees))
  25123. :style toggle :selected org-sparse-tree-open-archived-trees]
  25124. ["Cycling opens ARCHIVE trees"
  25125. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25126. :style toggle :selected org-cycle-open-archived-trees]
  25127. ["Agenda includes ARCHIVE trees"
  25128. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25129. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25130. "--"
  25131. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25132. ; ["Check and Move Children" (org-archive-subtree '(4))
  25133. ; :active t :keys "C-u C-c C-x C-s"]
  25134. )
  25135. "--"
  25136. ("TODO Lists"
  25137. ["TODO/DONE/-" org-todo t]
  25138. ("Select keyword"
  25139. ["Next keyword" org-shiftright (org-on-heading-p)]
  25140. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25141. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25142. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25143. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25144. ["Show TODO Tree" org-show-todo-tree t]
  25145. ["Global TODO list" org-todo-list t]
  25146. "--"
  25147. ["Set Priority" org-priority t]
  25148. ["Priority Up" org-shiftup t]
  25149. ["Priority Down" org-shiftdown t])
  25150. ("TAGS and Properties"
  25151. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25152. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25153. "--"
  25154. ["Set property" 'org-set-property t]
  25155. ["Column view of properties" org-columns t]
  25156. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25157. ("Dates and Scheduling"
  25158. ["Timestamp" org-time-stamp t]
  25159. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25160. ("Change Date"
  25161. ["1 Day Later" org-shiftright t]
  25162. ["1 Day Earlier" org-shiftleft t]
  25163. ["1 ... Later" org-shiftup t]
  25164. ["1 ... Earlier" org-shiftdown t])
  25165. ["Compute Time Range" org-evaluate-time-range t]
  25166. ["Schedule Item" org-schedule t]
  25167. ["Deadline" org-deadline t]
  25168. "--"
  25169. ["Custom time format" org-toggle-time-stamp-overlays
  25170. :style radio :selected org-display-custom-times]
  25171. "--"
  25172. ["Goto Calendar" org-goto-calendar t]
  25173. ["Date from Calendar" org-date-from-calendar t])
  25174. ("Logging work"
  25175. ["Clock in" org-clock-in t]
  25176. ["Clock out" org-clock-out t]
  25177. ["Clock cancel" org-clock-cancel t]
  25178. ["Goto running clock" org-clock-goto t]
  25179. ["Display times" org-clock-display t]
  25180. ["Create clock table" org-clock-report t]
  25181. "--"
  25182. ["Record DONE time"
  25183. (progn (setq org-log-done (not org-log-done))
  25184. (message "Switching to %s will %s record a timestamp"
  25185. (car org-done-keywords)
  25186. (if org-log-done "automatically" "not")))
  25187. :style toggle :selected org-log-done])
  25188. "--"
  25189. ["Agenda Command..." org-agenda t]
  25190. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25191. ("File List for Agenda")
  25192. ("Special views current file"
  25193. ["TODO Tree" org-show-todo-tree t]
  25194. ["Check Deadlines" org-check-deadlines t]
  25195. ["Timeline" org-timeline t]
  25196. ["Tags Tree" org-tags-sparse-tree t])
  25197. "--"
  25198. ("Hyperlinks"
  25199. ["Store Link (Global)" org-store-link t]
  25200. ["Insert Link" org-insert-link t]
  25201. ["Follow Link" org-open-at-point t]
  25202. "--"
  25203. ["Next link" org-next-link t]
  25204. ["Previous link" org-previous-link t]
  25205. "--"
  25206. ["Descriptive Links"
  25207. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25208. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25209. ["Literal Links"
  25210. (progn
  25211. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25212. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25213. "--"
  25214. ["Export/Publish..." org-export t]
  25215. ("LaTeX"
  25216. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25217. :selected org-cdlatex-mode]
  25218. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25219. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25220. ["Modify math symbol" org-cdlatex-math-modify
  25221. (org-inside-LaTeX-fragment-p)]
  25222. ["Export LaTeX fragments as images"
  25223. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25224. :style toggle :selected org-export-with-LaTeX-fragments])
  25225. "--"
  25226. ("Documentation"
  25227. ["Show Version" org-version t]
  25228. ["Info Documentation" org-info t])
  25229. ("Customize"
  25230. ["Browse Org Group" org-customize t]
  25231. "--"
  25232. ["Expand This Menu" org-create-customize-menu
  25233. (fboundp 'customize-menu-create)])
  25234. "--"
  25235. ["Refresh setup" org-mode-restart t]
  25236. ))
  25237. (defun org-info (&optional node)
  25238. "Read documentation for Org-mode in the info system.
  25239. With optional NODE, go directly to that node."
  25240. (interactive)
  25241. (require 'info)
  25242. (Info-goto-node (format "(org)%s" (or node ""))))
  25243. (defun org-install-agenda-files-menu ()
  25244. (let ((bl (buffer-list)))
  25245. (save-excursion
  25246. (while bl
  25247. (set-buffer (pop bl))
  25248. (if (org-mode-p) (setq bl nil)))
  25249. (when (org-mode-p)
  25250. (easy-menu-change
  25251. '("Org") "File List for Agenda"
  25252. (append
  25253. (list
  25254. ["Edit File List" (org-edit-agenda-file-list) t]
  25255. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25256. ["Remove Current File from List" org-remove-file t]
  25257. ["Cycle through agenda files" org-cycle-agenda-files t]
  25258. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25259. "--")
  25260. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25261. ;;;; Documentation
  25262. (defun org-customize ()
  25263. "Call the customize function with org as argument."
  25264. (interactive)
  25265. (customize-browse 'org))
  25266. (defun org-create-customize-menu ()
  25267. "Create a full customization menu for Org-mode, insert it into the menu."
  25268. (interactive)
  25269. (if (fboundp 'customize-menu-create)
  25270. (progn
  25271. (easy-menu-change
  25272. '("Org") "Customize"
  25273. `(["Browse Org group" org-customize t]
  25274. "--"
  25275. ,(customize-menu-create 'org)
  25276. ["Set" Custom-set t]
  25277. ["Save" Custom-save t]
  25278. ["Reset to Current" Custom-reset-current t]
  25279. ["Reset to Saved" Custom-reset-saved t]
  25280. ["Reset to Standard Settings" Custom-reset-standard t]))
  25281. (message "\"Org\"-menu now contains full customization menu"))
  25282. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25283. ;;;; Miscellaneous stuff
  25284. ;;; Generally useful functions
  25285. (defun org-context ()
  25286. "Return a list of contexts of the current cursor position.
  25287. If several contexts apply, all are returned.
  25288. Each context entry is a list with a symbol naming the context, and
  25289. two positions indicating start and end of the context. Possible
  25290. contexts are:
  25291. :headline anywhere in a headline
  25292. :headline-stars on the leading stars in a headline
  25293. :todo-keyword on a TODO keyword (including DONE) in a headline
  25294. :tags on the TAGS in a headline
  25295. :priority on the priority cookie in a headline
  25296. :item on the first line of a plain list item
  25297. :item-bullet on the bullet/number of a plain list item
  25298. :checkbox on the checkbox in a plain list item
  25299. :table in an org-mode table
  25300. :table-special on a special filed in a table
  25301. :table-table in a table.el table
  25302. :link on a hyperlink
  25303. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25304. :target on a <<target>>
  25305. :radio-target on a <<<radio-target>>>
  25306. :latex-fragment on a LaTeX fragment
  25307. :latex-preview on a LaTeX fragment with overlayed preview image
  25308. This function expects the position to be visible because it uses font-lock
  25309. faces as a help to recognize the following contexts: :table-special, :link,
  25310. and :keyword."
  25311. (let* ((f (get-text-property (point) 'face))
  25312. (faces (if (listp f) f (list f)))
  25313. (p (point)) clist o)
  25314. ;; First the large context
  25315. (cond
  25316. ((org-on-heading-p t)
  25317. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25318. (when (progn
  25319. (beginning-of-line 1)
  25320. (looking-at org-todo-line-tags-regexp))
  25321. (push (org-point-in-group p 1 :headline-stars) clist)
  25322. (push (org-point-in-group p 2 :todo-keyword) clist)
  25323. (push (org-point-in-group p 4 :tags) clist))
  25324. (goto-char p)
  25325. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25326. (if (looking-at "\\[#[A-Z0-9]\\]")
  25327. (push (org-point-in-group p 0 :priority) clist)))
  25328. ((org-at-item-p)
  25329. (push (org-point-in-group p 2 :item-bullet) clist)
  25330. (push (list :item (point-at-bol)
  25331. (save-excursion (org-end-of-item) (point)))
  25332. clist)
  25333. (and (org-at-item-checkbox-p)
  25334. (push (org-point-in-group p 0 :checkbox) clist)))
  25335. ((org-at-table-p)
  25336. (push (list :table (org-table-begin) (org-table-end)) clist)
  25337. (if (memq 'org-formula faces)
  25338. (push (list :table-special
  25339. (previous-single-property-change p 'face)
  25340. (next-single-property-change p 'face)) clist)))
  25341. ((org-at-table-p 'any)
  25342. (push (list :table-table) clist)))
  25343. (goto-char p)
  25344. ;; Now the small context
  25345. (cond
  25346. ((org-at-timestamp-p)
  25347. (push (org-point-in-group p 0 :timestamp) clist))
  25348. ((memq 'org-link faces)
  25349. (push (list :link
  25350. (previous-single-property-change p 'face)
  25351. (next-single-property-change p 'face)) clist))
  25352. ((memq 'org-special-keyword faces)
  25353. (push (list :keyword
  25354. (previous-single-property-change p 'face)
  25355. (next-single-property-change p 'face)) clist))
  25356. ((org-on-target-p)
  25357. (push (org-point-in-group p 0 :target) clist)
  25358. (goto-char (1- (match-beginning 0)))
  25359. (if (looking-at org-radio-target-regexp)
  25360. (push (org-point-in-group p 0 :radio-target) clist))
  25361. (goto-char p))
  25362. ((setq o (car (delq nil
  25363. (mapcar
  25364. (lambda (x)
  25365. (if (memq x org-latex-fragment-image-overlays) x))
  25366. (org-overlays-at (point))))))
  25367. (push (list :latex-fragment
  25368. (org-overlay-start o) (org-overlay-end o)) clist)
  25369. (push (list :latex-preview
  25370. (org-overlay-start o) (org-overlay-end o)) clist))
  25371. ((org-inside-LaTeX-fragment-p)
  25372. ;; FIXME: positions wrong.
  25373. (push (list :latex-fragment (point) (point)) clist)))
  25374. (setq clist (nreverse (delq nil clist)))
  25375. clist))
  25376. ;; FIXME: Compare with at-regexp-p Do we need both?
  25377. (defun org-in-regexp (re &optional nlines visually)
  25378. "Check if point is inside a match of regexp.
  25379. Normally only the current line is checked, but you can include NLINES extra
  25380. lines both before and after point into the search.
  25381. If VISUALLY is set, require that the cursor is not after the match but
  25382. really on, so that the block visually is on the match."
  25383. (catch 'exit
  25384. (let ((pos (point))
  25385. (eol (point-at-eol (+ 1 (or nlines 0))))
  25386. (inc (if visually 1 0)))
  25387. (save-excursion
  25388. (beginning-of-line (- 1 (or nlines 0)))
  25389. (while (re-search-forward re eol t)
  25390. (if (and (<= (match-beginning 0) pos)
  25391. (>= (+ inc (match-end 0)) pos))
  25392. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25393. (defun org-at-regexp-p (regexp)
  25394. "Is point inside a match of REGEXP in the current line?"
  25395. (catch 'exit
  25396. (save-excursion
  25397. (let ((pos (point)) (end (point-at-eol)))
  25398. (beginning-of-line 1)
  25399. (while (re-search-forward regexp end t)
  25400. (if (and (<= (match-beginning 0) pos)
  25401. (>= (match-end 0) pos))
  25402. (throw 'exit t)))
  25403. nil))))
  25404. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25405. "Call `multi-occur' with buffers for all agenda files."
  25406. (interactive "sOrg-files matching: \np")
  25407. (let* ((files (org-agenda-files))
  25408. (tnames (mapcar 'file-truename files))
  25409. (extra org-agenda-multi-occur-extra-files)
  25410. f)
  25411. (while (setq f (pop extra))
  25412. (unless (member (file-truename f) tnames)
  25413. (add-to-list 'files f 'append)
  25414. (add-to-list 'tnames (file-truename f) 'append)))
  25415. (multi-occur
  25416. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25417. regexp)))
  25418. (if (boundp 'occur-mode-find-occurrence-hook)
  25419. ;; Emacs 23
  25420. (add-hook 'occur-mode-find-occurrence-hook
  25421. (lambda ()
  25422. (when (org-mode-p)
  25423. (org-reveal))))
  25424. ;; Emacs 22
  25425. (defadvice occur-mode-goto-occurrence
  25426. (after org-occur-reveal activate)
  25427. (and (org-mode-p) (org-reveal)))
  25428. (defadvice occur-mode-goto-occurrence-other-window
  25429. (after org-occur-reveal activate)
  25430. (and (org-mode-p) (org-reveal)))
  25431. (defadvice occur-mode-display-occurrence
  25432. (after org-occur-reveal activate)
  25433. (when (org-mode-p)
  25434. (let ((pos (occur-mode-find-occurrence)))
  25435. (with-current-buffer (marker-buffer pos)
  25436. (save-excursion
  25437. (goto-char pos)
  25438. (org-reveal)))))))
  25439. (defun org-uniquify (list)
  25440. "Remove duplicate elements from LIST."
  25441. (let (res)
  25442. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25443. res))
  25444. (defun org-delete-all (elts list)
  25445. "Remove all elements in ELTS from LIST."
  25446. (while elts
  25447. (setq list (delete (pop elts) list)))
  25448. list)
  25449. (defun org-back-over-empty-lines ()
  25450. "Move backwards over witespace, to the beginning of the first empty line.
  25451. Returns the number o empty lines passed."
  25452. (let ((pos (point)))
  25453. (skip-chars-backward " \t\n\r")
  25454. (beginning-of-line 2)
  25455. (goto-char (min (point) pos))
  25456. (count-lines (point) pos)))
  25457. (defun org-skip-whitespace ()
  25458. (skip-chars-forward " \t\n\r"))
  25459. (defun org-point-in-group (point group &optional context)
  25460. "Check if POINT is in match-group GROUP.
  25461. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25462. match. If the match group does ot exist or point is not inside it,
  25463. return nil."
  25464. (and (match-beginning group)
  25465. (>= point (match-beginning group))
  25466. (<= point (match-end group))
  25467. (if context
  25468. (list context (match-beginning group) (match-end group))
  25469. t)))
  25470. (defun org-switch-to-buffer-other-window (&rest args)
  25471. "Switch to buffer in a second window on the current frame.
  25472. In particular, do not allow pop-up frames."
  25473. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25474. special-display-function)
  25475. (apply 'switch-to-buffer-other-window args)))
  25476. (defun org-combine-plists (&rest plists)
  25477. "Create a single property list from all plists in PLISTS.
  25478. The process starts by copying the first list, and then setting properties
  25479. from the other lists. Settings in the last list are the most significant
  25480. ones and overrule settings in the other lists."
  25481. (let ((rtn (copy-sequence (pop plists)))
  25482. p v ls)
  25483. (while plists
  25484. (setq ls (pop plists))
  25485. (while ls
  25486. (setq p (pop ls) v (pop ls))
  25487. (setq rtn (plist-put rtn p v))))
  25488. rtn))
  25489. (defun org-move-line-down (arg)
  25490. "Move the current line down. With prefix argument, move it past ARG lines."
  25491. (interactive "p")
  25492. (let ((col (current-column))
  25493. beg end pos)
  25494. (beginning-of-line 1) (setq beg (point))
  25495. (beginning-of-line 2) (setq end (point))
  25496. (beginning-of-line (+ 1 arg))
  25497. (setq pos (move-marker (make-marker) (point)))
  25498. (insert (delete-and-extract-region beg end))
  25499. (goto-char pos)
  25500. (move-to-column col)))
  25501. (defun org-move-line-up (arg)
  25502. "Move the current line up. With prefix argument, move it past ARG lines."
  25503. (interactive "p")
  25504. (let ((col (current-column))
  25505. beg end pos)
  25506. (beginning-of-line 1) (setq beg (point))
  25507. (beginning-of-line 2) (setq end (point))
  25508. (beginning-of-line (- arg))
  25509. (setq pos (move-marker (make-marker) (point)))
  25510. (insert (delete-and-extract-region beg end))
  25511. (goto-char pos)
  25512. (move-to-column col)))
  25513. (defun org-replace-escapes (string table)
  25514. "Replace %-escapes in STRING with values in TABLE.
  25515. TABLE is an association list with keys like \"%a\" and string values.
  25516. The sequences in STRING may contain normal field width and padding information,
  25517. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25518. so values can contain further %-escapes if they are define later in TABLE."
  25519. (let ((case-fold-search nil)
  25520. e re rpl)
  25521. (while (setq e (pop table))
  25522. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25523. (while (string-match re string)
  25524. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25525. (cdr e)))
  25526. (setq string (replace-match rpl t t string))))
  25527. string))
  25528. (defun org-sublist (list start end)
  25529. "Return a section of LIST, from START to END.
  25530. Counting starts at 1."
  25531. (let (rtn (c start))
  25532. (setq list (nthcdr (1- start) list))
  25533. (while (and list (<= c end))
  25534. (push (pop list) rtn)
  25535. (setq c (1+ c)))
  25536. (nreverse rtn)))
  25537. (defun org-find-base-buffer-visiting (file)
  25538. "Like `find-buffer-visiting' but alway return the base buffer and
  25539. not an indirect buffer"
  25540. (let ((buf (find-buffer-visiting file)))
  25541. (if buf
  25542. (or (buffer-base-buffer buf) buf)
  25543. nil)))
  25544. (defun org-image-file-name-regexp ()
  25545. "Return regexp matching the file names of images."
  25546. (if (fboundp 'image-file-name-regexp)
  25547. (image-file-name-regexp)
  25548. (let ((image-file-name-extensions
  25549. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25550. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25551. (concat "\\."
  25552. (regexp-opt (nconc (mapcar 'upcase
  25553. image-file-name-extensions)
  25554. image-file-name-extensions)
  25555. t)
  25556. "\\'"))))
  25557. (defun org-file-image-p (file)
  25558. "Return non-nil if FILE is an image."
  25559. (save-match-data
  25560. (string-match (org-image-file-name-regexp) file)))
  25561. ;;; Paragraph filling stuff.
  25562. ;; We want this to be just right, so use the full arsenal.
  25563. (defun org-indent-line-function ()
  25564. "Indent line like previous, but further if previous was headline or item."
  25565. (interactive)
  25566. (let* ((pos (point))
  25567. (itemp (org-at-item-p))
  25568. column bpos bcol tpos tcol bullet btype bullet-type)
  25569. ;; Find the previous relevant line
  25570. (beginning-of-line 1)
  25571. (cond
  25572. ((looking-at "#") (setq column 0))
  25573. ((looking-at "\\*+ ") (setq column 0))
  25574. (t
  25575. (beginning-of-line 0)
  25576. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25577. (beginning-of-line 0))
  25578. (cond
  25579. ((looking-at "\\*+[ \t]+")
  25580. (goto-char (match-end 0))
  25581. (setq column (current-column)))
  25582. ((org-in-item-p)
  25583. (org-beginning-of-item)
  25584. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25585. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25586. (setq bpos (match-beginning 1) tpos (match-end 0)
  25587. bcol (progn (goto-char bpos) (current-column))
  25588. tcol (progn (goto-char tpos) (current-column))
  25589. bullet (match-string 1)
  25590. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25591. (if (not itemp)
  25592. (setq column tcol)
  25593. (goto-char pos)
  25594. (beginning-of-line 1)
  25595. (if (looking-at "\\S-")
  25596. (progn
  25597. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25598. (setq bullet (match-string 1)
  25599. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25600. (setq column (if (equal btype bullet-type) bcol tcol)))
  25601. (setq column (org-get-indentation)))))
  25602. (t (setq column (org-get-indentation))))))
  25603. (goto-char pos)
  25604. (if (<= (current-column) (current-indentation))
  25605. (indent-line-to column)
  25606. (save-excursion (indent-line-to column)))
  25607. (setq column (current-column))
  25608. (beginning-of-line 1)
  25609. (if (looking-at
  25610. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25611. (replace-match (concat "\\1" (format org-property-format
  25612. (match-string 2) (match-string 3)))
  25613. t nil))
  25614. (move-to-column column)))
  25615. (defun org-set-autofill-regexps ()
  25616. (interactive)
  25617. ;; In the paragraph separator we include headlines, because filling
  25618. ;; text in a line directly attached to a headline would otherwise
  25619. ;; fill the headline as well.
  25620. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25621. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25622. ;; The paragraph starter includes hand-formatted lists.
  25623. (org-set-local 'paragraph-start
  25624. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25625. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25626. ;; But only if the user has not turned off tables or fixed-width regions
  25627. (org-set-local
  25628. 'auto-fill-inhibit-regexp
  25629. (concat "\\*+ \\|#\\+"
  25630. "\\|[ \t]*" org-keyword-time-regexp
  25631. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25632. (concat
  25633. "\\|[ \t]*["
  25634. (if org-enable-table-editor "|" "")
  25635. (if org-enable-fixed-width-editor ":" "")
  25636. "]"))))
  25637. ;; We use our own fill-paragraph function, to make sure that tables
  25638. ;; and fixed-width regions are not wrapped. That function will pass
  25639. ;; through to `fill-paragraph' when appropriate.
  25640. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25641. ; Adaptive filling: To get full control, first make sure that
  25642. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25643. (org-set-local 'adaptive-fill-regexp "\000")
  25644. (org-set-local 'adaptive-fill-function
  25645. 'org-adaptive-fill-function)
  25646. (org-set-local
  25647. 'align-mode-rules-list
  25648. '((org-in-buffer-settings
  25649. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25650. (modes . '(org-mode))))))
  25651. (defun org-fill-paragraph (&optional justify)
  25652. "Re-align a table, pass through to fill-paragraph if no table."
  25653. (let ((table-p (org-at-table-p))
  25654. (table.el-p (org-at-table.el-p)))
  25655. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25656. (save-excursion (goto-char (point-at-bol))
  25657. (looking-at outline-regexp)))
  25658. t) ; skip headlines
  25659. (table.el-p t) ; skip table.el tables
  25660. (table-p (org-table-align) t) ; align org-mode tables
  25661. (t nil)))) ; call paragraph-fill
  25662. ;; For reference, this is the default value of adaptive-fill-regexp
  25663. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25664. (defun org-adaptive-fill-function ()
  25665. "Return a fill prefix for org-mode files.
  25666. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25667. work correctly."
  25668. (cond ((looking-at "#[ \t]+")
  25669. (match-string 0))
  25670. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25671. (save-excursion
  25672. (goto-char (match-end 0))
  25673. (make-string (current-column) ?\ )))
  25674. (t nil)))
  25675. ;;;; Functions extending outline functionality
  25676. (defun org-beginning-of-line (&optional arg)
  25677. "Go to the beginning of the current line. If that is invisible, continue
  25678. to a visible line beginning. This makes the function of C-a more intuitive.
  25679. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25680. first attempt, and only move to after the tags when the cursor is already
  25681. beyond the end of the headline."
  25682. (interactive "P")
  25683. (let ((pos (point)))
  25684. (beginning-of-line 1)
  25685. (if (bobp)
  25686. nil
  25687. (backward-char 1)
  25688. (if (org-invisible-p)
  25689. (while (and (not (bobp)) (org-invisible-p))
  25690. (backward-char 1)
  25691. (beginning-of-line 1))
  25692. (forward-char 1)))
  25693. (when org-special-ctrl-a/e
  25694. (cond
  25695. ((and (looking-at org-todo-line-regexp)
  25696. (= (char-after (match-end 1)) ?\ ))
  25697. (goto-char
  25698. (if (eq org-special-ctrl-a/e t)
  25699. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25700. ((= pos (point)) (match-beginning 3))
  25701. (t (point)))
  25702. (cond ((> pos (point)) (point))
  25703. ((not (eq last-command this-command)) (point))
  25704. (t (match-beginning 3))))))
  25705. ((org-at-item-p)
  25706. (goto-char
  25707. (if (eq org-special-ctrl-a/e t)
  25708. (cond ((> pos (match-end 4)) (match-end 4))
  25709. ((= pos (point)) (match-end 4))
  25710. (t (point)))
  25711. (cond ((> pos (point)) (point))
  25712. ((not (eq last-command this-command)) (point))
  25713. (t (match-end 4))))))))))
  25714. (defun org-end-of-line (&optional arg)
  25715. "Go to the end of the line.
  25716. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25717. first attempt, and only move to after the tags when the cursor is already
  25718. beyond the end of the headline."
  25719. (interactive "P")
  25720. (if (or (not org-special-ctrl-a/e)
  25721. (not (org-on-heading-p)))
  25722. (end-of-line arg)
  25723. (let ((pos (point)))
  25724. (beginning-of-line 1)
  25725. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25726. (if (eq org-special-ctrl-a/e t)
  25727. (if (or (< pos (match-beginning 1))
  25728. (= pos (match-end 0)))
  25729. (goto-char (match-beginning 1))
  25730. (goto-char (match-end 0)))
  25731. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25732. (goto-char (match-end 0))
  25733. (goto-char (match-beginning 1))))
  25734. (end-of-line arg)))))
  25735. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25736. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25737. (defun org-kill-line (&optional arg)
  25738. "Kill line, to tags or end of line."
  25739. (interactive "P")
  25740. (cond
  25741. ((or (not org-special-ctrl-k)
  25742. (bolp)
  25743. (not (org-on-heading-p)))
  25744. (call-interactively 'kill-line))
  25745. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  25746. (kill-region (point) (match-beginning 1))
  25747. (org-set-tags nil t))
  25748. (t (kill-region (point) (point-at-eol)))))
  25749. (define-key org-mode-map "\C-k" 'org-kill-line)
  25750. (defun org-invisible-p ()
  25751. "Check if point is at a character currently not visible."
  25752. ;; Early versions of noutline don't have `outline-invisible-p'.
  25753. (if (fboundp 'outline-invisible-p)
  25754. (outline-invisible-p)
  25755. (get-char-property (point) 'invisible)))
  25756. (defun org-invisible-p2 ()
  25757. "Check if point is at a character currently not visible."
  25758. (save-excursion
  25759. (if (and (eolp) (not (bobp))) (backward-char 1))
  25760. ;; Early versions of noutline don't have `outline-invisible-p'.
  25761. (if (fboundp 'outline-invisible-p)
  25762. (outline-invisible-p)
  25763. (get-char-property (point) 'invisible))))
  25764. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25765. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25766. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25767. (defun org-at-heading-or-item-p ()
  25768. (or (org-on-heading-p) (org-at-item-p)))
  25769. (defun org-on-target-p ()
  25770. (or (org-in-regexp org-radio-target-regexp)
  25771. (org-in-regexp org-target-regexp)))
  25772. (defun org-up-heading-all (arg)
  25773. "Move to the heading line of which the present line is a subheading.
  25774. This function considers both visible and invisible heading lines.
  25775. With argument, move up ARG levels."
  25776. (if (fboundp 'outline-up-heading-all)
  25777. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25778. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25779. (defun org-up-heading-safe ()
  25780. "Move to the heading line of which the present line is a subheading.
  25781. This version will not throw an error. It will return the level of the
  25782. headline found, or nil if no higher level is found."
  25783. (let ((pos (point)) start-level level
  25784. (re (concat "^" outline-regexp)))
  25785. (catch 'exit
  25786. (outline-back-to-heading t)
  25787. (setq start-level (funcall outline-level))
  25788. (if (equal start-level 1) (throw 'exit nil))
  25789. (while (re-search-backward re nil t)
  25790. (setq level (funcall outline-level))
  25791. (if (< level start-level) (throw 'exit level)))
  25792. nil)))
  25793. (defun org-first-sibling-p ()
  25794. "Is this heading the first child of its parents?"
  25795. (interactive)
  25796. (let ((re (concat "^" outline-regexp))
  25797. level l)
  25798. (unless (org-at-heading-p t)
  25799. (error "Not at a heading"))
  25800. (setq level (funcall outline-level))
  25801. (save-excursion
  25802. (if (not (re-search-backward re nil t))
  25803. t
  25804. (setq l (funcall outline-level))
  25805. (< l level)))))
  25806. (defun org-goto-sibling (&optional previous)
  25807. "Goto the next sibling, even if it is invisible.
  25808. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25809. when a sibling was found. When none is found, return nil and don't
  25810. move point."
  25811. (let ((fun (if previous 're-search-backward 're-search-forward))
  25812. (pos (point))
  25813. (re (concat "^" outline-regexp))
  25814. level l)
  25815. (when (condition-case nil (org-back-to-heading t) (error nil))
  25816. (setq level (funcall outline-level))
  25817. (catch 'exit
  25818. (or previous (forward-char 1))
  25819. (while (funcall fun re nil t)
  25820. (setq l (funcall outline-level))
  25821. (when (< l level) (goto-char pos) (throw 'exit nil))
  25822. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25823. (goto-char pos)
  25824. nil))))
  25825. (defun org-show-siblings ()
  25826. "Show all siblings of the current headline."
  25827. (save-excursion
  25828. (while (org-goto-sibling) (org-flag-heading nil)))
  25829. (save-excursion
  25830. (while (org-goto-sibling 'previous)
  25831. (org-flag-heading nil))))
  25832. (defun org-show-hidden-entry ()
  25833. "Show an entry where even the heading is hidden."
  25834. (save-excursion
  25835. (org-show-entry)))
  25836. (defun org-flag-heading (flag &optional entry)
  25837. "Flag the current heading. FLAG non-nil means make invisible.
  25838. When ENTRY is non-nil, show the entire entry."
  25839. (save-excursion
  25840. (org-back-to-heading t)
  25841. ;; Check if we should show the entire entry
  25842. (if entry
  25843. (progn
  25844. (org-show-entry)
  25845. (save-excursion
  25846. (and (outline-next-heading)
  25847. (org-flag-heading nil))))
  25848. (outline-flag-region (max (point-min) (1- (point)))
  25849. (save-excursion (outline-end-of-heading) (point))
  25850. flag))))
  25851. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25852. ;; This is an exact copy of the original function, but it uses
  25853. ;; `org-back-to-heading', to make it work also in invisible
  25854. ;; trees. And is uses an invisible-OK argument.
  25855. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25856. (org-back-to-heading invisible-OK)
  25857. (let ((first t)
  25858. (level (funcall outline-level)))
  25859. (while (and (not (eobp))
  25860. (or first (> (funcall outline-level) level)))
  25861. (setq first nil)
  25862. (outline-next-heading))
  25863. (unless to-heading
  25864. (if (memq (preceding-char) '(?\n ?\^M))
  25865. (progn
  25866. ;; Go to end of line before heading
  25867. (forward-char -1)
  25868. (if (memq (preceding-char) '(?\n ?\^M))
  25869. ;; leave blank line before heading
  25870. (forward-char -1))))))
  25871. (point))
  25872. (defun org-show-subtree ()
  25873. "Show everything after this heading at deeper levels."
  25874. (outline-flag-region
  25875. (point)
  25876. (save-excursion
  25877. (outline-end-of-subtree) (outline-next-heading) (point))
  25878. nil))
  25879. (defun org-show-entry ()
  25880. "Show the body directly following this heading.
  25881. Show the heading too, if it is currently invisible."
  25882. (interactive)
  25883. (save-excursion
  25884. (condition-case nil
  25885. (progn
  25886. (org-back-to-heading t)
  25887. (outline-flag-region
  25888. (max (point-min) (1- (point)))
  25889. (save-excursion
  25890. (re-search-forward
  25891. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25892. (or (match-beginning 1) (point-max)))
  25893. nil))
  25894. (error nil))))
  25895. (defun org-make-options-regexp (kwds)
  25896. "Make a regular expression for keyword lines."
  25897. (concat
  25898. "^"
  25899. "#?[ \t]*\\+\\("
  25900. (mapconcat 'regexp-quote kwds "\\|")
  25901. "\\):[ \t]*"
  25902. "\\(.+\\)"))
  25903. ;; Make isearch reveal the necessary context
  25904. (defun org-isearch-end ()
  25905. "Reveal context after isearch exits."
  25906. (when isearch-success ; only if search was successful
  25907. (if (featurep 'xemacs)
  25908. ;; Under XEmacs, the hook is run in the correct place,
  25909. ;; we directly show the context.
  25910. (org-show-context 'isearch)
  25911. ;; In Emacs the hook runs *before* restoring the overlays.
  25912. ;; So we have to use a one-time post-command-hook to do this.
  25913. ;; (Emacs 22 has a special variable, see function `org-mode')
  25914. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25915. isearch-mode-end-hook-quit)
  25916. ;; Only when the isearch was not quitted.
  25917. (org-add-hook 'post-command-hook 'org-isearch-post-command
  25918. 'append 'local)))))
  25919. (defun org-isearch-post-command ()
  25920. "Remove self from hook, and show context."
  25921. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  25922. (org-show-context 'isearch))
  25923. ;;;; Integration with and fixes for other packages
  25924. ;;; Imenu support
  25925. (defvar org-imenu-markers nil
  25926. "All markers currently used by Imenu.")
  25927. (make-variable-buffer-local 'org-imenu-markers)
  25928. (defun org-imenu-new-marker (&optional pos)
  25929. "Return a new marker for use by Imenu, and remember the marker."
  25930. (let ((m (make-marker)))
  25931. (move-marker m (or pos (point)))
  25932. (push m org-imenu-markers)
  25933. m))
  25934. (defun org-imenu-get-tree ()
  25935. "Produce the index for Imenu."
  25936. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  25937. (setq org-imenu-markers nil)
  25938. (let* ((n org-imenu-depth)
  25939. (re (concat "^" outline-regexp))
  25940. (subs (make-vector (1+ n) nil))
  25941. (last-level 0)
  25942. m tree level head)
  25943. (save-excursion
  25944. (save-restriction
  25945. (widen)
  25946. (goto-char (point-max))
  25947. (while (re-search-backward re nil t)
  25948. (setq level (org-reduced-level (funcall outline-level)))
  25949. (when (<= level n)
  25950. (looking-at org-complex-heading-regexp)
  25951. (setq head (org-match-string-no-properties 4)
  25952. m (org-imenu-new-marker))
  25953. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  25954. (if (>= level last-level)
  25955. (push (cons head m) (aref subs level))
  25956. (push (cons head (aref subs (1+ level))) (aref subs level))
  25957. (loop for i from (1+ level) to n do (aset subs i nil)))
  25958. (setq last-level level)))))
  25959. (aref subs 1)))
  25960. (eval-after-load "imenu"
  25961. '(progn
  25962. (add-hook 'imenu-after-jump-hook
  25963. (lambda () (org-show-context 'org-goto)))))
  25964. ;; Speedbar support
  25965. (defun org-speedbar-set-agenda-restriction ()
  25966. "Restrict future agenda commands to the location at point in speedbar.
  25967. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  25968. (interactive)
  25969. (let (p m tp np dir txt w)
  25970. (cond
  25971. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25972. 'org-imenu t))
  25973. (setq m (get-text-property p 'org-imenu-marker))
  25974. (save-excursion
  25975. (save-restriction
  25976. (set-buffer (marker-buffer m))
  25977. (goto-char m)
  25978. (org-agenda-set-restriction-lock 'subtree))))
  25979. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25980. 'speedbar-function 'speedbar-find-file))
  25981. (setq tp (previous-single-property-change
  25982. (1+ p) 'speedbar-function)
  25983. np (next-single-property-change
  25984. tp 'speedbar-function)
  25985. dir (speedbar-line-directory)
  25986. txt (buffer-substring-no-properties (or tp (point-min))
  25987. (or np (point-max))))
  25988. (save-excursion
  25989. (save-restriction
  25990. (set-buffer (find-file-noselect
  25991. (let ((default-directory dir))
  25992. (expand-file-name txt))))
  25993. (unless (org-mode-p)
  25994. (error "Cannot restrict to non-Org-mode file"))
  25995. (org-agenda-set-restriction-lock 'file))))
  25996. (t (error "Don't know how to restrict Org-mode's agenda")))
  25997. (org-move-overlay org-speedbar-restriction-lock-overlay
  25998. (point-at-bol) (point-at-eol))
  25999. (setq current-prefix-arg nil)
  26000. (org-agenda-maybe-redo)))
  26001. (eval-after-load "speedbar"
  26002. '(progn
  26003. (speedbar-add-supported-extension ".org")
  26004. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26005. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26006. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26007. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26008. (add-hook 'speedbar-visiting-tag-hook
  26009. (lambda () (org-show-context 'org-goto)))))
  26010. ;;; Fixes and Hacks
  26011. ;; Make flyspell not check words in links, to not mess up our keymap
  26012. (defun org-mode-flyspell-verify ()
  26013. "Don't let flyspell put overlays at active buttons."
  26014. (not (get-text-property (point) 'keymap)))
  26015. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26016. (eval-after-load "bookmark"
  26017. '(if (boundp 'bookmark-after-jump-hook)
  26018. ;; We can use the hook
  26019. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26020. ;; Hook not available, use advice
  26021. (defadvice bookmark-jump (after org-make-visible activate)
  26022. "Make the position visible."
  26023. (org-bookmark-jump-unhide))))
  26024. (defun org-bookmark-jump-unhide ()
  26025. "Unhide the current position, to show the bookmark location."
  26026. (and (org-mode-p)
  26027. (or (org-invisible-p)
  26028. (save-excursion (goto-char (max (point-min) (1- (point))))
  26029. (org-invisible-p)))
  26030. (org-show-context 'bookmark-jump)))
  26031. ;; Fix a bug in htmlize where there are text properties (face nil)
  26032. (eval-after-load "htmlize"
  26033. '(progn
  26034. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26035. "Make sure there are no nil faces"
  26036. (setq ad-return-value (delq nil ad-return-value)))))
  26037. ;; Make session.el ignore our circular variable
  26038. (eval-after-load "session"
  26039. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26040. ;;;; Experimental code
  26041. (defun org-closed-in-range ()
  26042. "Sparse tree of items closed in a certain time range.
  26043. Still experimental, may disappear in the future."
  26044. (interactive)
  26045. ;; Get the time interval from the user.
  26046. (let* ((time1 (time-to-seconds
  26047. (org-read-date nil 'to-time nil "Starting date: ")))
  26048. (time2 (time-to-seconds
  26049. (org-read-date nil 'to-time nil "End date:")))
  26050. ;; callback function
  26051. (callback (lambda ()
  26052. (let ((time
  26053. (time-to-seconds
  26054. (apply 'encode-time
  26055. (org-parse-time-string
  26056. (match-string 1))))))
  26057. ;; check if time in interval
  26058. (and (>= time time1) (<= time time2))))))
  26059. ;; make tree, check each match with the callback
  26060. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26061. ;;;; Finish up
  26062. (provide 'org)
  26063. (run-hooks 'org-load-hook)
  26064. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26065. ;;; org.el ends here